-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplacedobjects.cpp
More file actions
621 lines (497 loc) · 15.9 KB
/
placedobjects.cpp
File metadata and controls
621 lines (497 loc) · 15.9 KB
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <list>
#include <map>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include "tinyxml.h"
#include "outstreams.hpp"
#include "config.hpp"
#include "coordinates.hpp"
#include "SC2Map.hpp"
#include "sc2mapTypes.hpp"
struct FootToApply
{
point loc;
float rot;
string type;
string name;
};
static list<point> pathingFills;
static list<FootToApply> footsToApply;
void SC2Map::processPlacedObject( ObjectMode objMode, TiXmlElement* object )
{
char* objPoint;
char* objUnit;
char* objDoodad;
char* objGroup;
char* attName;
char* attType;
char* attPosition;
char* attRotation;
char* attUnitType;
char* attResources;
char* chdFlag;
char* flgIndex;
char* flgValue;
char* poWatchtower;
char* poRichMineralField;
list<string> objsToIgnore;
switch( objMode )
{
case OBJMODE_BETAPH1_v26:
objPoint = (char*)"Point";
objUnit = (char*)"Unit";
objDoodad = (char*)"Doodad";
objGroup = (char*)"Group";
attName = (char*)"name";
attType = (char*)"type";
attPosition = (char*)"position";
attRotation = (char*)"rotation";
attUnitType = (char*)"unitType";
attResources = (char*)"resources";
chdFlag = (char*)"";
flgIndex = (char*)"";
flgValue = (char*)"";
poWatchtower = (char*)"Observatory";
poRichMineralField = (char*)"HighYieldMineralField";
break;
case OBJMODE_BETAPH2_v26:
objPoint = (char*)"ObjectPoint";
objUnit = (char*)"ObjectUnit";
objDoodad = (char*)"ObjectDoodad";
objGroup = (char*)"Group";
attName = (char*)"Name";
attType = (char*)"Type";
attPosition = (char*)"Position";
attRotation = (char*)"Rotation";
attUnitType = (char*)"UnitType";
attResources = (char*)"Resources";
chdFlag = (char*)"Flag";
flgIndex = (char*)"Index";
flgValue = (char*)"Value";
poWatchtower = (char*)"XelNagaTower";
poRichMineralField = (char*)"RichMineralField";
objsToIgnore.push_back( "Observatory" );
objsToIgnore.push_back( "HighYieldMineralField" );
break;
default:
printError( "Unknown object processing mode?\n" );
exit( -1 );
}
// ignore groups, don't affect analysis
if( strcmp( object->Value(), objGroup ) == 0 )
{
return;
}
// everything has a position, right?
float mx, my, mz;
const char* strPosition = object->Attribute( attPosition );
if( strPosition == NULL ) {
const char* strID = object->Attribute( "id" );
printError( "PlacedObject id=%s has no position.\n", strID );
}
sscanf( strPosition, "%f,%f,%f", &mx, &my, &mz );
float rot;
const char* strRotation = object->Attribute( attRotation );
if( strRotation == NULL ) {
// not everything has an explicit rotation, default to zero
rot = 0.0f;
} else {
sscanf( strRotation, "%f", &rot );
}
// for some reason the angle in degrees used for placed objects
// within the editor is not what gets written into the Objects
// file--it gets written as radians AND shifted by PI/2!!
// convert back to degrees to match editor, easier
rot = (rot - 0.5f*M_PI) * 180.0f / M_PI;
if( rot < 0.0f ) {
rot = 360.0f + rot;
}
// Points might be a start location
if( strcmp( object->Value(), objPoint ) == 0 )
{
const char* strType = object->Attribute( attType );
if( strcmp( strType, "StartLoc" ) == 0 )
{
StartLoc* sl = new StartLoc;
sl->loc.mSet( mx, my );
startLocs.push_back( sl );
return;
}
if( strcmp( strType, "BlockPathing" ) == 0 )
{
point c;
c.mSet( mx, my );
pathingFills.push_back( c );
return;
}
// just points placed on map
if( strcmp( strType, "Normal" ) == 0 )
{
const char* strName = object->Attribute( attName );
if( strncmp( strName, "testDetectChoke", 15 ) == 0 )
{
point c;
c.mSet( mx, my );
opennessNeighborhoodsToRender.push_back( c );
return;
}
}
}
// doodads might have a footprint, don't worry about scaled
// doodads--their footprint is the same
if( strcmp( object->Value(), objDoodad ) == 0 )
{
// check if the "don't use footprint" flag of this doodad
// is set, which means we can ignore it
for( TiXmlElement* flag = object->FirstChildElement( chdFlag );
flag;
flag = flag->NextSiblingElement( chdFlag ) )
{
const char* indexStr = flag->Attribute( flgIndex );
const char* valueStr = flag->Attribute( flgValue );
if( indexStr != NULL &&
valueStr != NULL &&
strcmp( indexStr, "NoDoodadFootprint" ) == 0 &&
strcmp( valueStr, "1" ) == 0 )
{
return;
}
}
// otherwise find appropriate footprint
const char* strType = object->Attribute( attType );
string type( "doodad" );
string name( strType );
FootToApply fta;
fta.loc.mSet( mx, my );
fta.rot = rot;
fta.type.assign( type );
fta.name.assign( name );
footsToApply.push_back( fta );
type.assign( "nobuild" );
fta.type.assign( type );
footsToApply.push_back( fta );
type.assign( "losb" );
fta.type.assign( type );
footsToApply.push_back( fta );
return;
}
// units come in three flavors:
// resource - can be mined, has a footprint
// destruct - can be destroyed, has a footprint
// unit - may have a footprint
if( strcmp( object->Value(), objUnit ) == 0 )
{
// every unit has a unitType (TRUE?)
const char* strType = object->Attribute( attUnitType );
// might be used by resources below
const char* strResources = object->Attribute( attResources );
string name( strType );
for( list<string>::iterator itr = objsToIgnore.begin();
itr != objsToIgnore.end();
++itr )
{
if( name == *itr )
{
return;
}
}
// an object may have more than one type of footprint,
// all of which will be applied
FootToApply fta;
string type( "destruct" );
fta.loc.mSet( mx, my );
fta.rot = rot;
fta.type.assign( type );
fta.name.assign( name );
footsToApply.push_back( fta );
type.assign( "unit" );
fta.type.assign( type );
footsToApply.push_back( fta );
type.assign( "resource" );
fta.type.assign( type );
footsToApply.push_back( fta );
type.assign( "nobuildmain" );
fta.type.assign( type );
footsToApply.push_back( fta );
type.assign( "nobuild" );
fta.type.assign( type );
footsToApply.push_back( fta );
type.assign( "losb" );
fta.type.assign( type );
footsToApply.push_back( fta );
if( strcmp( strType, poWatchtower ) == 0 )
{
Watchtower* wt = new Watchtower;
wt->loc.mSet( mx, my );
wt->range = 22.0f;
watchtowers.push_back( wt );
return;
}
if( strcmp( strType, "MineralField" ) == 0 )
{
Resource* r = new Resource;
r->loc.mSet( mx, my );
r->cliffLevel = getHeight( &(r->loc) );
r->type = MINERALS;
if( strResources != NULL )
{
r->amount = atof( strResources );
} else {
r->amount = getfConstant( "defaultMineralAmount" );
}
resources.push_back( r );
return;
}
if( strcmp( strType, poRichMineralField ) == 0 )
{
Resource* r = new Resource;
r->loc.mSet( mx, my );
r->cliffLevel = getHeight( &(r->loc) );
r->type = MINERALS_HY;
if( strResources != NULL )
{
r->amount = atof( strResources );
} else {
r->amount = getfConstant( "defaultMineralAmount" );
}
resources.push_back( r );
return;
}
if( strcmp( strType, "VespeneGeyser" ) == 0 ||
strcmp( strType, "SpacePlatformGeyser" ) == 0 )
{
Resource* r = new Resource;
r->loc.mSet( mx, my );
r->cliffLevel = getHeight( &(r->loc) );
r->type = VESPENEGAS;
if( strResources != NULL )
{
r->amount = atof( strResources );
} else {
r->amount = getfConstant( "defaultGeyserAmount" );
}
resources.push_back( r );
return;
}
if( strcmp( strType, "RichVespeneGeyser" ) == 0 )
{
Resource* r = new Resource;
r->loc.mSet( mx, my );
r->cliffLevel = getHeight( &(r->loc) );
r->type = VESPENEGAS_HY;
if( strResources != NULL )
{
r->amount = atof( strResources );
} else {
r->amount = getfConstant( "defaultGeyserAmount" );
}
resources.push_back( r );
return;
}
}
}
// From editor testing:
// dynamic pathing fills flow across ramps!
// they do NOT flow across painted green pathing
// they DO flow across (ignore) doodads
// THEREFORE, the dynamic fills should be executed before
// any placed objects modify the pathing map made from
// the terrain only
//
// SPECIAL NOTE: as if this wasn't already complicated--this
// tool already applies the painted pathing layer BEFORE doing
// placed objects so we can tell when resources are in unpathable
// areas, which means we can't do dynamic fills BEFORE painted
// pathing is applied. The way to really handle this is to keep
// painted pathing as a separate layer up to this point and apply
// after, then to do resources you check if they are in the
// terrain pathing OR in the painted pathing layer
void SC2Map::applyFillsAndFootprints()
{
for( list<point>::iterator itr = pathingFills.begin();
itr != pathingFills.end();
++itr )
{
applyFill( &(*itr) );
}
pathingFills.clear();
for( list<FootToApply>::iterator itr = footsToApply.begin();
itr != footsToApply.end();
++itr )
{
FootToApply* fta = &(*itr);
applyFootprint( &(fta->loc), fta->rot, &(fta->type), &(fta->name) );
}
footsToApply.clear();
}
void SC2Map::applyFill( point* src )
{
if( !isPlayableCell( src ) )
{
return;
}
pathingFillsToRender.push_back( *src );
map<int, point> fillSet;
map<int, point> workSet;
propagateFill( 0, 0, src, &fillSet, &workSet );
while( !workSet.empty() )
{
map<int, point>::iterator itr = workSet.begin();
point c = itr->second;
workSet.erase( makePointKey( &c ) );
fillSet[makePointKey( &c )] = c;
// DON'T PROPAGATE DIAGONALLY WITH NEW PATHING?
//propagateFill( 1, 1, &c, &fillSet, &workSet );
//propagateFill( 1, -1, &c, &fillSet, &workSet );
//propagateFill( -1, -1, &c, &fillSet, &workSet );
//propagateFill( -1, 1, &c, &fillSet, &workSet );
propagateFill( 1, 0, &c, &fillSet, &workSet );
propagateFill( 0, -1, &c, &fillSet, &workSet );
propagateFill( -1, 0, &c, &fillSet, &workSet );
propagateFill( 0, 1, &c, &fillSet, &workSet );
}
for( map<int, point>::iterator itr = fillSet.begin();
itr != fillSet.end();
++itr )
{
point c = itr->second;
for( int t = 0; t < NUM_PATH_TYPES; ++t )
{
setPathing( &c, (PathType)t, false );
}
}
}
int SC2Map::makePointKey( point* c )
{
return c->pcx + c->pcy*cxDimPlayable;
}
void SC2Map::propagateFill( int dx, int dy, point* c, map<int, point>* fillSet, map<int, point>* workSet )
{
point cn;
cn.pcSet( c->pcx + dx, c->pcy + dy );
if( !isPlayableCell( &cn ) )
{
return;
}
if( !getPathing( &cn, PATH_GROUND_NOROCKS ) )
{
return;
}
int k = makePointKey( &cn );
map<int, point>::iterator itr = fillSet->find( k );
if( itr == fillSet->end() )
{
(*workSet)[k] = cn;
}
}
void SC2Map::applyFootprint( point* c, float rot, string* type, string* name )
{
Footprint* foot = NULL;
map<string, Footprint*>* innerMap;
// if there is a footprint for the specific
// rotation, use that, otherwise default to
// the one footprint
string specificRot( *name );
if( rot > 45.01f && rot <= 135.01f ) {
specificRot.append( footprintRot270cw );
} else if( rot > 135.01f && rot <= 225.01f ) {
specificRot.append( footprintRot180cw );
} else if( rot > 225.01f && rot <= 315.01f ) {
specificRot.append( footprintRot90cw );
}
// first check local user config
innerMap = configUserLocal.type2name2foot[*type];
if( innerMap != NULL )
{
foot = (*innerMap)[specificRot];
if( foot == NULL ) {
foot = (*innerMap)[*name];
}
}
// if nothing there, check global
if( foot == NULL )
{
innerMap = configUserGlobal.type2name2foot[*type];
if( innerMap != NULL )
{
foot = (*innerMap)[specificRot];
if( foot == NULL ) {
foot = (*innerMap)[*name];
}
}
}
// note: there are NO FOOTPRINTS in the global internal
// config, so if we don't have one by now, just quit
if( foot == NULL )
{
return;
}
for( list<int>::iterator itr = foot->relativeCoordinates.begin();
itr != foot->relativeCoordinates.end();
++itr )
{
int dx = *itr;
// there should be an even number of coordinates!
++itr;
if( itr == foot->relativeCoordinates.end() )
{
printError( "Odd number of coordinates for footprint %s %s.\n",
type->data(),
name->data() );
exit( -1 );
}
int dy = *itr;
point dc;
dc.pcSet( c->pcx + dx, c->pcy + dy );
if( !isPlayableCell( &dc ) )
{
continue;
}
if( *type == "doodad" || *type == "unit" ) {
// remove all pathing
setPathing( &dc, PATH_GROUND_NOROCKS, false );
setPathing( &dc, PATH_GROUND_WITHROCKS, false );
setPathing( &dc, PATH_CWALK_NOROCKS, false );
setPathing( &dc, PATH_CWALK_WITHROCKS, false );
setPathing( &dc, PATH_GROUND_WITHROCKS_NORESOURCES, false );
setPathing( &dc, PATH_BUILDABLE, false );
setPathing( &dc, PATH_BUILDABLE_MAIN, false );
} else if( *type == "destruct" ) {
// remove from WITHROCKS pathing types
setPathing( &dc, PATH_GROUND_WITHROCKS, false );
setPathing( &dc, PATH_CWALK_WITHROCKS, false );
setPathing( &dc, PATH_GROUND_WITHROCKS_NORESOURCES, false );
Destruct* destruct = new Destruct;
destruct->loc.set( &dc );
destructs.push_back( destruct );
} else if( *type == "resource" ) {
// remove all pathing except WITHOUTRESOURCES
setPathing( &dc, PATH_GROUND_NOROCKS, false );
setPathing( &dc, PATH_GROUND_WITHROCKS, false );
setPathing( &dc, PATH_CWALK_NOROCKS, false );
setPathing( &dc, PATH_CWALK_WITHROCKS, false );
setPathing( &dc, PATH_BUILDABLE, false );
setPathing( &dc, PATH_BUILDABLE_MAIN, false );
} else if( *type == "nobuildmain" ) {
setPathing( &dc, PATH_BUILDABLE_MAIN, false );
} else if( *type == "nobuild" ) {
setPathing( &dc, PATH_BUILDABLE, false );
setPathing( &dc, PATH_BUILDABLE_MAIN, false );
} else if( *type == "losb" ) {
setPathing( &dc, PATH_BUILDABLE, false );
setPathing( &dc, PATH_BUILDABLE_MAIN, false );
LoSB* losb = new LoSB;
losb->loc.set( &dc );
losbs.push_back( losb );
} else {
printError( "Placed object %s has unknown type %s.",
name->data(),
type->data() );
exit( -1 );
}
}
}