-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCharacter.cpp
More file actions
1503 lines (1217 loc) · 27.5 KB
/
Character.cpp
File metadata and controls
1503 lines (1217 loc) · 27.5 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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef WORLDSIM_CHARACTER_CPP
#define WORLDSIM_CHARACTER_CPP
/* WorldSim: Character
#include "Character.cpp"
Implementation of Character.hpp
*/
#include "Social.cpp"
#include "World.hpp"
#include "Character.hpp"
#include "Character_Knowledge.hpp"
#include "Tribe.hpp"
#include "Job.cpp"
#include "Location.hpp"
const int MAX_CHILDREN = 5;
//#include <Graphics/Texture/Texture.hpp>
class Texture;
Character::Character(): social(this)
{
firstName="";
lastName="";
epithet="";
/* Give every character a unique number */
static long unsigned int STATIC_ID = 0;
id = STATIC_ID++;
isMale = true;
age = -1;
daysCounter=0;
tickCounter=0;
isAlive=true;
isMarried=false;
isPregnant=false;
pregnantCounter=0;
actionPoints = 0;
father=0;
mother=0;
spouse = 0;
skillFishing=0;
skillMarksmanship=0;
skillMining=0;
skillFarming=0;
skillMetalsmithing.init("metalsmithing",100);
health=0;
maxHealth=0;
hunger=0;
thirst=0;
location=0;
birthLocation.setXY(0,0);
deathLocation.setXY(0,0);
tribe = 0;
causeOfDeath = UNKNOWN;
isFavourite=false;
knowledge=0;
worldX = -1;
worldY = -1;
idleCounter=0;
isSneaking=false;
skillFishing=0;
skillMarksmanship=0;
nPelt = 1;
nMeat = 1;
//enum enumCauseOfDeath { UNKNOWN=0, STARVATION=1, OLD_AGE=2 };
map = 0;
isUnderground=false;
location=0;
}
//_sex: 0 - Roll, 1 - Male, 2 - Female.
void Character::init(const int _sex /* =0 */)
{
if (_sex == 1) {
isMale = true;
}
else if (_sex == 2) {
isMale = false;
}
else {
isMale = Random::flip();
}
firstName = globalNameGen.generate();
lastName = globalNameGen.generate();
age=0;
// There will be some differences in stats between genders, but
// both genders will be able to potentially obtain 100 on every stat.
// This is done to affect high-level gameplay, for example gender
// distribution in battles.
// TODO: Stats should be affected by age. However that will be calculated
// by using age as a modifier. Not by modifying the base stat.
baseSkill.roll(isMale);
health=10;
maxHealth=10;
hunger=0;
thirst=0;
tribe=0;
civ=0;
settlement=0;
isSneaking=false;
dateOfBirth.set(&globalCalendar);
//knowledge = new Character_Knowledge;
//knowledge->init();
initialiseKnowledge();
social.setCompatibility(globalRandom.rand8(),globalRandom.rand8());
}
std::string Character::getLocation()
{
if ( location == 0 )
{
return "unknown";
}
return location->getName();
}
bool Character::moveToLocationType(enumLocation _location)
{
if (settlement == nullptr)
{
return false;
}
Vector <Location*>* vLocation = settlement->location.getLocation(_location);
if (vLocation->empty())
{
delete vLocation;
return false;
}
vLocation->shuffle();
for (int i=0;i<vLocation->size();++i)
{
if ((*vLocation)(i)->putCharacter(this))
{
delete vLocation;
return true;
}
}
delete vLocation;
return false;
}
char Character::getBaseSkill(AttributeManager::TYPE skill)
{
return baseSkill.getSkillValue(skill);
}
void Character::setBaseSkill(AttributeManager::TYPE skill, char value)
{
baseSkill.setSkillValue(skill,value);
}
void Character::skillUpFarming()
{
if ( skillFarming < 100 )
{
++skillFarming;
}
}
void Character::skillUpMining()
{
if ( skillMining < 100 )
{
++skillMining;
}
}
void Character::skillUpMarksmanship()
{
if ( skillMarksmanship < 100 )
{
++skillMarksmanship;
}
}
char Character::getCharisma()
{
return baseSkill.getSkillValue(AttributeManager::CHARISMA);
}
void Character::setCharisma(char amount)
{
baseSkill.setSkillValue(AttributeManager::CHARISMA,amount);
}
char Character::getIntelligence()
{
return baseSkill.getSkillValue(AttributeManager::INTELLIGENCE);
}
void Character::setIntelligence(char amount)
{
baseSkill.setSkillValue(AttributeManager::INTELLIGENCE,amount);
}
char Character::getStrength()
{
return baseSkill.getSkillValue(AttributeManager::STRENGTH);
}
void Character::setStrength(char amount)
{
baseSkill.setSkillValue(AttributeManager::STRENGTH,amount);
}
Vector <Character*> Character::getAllKnownCharacters()
{
Vector <Relationship> vRelation = social.getAcquaintances();
Vector <Character*> vChar;
for (int i=0;i<vRelation.size();++i)
{
vChar.push(vRelation(i).destinationCharacter);
}
return vChar;
}
bool Character::hasIdea(Idea idea)
{
for (int i=0;i<vIdea.size();++i)
{
if (vIdea(i).id == idea.id)
{
return true;
}
}
return false;
}
void Character::giveIdea(Idea idea)
{
Idea copyIdea = idea;
vIdea.push(copyIdea);
}
void Character::updateSocial()
{
social.updateLists(getBaseSkill(AttributeManager::CHARISMA));
//social.updateLists(1);
}
void Character::shareIdeas(Character* c)
{
//std::cout<<"Sharing ideas\n";
if (vIdea.empty())
{
//std::cout<<"No ideas to share\n";
return;
}
// pick random idea to share
int ideaToShare=0;
if (vIdea.size()>1)
{
ideaToShare=globalRandom.rand(vIdea.size()-1);
}
if ( c->hasIdea(vIdea(ideaToShare)))
{
//std::cout<<"Already knows\n";
return;
}
//std::cout<<"Idea shared\n";
c->giveIdea(vIdea(ideaToShare));
}
//ITEM FUNCTIONS
void Character::giveItem(Item* _item)
{
if (_item==0) {
return;
}
vInventory.push(_item);
//world(worldX,worldY)->put(_item,x,y);
//world(worldX,worldY)->vItem.push(_item);
}
void Character::recieveRequestedItem(Item* item)
{
//std::cout<<getFullName()<<" recieved requested item: "<<item->getName()<<"\n";
giveItem(item);
}
void Character::takeItem(Item* _item)
{
if (_item==nullptr)
{ return; }
vInventory.remove(_item);
//world(worldX,worldY)->vItem.push(_item);
}
void Character::consume(Item* _item)
{
Console("NUM NUM NUM");
hunger-=_item->hungerRestore;
if (hunger<0) {
hunger=0;
}
world(worldX,worldY)->erase(_item);
// We should probably consider ways to avoid this check, because it is rarely true.
// However the only possibility I can think of is making the player their own special class.
if (this == playerCharacter)
{
removeFromInventoryGrid(_item);
}
}
void Character::removeFromInventoryGrid(Item* _item) /* Player-only function */
{
for (int _y=0; _y<10; ++_y)
{
for (int _x=0; _x<10; ++_x)
{
if (inventoryGrid[_x][_y]==_item)
{
inventoryGrid[_x][_y]=0;
}
}
}
}
bool Character::hasItemType(ItemType type)
{
for (int i=0;i<vInventory.size();++i)
{
if ( vInventory(i)->type == type )
{
return true;
}
}
return false;
}
Item* Character::getBestItemFor(Job job)
{
return job.getBestItem(&vInventory);
}
Item* Character::getBestItemFor(Job* job)
{
return job->getBestItem(&vInventory);
}
std::string Character::getFullName() const
{
return firstName + " " + lastName;
}
#include <sstream>
std::string Character::getBiography()
{
std::ostringstream biography;
const std::string gender1 = isMale ? "He" : "She";
const std::string tense = isAlive ? "is" : "was";
biography << getFullName();
if (father == nullptr || mother == nullptr)
{
biography << " was divinely conceived.";
}
else
{
biography << " was the child of " << father->getFullName() << " and " << mother->getFullName() << ".";
biography << " was born in BIOME, in the land of LAND.";
}
biography << " " << gender1 << " " << tense << (isMarried ? " married to X." : " not married.");
if (tribe!=0)
{
biography<<" "<<gender1<<" is a member of a tribe.\n";
}
if (settlement!=0)
{
if ( settlement->government.leader == this )
{
biography<<" "<<gender1<<" is the leader of a settlement.\n";
}
else if ( settlement->government.scribe == this )
{
biography<<" "<<gender1<<" is the head scribe of a settlement.\n";
}
else if ( settlement->government.captain == this )
{
biography<<" "<<gender1<<" is the military captain of a settlement.\n";
}
else
{
biography<<" "<<gender1<<" is a member of a settlement.\n";
}
}
if (!isAlive)
{
if ( causeOfDeath == SMITED )
{
biography << " " << gender1 << " died in " << world.getLandmassName(&deathLocation) << " after saying "<<
"\"if God is real then may he strike me down right here\", upon which "<<gender1<<" was subsequently "<<
"struck dead by a bolt of lightning.";
}
else
{
biography << " " << gender1 << " died in " << world.getLandmassName(&deathLocation) << ".";
}
}
else
{
biography << " " << gender1 << " currently lives in " << world.getLandmassName(tribe->worldX, tribe->worldY) << ".";
}
if ( vIdea.size() > 0 )
{
biography <<" "<<gender1<<" currently has "<<vIdea.size()<<" ideas.\n";
}
if ( vOriginalSpecialIdea.size() > 0 )
{
for (int i=0;i<vOriginalSpecialIdea.size();++i)
{
biography <<gender1<<" "<<vOriginalSpecialIdea(i).biographyText<<".\n";
}
}
biography<<"\n\n"<<getBestSkills()<<"\n";
if (!vKills.empty())
{
biography << "\n\nKills: " << vKills.size() << "\n";
for (const auto& kill : vKills)
{
biography << " " << kill->getFullName() << "\n";
}
}
return biography.str();
}
std::string Character::getBestSkills()
{
std::vector<std::string> notableSkills;
auto addSkillDescription = [¬ableSkills](const std::string& skillName, char skillLevel)
{
if (skillLevel > 6)
{
std::string description;
if (skillLevel == 10)
{
description = "legendary for their " + skillName;
}
else if (skillLevel == 9)
{
description = "renowned for their " + skillName;
}
else
{
description = "admired for their " + skillName;
}
notableSkills.push_back(description);
}
};
addSkillDescription("strength", baseSkill.strength);
addSkillDescription("agility", baseSkill.agility);
addSkillDescription("charisma", baseSkill.charisma);
addSkillDescription("intelligence", baseSkill.intelligence);
addSkillDescription("perception", baseSkill.perception);
addSkillDescription("endurance", baseSkill.endurance);
addSkillDescription("courage", baseSkill.courage);
if (notableSkills.empty())
{
return "This character has no notable skills.";
}
else
{
std::string description = "This character is ";
for (size_t i = 0; i < notableSkills.size(); ++i)
{
if (i > 0)
{
if (i == notableSkills.size() - 1)
{
description += " and ";
}
else
{
description += ", ";
}
}
description += notableSkills[i];
}
description += ".";
return description;
}
}
void Character::aiManager()
{
// assess situation and decide current ai state
//std::cout<<"AI manager for: "<<getFullName()<<"\n";
}
void Character::incrementTicks(int nTicks)
{
aiManager();
// character ai goes here
// character could be doing one of several things:
// crafting, making useful equipment
// gathering resources
// hunting
//looking after family
// combat
// research
tickCounter+=nTicks;
hunger+=nTicks;
if (hunger > MAX_HUNGER) {
hunger = MAX_HUNGER; /* isAlive=false; */
}
thirst+=nTicks;
if (thirst > MAX_THIRST) {
thirst = MAX_THIRST; /* isAlive=false; */
}
while(tickCounter>=TICKS_PER_DAY)
{
++daysCounter;
tickCounter-=TICKS_PER_DAY;
}
//std::cout<<"Dayscounter: "<<daysCounter<<".\n";
while(daysCounter >= DAYS_PER_YEAR)
{
age++;
daysCounter-=DAYS_PER_YEAR;
// if ( hunger > 0 )
// {
// // Chance of dying at 100 hunger = 1 in 100.
// // Chance of dying at 1 hunger = 1 in 5000
// int starvationChance = (101-hunger)*10;
// if ( Random::oneIn(starvationChance) )
// {
// //die();
// }
// }
}
wander();
}
void Character::wander()
{
//int currentX = x;
//int currentY = y;
if (playerCharacter == this)
{
return;
}
if (map==0)
{
//std::cout<<"Error, character has no map.\n";
return;
}
else
{
//std::cout<<"CHARMAP\n";
}
if (globalRandom.flip())
{
// do nothing (50% chance)
}
else if (globalRandom.flip())
{
map->remove(this);
// alter x (25% chance)
if (globalRandom.flip())
{
if (x<LOCAL_MAP_SIZE-1)
{
map->put(this,x+1,y);
// ++x;
// ++fullX;
}
}
else
{
if (x > 0)
{
map->put(this,x-1,y);
// --x;
// --fullX;
}
}
}
else
{
map->remove(this);
// alter y (25% chance)
if (globalRandom.flip())
{
if (y<LOCAL_MAP_SIZE-1)
{
map->put(this,x,y+1);
// ++y;
// ++fullY;
}
}
else
{
if (y > 0)
{
map->put(this,x,y-1);
// --y;
// --fullY;
}
}
}
updateKnowledge();
return;
if ( map==0 ) {
return;
}
int newX = x;
int newY = y;
char moveDirection = '?';
if (knowledge)
{
// PICK A DESTINATION IF NECESSARY
if (map->isSafe(&(knowledge->currentGoal))==false ||
(knowledge->currentGoal.x == x && knowledge->currentGoal.y ==y))
{
HasXY* randomDestination = map->getRandomTile();
knowledge->currentGoal.set(randomDestination);
Pathing_Local p;
p.init(map);
p.pathLocal(x, y, randomDestination->x, randomDestination->y, 10, false);
if (p.vPath.size() > 0)
{
knowledge->vPath = p.vPath;
moveDirection=p.vPath.back();
p.vPath.popBack();
}
else // Go somewhere else.
{ knowledge->currentGoal.set(-1,-1);
}
delete randomDestination;
}
else
{
Pathing_Local p;
p.init(map);
bool pathingSuccess = p.pathLocal(x, y, knowledge->currentGoal.x, knowledge->currentGoal.y, 10, false);
if (pathingSuccess == false && p.vPath.size() < 9 )
{ knowledge->currentGoal.set(-1,-1);
}
if (p.vPath.size() > 0)
{
moveDirection=p.vPath(0);
}
else {
knowledge->currentGoal.set(-1,-1);
}
}
}
else {
std::cout<<"noknow\n";
}
int direction = Random::randomInt(3);
if ( moveDirection == 'E' )
{
direction = 0;
}
else if (moveDirection == 'N')
{
direction = 2;
}
else if (moveDirection == 'S')
{
direction = 3;
}
else if (moveDirection == 'W')
{
direction = 1;
}
if ( direction==0 ) {
++newX;
}
else if ( direction==1 ) {
--newX;
}
else if ( direction==2 ) {
++newY;
}
else {
--newY;
}
if ( map->isSafe(newX,newY) && map->data->aLocalTile(newX,newY).hasMovementBlocker() == false )
{
map->remove(this);
if (map->put(this,newX,newY) == false)
{
map->put(this,x,y);
}
if (Random::oneIn(10))
{
//delete map->aLocalTile(x,y).footprint;
//map->aLocalTile(x,y).footprint = new Creature_Footprint;
}
}
//updateKnowledge();
}
void Character::die(enumCauseOfDeath _causeOfDeath /* =UNKNOWN */)
{
isAlive = false;
dateOfDeath.set(&globalCalendar);
causeOfDeath = _causeOfDeath;
if ( tribe != 0 )
{
deathLocation.setXY(tribe->worldX,tribe->worldY);
}
else
{
std::cout<<"no tribe...\n";
}
}
bool Character::marry(Character* c)
{
if (c==0) {
return false;
}
isMarried = true;
c->isMarried=true;
spouse = c;
c->spouse = this;
if (isMale == true && c->isMale == false)
{
c->lastName = lastName;
}
else if (isMale == false && c->isMale == true)
{
lastName = c->lastName;
}
dateOfMarriage.set(&globalCalendar);
c->dateOfMarriage.set(&globalCalendar);
// If the spouse is from a different tribe, move them to the new one.
if ( tribe != c->tribe )
{
c->tribe->removeCharacter(c);
tribe->addCharacter(c);
}
social.addFamily(c);
c->social.addFamily(this);
return true;
}
Vector <Character*> * Character::getDescendants(Vector <Character*> * vDescendants)
{
if (vDescendants==0) {
return 0;
}
/* GET ALL CHILDREN, THEN GET ALL CHILDREN'S CHILDREN. RETURN 0 IF NO CHILDREN. */
if (vChildren.size() == 0)
{
return vDescendants;
}
//Vector <Character*> * vChildren = new Vector <Character*>;
//auto vDescendants = new Vector <Character*>;
// for (auto i=0;i<vChildren.size();++i)
// {
// }
//for(auto const& value: vChildren.data) { }
//for(auto const& v: vChildren) { std::cout<<"Person: "<<v->firstName<<".\n"; }
// Push all character's children onto vector.
for(auto const& v: vChildren) {
vDescendants->push(v);
v->getDescendants(vDescendants);
}
//Push all children's descendants onto vector.
//for(auto const& v: vChildren) { v->getDescendants(vDescendants); }
vDescendants->removeDuplicates();
return vDescendants;
}
Vector <Character*> * Character::getRelatives()
{
Vector <Character*> vCloseAncestors;
//auto vCloseAncestors = new Vector <Character*>;
auto vAncestors = new Vector <Character*>;
// The character cannot marry any descendants of their grandparents. So first we get all 4 grandparents. If there are no grandparents, we work from parents. If there are no parents, we work from this.
if ( father != 0)
{
if (father->father != 0)
{
vCloseAncestors.push(father->father);
}
if(father->mother != 0)
{
vCloseAncestors.push(father->mother);
}
if ( father->father == 0 && father->mother == 0)
{
vCloseAncestors.push(father);
}
}
else
{
vCloseAncestors.push(this);
}
if ( mother != 0)
{
if (mother->father != 0)
{
vCloseAncestors.push(father->father);
}
if(mother->mother != 0)
{
vCloseAncestors.push(mother->mother);
}
if ( mother->father == 0 && mother->mother == 0)
{
vCloseAncestors.push(mother);
}
}
else
{
vCloseAncestors.push(this);
}
// Get all descendants of all these relatives.
for(auto const& v: vCloseAncestors) {
vAncestors->push(v);
v->getDescendants(vAncestors);
}
vAncestors->removeDuplicates();
return vAncestors;
}
void Character::starve()
{
//--health;
++hunger;
if (hunger > 100)
{
hunger = 100;
}
//if ( health <= 0)
//{
//std::cout<<"Starved to death.\n";
// isAlive = false;
//}
}
bool Character::canMarry(Character* c)
{
//std::cout<<"Checking if can marry.\n";
//Cannot marry null pointer or yourself or underage or same sex.
if(c==0 || c==this || isAlive==false || c->isAlive==false || age<16 || c->age<16 || isMale==c->isMale || spouse !=0 || c->spouse!=0 ) {
return false;
}
//auto vRelatives = getRelatives();
//if ( vRelatives->contains(c) )
//{
// delete vRelatives;
// return false;
//}
return true;
}
Character* Character::giveBirth()
{
Character * babby = new Character;
babby->init();
babby->lastName = lastName;
// Inherit tribe membership from mother I guess.
babby->tribe = tribe;
babby->settlement = settlement;
vChildren.push(babby);
spouse->vChildren.push(babby);
if(isMale)