forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker.cpp
910 lines (777 loc) · 22.3 KB
/
walker.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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "walker.hpp"
#include <iostream>
#include "building_data.hpp"
#include "exception.hpp"
#include "scenario.hpp"
#include "walker_market_buyer.hpp"
#include "walker_cart_pusher.hpp"
#include "oc3_positioni.h"
std::map<WalkerType, Walker*> Walker::_mapWalkerByID; // key=walkerType, value=instance
Walker::Walker()
{
_action._action = WA_MOVE;
_action._direction = D_NONE;
_walkerType = WT_NONE;
_walkerGraphic = WG_NONE;
_animation = NULL;
_animIndex = 0;
_speed = 1; // default speed
_isDeleted = false;
_midTileI = 7;
_midTileJ = 7;
};
Walker::~Walker()
{
}
WalkerType Walker::getType() const
{
return _walkerType;
}
Walker* Walker::getInstance(const WalkerType walkerType)
{
if (_mapWalkerByID.empty())
{
// first call to this method
_mapWalkerByID[WT_SERVICE] = new ServiceWalker(S_MAX); // dummy serviceType
_mapWalkerByID[WT_MARKET_BUYER] = new MarketBuyer();
_mapWalkerByID[WT_CART_PUSHER] = new CartPusher();
//_mapWalkerByID[WT_IMMIGRANT] = new Immigrant();
_mapWalkerByID[WT_TRAINEE] = new TraineeWalker(WTT_NONE);
}
std::map<WalkerType, Walker*>::iterator mapIt;
mapIt = _mapWalkerByID.find(walkerType);
Walker *res;
if (mapIt == _mapWalkerByID.end())
{
// THROW("Unknown walker type:" << walkerType);
res = NULL;
}
else
{
res = mapIt->second;
}
return res;
}
void Walker::timeStep(const unsigned long time)
{
switch (_action._action)
{
case WA_MOVE:
walk();
break;
default:
break;
}
}
bool Walker::isDeleted() const
{
return _isDeleted;
}
void Walker::setIJ(const int i, const int j)
{
_i = i;
_j = j;
_si = _midTileI;
_sj = _midTileJ;
_ii = 15*i + _si;
_jj = 15*j + _sj;
}
int Walker::getI() const
{
return _i;
}
int Walker::getJ() const
{
return _j;
}
int Walker::getII() const
{
return _ii;
}
int Walker::getJJ() const
{
return _jj;
}
void Walker::setPathWay(PathWay &pathWay)
{
_pathWay = pathWay;
_pathWay.begin();
onMidTile();
}
void Walker::setDestinationIJ(const int i, const int j)
{
// Propagator pathPropagator;
// pathPropagator.init();
// Tilemap& tilemap = Scenario::instance().getCity().getTilemap();
// TerrainTile ¤tTerrain = tilemap.at(_i, _j).get_terrain();
// Road* currentRoad = dynamic_cast<Road*> (currentTerrain.getOverlay());
// TerrainTile &destTerrain = tilemap.at(i, j).get_terrain();
// Road* destRoad = dynamic_cast<Road*> (destTerrain.getOverlay());
// pathPropagator.getPath(*currentRoad, *destRoad, _pathWay);
// _pathWay.begin();
// onMidTile();
}
void Walker::setSpeed(const int speed)
{
_speed = speed;
}
WalkerGraphicType Walker::getWalkerGraphic() const
{
return _walkerGraphic;
}
// ioSI: subtile index, ioI: tile index, ioAmount: distance, iMidPos: subtile offset 0, oNewTile: true if tile change, oMidTile: true if on tile center
void Walker::inc(int &ioSI, int &ioI, int &ioAmount, const int iMidPos, bool &oNewTile, bool &oMidTile)
{
oMidTile = false;
oNewTile = false;
int delta = ioAmount;
if ((ioSI<iMidPos) && (ioSI+delta>=iMidPos)) // midpos is ahead and inside the current movement
{
// we will stop at the mid tile!
delta = iMidPos - ioSI;
oMidTile = true;
}
if (ioSI+delta>15) // the start of next tile is inside the current movement
{
// we will stop at the beginning of the new tile!
delta = 16 - ioSI;
oNewTile = true;
ioI += 1; // next tile
ioSI = ioSI - 15;
}
ioAmount -= delta;
ioSI += delta;
}
// ioSI: subtile index, ioI: tile index, ioAmount: distance, iMidPos: subtile offset 0, oNewTile: true if tile change, oMidTile: true if on tile center
void Walker::dec(int &ioSI, int &ioI, int &ioAmount, const int iMidPos, bool &oNewTile, bool &oMidTile)
{
oMidTile = false;
oNewTile = false;
int delta = ioAmount;
if ((ioSI>iMidPos) && (ioSI-delta<=iMidPos)) // midpos is ahead and inside the current movement
{
// we will stop at the mid tile!
delta = ioSI - iMidPos;
oMidTile = true;
}
if (ioSI-delta<0) // the start of next tile is inside the current movement
{
// we will stop at the beginning of the new tile!
delta = ioSI+1;
oNewTile = true;
ioI -= 1; // next tile
ioSI = ioSI + 15;
}
ioAmount -= delta;
ioSI -= delta;
}
void Walker::walk()
{
if (_action._direction == D_NONE)
{
// nothing to do
return;
}
_animIndex = (_animIndex+1) % 12;
int amount = _speed; // amount of movement
bool newTile = false;
bool midTile = false;
// std::cout << "walker step, amount :" << amount << std::endl;
while (amount > 0)
{
switch (_action._direction)
{
case D_NORTH:
inc(_sj, _j, amount, _midTileJ, newTile, midTile);
break;
case D_NORTH_EAST:
inc(_sj, _j, amount, _midTileJ, newTile, midTile);
inc(_si, _i, amount, _midTileI, newTile, midTile);
break;
case D_EAST:
inc(_si, _i, amount, _midTileI, newTile, midTile);
break;
case D_SOUTH_EAST:
dec(_sj, _j, amount, _midTileJ, newTile, midTile);
inc(_si, _i, amount, _midTileI, newTile, midTile);
break;
case D_SOUTH:
dec(_sj, _j, amount, _midTileJ, newTile, midTile);
break;
case D_SOUTH_WEST:
dec(_sj, _j, amount, _midTileJ, newTile, midTile);
dec(_si, _i, amount, _midTileI, newTile, midTile);
break;
case D_WEST:
dec(_si, _i, amount, _midTileI, newTile, midTile);
break;
case D_NORTH_WEST:
inc(_sj, _j, amount, _midTileJ, newTile, midTile);
dec(_si, _i, amount, _midTileI, newTile, midTile);
break;
default:
THROW("Invalid move direction: " << _action._direction);
break;
}
if (newTile)
{
// walker is now on a new tile!
onNewTile();
}
if (midTile)
{
// walker is now on the middle of the tile!
onMidTile();
}
// if (midTile) std::cout << "walker mid tile" << std::endl;
// if (newTile) std::cout << "walker new tile" << std::endl;
// if (amount != 0) std::cout << "walker remaining step :" << amount << std::endl;
}
_ii = _i*15+_si;
_jj = _j*15+_sj;
}
void Walker::onNewTile()
{
// std::cout << "Walker is on a new tile! coord=" << _i << "," << _j << std::endl;
Tilemap& tilemap = Scenario::instance().getCity().getTilemap();
Tile& currentTile = tilemap.at(_i, _j);
if( !currentTile.get_terrain().isRoad() )
{
std::cout << "Walker at " << _i << "," << _j << " is not on a road!!!" << std::endl;
}
}
void Walker::onMidTile()
{
// std::cout << "Walker is on mid tile! coord=" << _i << "," << _j << std::endl;
if (_pathWay.isDestination())
{
onDestination();
}
else
{
// compute the direction to reach the destination
computeDirection();
}
}
void Walker::onDestination()
{
// std::cout << "Walker arrived at destination! coord=" << _i << "," << _j << std::endl;
_action._action=WA_NONE; // stop moving
_animation=NULL;
}
void Walker::onNewDirection()
{
_animation=NULL; // need to fetch the new animation
}
void Walker::computeDirection()
{
DirectionType lastDirection = _action._direction;
_action._direction = _pathWay.getNextDirection();
if (lastDirection != _action._direction)
{
onNewDirection();
}
}
DirectionType Walker::getDirection()
{
return _action._direction;
}
void Walker::getPictureList(std::vector<Picture*> &oPics)
{
oPics.clear();
oPics.push_back(&getMainPicture());
}
Picture& Walker::getMainPicture()
{
if (_animation == NULL)
{
const std::map<WalkerAction, Animation>& animMap = WalkerLoader::instance().getAnimationMap(getWalkerGraphic());
std::map<WalkerAction, Animation>::const_iterator itAnimMap;
if (_action._action == WA_NONE || _action._direction == D_NONE)
{
WalkerAction action;
action._action = WA_MOVE; // default action
if (_action._direction == D_NONE)
{
action._direction = D_NORTH; // default direction
}
else
{
action._direction = _action._direction; // last direction of the walker
}
_animIndex = 0; // first animation frame
itAnimMap = animMap.find(action);
}
else
{
itAnimMap = animMap.find(_action);
}
_animation = &(itAnimMap->second);
}
return *_animation->get_pictures()[_animIndex];
}
void Walker::serialize(OutputSerialStream &stream)
{
stream.write_objectID(this);
stream.write_int((int) _walkerType, 1, 0, WT_MAX);
_pathWay.serialize(stream);
stream.write_int((int) _action._action, 1, 0, WA_MAX);
stream.write_int((int) _action._direction, 1, 0, D_MAX);
stream.write_int(_i, 2, 0, 1000);
stream.write_int(_j, 2, 0, 1000);
stream.write_int(_si, 1, 0, 50);
stream.write_int(_sj, 1, 0, 50);
stream.write_int(_ii, 4, 0, 1000000);
stream.write_int(_jj, 4, 0, 1000000);
stream.write_int(_speed, 1, 0, 50);
stream.write_int(_midTileI, 1, 0, 50);
stream.write_int(_midTileJ, 1, 0, 50);
stream.write_int(_animIndex, 1, 0, 50);
}
Walker& Walker::unserialize_all(InputSerialStream &stream)
{
int objectID = stream.read_objectID();
WalkerType walkerType = (WalkerType) stream.read_int(1, 0, WT_MAX);
Walker *instance = getInstance(walkerType);
Walker *res = instance->clone();
res->unserialize(stream);
stream.link(objectID, res);
return *res;
}
void Walker::unserialize(InputSerialStream &stream)
{
_pathWay.unserialize(stream);
_action._action = (WalkerActionType) stream.read_int(1, 0, WA_MAX);
_action._direction = (DirectionType) stream.read_int(1, 0, D_MAX);
_i = stream.read_int(2, 0, 1000);
_j = stream.read_int(2, 0, 1000);
_si = stream.read_int(1, 0, 50);
_sj = stream.read_int(1, 0, 50);
_ii = stream.read_int(4, 0, 1000000);
_jj = stream.read_int(4, 0, 1000000);
_speed = stream.read_int(1, 0, 50);
_midTileI = stream.read_int(1, 0, 50);
_midTileJ = stream.read_int(1, 0, 50);
_animIndex = stream.read_int(1, 0, 50);
}
class Immigrant::Impl
{
public:
Point destination;
};
Immigrant::Immigrant() : _d( new Impl )
{
_walkerType = WT_IMMIGRANT;
_walkerGraphic = WG_HOMELESS;
}
Immigrant* Immigrant::clone() const
{
Immigrant* ret = new Immigrant();
ret->_d->destination = _d->destination;
return ret;
}
void Immigrant::assignPath( const Building& home )
{
City& city = Scenario::instance().getCity();
Tile& exitTile = city.getTilemap().at( city.getRoadExitI(), city.getRoadExitJ() );
Road* exitRoad = dynamic_cast< Road* >( exitTile.get_terrain().getOverlay() );
if( exitRoad )
{
Propagator pathfinder;
PathWay pathWay;
pathfinder.init( const_cast< Building& >( home ) );
bool findPath = pathfinder.getPath( *exitRoad, pathWay );
if( findPath )
{
setPathWay( pathWay );
setIJ(_pathWay.getOrigin().getI(), _pathWay.getOrigin().getJ());
}
}
else
_isDeleted = true;
}
void Immigrant::onDestination()
{
_isDeleted = true;
}
Immigrant* Immigrant::create( const Building& startPoint )
{
Immigrant* newImmigrant = new Immigrant();
newImmigrant->assignPath( startPoint );
return newImmigrant;
}
Immigrant::~Immigrant()
{
}
Soldier::Soldier()
{
_walkerType = WT_SOLDIER;
_walkerGraphic = WG_HORSEMAN;
}
Soldier* Soldier::clone() const
{
return new Soldier(*this);
}
ServiceWalker::ServiceWalker(const ServiceType service)
{
_walkerType = WT_SERVICE;
_building = NULL;
_maxDistance = 5; // TODO: _building.getMaxDistance() ?
init(service);
}
void ServiceWalker::init(const ServiceType service)
{
_service = service;
switch (_service)
{
case S_WELL:
case S_FOUNTAIN:
case S_SENATE:
case S_TEMPLE_ORACLE:
_walkerGraphic = WG_NONE;
break;
case S_ENGINEER:
_walkerGraphic = WG_ENGINEER;
break;
case S_PREFECT:
_walkerGraphic = WG_PREFECT;
break;
case S_TEMPLE_NEPTUNE:
case S_TEMPLE_CERES:
case S_TEMPLE_VENUS:
case S_TEMPLE_MARS:
case S_TEMPLE_MERCURE:
_walkerGraphic = WG_PRIEST;
break;
case S_DOCTOR:
case S_HOSPITAL:
_walkerGraphic = WG_DOCTOR;
break;
case S_BARBER:
_walkerGraphic = WG_BARBER;
break;
case S_BATHS:
_walkerGraphic = WG_BATH;
break;
case S_SCHOOL:
_walkerGraphic = WG_CHILD;
break;
case S_LIBRARY:
case S_COLLEGE:
_walkerGraphic = WG_LIBRARIAN;
break;
case S_THEATER:
case S_AMPHITHEATER:
case S_HIPPODROME:
case S_COLLOSSEUM:
_walkerGraphic = WG_ACTOR;
break;
case S_MARKET:
_walkerGraphic = WG_CHILD;
break;
case S_FORUM:
_walkerGraphic = WG_TAX;
break;
case S_MAX:
break;
}
}
ServiceWalker* ServiceWalker::clone() const
{
return new ServiceWalker(*this);
}
void ServiceWalker::setServiceBuilding(ServiceBuilding &building)
{
_building = &building;
}
ServiceBuilding &ServiceWalker::getServiceBuilding()
{
if (_building == NULL) THROW("ServiceBuilding is not initialized");
return *_building;
}
ServiceType ServiceWalker::getService()
{
return _service;
}
void ServiceWalker::computeWalkerPath()
{
std::list<PathWay> pathWayList;
Propagator pathPropagator;
pathPropagator.init(*_building);
pathPropagator.getAllPaths(_maxDistance, pathWayList);
float maxPathValue = 0.0;
PathWay* bestPath = NULL;
for (std::list<PathWay>::iterator itPath = pathWayList.begin(); itPath != pathWayList.end(); ++itPath)
{
PathWay &path = *itPath;
float pathValue = evaluatePath(path);
if (pathValue > maxPathValue)
{
bestPath = &path;
maxPathValue = pathValue;
}
}
if (bestPath == NULL)
{
// no good path
_isDeleted = true;
return;
}
reservePath(*bestPath);
setPathWay(*bestPath);
setIJ(_pathWay.getOrigin().getI(), _pathWay.getOrigin().getJ());
Scenario::instance().getCity().getWalkerList().push_back(this);
}
std::set<Building*> ServiceWalker::getReachedBuildings(const int i, const int j)
{
std::set<Building*> res;
int reachDistance = 2;
int i1 = i - reachDistance;
int j1 = j - reachDistance;
int i2 = i + reachDistance;
int j2 = j + reachDistance;
std::list<Tile*> reachedTiles = Scenario::instance().getCity().getTilemap().getFilledRectangle(i1, j1, i2, j2);
for (std::list<Tile*>::iterator itTile = reachedTiles.begin(); itTile != reachedTiles.end(); ++itTile)
{
Tile &tile = **itTile;
TerrainTile &terrain = tile.get_terrain();
if (terrain.isBuilding())
{
Building* building = dynamic_cast<Building*>( terrain.getOverlay() );
if( building )
{
res.insert(building);
}
}
}
return res;
}
float ServiceWalker::evaluatePath(PathWay &pathWay)
{
// evaluate all buildings along the path
std::set<Building*> doneBuildings; // list of evaluated building: don't do them again
std::list<Tile*>& pathTileList = pathWay.getAllTiles();
int distance = 0;
float res = 0.0;
for (std::list<Tile*>::iterator itTile = pathTileList.begin(); itTile != pathTileList.end(); ++itTile)
{
Tile &tile = **itTile;
std::set<Building*> reachedBuildings = getReachedBuildings(tile.getI(), tile.getJ());
for (std::set<Building*>::iterator itBuilding = reachedBuildings.begin(); itBuilding != reachedBuildings.end(); ++itBuilding)
{
Building& building = **itBuilding;
std::pair<std::set<Building*>::iterator, bool> rc = doneBuildings.insert(&building);
if (rc.second == true)
{
// the building has not been evaluated yet
res = res + building.evaluateService(*this);
}
}
distance++;
}
// std::cout << "evaluate path ";
// pathWay.prettyPrint();
// std::cout << " = " << res << std::endl;
return res;
}
void ServiceWalker::reservePath(PathWay &pathWay)
{
// reserve all buildings along the path
std::set<Building*> doneBuildings; // list of evaluated building: don't do them again
std::list<Tile*>& pathTileList = pathWay.getAllTiles();
for (std::list<Tile*>::iterator itTile = pathTileList.begin(); itTile != pathTileList.end(); ++itTile)
{
Tile &tile = **itTile;
std::set<Building*> reachedBuildings = getReachedBuildings(tile.getI(), tile.getJ());
for (std::set<Building*>::iterator itBuilding = reachedBuildings.begin(); itBuilding != reachedBuildings.end(); ++itBuilding)
{
Building &building = **itBuilding;
std::pair<std::set<Building*>::iterator, bool> rc = doneBuildings.insert(&building);
if (rc.second == true)
{
// the building has not been reserved yet
building.reserveService(_service);
}
}
}
}
void ServiceWalker::start()
{
computeWalkerPath();
}
void ServiceWalker::onNewTile()
{
Walker::onNewTile();
std::set<Building*> reachedBuildings = getReachedBuildings(getI(), getJ());
for (std::set<Building*>::iterator itBuilding = reachedBuildings.begin(); itBuilding != reachedBuildings.end(); ++itBuilding)
{
Building &building = **itBuilding;
building.applyService(*this);
}
}
void ServiceWalker::onDestination()
{
Walker::onDestination();
if (_pathWay.isReverse())
{
// walker is back in the market
_isDeleted= true;
}
else
{
// walker finished service => get back to service building
_pathWay.rbegin();
_action._action=WA_MOVE;
computeDirection();
}
}
void ServiceWalker::serialize(OutputSerialStream &stream)
{
Walker::serialize(stream);
stream.write_int((int) _service, 1, 0, S_MAX);
stream.write_objectID(_building);
stream.write_int(_maxDistance, 2, 0, 65535);
}
void ServiceWalker::unserialize(InputSerialStream &stream)
{
Walker::unserialize(stream);
_service = (ServiceType) stream.read_int(1, 0, S_MAX);
init(_service);
stream.read_objectID((void**)&_building);
_maxDistance = stream.read_int(2, 0, 65535);
}
TraineeWalker::TraineeWalker(const WalkerTraineeType traineeType)
{
_walkerType = WT_TRAINEE;
_originBuilding = NULL;
_destinationBuilding = NULL;
_maxDistance = 30;
init(traineeType);
}
void TraineeWalker::init(const WalkerTraineeType traineeType)
{
_traineeType = traineeType;
switch (_traineeType)
{
case WTT_ACTOR:
_walkerGraphic = WG_ACTOR;
_buildingNeed.push_back(B_THEATER);
_buildingNeed.push_back(B_AMPHITHEATER);
break;
case WTT_GLADIATOR:
_walkerGraphic = WG_GLADIATOR;
_buildingNeed.push_back(B_AMPHITHEATER);
_buildingNeed.push_back(B_COLLOSSEUM);
break;
case WTT_TAMER:
_walkerGraphic = WG_TAMER;
_buildingNeed.push_back(B_COLLOSSEUM);
break;
case WTT_CHARIOT:
_walkerGraphic = WG_NONE; // TODO
break;
case WTT_NONE:
case WTT_MAX:
break;
}
}
TraineeWalker* TraineeWalker::clone() const
{
return new TraineeWalker(*this);
}
void TraineeWalker::setOriginBuilding(Building &originBuilding)
{
_originBuilding = &originBuilding;
}
WalkerTraineeType TraineeWalker::getType() const
{
return _traineeType;
}
void TraineeWalker::computeWalkerPath()
{
_maxNeed = 0; // need of this trainee in buildings
Propagator pathPropagator;
pathPropagator.init(*_originBuilding);
pathPropagator.propagate(_maxDistance);
for (std::list<BuildingType>::iterator itType = _buildingNeed.begin(); itType != _buildingNeed.end(); ++itType)
{
BuildingType buildingType = *itType;
checkDestination(buildingType, pathPropagator);
}
if (_destinationBuilding != NULL)
{
// some building needs that trainee!
// std::cout << "trainee sent!" << std::endl;
PathWay pathWay;
pathPropagator.getPath(*_destinationBuilding, pathWay);
setPathWay(pathWay);
setIJ(_pathWay.getOrigin().getI(), _pathWay.getOrigin().getJ());
Scenario::instance().getCity().getWalkerList().push_back(this);
_destinationBuilding->reserveTrainee(_traineeType);
}
else
{
// nobody needs him...
// std::cout << "trainee suicide!" << std::endl;
_isDeleted = true;
}
}
void TraineeWalker::checkDestination(const BuildingType buildingType, Propagator &pathPropagator)
{
std::map<Building*, PathWay> pathWayList;
pathPropagator.getReachedBuildings(buildingType, pathWayList);
for (std::map<Building*, PathWay>::iterator pathWayIt= pathWayList.begin(); pathWayIt != pathWayList.end(); ++pathWayIt)
{
// for every building within range
Building& building = *(pathWayIt->first);
float need = building.evaluateTrainee(_traineeType);
if (need > _maxNeed)
{
_maxNeed = need;
_destinationBuilding = &building;
}
}
}
void TraineeWalker::start()
{
computeWalkerPath();
}
void TraineeWalker::onDestination()
{
Walker::onDestination();
_isDeleted= true;
_destinationBuilding->applyTrainee(_traineeType);
}
void TraineeWalker::serialize(OutputSerialStream &stream)
{
Walker::serialize(stream);
stream.write_int((int) _traineeType, 1, 0, WTT_MAX);
stream.write_objectID(_originBuilding);
stream.write_objectID(_destinationBuilding);
stream.write_int(_maxDistance, 2, 0, 65535);
}
void TraineeWalker::unserialize(InputSerialStream &stream)
{
Walker::unserialize(stream);
_traineeType = (WalkerTraineeType) stream.read_int(1, 0, WTT_MAX);
init(_traineeType);
stream.read_objectID((void**)&_originBuilding);
stream.read_objectID((void**)&_destinationBuilding);
_maxDistance = stream.read_int(2, 0, 65535);
}