-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMetalMap.cpp
542 lines (443 loc) · 15 KB
/
MetalMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <string>
#include <sstream>
#include "System/Util.h"
#include "IncExternAI.h"
#include "IncGlobalAI.h"
CMetalMap::CMetalMap(AIClasses* ai) {
this->ai = ai;
// from 0-255, the minimum percentage of metal a spot needs to have from
// the maximum to be saved, prevents crappier spots in between taken spaces
// (they are still perfectly valid and will generate metal mind you!)
MinMetalForSpot = 50;
// if more spots than that are found the map is considered a metalmap, tweak this as needed
MaxSpots = 10000;
// metal map has 1/2 resolution of normal map
MetalMapHeight = ai->cb->GetMapHeight() / 2;
MetalMapWidth = ai->cb->GetMapWidth() / 2;
TotalCells = MetalMapHeight * MetalMapWidth;
XtractorRadiusOrg = ai->cb->GetExtractorRadius();
XtractorRadius = int(ai->cb->GetExtractorRadius() / 16.0f);
DoubleRadius = XtractorRadius * 2;
SquareRadius = XtractorRadius * XtractorRadius;
DoubleSquareRadius = DoubleRadius * DoubleRadius;
MexArrayA = new unsigned char [TotalCells];
MexArrayB = new unsigned char [TotalCells];
// used for drawing the TGA, not really needed with a couple of changes
MexArrayC = new unsigned char [TotalCells];
TempAverage = new int [TotalCells];
TotalMetal = MaxMetal = NumSpotsFound = 0;
Stopme = false;
}
CMetalMap::~CMetalMap() {
delete[] MexArrayA;
delete[] MexArrayB;
delete[] MexArrayC;
delete[] TempAverage;
}
// KLOOTNOTE: this needs to ignore spots already taken by allies
float3 CMetalMap::GetNearestMetalSpot(int builderid, const UnitDef* extractor) {
float TempScore = 0.0f;
float MaxDivergence = 16.0f;
float3 spotCoords = ERRORVECTOR;
float3 bestSpot = ERRORVECTOR;
if (VectoredSpots.size()) {
for (unsigned int i = 0; i != VectoredSpots.size(); i++) {
spotCoords = ai->cb->ClosestBuildSite(extractor, VectoredSpots[i], MaxDivergence, 2);
if (spotCoords.x >= 0.0f) {
float distance = spotCoords.distance2D(ai->cb->GetUnitPos(builderid)) + 150;
float myThreat = ai->tm->ThreatAtThisPoint(spotCoords);
float spotScore = VectoredSpots[i].y / distance / (myThreat + 10);
// along with threatmap try to search for enemy armed units around cause
// there could be armored MEX nearby which gives the highest threat anyway...
int numEnemies = ai->ccb->GetEnemyUnits(&ai->unitIDs[0], spotCoords, XtractorRadiusOrg * 1.5f);
while(numEnemies > 0)
{
numEnemies--;
const UnitDef* ud = ai->ccb->GetUnitDef(ai->unitIDs[numEnemies]);
if(ud && !ud->weapons.empty())
{
numEnemies++;
break;
}
}
bool bOccupied = false; // flag: metal spot is occupied by allied unit
if(NumSpotsFound < 100)
{
int numAllies = ai->cb->GetFriendlyUnits(&ai->unitIDs[0], spotCoords, XtractorRadiusOrg * 1.5f);
for(unsigned int j = 0; j < numAllies; j++)
{
if(ai->ut->GetCategory(ai->unitIDs[j]) == CAT_MEX)
{
bOccupied = true;
break;
}
}
}
// NOTE: threat at spotCoords is determined
// by presence of ARMED enemy units or buildings
bool b1 = (TempScore < spotScore);
bool b2 = (numEnemies == 0);
bool b3 = (myThreat <= (ai->tm->GetAverageThreat() * 1.5));
bool b4 = (ai->uh->TaskPlanExist(spotCoords, extractor));
if (b1 && b2 && b3 && !b4 && !bOccupied) {
TempScore = spotScore;
bestSpot = spotCoords;
bestSpot.y = VectoredSpots[i].y;
}
}
}
}
// no spot found if TempScore is zero
return bestSpot;
}
void CMetalMap::Init() {
const int frame = ai->cb->GetCurrentFrame();
// leave this line if you want to use this class in your AI
ai->cb->SendTextMsg("KAI Metal Class by Krogothe", 0);
// if there's no available load file, create one and save it
if (!LoadMetalMap()) {
GetMetalPoints();
SaveMetalMap();
// std::string mapname = std::string("Metal - ") + ai->cb->GetMapName();
// mapname.resize(mapname.size() - 4);
// ai->debug->MakeBWTGA(MexArrayC, MetalMapWidth, MetalMapHeight, mapname);
}
std::stringstream msg;
msg << "[CMetalMap::Init()] number of metal spots found: " << NumSpotsFound << "\n";
ai->GetLogger()->Log(msg.str());
}
void CMetalMap::GetMetalPoints() {
int* xend = new int[DoubleRadius + 1];
for (int a = 0; a < DoubleRadius + 1; a++) {
float z = a - XtractorRadius;
float floatsqrradius = SquareRadius;
xend[a] = int(sqrtf(floatsqrradius - z * z));
}
// load up the metal values in each pixel
const unsigned char* metalMapArray = ai->cb->GetMetalMap();
double TotalMetalDouble = 0;
for (int i = 0; i < TotalCells; i++) {
// count the total metal so you can work out an average of the whole map
TotalMetalDouble += MexArrayA[i] = metalMapArray[i];
}
// do the average
AverageMetal = TotalMetalDouble / TotalCells;
// quick test for no metal map:
if (TotalMetalDouble < 0.9) {
// the map doesn't have any metal, just stop
NumSpotsFound = 0;
delete[] xend;
return;
}
// Now work out how much metal each spot can make by adding up the metal from nearby spots
for (int y = 0; y < MetalMapHeight; y++) {
for (int x = 0; x < MetalMapWidth; x++) {
TotalMetal = 0;
// first spot needs full calculation
if (x == 0 && y == 0)
for (int sy = y - XtractorRadius, a = 0; sy <= y + XtractorRadius; sy++, a++) {
if (sy >= 0 && sy < MetalMapHeight){
for (int sx = x - xend[a]; sx <= x + xend[a]; sx++) {
if (sx >= 0 && sx < MetalMapWidth) {
// get the metal from all pixels around the extractor radius
TotalMetal += MexArrayA[sy * MetalMapWidth + sx];
}
}
}
}
// quick calc test
if (x > 0) {
TotalMetal = TempAverage[y * MetalMapWidth + x - 1];
for (int sy = y - XtractorRadius, a = 0; sy <= y + XtractorRadius; sy++, a++) {
if (sy >= 0 && sy < MetalMapHeight) {
int addX = x + xend[a];
int remX = x - xend[a] - 1;
if (addX < MetalMapWidth)
TotalMetal += MexArrayA[sy * MetalMapWidth + addX];
if (remX >= 0)
TotalMetal -= MexArrayA[sy * MetalMapWidth + remX];
}
}
}
else if (y > 0) {
// x == 0 here
TotalMetal = TempAverage[(y - 1) * MetalMapWidth];
// remove the top half
int a = XtractorRadius;
for (int sx = 0; sx <= XtractorRadius; sx++, a++) {
if (sx < MetalMapWidth) {
int remY = y - xend[a] - 1;
if (remY >= 0)
TotalMetal -= MexArrayA[remY * MetalMapWidth + sx];
}
}
// add the bottom half
a = XtractorRadius;
for (int sx = 0; sx <= XtractorRadius; sx++, a++) {
if (sx < MetalMapWidth) {
int addY = y + xend[a];
if (addY < MetalMapHeight)
TotalMetal += MexArrayA[addY * MetalMapWidth + sx];
}
}
// comment out for debug
TotalMetal = TotalMetal;
}
// set that spot's metal making ability (divide by cells to values are small)
TempAverage[y * MetalMapWidth + x] = TotalMetal;
if (MaxMetal < TotalMetal) {
// find the spot with the highest metal to set as the map's max
MaxMetal = TotalMetal;
}
}
}
// make a list for the distribution of values
int* valueDist = new int[256];
for (int i = 0; i < 256; i++) {
// clear the array (useless?)
valueDist[i] = 0;
}
// this will get the total metal a mex placed at each spot would make
for (int i = 0; i < TotalCells; i++) {
// scale the metal so any map will have values 0-255, no matter how much metal it has
MexArrayB[i] = TempAverage[i] * 255 / MaxMetal;
// clear out the array since it has never been used
MexArrayC[i] = 0;
int value = MexArrayB[i];
valueDist[value]++;
}
// find the current best value
int bestValue = 0;
int numberOfValues = 0;
int usedSpots = 0;
for (int i = 255; i >= 0; i--) {
if (valueDist[i] != 0) {
bestValue = i;
numberOfValues = valueDist[i];
break;
}
}
// make a list of the indexes of the best spots
// (make sure that the list wont be too big)
if (numberOfValues > 256)
numberOfValues = 256;
int* bestSpotList = new int[numberOfValues];
for (int i = 0; i < TotalCells; i++) {
if (MexArrayB[i] == bestValue) {
// add the index of this spot to the list
bestSpotList[usedSpots] = i;
usedSpots++;
if (usedSpots == numberOfValues) {
// the list is filled, stop the loop
usedSpots = 0;
break;
}
}
}
for (int a = 0; a < MaxSpots; a++) {
if (!Stopme) {
// reset tempmetal so it can find new spots
TempMetal = 0;
// take the first spot
int speedTempMetal_x = 0;
int speedTempMetal_y = 0;
int speedTempMetal = 0;
bool found = false;
while (!found) {
if (usedSpots == numberOfValues) {
// the list is empty now, refill it
// make a list of all the best spots
for (int i = 0; i < 256; i++) {
// clear the array
valueDist[i] = 0;
}
// find the metal distribution
for (int i = 0; i < TotalCells; i++) {
int value = MexArrayB[i];
valueDist[value]++;
}
// find the current best value
bestValue = 0;
numberOfValues = 0;
usedSpots = 0;
for (int i = 255; i >= 0; i--) {
if (valueDist[i] != 0) {
bestValue = i;
numberOfValues = valueDist[i];
break;
}
}
// make a list of the indexes of the best spots
// (make sure that the list wont be too big)
if (numberOfValues > 256)
numberOfValues = 256;
delete[] bestSpotList;
bestSpotList = new int[numberOfValues];
for (int i = 0; i < TotalCells; i++) {
if (MexArrayB[i] == bestValue) {
// add the index of this spot to the list
bestSpotList[usedSpots] = i;
usedSpots++;
if (usedSpots == numberOfValues) {
// the list is filled, stop the loop
usedSpots = 0;
break;
}
}
}
}
// The list is not empty now.
int spotIndex = bestSpotList[usedSpots];
if (MexArrayB[spotIndex] == bestValue) {
// the spot is still valid, so use it
speedTempMetal_x = spotIndex % MetalMapWidth;
speedTempMetal_y = spotIndex / MetalMapWidth;
speedTempMetal = bestValue;
found = true;
}
// update the bestSpotList index
usedSpots++;
}
coordx = speedTempMetal_x;
coordy = speedTempMetal_y;
TempMetal = speedTempMetal;
}
if (TempMetal < MinMetalForSpot) {
// if the spots get too crappy it will stop running the loops to speed it all up
Stopme = 1;
}
if (!Stopme) {
// format metal coords to game-coords
BufferSpot.x = coordx * 16 + 8;
BufferSpot.z = coordy * 16 + 8;
// gets the actual amount of metal an extractor can make
BufferSpot.y = TempMetal * (ai->cb->GetMaxMetal()) * MaxMetal / 255;
VectoredSpots.push_back(BufferSpot);
// plot TGA array (not necessary) for debug
MexArrayC[coordy * MetalMapWidth + coordx] = TempMetal;
NumSpotsFound += 1;
// small speedup of "wipes the metal around the spot so its not counted twice"
for (int sy = coordy - XtractorRadius, a = 0; sy <= coordy + XtractorRadius; sy++, a++) {
if (sy >= 0 && sy < MetalMapHeight) {
int clearXStart = coordx - xend[a];
int clearXEnd = coordx + xend[a];
if (clearXStart < 0)
clearXStart = 0;
if (clearXEnd >= MetalMapWidth)
clearXEnd = MetalMapWidth - 1;
for (int xClear = clearXStart; xClear <= clearXEnd; xClear++) {
// wipes the metal around the spot so it's not counted twice
MexArrayA[sy * MetalMapWidth + xClear] = 0;
MexArrayB[sy * MetalMapWidth + xClear] = 0;
TempAverage[sy * MetalMapWidth + xClear] = 0;
}
}
}
// redo the whole averaging process around the picked spot so other spots can be found around it
for (int y = coordy - DoubleRadius; y <= coordy + DoubleRadius; y++) {
if (y >=0 && y < MetalMapHeight) {
for (int x = coordx - DoubleRadius; x <= coordx + DoubleRadius; x++) {
if (x >=0 && x < MetalMapWidth) {
TotalMetal = 0;
// comment out for debug
if (x == 0 && y == 0)
for (int sy = y - XtractorRadius, a = 0; sy <= y + XtractorRadius; sy++, a++) {
if (sy >= 0 && sy < MetalMapHeight) {
for (int sx = x - xend[a]; sx <= x + xend[a]; sx++) {
if (sx >= 0 && sx < MetalMapWidth) {
// get the metal from all pixels around the extractor radius
TotalMetal += MexArrayA[sy * MetalMapWidth + sx];
}
}
}
}
// quick calc test
if (x > 0) {
TotalMetal = TempAverage[y * MetalMapWidth + x - 1];
for (int sy = y - XtractorRadius, a = 0; sy <= y + XtractorRadius; sy++, a++) {
if (sy >= 0 && sy < MetalMapHeight) {
int addX = x + xend[a];
int remX = x - xend[a] - 1;
if (addX < MetalMapWidth)
TotalMetal += MexArrayA[sy * MetalMapWidth + addX];
if (remX >= 0)
TotalMetal -= MexArrayA[sy * MetalMapWidth + remX];
}
}
}
else if (y > 0) {
// x == 0 here
TotalMetal = TempAverage[(y - 1) * MetalMapWidth];
// remove the top half
int a = XtractorRadius;
for (int sx = 0; sx <= XtractorRadius; sx++, a++) {
if (sx < MetalMapWidth) {
int remY = y - xend[a] - 1;
if (remY >= 0)
TotalMetal -= MexArrayA[remY * MetalMapWidth + sx];
}
}
// add the bottom half
a = XtractorRadius;
for (int sx = 0; sx <= XtractorRadius; sx++, a++) {
if (sx < MetalMapWidth) {
int addY = y + xend[a];
if (addY < MetalMapHeight)
TotalMetal += MexArrayA[addY * MetalMapWidth + sx];
}
}
}
TempAverage[y * MetalMapWidth + x] = TotalMetal;
// set that spot's metal amount
MexArrayB[y * MetalMapWidth + x] = TotalMetal * 255 / MaxMetal;
}
}
}
}
}
}
// kill the lists
delete[] bestSpotList;
delete[] valueDist;
delete[] xend;
// 0.95 used for for reliability
// bool isMetalMap = (NumSpotsFound > MaxSpots * 0.95);
}
void CMetalMap::SaveMetalMap() {
std::string map = GetCacheName();
FILE* saveFile = fopen(map.c_str(), "wb");
assert(saveFile != NULL);
fwrite(&NumSpotsFound, sizeof(int), 1, saveFile);
fwrite(&AverageMetal, sizeof(float), 1, saveFile);
for (int i = 0; i < NumSpotsFound; i++) {
fwrite(&VectoredSpots[i], sizeof(float3), 1, saveFile);
}
fclose(saveFile);
}
bool CMetalMap::LoadMetalMap() {
std::string map = GetCacheName();
FILE* loadFile = fopen(map.c_str(), "rb");
if (loadFile != NULL) {
fread(&NumSpotsFound, sizeof(int), 1, loadFile);
VectoredSpots.resize(NumSpotsFound);
fread(&AverageMetal, sizeof(float), 1, loadFile);
for (int i = 0; i < NumSpotsFound; i++) {
fread(&VectoredSpots[i], sizeof(float3), 1, loadFile);
}
fclose(loadFile);
return true;
}
return false;
}
std::string CMetalMap::GetCacheName() const {
// name is used for human readability,
// while hash is used for uniqueness
// (in case the map maker forgets changing the name inbetween versions)
std::string relFile =
std::string(METALFOLDER) +
AIUtil::MakeFileSystemCompatible(ai->cb->GetMapName()) +
"-" + IntToString(ai->cb->GetMapHash(), "%x") +
".Metal";
std::string absFile = AIUtil::GetAbsFileName(ai->cb, relFile);
return absFile;
}