-
Notifications
You must be signed in to change notification settings - Fork 3
/
item_character.cpp
1697 lines (1337 loc) · 52.9 KB
/
item_character.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
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
/*
* FILE: item_character.cpp
*
* PROJECT: MidnightEngine
*
* froupedD:
*
* AUTHOR: Chris Wild
*
* Copyright 2011 Chilli Hugger. All rights reserved.
*
* PURPOSE:
*
*
*/
#include "../baseinc/tme_internal.h"
#include "../scenario/helpers/race_terrain_movement_modifier.h"
#include <memory>
#include <string>
#if defined(_DDR_)
#include "../scenarios/ddr/scenario_ddr_internal.h"
#endif
#define ENABLE_GROUPING 1
namespace tme {
mxcharacter::mxcharacter()
{
mxentity::type = IDT_CHARACTER ;
//memory=NULL;
}
mxcharacter::~mxcharacter()
{
}
void mxcharacter::Serialize ( archive& ar )
{
mxitem::Serialize(ar);
if ( ar.IsStoring() ) {
ar << longname;
ar << shortname;
WRITE_ENUM( looking );
WRITE_ENUM( time );
ar << riders;
ar << warriors;
ar << battleloc;
ar << battleslew;
ar << reckless;
ar << energy;
ar << strength;
ar << cowardess;
ar << courage;
ar << fear;
ar << RecruitingKey;
ar << RecruitedBy;
WRITE_ENUM( race );
ar << carrying ;
ar << killedbyobject ;
WRITE_ENUM( gender ) ;
WRITE_ENUM( loyalty );
ar << liege ;
ar << foe ;
WRITE_ENUM( wait );
WRITE_ENUM(orders);
ar << despondency;
ar << traits ;
ar << following ;
ar << followers ;
}else{
ar >> longname;
ar >> shortname;
READ_ENUM ( looking );
READ_ENUM ( time );
ar >> riders;
ar >> warriors;
ar >> battleloc;
ar >> battleslew;
ar >> reckless;
ar >> energy;
ar >> strength;
ar >> cowardess;
ar >> courage;
ar >> fear;
ar >> RecruitingKey;
ar >> RecruitedBy;
READ_ENUM ( race );
ar >> carrying ;
ar >> killedbyobject ;
READ_ENUM ( gender );
READ_ENUM ( loyalty );
ar >> liege ;
ar >> foe ;
READ_ENUM ( wait );
READ_ENUM(orders) ;
ar >> despondency;
ar >> traits ;
if ( tme::mx->SaveGameVersion() > 2 )
ar >> following ;
else
following = NULL ;
if ( tme::mx->SaveGameVersion() > 4 )
ar >> followers ;
else
followers = 0 ;
// temp bug fix
if ( (s32)energy < 0 )
energy= 0;
}
}
void mxcharacter::ForEachFollower(const std::function<void(mxcharacter*)> &callback)
{
if (!HasFollowers()) return;
for ( auto follower : mx->scenario->GetCharacterFollowers(this) ) {
callback(follower);
}
}
mxcharacter* mxcharacter::FindFollower(const std::function<bool(mxcharacter*)> &callback)
{
if (!HasFollowers()) return nullptr;
for ( auto follower : mx->scenario->GetCharacterFollowers(this) ) {
if (callback(follower)) {
return follower;
}
}
return nullptr;
}
void mxcharacter::RefreshLocationBasedVariables ( const mxlocinfo* info )
{
fear = info->adj_fear ;
s32 temp = cowardess-(fear/7);
temp = std::max<int>( temp, 0 ) / 8 ;
courage = std::min<int>( temp, 7 ) ;
}
void mxcharacter::ForcedVariableRefresh () const
{
// NOTE: This breaks the rules of this function being const
// but i am only modifying temporary variables, so this should be okay
mxcharacter* newcharacter = mx->CharacterById(Id());
mxlocinfo* info = new mxlocinfo ( Location(), newcharacter, 0 );
newcharacter->RefreshLocationBasedVariables ( info );
SAFEDELETE ( info );
}
mxlocinfo* mxcharacter::GetLocInfo()
{
mxlocinfo* info;
// create a new info class
// for our current location and the direction
// we are looking
info = new mxlocinfo ( Location(), Looking(), this, (IsInTunnel() ? slf_tunnel : slf_none) );
//info->owner = (mxcharacter*)this ;
if ( IsDead() )
return info;
// set the characters courage and fear
RefreshLocationBasedVariables ( info );
// if the character is hidden then
// nothing else is possible
info->flags.Set(lif_hide); // = TRUE ;
if ( IsHidden() ) {
info->flags.Reset(lif_enterbattle); // = FALSE ;
info->flags.Set(lif_unhide);
if( mx->Difficulty() > DF_EASY && IsNight())
info->flags.Reset(lif_unhide);
return info;
}
// can we seek?
info->flags.Set(lif_seek); // = TRUE ;
// can we hide ?
// 1. Only if we are allowed to an we have no armies
// 2. TODO how about allowing hiding when armies
// are below a certain number? - maybe warriors only
if ( !IsAllowedHide() || HasArmy() || IsFollowing() || HasFollowers() )
info->flags.Reset(lif_hide); // = FALSE ;
// can't hide or seek at night
if( mx->Difficulty() > DF_EASY && IsNight())
info->flags.Reset(lif_hide|lif_seek); // = false ;
// TODO Stop hiding when just entered into battle
//if ( IsInBattle() )
// info->flags.Reset(lif_hide); ;
#if !defined(_DDR_)
// if in battle there is nothing more we can do
if ( info->foe.armies /*IsInBattle() */ )
return info;
#endif
// make a list of all other recruitable
// characters available
// TODO in LOM this can only be one
if ( mx->scenario->IsFeature(SF_APPROACH_DDR) ) {
if(!mx->scenario->isLocationImpassable(info->loc_infront, info->owner)) {
info->FindApproachCharactersInfront(info->owner);
}
} else {
info->FindRecruitCharactersHere (info->owner);
}
// can we recruit ?
if ( info->objRecruit.Count() ) {
info->flags.Set(lif_recruitchar); // = TRUE ;
if( mx->Difficulty() > DF_EASY && IsNight())
info->flags.Reset(lif_recruitchar); // = false ;
}
// can we recruitmen or guardmen ?
// Recruit:
// 1. only if there is a stronghold here
// 2. Is the stronghold race the same as us?
// 3. Are we allowed an army?
// 4. does the stronghold have more than its minimum in?
// 5. can we take any more soldiers?
// guard:
// 1. only if there is a stronghold here
// 2. Is the stronghold race the same as us?
// 3. does the stronghold have less than its maximum in?
// 4. do we have enough soldiers of the required type?
// TODO this is too restrictive, the stronghold::add routine
// allows for arbitruary values to be added, but this
// uses the LOM model of having to place 100 at a time
// should this change?
if ( info->objStrongholds.Count() ) {
mxstronghold* stronghold = (mxstronghold*)info->objStrongholds[0] ;
#if defined(_LOM_)
if ( stronghold->OccupyingRace() == Race() ) {
#endif
#if defined(_DDR_)
if ( static_cast<ddr_stronghold*>(stronghold)->IsFriend(this) && stronghold->OccupyingRace() == Race() ) {
#endif
// can we recruit men ?
// this is not an exact check, a stronghold CAN have less than the minimum
// in, however you can no longer recruit after the min has been reached
if ( IsAllowedArmy() && stronghold->TotalTroops() > stronghold->MinTroops() ) {
#if defined(_DDR_)
if ( IsAllowedWarriors() )
#endif
if ( (stronghold->Type()==UT_WARRIORS) && (warriors.total + sv_character_recruit_amount <= sv_character_max_warriors ) )
info->flags.Set(lif_recruitmen) ;// = TRUE ;
#if defined(_DDR_)
if ( IsAllowedRiders() )
#endif
if ( (stronghold->Type()==UT_RIDERS) && (riders.total + sv_character_recruit_amount <= sv_character_max_riders ) )
info->flags.Set(lif_recruitmen); // = TRUE ;
}
// can we guard men ?
if ( stronghold->TotalTroops() + sv_character_guard_amount <= stronghold->MaxTroops() ) {
if ( (stronghold->Type()==UT_WARRIORS) && (warriors.total >= sv_character_guard_amount ) )
info->flags.Set(lif_guardmen); // = TRUE ;
if ( (stronghold->Type()==UT_RIDERS) && (riders.total >= sv_character_guard_amount ) )
info->flags.Set(lif_guardmen); // = TRUE ;
}
}
}
// if the character is at night then
// nothing else is possible
//if ( IsNight() )
// return info;
// use
// 1. are we carrying our object
// 2. have we used it
// take
// 1. is there an object here?
// give
// 1. do we have an object
// 2. is there another character here
// 3. are we generous
// rest
// info->flags.Set(lif_rest);
mxthing_t thing = (mxthing_t)info->mapsqr.object;
#if defined(_DDR_)
if ( info->mapsqr.IsTunnelPassageway() && !IsInTunnel() )
thing=OB_NONE;
#endif
// can we fight ?
// can we fight the object currently in this location?
mxobject* obj = mx->ObjectById(thing);
if ( CheckFightObject ( obj ) ) {
info->flags.Set(lif_fight); // = TRUE ;
info->fightthing = thing ;
}
// attack
if ( info->flags.Is(lif_enterbattle) ) {
// if a battle is allowed and we don't have any courage
// then we can't actually enter the battle
if ( IsCoward() )
info->flags.Reset(lif_enterbattle) ;// = FALSE ;
if ( IsFollowing() )
info->flags.Reset(lif_enterbattle) ;// = FALSE ;
// if we have followers then all my followers
// must have courage
info->stubborn_follower_battle = FIND_FOLLOWER(f) {
return f->IsCoward();
});
}
// if we are leading
// then the whole group needs to be able to move
info->stubborn_follower_move = FIND_FOLLOWER(f) {
return !f->CanWalkForward();
});
return info ;
}
s32 mxcharacter::DistanceFromLoc ( mxgridref loc )
{
return Location() - loc ;
}
bool mxcharacter::CheckFightObject ( mxobject* obj ) const
{
return obj ? obj->CanFight() : FALSE ;
}
bool mxcharacter::CheckRecruitChar ( mxcharacter* character ) const
{
if ( character == this )
return false ;
if ( mx->scenario->IsFeature(SF_RECRUIT_DDR) ) {
// TODO: recruiting logic
return true ;
}else{
if(mx->isRuleEnabled(RF_LOM_UNRECRUITABLE_FEY)
&& character->Race() == RA_FEY)
{
return false;
}
if ( character->RecruitedBy & RecruitingKey )
return true ;
}
return false ;
}
bool mxcharacter::ShouldLoseHorse(s32 hint) const
{
return mxrandom() & 1;
}
bool mxcharacter::ShouldDieInFight() const
{
auto temp = (energy/2) - 64 + reckless;
return mxrandom(255) >= temp;
}
void mxcharacter::Dismount()
{
flags.Reset ( cf_riding );
}
void mxcharacter::LostFight ( s32 hint )
{
if ( !IsDead() ) {
if ( IsRiding() ) {
if (ShouldLoseHorse())
Dismount();
}
if (ShouldDieInFight()) {
Cmd_Dead();
}
}
}
void mxcharacter::Displace ( void )
{
mxgridref loc;
if ( IsDead() || IsFollowing() || HasFollowers() )
return;
for (;; ) {
loc = Location() + (mxdir_t)mxrandom(0, 7);
if ( !mx->scenario->isLocationImpassable( loc, this ) ) {
Flags().Reset(cf_battleover);
location = loc ;
if ( IsRecruited() ) {
EnterLocation ( loc );
mx->scenario->LookInDirection ( Location(), Looking(), IsInTunnel() );
}
break;
}
}
}
void mxcharacter::LostBattle(bool canFlee)
{
// we might not have displaced the lord due to grouping
// so mark them as not being able to fight anyone else in this round
Flags().Set(cf_battleover);
if ( IsFollowing() ) {
if ( mx->Difficulty() == DF_HARD ) {
// if we are a member of the group, then we need to leave the group
Cmd_UnFollow(Following());
}else{
return;
}
}
if ( HasFollowers() ) {
if ( mx->Difficulty() == DF_HARD ) {
// if we are leading a group, then we must disband the group
Cmd_DisbandGroup();
}else{
return;
}
}
if (canFlee) {
// lord flees
Displace();
}
}
void mxcharacter::SetLastCommand ( command_t cmd, mxid id )
{
lastcommand = cmd;
lastcommandid = id;
}
s32 mxcharacter::CommandSuccessTime(command_t cmd)
{
auto command = mx->CommandById( cmd );
if ( command == nullptr )
return 0; // invalid command
return command->SuccessTime();
}
void mxcharacter::CommandTakesTime ( bool success )
{
// ** CHEAT
if ( sv_cheat_commands_free )
return;
mxcommand* command = mx->CommandById( GET_ID(lastcommand) );
if ( command == nullptr )
return; // invalid command
int timetaken = success ? command->SuccessTime() : command->failuretime;
time = (mxtime_t) BSub(time,timetaken,0);
}
MXRESULT mxcharacter::Cmd_MoveForward ( void )
{
std::unique_ptr<mxlocinfo> info;
SetLastCommand ( CMD_MOVE, IDT_NONE);
// lords who are following cannot be moved on their own
if ( IsFollowing() )
return MX_FAILED ;
if ( !CanWalkForward() )
return MX_FAILED ;
// if we are leading
// then the whole group needs to be able to move
auto cantWalkForward = FIND_FOLLOWER(f) { return !f->CanWalkForward(); });
if ( cantWalkForward != nullptr )
return MX_FAILED;
// find where we will be moving to
info.reset(GetLocInfo());
// something blocking our path!
if ( !info->flags.Is(lif_moveforward) ) {
return MX_FAILED;
}
#if defined(_LOM_)
// if there is an army here
// you can only move first thing
if ( !sv_cheat_armies_noblock ) {
if ( !IsDawn() && info->foe.armies ) {
return MX_FAILED;
}
}
// something needs killing!
if ( !sv_cheat_nasties_noblock ) {
if ( info->flags.Is(lif_fight) ) {
return MX_FAILED;
}
}
#endif
bool approach = false;
if ( sv_auto_approach ) {
info->infront->FindRecruitCharactersHere(this);
approach = info->infront->objRecruit.Count() == 1;
}
return Cmd_WalkForward(sv_auto_seek, approach);
}
bool mxcharacter::CanWalkForward ( void )
{
// dead men don't walk!
if ( IsDead() ) {
return false;
}
// completely and untterly shattered?
if ( energy <= sv_energy_cannot_continue ) {
return false;
}
// hidden under a rock?
if ( IsHidden() ) {
// if auto unhide turned on then we must unhide and carry on
if ( !sv_auto_unhide )
return false;
}
// should be sleeping!
if ( IsNight() ) {
return false;
}
return true;
}
MXRESULT mxcharacter::Cmd_WalkForward ( bool perform_seek, bool perform_approach )
{
s32 TimeCost;
mxrace* rinfo;
// hidden under a rock?
// if auto unhide turned on then we must unhide and carry on
if ( IsHidden() && sv_auto_unhide ) {
Cmd_UnHide();
}
rinfo = mx->RaceById(Race());
// remove army from location
mx->gamemap->SetLocationArmy(Location(),0);
mx->gamemap->SetLocationCharacter(Location(),0);
// set the current location to visited, just to make sure
mx->gamemap->SetLocationVisited(location, TRUE);
// and move us there
location = location + Looking();
// set the new current location to visited
mx->gamemap->SetLocationVisited(location, TRUE);
// add armies and characters back to the map
// this is overkill!!!!
mx->scenario->SetMapArmies();
EnterLocation ( location );
mx->scenario->LookInDirection ( Location(), Looking(), IsInTunnel() );
// calculate our movement values
TimeCost = rinfo->InitialMovementValue();
// are we moving diagonally?
if ( IS_DIAGONAL(Looking()) )
TimeCost += rinfo->DiagonalMovementModifier();
// are we on horseback?
if ( !IsRiding() )
TimeCost = (s32) ((f64)TimeCost * rinfo->RidingMovementMultiplier()) ;
// adjust for terrain
TimeCost += TME::helpers::RaceTerrainMovementModifier::Get(
Race(),
(mxterrain_t)mx->gamemap->GetAt ( location ).terrain
);
// check for max out
TimeCost = std::min<int>( TimeCost, (int)rinfo->MovementMax() );
// ** CHEATS
if ( sv_cheat_movement_cheap )
TimeCost=1;
if ( sv_cheat_movement_free )
TimeCost=0;
// ** END CHEATS
// time
time = (mxtime_t) BSub(time,TimeCost,0);
// energy
DecreaseEnergy(TimeCost);
CommandTakesTime(TRUE);
// if we have moved, we are no longer in battle
flags.Reset(cf_inbattle|cf_preparesbattle);
// auto approach
if ( perform_approach ) {
// we need to recruit before anyone else joins us
if ( Cmd_Approach() != nullptr) {
perform_seek = false;
}
}
// auto seek
if ( perform_seek ) {
if ( mx->gamemap->getLocationObject(this, Location())!=OB_NONE ) {
Cmd_Seek();
}
}
WalkFollowersForward();
return MX_OK ;
}
void mxcharacter::WalkFollowersForward()
{
// if we are leading
// then the whole group needs to be able to move
FOR_EACH_FOLLOWER(f) {
// must look the same direction to make them move in the
// correct direction
f->looking = Looking();
f->Cmd_WalkForward();
});
}
void mxcharacter::DecreaseEnergy ( s32 amount )
{
energy = BSub(energy,amount,0);
riders.energy = BSub(riders.energy,amount,0);
warriors.energy = BSub(warriors.energy,amount,0);
}
void mxcharacter::IncreaseEnergy ( s32 amount )
{
mxrace* rinfo = mx->RaceById(Race());
s32 baserest = rinfo->BaseRestAmount() ;
energy =
BAdd ( energy, amount + baserest, (u32)sv_character_max_energy );
riders.energy =
BAdd ( riders.energy, amount + riders.BaseRestModifier(), (u32)sv_riders_max_energy );
warriors.energy =
BAdd ( warriors.energy, amount + warriors.BaseRestModifier(), (u32)sv_warriors_max_energy );
}
MXRESULT mxcharacter::Cmd_LookLeft ( void )
{
//looking=(mxdir_t) ((looking-1)&7);
looking = PREV_DIRECTION(looking);
mx->scenario->LookInDirection ( Location(), Looking(), IsInTunnel() );
#if defined(_DDR_)
mx->scenario->MakeMapAreaVisible(Location(), this);
#endif
return MX_OK ;
}
MXRESULT mxcharacter::Cmd_LookRight ( void )
{
//looking=(mxdir_t)((looking+1)&7);
looking = NEXT_DIRECTION(looking);
mx->scenario->LookInDirection ( Location(), Looking(), IsInTunnel() );
#if defined(_DDR_)
mx->scenario->MakeMapAreaVisible(Location(), this);
#endif
return MX_OK ;
}
MXRESULT mxcharacter::Cmd_LookDir ( mxdir_t dir )
{
looking=(mxdir_t)((dir)&7);
mx->scenario->LookInDirection ( Location(), Looking(), IsInTunnel() );
#if defined(_DDR_)
mx->scenario->MakeMapAreaVisible(Location(), this);
#endif
return MX_OK ;
}
bool mxcharacter::EnterLocation ( mxgridref loc )
{
mx->scenario->MakeMapAreaVisible ( loc, this );
return TRUE ;
}
MXRESULT mxcharacter::Cmd_Lookat(mxgridref loc)
{
return Cmd_LookDir ( location.DirFromHere(loc) );
}
MXRESULT mxcharacter::Cmd_Place(mxgridref loc)
{
Location(loc);
EnterLocation( Location() );
return MX_OK ;
}
MXRESULT mxcharacter::Cmd_Rest ( void )
{
SetLastCommand ( CMD_REST, IDT_NONE );
if ( IsNight() || IsResting() )
return MX_FAILED ;
flags.Set(cf_resting);
FOR_EACH_FOLLOWER(f) {
f->Cmd_Rest();
});
CommandTakesTime(TRUE);
return MX_OK ;
}
MXRESULT mxcharacter::Cmd_RecruitMen ( mxstronghold* stronghold, u32 qty )
{
s32 delta;
mxstronghold* def_stronghold = NULL ;
SetLastCommand ( CMD_RECRUITMEN, IDT_NONE );
// get location info
std::unique_ptr<mxlocinfo> info ( GetLocInfo() );
if ( info->objStrongholds.Count() )
def_stronghold = (mxstronghold*)info->objStrongholds[0] ;
// are we allowed to recruitmen ?
if ( !info->flags.Is(lif_recruitmen) ) {
return MX_FAILED ;
}
// get the default stronghold
if ( stronghold == NULL ) {
stronghold = def_stronghold ;
if ( stronghold == NULL )
return MX_FAILED ;
}
if ( qty == 0 )
qty = (u32)sv_character_recruit_amount;
// try and recruit upto the RECRUIT_AMOUNT
if ( stronghold->Type() == UT_RIDERS ) {
delta = (s32)sv_character_max_riders - riders.total ;
qty = stronghold->Remove( Race(), UT_RIDERS, std::min<int>((s32)qty,delta) );
riders.total += qty;
} else if ( stronghold->Type() == UT_WARRIORS ) {
delta = (s32)sv_character_max_warriors - warriors.total ;
qty = stronghold->Remove( Race(), UT_WARRIORS, std::min<int>((s32)qty,delta) );
warriors.total += qty;
}
mx->SetLastActionMsg(mx->text->DescribeCharacterRecruitMen( this, stronghold, qty ));
CommandTakesTime(TRUE);
return MX_OK ;
}
MXRESULT mxcharacter::Cmd_PostMen ( mxstronghold* stronghold, u32 qty )
{
mxstronghold* def_stronghold = NULL ;
SetLastCommand ( CMD_POSTMEN, IDT_NONE );
// get location info
std::unique_ptr<mxlocinfo> info ( GetLocInfo() );
if ( info->objStrongholds.Count() )
def_stronghold = (mxstronghold*)info->objStrongholds[0] ;
// are we allowed to postmen ?
if ( !info->flags.Is(lif_guardmen) ) {
return MX_FAILED ;
}
// get the default stronghold
if ( stronghold == NULL ) {
stronghold = def_stronghold ;
if ( stronghold == NULL )
return MX_FAILED ;
}
if ( qty == 0 )
qty = (u32)sv_character_guard_amount;
// try and guard upto the GUARD_AMOUNT
if ( stronghold->Type() == UT_RIDERS )
riders.total -= stronghold->Add( Race(), UT_RIDERS, std::min<int>(riders.total,qty) );
else if ( stronghold->Type() == UT_WARRIORS )
warriors.total -= stronghold->Add( Race(), UT_WARRIORS, std::min<int>(warriors.total,qty));
mx->SetLastActionMsg(mx->text->DescribeCharacterPostMen( this, stronghold, qty ));
CommandTakesTime(TRUE);
return MX_OK ;
}
mxcharacter* mxcharacter::Cmd_Approach ( mxcharacter* character )
{
SetLastCommand ( CMD_APPROACH, IDT_NONE );
if ( character == nullptr ) {
// get location info
std::unique_ptr<mxlocinfo> info ( GetLocInfo() );
// are we allowed to approach ?
if ( !info->flags.Is(lif_recruitchar) ) {
return nullptr ;
}
character = static_cast<mxcharacter*>(info->objRecruit.First()) ;
}
RETURN_IF_NULL(character) nullptr ;
if ( character->Recruited ( this ) ) {
SetLastCommand ( CMD_APPROACH, mxentity::SafeIdt(character) );
character->SetLastCommand ( CMD_APPROACHED, mxentity::SafeIdt(this) );
return character ;
}
CommandTakesTime(false);
return nullptr ;
}
bool mxcharacter::Recruited ( mxcharacter* recruiter )
{
// set loyalty?
flags.Set ( cf_recruited );
// 1. set time of day to recruiting character
SetRecruitmentTime(recruiter);
// 2. our liege becomes the recruiting character
liege = recruiter->Liege() ;
// 3. our loyalty race becomes recruiting character loyalty race
loyalty = recruiter->NormalisedLoyalty() ;
// 4. get the my foe's loyalty, if they are loyal to the recruiting char's loyalty
// then we need the recruiting characters foe
//
mxcharacter* foe = Foe();
if ( foe->NormalisedLoyalty() == loyalty ) {
foe = recruiter->Foe() ;
}
CommandTakesTime(true);
return true ;
}
void mxcharacter::SetRecruitmentTime( mxcharacter* recruiter )
{
s32 TimeCost = 1 * sv_time_scale; // 1 hr
if ( mx->scenario->IsFeature(SF_RECRUIT_TIME) )
time = recruiter->Time() ;
if ( mx->Difficulty() == DF_EASY )
time = sv_time_dawn ;
else if ( mx->Difficulty() == DF_MEDIUM ) {
recruiter->time = (mxtime_t) BSub(recruiter->time,TimeCost,0);
time = recruiter->Time() ;
} else if ( mx->Difficulty() == DF_HARD ) {
time = sv_time_night ;
recruiter->time = time;
}
}
MXRESULT mxcharacter::EnterBattle ( void )
{
Flags().Set(cf_inbattle); // force the battle flag early!
return MX_OK;
}
MXRESULT mxcharacter::Cmd_Attack ( void )
{
if ( IsFollowing() )
return MX_FAILED ;
std::unique_ptr<mxlocinfo> info ( GetLocInfo() );
bool battle = info->flags.Is(lif_enterbattle) ;
mxcharacter* stubborn_follower = info->stubborn_follower_battle;
if ( !battle || stubborn_follower )
return MX_FAILED;
SetLastCommand(CMD_ATTACK,IDT_NONE);
MXRESULT result = Cmd_WalkForward(false) ;
if ( result == MX_OK ) {
EnterBattle();
}
return result;
}
bool mxcharacter::ShouldHaveOneToOneWithNasty() const
{
// allow game difficulty to have an effect
// on the grouping
if ( mx->Difficulty() == DF_EASY && followers>= 2 ) {
return false;
}
if ( mx->Difficulty() == DF_MEDIUM || mx->Difficulty() == DF_NORMAL ) {
if ( followers >= 3 )
return false;
}
s32 total = warriors.total + riders.total ;
if ( mx->Difficulty() == DF_EASY && total >= 250 )
return false;
if ( mx->Difficulty() == DF_MEDIUM && total >= 750 )
return false;
return true;
}
mxobject* mxcharacter::Cmd_Fight ( void )
{
bool needfight = true ;
bool killedwithobject = false;
SetLastCommand ( CMD_FIGHT, IDT_NONE );
std::unique_ptr<mxlocinfo> info ( GetLocInfo() );
// are we allowed to fight
if ( !info->flags.Is(lif_fight) )
return nullptr ;