-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreature.cpp
745 lines (670 loc) · 21.1 KB
/
Creature.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
/*
* Creature.cpp
* sfmltest1
*
* Created by Steven Hamilton on 25/06/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.Creature
*
*/
#include <iostream>
#include <string>
#include <vector>
#include "Creature.h"
#include "DisplayManager.h"
#include "fov.h"
#include "GameWorld.h"
#include "Logger.h"
#include "NameGenerator.h"
#include "Utility.h"
#include "Item.h"
class GameWorld;
Creature::Creature()
{
isAlive = true;
faction = FRIEND;
gfxId=PLAYER_S;
fovMap.resize(MAPHEIGHT);
endTurn = false;
firingMode=NONE;
for (int i=0; i < MAPHEIGHT; ++i)
fovMap[i].resize(MAPWIDTH);
//redraw=true;
}
Creature::~Creature()
{
}
Creature::Creature(creature_t creatureType, faction_t fact)
{
Utility tool;
NameGenerator nameGen;
objectId=WORLD.getObjectId();
type=creatureType;
endTurn = false;
reserveMode=NONE;
onMap=false;
fovMap.resize(MAPHEIGHT);
for (int i=0; i < MAPHEIGHT; ++i)
fovMap[i].resize(MAPWIDTH);
level = 1;
xp = 0;
oldXP=0;
spareSkillPoints=0;
//initial skills list. Will be replaced by skills object in world.
skills["Clubs"]=0;
skills["Daggers"]=0;
skills["HandGuns"]=0;
skills["Carbines"]=0;
skills["LongGuns"]=0;
skills["ShortSwords"]=0;
generateSkills(type);
switch (type) {
case HUMAN:{
name = nameGen.name();
xpValue=100;
strength=tool.rollDice(3,6);
dexterity=tool.rollDice(3,6);
intelligence=tool.rollDice(3,6);
totalHitPoints = strength + tool.rollDice(1,10);
currentHitPoints = totalHitPoints;
totalActionPoints = dexterity + tool.rollDice(4,6);
currentActionPoints = totalActionPoints;
isAlive = true;
faction = fact;
gfxId=PLAYER_S;
wielding=0;
}
break;
case SPIDER:{
name = "Arach";
xpValue=MONSTER_SPIDER_XP;
strength=tool.rollDice(2,6);
dexterity=tool.rollDice(3,6);
intelligence=tool.rollDice(1,6);
totalHitPoints = 7;
currentHitPoints = totalHitPoints;
totalActionPoints = 15;
currentActionPoints = totalActionPoints;
isAlive = true;
faction = fact;
gfxId=SPIDER_S;
position.x = (rand() % (MAPWIDTH-10))+10;
position.y = rand() % MAPHEIGHT;
//init AI
aiFSM.initMachine(AI_MACH_SLITHER);
//create a weapon for it
Item* weapon=WORLD.generateItem(WOOD_MANDIBLE, objectId);
weapon->onMap=true;
wielding = weapon->objectId;
}
break;
case SLITHER:{
name="Slither";
xpValue=MONSTER_SLITHER_XP;
strength=tool.rollDice(2,6);
dexterity=tool.rollDice(3,6);
intelligence=tool.rollDice(1,6);
totalHitPoints=12;
currentHitPoints=totalHitPoints;
totalActionPoints=16;
currentActionPoints=totalActionPoints;
isAlive=true;
faction=fact;
gfxId=SLITHER_S;
position.x=(rand() % (MAPWIDTH-10))+10;
position.y=rand() % MAPHEIGHT;
aiFSM.initMachine(AI_MACH_SLITHER);
//create a weapon for it
Item* weapon=WORLD.generateItem(BRASS_FANG, objectId);
weapon->onMap=true;
weapon->ownerObjectId=objectId;
wielding = weapon->objectId;
}
break;
case GIMP:{
name = "Gimp";
xpValue=MONSTER_GIMP_XP;
strength=tool.rollDice(3,6);
dexterity=tool.rollDice(3,6);
intelligence=tool.rollDice(3,6);
totalHitPoints = 20;
currentHitPoints = totalHitPoints;
totalActionPoints = 20;
currentActionPoints = totalActionPoints;
isAlive = true;
faction = fact;
gfxId=GIMP_S;
position.x = (rand() % (MAPWIDTH-10))+10;
position.y = rand() % MAPHEIGHT;
//init AI
aiFSM.initMachine(AI_MACH_GIMP);
//create a weapon for it
Item* weapon=WORLD.generateItem(MUSKETOON, objectId);
weapon->onMap=true;
wielding = weapon->objectId;
}
break;
default:
break;
}
}
Creature::Creature(std::map<std::string, std::string> &attribs, Position pos,std::map<std::string, int> &skillmap)
{
Utility tool;
reserveMode=NONE;
fovMap.resize(MAPHEIGHT);
for (int i=0; i < MAPHEIGHT; ++i)
fovMap[i].resize(MAPWIDTH);
//update attributes
objectId=tool.stringToInt(attribs["objectId"]);
gfxId=(gfxId_t)tool.stringToInt(attribs["gfxId"]);
name=attribs["name"];
type=(creature_t)tool.stringToInt(attribs["type"]);
faction=(faction_t)tool.stringToInt(attribs["faction"]);
xp=tool.stringToInt(attribs["xp"]);
xpValue=tool.stringToInt(attribs["xpValue"]);
oldXP=tool.stringToInt(attribs["oldXP"]);
spareSkillPoints=tool.stringToInt(attribs["spareSkillPoints"]);
strength=tool.stringToInt(attribs["strength"]);
dexterity=tool.stringToInt(attribs["dexterity"]);
intelligence=tool.stringToInt(attribs["intelligence"]);
totalHitPoints=tool.stringToInt(attribs["totalHitPoints"]);
totalActionPoints=tool.stringToInt(attribs["totalActionPoints"]);
currentHitPoints=tool.stringToInt(attribs["currentHitPoints"]);
currentActionPoints=tool.stringToInt(attribs["currentActionPoints"]);
wielding=tool.stringToInt(attribs["wielding"]);
onMap=(bool)tool.stringToInt(attribs["onMap"]);
isAlive=(bool)tool.stringToInt(attribs["isAlive"]);
std::cout << "Creating custom creature : " << name << " wielding= " << wielding << std::endl;
switch (type){
case SPIDER:{
//init AI
aiFSM.initMachine(AI_MACH_SLITHER);
break;
}
case SLITHER:{
//init AI
aiFSM.initMachine(AI_MACH_SLITHER);
break;
}
case GIMP:{
aiFSM.initMachine(AI_MACH_GIMP);
break;
}
default:
break;
}
//update position
position = pos;
//update skills
skills = skillmap;
}
void Creature::generateSkills(creature_t type)
{
Utility tool;
std::map<std::string, int>::iterator it;
for (it=skills.begin();it!=skills.end();++it){
it->second=tool.rollDice(4,10);
}
switch (type){
case HUMAN:{
break;
}
case SLITHER:{
break;
}
case GIMP:{
skills["Pistol"]=tool.rollDice(4,12);
break;
}
default:
break;
}
}
int Creature::capacity()
{
return strength*STR_ENC_MODIFIER;
}
bool Creature::addToInventory(Item * item)
{
//we check weight of object and current encumberance.
std::vector<Item*> items=WORLD.getItems(objectId);
std::vector<Item*>::iterator it;
int encumberance=0;
for (it=items.begin();it<items.end();it++){
Item * item2=*it;
encumberance+=item2->weight;
}
if (item->weight<=capacity()-encumberance){
item->ownerObjectId=objectId;
return true;
}
return false;
}
std::string Creature::encumberanceString()
{
Utility tool;
std::vector<Item*> items=WORLD.getItems(objectId);
std::vector<Item*>::iterator it;
int encumberance=0;
for (it=items.begin();it<items.end();it++){
Item * item=*it;
encumberance+=item->weight;
}
std::string str=tool.stringFromInt(encumberance)+"/"+tool.stringFromInt(capacity());
return str;
}
bool Creature::move(move_t direction)
{
bool moved=false;
//validate the position is on map
Utility tool;
//validate move direction
if (direction > 9){
LOG << objectId << " Invalid move_t";
return moved;
}
//get our new position
Position newPosition=tool.getPositionFromDirection(position, direction);
//check its on the map
if (newPosition.validatePosition()){
if (isWielding(MELEE)){
//if someone occupies the new tile then attack
if (WORLD.isTileOccupied(newPosition)){
Creature mob = WORLD.getCreature(newPosition);
if (faction != mob.faction) attack(MELEE,direction);
}
//if we're moving onto a block terrain type then attack it
if (!WORLD.map.isTerrainPassable(newPosition)) attack(MELEE,direction);
}
//if noone occupies it then attempt a move
if (!WORLD.isTileOccupied(newPosition)){
//first check how much a move costs
int moveCost=WORLD.map.getMoveCost(newPosition);
int reserveCost;
if (reserveOn){
reserveCost=getReserveCost();
} else {
reserveCost=0;
}
bool canMove = hasActionPoints(moveCost+reserveCost);
LOG <<objectId << " moveCost:" << tool.stringFromInt(moveCost) << " onto "+newPosition.string();
LOG <<objectId << "reserveCost:" << tool.stringFromInt(reserveCost) << " onto "+newPosition.string();
if (!canMove && reserveCost>0){
if (faction==FRIEND) PROMPT("AP Reserved");
} else if (!canMove){
if (faction==FRIEND) PROMPT("Not enough AP");
}
//first ask the map if the tile is passable and we can afford to move
if(WORLD.map.isTerrainPassable(newPosition) && canMove){
//deduct AP
currentActionPoints -= moveCost;
//clear the tile we're leaving
WORLD.map.setTilePassable(position, true);
//move creature onto new tile
position=newPosition;
//make it unpassable
WORLD.map.setTilePassable(position, false);
moved=true;
}
}
}
return moved;
}
int Creature::damage(int damage)
{
int resultantDamage = damage;
currentHitPoints-=damage;
if (currentHitPoints<=0){
currentHitPoints=0;
setDead();
}
Utility tool;
std::cout << name << " is hit for " << damage << " and has " << currentHitPoints << " left " << std::endl;
MSG(name + " is hit for " + tool.stringFromInt(damage));
return resultantDamage;
}
void Creature::setDead()
{
isAlive=false;
onMap=false;
}
bool Creature::hasActionPoints(int cost)
{
if (currentActionPoints >= cost) {
return true;
} else {
return false;
}
}
bool Creature::isWielding()
{
bool result = false;
if (wielding) result = true;
//std::cout << "Creature Check : " << name << " wielding= " << wielding << std::endl;
return result;
}
bool Creature::isWielding(itemAttackType_t type)
{
bool result = false;
if (wielding){
Item * item = WORLD.getItemForObjectId(wielding);
if (item->itemAttackType==type) result = true;
}
return result;
}
itemAttackType_t Creature::wieldingAttackType()
{
itemAttackType_t result;
if (isWielding(MELEE)) result = MELEE;
else result = RANGED;
return result;
}
void Creature::resetAP()
{
currentActionPoints = totalActionPoints;
}
void Creature::resetHP()
{
currentHitPoints = totalHitPoints;
}
void Creature::resetOldXP()
{
oldXP=xp;
}
void Creature::processFOV()
{
//updates creatures awareness
Utility tool;
Fov::prepareTransparencyMap();
Fov::calculate (position.x, position.y, WORLD.map.parameterMap["visibility"]);
//copy visible tiles into creatures fovMap
for (int i=0;i < MAPWIDTH;i++){
for (int j=0;j < MAPHEIGHT;j++){
fovMap[j][i] = Fov::fovMap[i][j];
//std::cout << fovMap[j][i];
}
//std::cout << std::endl;
}
//scan map for enemies and save their positions
visibleEnemies.resize(0);
//iterate the worlds onMap creatures and find the one's that matches a visible tile position and are enemies
//get a list of all onMap creatures
std::vector<Creature*> creatureList=WORLD.getCreatures(true);
for (int p = 0;p < creatureList.size();p++){
if (fovMap[creatureList[p]->position.y][creatureList[p]->position.x] && creatureList[p]->faction!=faction && creatureList[p]->isAlive) {
visibleEnemies.push_back(creatureList[p]->position);
//std::cout << "id:" << objectId << " " << name << " is adding enemy id:" << creatureList[p]->objectId << " " << creatureList[p]->name <<" "<< creatureList[p]->position.string() << std::endl;
}
}
//std::cout << "visibleEnemies.size=" << tool.stringFromInt(visibleEnemies.size()) << std::endl;
}
void Creature::processReaction()
{
Utility tool;
//process our reaction to recent events. THis does not contain initiative
//or chance code
//first update awareness
processFOV();
WORLD.updateMapVisibility();
//find a target and check we're wielding
if(aiFindTarget() && isWielding(RANGED)){
//roll for initiative
//fetch targets DEX
int targetDex;
Creature target=WORLD.getCreature(WORLD.target);
targetDex=target.dexterity;
int toHit = (float)dexterity/(float)targetDex*100/2;
int rollToDefend = tool.rollDice(1,100);
//std::cout << "Reaction Roll: targetDex:" << targetDex << " dex:" << dexterity << " rollTODefend:" << rollToDefend << " against toHit:" << toHit << std::endl;
if (toHit>rollToDefend){
LOG << objectId << " Wins reaction roll. Attacking!";
attack(RANGED, NO_MOVE);
}
}
}
// ACTION CODE
bool Creature::attack(itemAttackType_t attackType, move_t direction)
{
LOG << "Creature::attack";
Utility tool;
bool attacked=false;
//set firingMode mod
switch (firingMode) {
case AIMED:
firingModeMod=SKILL_AIMED_MODIFIER;
break;
case SNAPSHOT:
firingModeMod=1/SKILL_SNAPSHOT_MODIFIER;
default:
break;
}
//check we have AP to attack
int firingCost=getFiringCost();
LOG << objectId << " Attempting attack with AP:" << tool.stringFromInt(currentActionPoints) << " and firingCost:" << tool.stringFromInt(firingCost);
//only attack if targetting something and we have action points
if (hasActionPoints(firingCost)){
switch (attackType){
case RANGED:
//don't shoot ourselves. :)
if (!(position==WORLD.target)){
//remove AP
currentActionPoints -=firingCost;
//get exact angle to the target position
double angle = tool.getAngle(position, WORLD.target);
//LOG <<"angle:"+tool.stringFromDouble(angle));
//apply modifiers
double modangle = modifyAimAngle(angle);
// LOG <<"modangle:"+tool.stringFromDouble(modangle));
WORLD.attack(this, modangle);
LOG << objectId << name << " AP after attack is:" << tool.stringFromInt(currentActionPoints);
attacked=true;
}
break;
case MELEE:{
Item * item = WORLD.getItemForObjectId(wielding);
int cost = item->properties["APCost"];
currentActionPoints -= cost;
WORLD.attack(this, direction);
attacked=true;
break;
}
default:
break;
}
} else {
LOG << objectId << " No AP for attack";
attacked=false;
}
return attacked;
}
double Creature::modifyAimAngle(double targetAngle)
{
Utility tool;
//get the weapon we're attacking with
Item * weapon = WORLD.getItemForObjectId(wielding);
//std::cout << "Wielded weapon is:" << weapon->name << " using skill: " << weapon->skill << std::endl;
//calc base range constant for selected weapon. This is the angle that
//would definitely hit the target tile at "Range"
float slope=0.5/weapon->properties["Range"];
std::cout << "slope angle=" << tool.stringFromDouble(slope) << " range:" << weapon->properties["Range"] << std::endl;
//get angle in degrees (0 is right)
float toleranceAngle = atan(slope) * 180/PI * 2;
std::cout << "tolerance angle=" << tool.stringFromDouble(toleranceAngle) << std::endl;
//add skill mod
float skillMod=(float)skills[weapon->skill]/100;
skillMod=1/skillMod/2;
toleranceAngle*=skillMod;
std::cout << "tolerance angle after skillmod=" << tool.stringFromDouble(toleranceAngle) << " skillMod:" << tool.stringFromDouble(skillMod) << std::endl;
toleranceAngle*=firingModeMod;
std::cout << "tolerance angle after firingModMod=" << tool.stringFromDouble(toleranceAngle) << " firingModeMod:" << tool.stringFromDouble(firingModeMod) << std::endl;
//add DEX mod
//// this is modded against STR.
float strMod=(float)strength/20;
float dexMod=(float)dexterity/20*(100/weapon->weight)*strMod;
toleranceAngle*=dexMod;
std::cout << "tolerance angle after dexMod=" << tool.stringFromDouble(toleranceAngle) << " dexMod:" << tool.stringFromDouble(dexMod) << std::endl;
//modify our target angle by a random amount inside our tolerance.
double angle=targetAngle+tool.randFloat(toleranceAngle*-1, toleranceAngle);
std::cout << "final firing angle=" << tool.stringFromDouble(angle) << std::endl;
return angle;
}
int Creature::getFiringCost()
{
//returns the AP cost to firing the current weapon
Item * item = WORLD.getItemForObjectId(wielding);
int apCost=item->properties["APCost"];
int cost;
switch (firingMode) {
case SNAPSHOT:
cost=apCost*SKILL_SNAPSHOT_MODIFIER;
default:
//AIMED or melee
cost=apCost*SKILL_AIMED_MODIFIER;
break;
}
return cost;
}
void Creature::setFiringMode(firingMode_t mode)
{
firingMode = mode;
}
void Creature::setReserve(firingMode_t mode)
{
reserveMode=mode;
if (mode==NONE){
reserveOn=false;
} else {
reserveOn=true;
}
std::cout << "reserveOn= " << reserveOn << std::endl;
}
firingMode_t Creature::getReserveMode()
{
return reserveMode;
}
int Creature::getReserveCost()
{
//returns the AP cost to firing the current weapon
Item * item = WORLD.getItemForObjectId(wielding);
int apCost=item->properties["APCost"];
int cost;
switch (reserveMode) {
case AIMED:
cost=apCost*SKILL_AIMED_MODIFIER;
break;
case SNAPSHOT:
cost=apCost*SKILL_SNAPSHOT_MODIFIER;
default:
break;
}
return cost;
}
//--------------------------------
//---------AI CODE BELOW HERE-----
//--------------------------------
bool Creature::aiFindTarget()
{
Utility tool;
double distance = 9999;
bool targetting=false;
//iterate through visibleEnemies and select the nearest valid one as target
for (int i=0;i < visibleEnemies.size();i++){
std::cout << "visibleEnemies.size=" << visibleEnemies.size() << std::endl;
double newDistance=tool.getDistance(position, visibleEnemies[i]);
if (newDistance<distance) {
WORLD.target=visibleEnemies[i];
//setTarget();
std::cout << name << " is targetting " << visibleEnemies[i].x << ":" << visibleEnemies[i].y << " at distance " << newDistance << std::endl;
return true;
}
}
return targetting;
}
Position Creature::aiFindSafety()
{
Utility tool;
std::vector<Position> positions;
positions.resize(0);
int positionsIndex=0;
Position goodPosition;
bool foundSafety=false;
//fetch a pathWeight map for our current position
std::vector<std::vector<Position> > pathWeights=tool.pathWeight(position);
//iterate map and select the one's within range
for (int i=0; i<MAPHEIGHT;i++){
for (int j=0; j<MAPWIDTH;j++){
Position selectedPosition = pathWeights[i][j];
if (selectedPosition.getPathWeight() <= currentActionPoints+1 && selectedPosition.getPathWeight()!=0 && selectedPosition.getPathWeight()!=1) positions.push_back(selectedPosition);
}
}
//we then find the closest ones that we can't see our visible enemies from. To do this we must fetch a FOV map for each tile.
//sort the list in order of pathweight so we start from the middle
std::sort(positions.begin(), positions.end());
//start iterating through all reachable positions.
Position currentPosition;
retry:
while (!foundSafety){
currentPosition=positions[positionsIndex];
//fetch FOV map from that position: NOTE! Remove monster positions from map before processing. Then they won't hide behind themselves.
Fov::prepareTransparencyMap();
//modify transparencyMap removing our monsters so they don't block vision
std::vector<Creature*> creatureList = WORLD.getCreatures(ENEMY, true);
for (int m=0;m<creatureList.size();m++){
Fov::transparencyMap[creatureList[m]->position.x][creatureList[m]->position.y]=1;
}
//fetch FOV
Fov::calculate (currentPosition.x, currentPosition.y, VISIBILITY_RANGE);
//Iterate through all players;
creatureList = WORLD.getCreatures(FRIEND, true);
for (int p=0; p<creatureList.size();++p) {
//if player's position is in the fovMap then it's visible. Jump out and try for next tile;
if (Fov::fovMap[creatureList[p]->position.x][creatureList[p]->position.y]) {
positionsIndex++;
if (positionsIndex==positions.size()){
goodPosition.setXY(-1,-1);
goto giveup;
}
goto retry;
}
}
goodPosition=currentPosition;
foundSafety=true;
}
giveup:
//print some debug
//std::cout << "Found the following positions" << std::endl;
//for (int i=0;i<20;i++){
// std::cout << positions[i].string() << std::endl;
//}
return goodPosition;
}
Position Creature::aiFindPatrolPoint()
{
Utility tool;
//returns a valid patrol point.
Position point;
std::vector<move_t> moves;
int attempt=0;
do {
point.x = rand() % MAPWIDTH;
point.y = rand() % MAPHEIGHT;
//check we can reach it or it's not valid.
moves=tool.findPath(position, point);
attempt++;
}
while (moves.size()==0 && attempt < 20);
//ensure we return an invalid position is the find failed;
if (attempt>19) point.setXY(-1,-1);
return point;
}
void Creature::aiMoveTo(std::vector<move_t> moves)
{
//iterate moves list, move one square then update display until end
for (int i=moves.size()-1; i > -1; i--) {
move(moves[i]);
std::cout << moves[i];
DISPLAY.drawScreen();
sf::Sleep(0.2f);
}
std::cout << std::endl;
}