-
Notifications
You must be signed in to change notification settings - Fork 178
/
Actor.cpp
9645 lines (8666 loc) · 266 KB
/
Actor.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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* This program 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 2
* of the License, or (at your option) any later version.
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
*/
//This class represents the .cre (creature) files.
//Any player or non-player character is a creature.
//Actor is a scriptable object (Scriptable). See Scriptable.cpp
#include "Scriptable/Actor.h"
#include "ie_feats.h"
#include "overlays.h"
#include "strrefs.h"
#include "opcode_params.h"
#include "win32def.h"
#include "Bitmap.h"
#include "DataFileMgr.h"
#include "DialogHandler.h" // checking for dialog
#include "Game.h"
#include "GlobalTimer.h"
#include "DisplayMessage.h"
#include "GameData.h"
#include "Image.h"
#include "Interface.h"
#include "Item.h"
#include "PolymorphCache.h" // stupid polymorph cache hack
#include "Projectile.h"
#include "ProjectileServer.h"
#include "ScriptEngine.h"
#include "Spell.h"
#include "Sprite2D.h"
#include "TableMgr.h"
#include "Video.h"
#include "damages.h"
#include "GameScript/GSUtils.h" //needed for DisplayStringCore
#include "GameScript/GameScript.h"
#include "GUI/GameControl.h"
#include "System/StringBuffer.h"
#include <cassert>
namespace GemRB {
//configurable?
ieDword ref_lightness = 43;
static const Color white = {
0xff, 0xff, 0xff, 0xff
};
static const Color green = {
0x00, 0xff, 0x00, 0xff
};
static const Color red = {
0xff, 0x00, 0x00, 0xff
};
static const Color yellow = {
0xff, 0xff, 0x00, 0xff
};
static const Color cyan = {
0x00, 0xff, 0xff, 0xff
};
static const Color magenta = {
0xff, 0x00, 0xff, 0xff
};
static int sharexp = SX_DIVIDE|SX_COMBAT;
static int classcount = -1;
static int extraslots = -1;
static char **clericspelltables = NULL;
static char **druidspelltables = NULL;
static char **wizardspelltables = NULL;
static char **classabilities = NULL;
static int *turnlevels = NULL;
static int *booktypes = NULL;
static int *xpbonus = NULL;
static int *defaultprof = NULL;
static int *castingstat = NULL;
static int xpbonustypes = -1;
static int xpbonuslevels = -1;
static int **levelslots = NULL;
static int *dualswap = NULL;
static int *multi = NULL;
static int *maxLevelForHpRoll = NULL;
static int *skillstats = NULL;
static int *skillabils = NULL;
static int skillcount = -1;
static int **afcomments = NULL;
static int afcount = -1;
static ieVariable CounterNames[4]={"GOOD","LAW","LADY","MURDER"};
//I keep the zero index the same as core rules (default setting)
static int dmgadjustments[6]={0, -50, -25, 0, 50, 100}; //default, easy, normal, core rules, hard, nightmare
//XP adjustments on easy setting (need research on the amount)
//Seems like bg1 halves xp, bg2 doesn't have any impact
static int xpadjustments[6]={0, 0, 0, 0, 0, 0};
static int FistRows = -1;
static int *wmlevels[20];
typedef ieResRef FistResType[MAX_LEVEL+1];
static FistResType *fistres = NULL;
static int *fistresclass = NULL;
static ieResRef DefaultFist = {"FIST"};
//verbal constant specific data
static int VCMap[VCONST_COUNT];
static ieDword sel_snd_freq = 0;
static ieDword cmd_snd_freq = 0;
static ieDword crit_hit_scr_shake = 1;
static ieDword bored_time = 3000;
static ieDword footsteps = 1;
static ieDword always_dither = 1;
static ieDword GameDifficulty = DIFF_CORE;
static ieDword NoExtraDifficultyDmg = 0;
//the chance to issue one of the rare select verbal constants
#define RARE_SELECT_CHANCE 5
//these are the max number of select sounds -- the size of the pool to choose from
#define NUM_RARE_SELECT_SOUNDS 2 //in bg and pst it is actually 4 TODO: check
#define NUM_SELECT_SOUNDS 6 //in bg1 this is 4 but doesn't need to be checked
#define NUM_MC_SELECT_SOUNDS 4 //number of main charater select sounds
//item usability array
struct ItemUseType {
ieResRef table; //which table contains the stat usability flags
ieByte stat; //which actor stat we talk about
ieByte mcol; //which column should be matched against the stat
ieByte vcol; //which column has the bit value for it
ieByte which; //which item dword should be used (1 = kit)
};
static ieResRef featspells[ES_COUNT];
static ItemUseType *itemuse = NULL;
static int usecount = -1;
//static ieDword *kituse = NULL;
//static int kitcount = -1;
static bool pstflags = false;
static bool nocreate = false;
static bool third = false;
static bool raresnd = false;
static bool iwd2class = false;
//used in many places, but different in engines
static ieDword state_invisible = STATE_INVISIBLE;
//item animation override array
struct ItemAnimType {
ieResRef itemname;
ieByte animation;
};
static ItemAnimType *itemanim = NULL;
static int animcount = -1;
static int fiststat = IE_CLASS;
//conversion for 3rd ed
static int isclass[ISCLASSES]={0,0,0,0,0,0,0,0,0,0,0,0,0};
static const int mcwasflags[ISCLASSES] = {
MC_WAS_FIGHTER, MC_WAS_MAGE, MC_WAS_THIEF, 0, 0, MC_WAS_CLERIC,
MC_WAS_DRUID, 0, 0, MC_WAS_RANGER, 0, 0, 0};
static const char *isclassnames[ISCLASSES] = {
"FIGHTER", "MAGE", "THIEF", "BARBARIAN", "BARD", "CLERIC",
"DRUID", "MONK", "PALADIN", "RANGER", "SORCERER", "CLASS12", "CLASS13" };
static const int levelslotsiwd2[ISCLASSES]={IE_LEVELFIGHTER, IE_LEVELMAGE, IE_LEVELTHIEF,
IE_LEVELBARBARIAN, IE_LEVELBARD, IE_LEVELCLERIC, IE_LEVELDRUID, IE_LEVELMONK,
IE_LEVELPALADIN, IE_LEVELRANGER, IE_LEVELSORCERER, IE_LEVELCLASS12, IE_LEVELCLASS13};
#define BGCLASSCNT 23
//fighter is the default level here
//fixme, make this externalized
//this map could probably be auto-generated BG2 class ID -> ISCLASS
static const int levelslotsbg[BGCLASSCNT]={ISFIGHTER, ISMAGE, ISFIGHTER, ISCLERIC, ISTHIEF,
ISBARD, ISPALADIN, 0, 0, 0, 0, ISDRUID, ISRANGER, 0,0,0,0,0,0,ISSORCERER, ISMONK,
ISCLASS12, ISCLASS13};
//this map could probably be auto-generated (isClass -> IWD2 class ID)
//autogenerating for non IWD2 now!!!
static unsigned int classesiwd2[ISCLASSES]={5, 11, 9, 1, 2, 3, 4, 6, 7, 8, 10, 12, 13};
//this map could probably be auto-generated (isClass -> IWD2 book ID)
static const int booksiwd2[ISCLASSES]={-1, IE_IWD2_SPELL_WIZARD, -1, -1,
IE_IWD2_SPELL_BARD, IE_IWD2_SPELL_CLERIC, IE_IWD2_SPELL_DRUID, -1,
IE_IWD2_SPELL_PALADIN, IE_IWD2_SPELL_RANGER, IE_IWD2_SPELL_SORCERER, -1, -1};
//stat values are 0-255, so a byte is enough
static ieByte featstats[MAX_FEATS]={0
};
static ieByte featmax[MAX_FEATS]={0
};
//holds the wspecial table for weapon prof bonuses
#define WSPECIAL_COLS 3
static int wspecial_max = 0;
static int wspattack_rows = 0;
static int wspattack_cols = 0;
static int **wspecial = NULL;
static int **wspattack = NULL;
//holds the weapon style bonuses
#define STYLE_MAX 3
static int **wsdualwield = NULL;
static int **wstwohanded = NULL;
static int **wsswordshield = NULL;
static int **wssingle = NULL;
//unhardcoded monk bonuses
static int **monkbon = NULL;
static unsigned int monkbon_cols = 0;
static unsigned int monkbon_rows = 0;
// reputation modifiers
#define CLASS_PCCUTOFF 32
#define CLASS_INNOCENT 155
#define CLASS_FLAMINGFIST 156
static ActionButtonRow *GUIBTDefaults = NULL; //qslots row count
static ActionButtonRow2 *OtherGUIButtons = NULL;
ActionButtonRow DefaultButtons = {ACT_TALK, ACT_WEAPON1, ACT_WEAPON2,
ACT_QSPELL1, ACT_QSPELL2, ACT_QSPELL3, ACT_CAST, ACT_USE, ACT_QSLOT1, ACT_QSLOT2,
ACT_QSLOT3, ACT_INNATE};
static int QslotTranslation = false;
static int DeathOnZeroStat = true;
static int IWDSound = false;
static ieDword TranslucentShadows = 0;
static int ProjectileSize = 0; //the size of the projectile immunity bitfield (dwords)
static const char iwd2gemrb[32] = {
0,0,20,2,22,25,0,14,
15,23,13,0,1,24,8,21,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0
};
static const char gemrb2iwd[32] = {
11,12,3,71,72,73,0,0, //0
14,80,83,82,81,10,7,8, //8
0,0,0,0,2,15,4,9, //16
13,5,0,0,0,0,0,0 //24
};
//letters for char sound resolution bg1/bg2
static char csound[VCONST_COUNT];
static void InitActorTables();
#define DAMAGE_LEVELS 19
#define ATTACKROLL 20
#define SAVEROLL 20
#define DEFAULTAC 10
//TODO: externalise
#define TURN_PANIC_LVL_MOD 3
#define TURN_DEATH_LVL_MOD 7
static ieResRef d_main[DAMAGE_LEVELS] = {
//slot 0 is not used in the original engine
"BLOODCR","BLOODS","BLOODM","BLOODL", //blood
"SPFIRIMP","SPFIRIMP","SPFIRIMP", //fire
"SPSHKIMP","SPSHKIMP","SPSHKIMP", //spark
"SPFIRIMP","SPFIRIMP","SPFIRIMP", //ice
"SHACID","SHACID","SHACID", //acid
"SPDUSTY2","SPDUSTY2","SPDUSTY2" //disintegrate
};
static ieResRef d_splash[DAMAGE_LEVELS] = {
"","","","",
"SPBURN","SPBURN","SPBURN", //flames
"SPSPARKS","SPSPARKS","SPSPARKS", //sparks
"","","",
"","","",
"","",""
};
#define BLOOD_GRADIENT 19
#define FIRE_GRADIENT 19
#define ICE_GRADIENT 71
#define STONE_GRADIENT 93
static int d_gradient[DAMAGE_LEVELS] = {
BLOOD_GRADIENT,BLOOD_GRADIENT,BLOOD_GRADIENT,BLOOD_GRADIENT,
FIRE_GRADIENT,FIRE_GRADIENT,FIRE_GRADIENT,
-1,-1,-1,
ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT,
-1,-1,-1,
-1,-1,-1
};
static ieResRef hc_overlays[OVERLAY_COUNT]={"SANCTRY","SPENTACI","SPMAGGLO","SPSHIELD",
"GREASED","WEBENTD","MINORGLB","","","","","","","","","","","","","","",
"","","","SPTURNI2","SPTURNI","","","","","",""};
static ieDword hc_locations=0x2ba80030;
static int hc_flags[OVERLAY_COUNT];
#define HC_INVISIBLE 1
static int *mxsplwis = NULL;
static int spllevels;
// thieving skill dexterity and race boni vectors
std::vector<std::vector<int> > skilldex;
std::vector<std::vector<int> > skillrac;
// iwd2 class to-hit and apr tables read into a single object
std::map<char *, std::vector<BABTable> > IWD2HitTable;
typedef std::map<char *, std::vector<BABTable> >::iterator IWD2HitTableIter;
std::map<int, char *> BABClassMap; // maps classis (not id!) to the BAB table
//for every game except IWD2 we need to reverse TOHIT
static int ReverseToHit=true;
static int CheckAbilities=false;
static EffectRef fx_sleep_ref = { "State:Helpless", -1 };
static EffectRef fx_cleave_ref = { "Cleave", -1 };
static EffectRef fx_tohit_vs_creature_ref = { "ToHitVsCreature", -1 };
static EffectRef fx_damage_vs_creature_ref = { "DamageVsCreature", -1 };
static EffectRef fx_mirrorimage_ref = { "MirrorImageModifier", -1 };
static EffectRef fx_set_charmed_state_ref = { "State:Charmed", -1 };
static EffectRef fx_cure_sleep_ref = { "Cure:Sleep", -1 };
static EffectRef fx_damage_bonus_modifier_ref = { "DamageBonusModifier2", -1 };
//bg2 and iwd1
static EffectRef control_creature_ref = { "ControlCreature", -1 };
//iwd2
static EffectRef control_undead_ref = { "ControlUndead2", -1 };
static EffectRef fx_cure_poisoned_state_ref = { "Cure:Poison", -1 };
static EffectRef fx_cure_hold_state_ref = { "Cure:Hold", -1 };
static EffectRef fx_cure_stun_state_ref = { "Cure:Stun", -1 };
static EffectRef fx_remove_portrait_icon_ref = { "Icon:Remove", -1 };
static EffectRef fx_unpause_caster_ref = { "Cure:CasterHold", -1 };
static EffectRef fx_ac_vs_creature_type_ref = { "ACVsCreatureType", -1 };
static EffectRef fx_puppetmarker_ref = { "PuppetMarker", -1 };
static EffectRef fx_stoneskin_ref = { "StoneSkinModifier", -1 };
static EffectRef fx_stoneskin2_ref = { "StoneSkin2Modifier", -1 };
static EffectRef fx_aegis_ref = { "Aegis", -1 };
static EffectRef fx_cloak_ref = { "Overlay", -1 };
static EffectRef fx_damage_ref = { "Damage", -1 };
static EffectRef fx_melee_ref = { "SetMeleeEffect", -1 };
static EffectRef fx_ranged_ref = { "SetRangedEffect", -1 };
static EffectRef fx_cant_use_item_ref = { "CantUseItem", -1 };
static EffectRef fx_cant_use_item_type_ref = { "CantUseItemType", -1 };
static EffectRef fx_remove_invisible_state_ref = { "ForceVisible", -1 };
static EffectRef fx_remove_sanctuary_ref = { "Cure:Sanctuary", -1 };
static EffectRef fx_disable_button_ref = { "DisableButton", -1 };
static EffectRef fx_damage_reduction_ref = { "DamageReduction", -1 };
static EffectRef fx_missile_damage_reduction_ref = { "MissileDamageReduction", -1 };
//used by iwd2
static ieResRef resref_cripstr={"cripstr"};
static ieResRef resref_dirty={"dirty"};
static const int weapon_damagetype[] = {DAMAGE_CRUSHING, DAMAGE_PIERCING,
DAMAGE_CRUSHING, DAMAGE_SLASHING, DAMAGE_MISSILE, DAMAGE_STUNNING};
//internal flags for calculating to hit
#define WEAPON_FIST 0
#define WEAPON_MELEE 1
#define WEAPON_RANGED 2
#define WEAPON_STYLEMASK 15
#define WEAPON_LEFTHAND 16
#define WEAPON_USESTRENGTH 32
#define WEAPON_FINESSE 64
#define WEAPON_BYPASS 0x10000
#define WEAPON_KEEN 0x20000
static int avBase, avStance;
struct avType {
ieResRef avresref;
AutoTable avtable;
int stat;
};
static avType *avPrefix;
static int avCount = -1;
/* counts the on bits in a number */
ieDword bitcount (ieDword n)
{
ieDword count=0;
while (n) {
count += n & 0x1u;
n >>= 1;
}
return count;
}
void ReleaseMemoryActor()
{
if (mxsplwis) {
//calloc'd x*y integer matrix
free (mxsplwis);
mxsplwis = NULL;
}
if (fistres) {
delete [] fistres;
fistres = NULL;
delete [] fistresclass;
fistresclass = NULL;
}
if (itemuse) {
delete [] itemuse;
itemuse = NULL;
}
/*
if (kituse) {
delete [] kituse;
kituse = NULL;
}
*/
if (itemanim) {
delete [] itemanim;
itemanim = NULL;
}
FistRows = -1;
}
Actor::Actor()
: Movable( ST_ACTOR )
{
int i;
for (i = 0; i < MAX_STATS; i++) {
BaseStats[i] = 0;
Modified[i] = 0;
}
PrevStats = NULL;
SmallPortrait[0] = 0;
LargePortrait[0] = 0;
anims = NULL;
ShieldRef[0]=0;
HelmetRef[0]=0;
WeaponRef[0]=0;
for (i = 0; i < EXTRA_ACTORCOVERS; ++i)
extraCovers[i] = NULL;
LongName = NULL;
ShortName = NULL;
LongStrRef = (ieStrRef) -1;
ShortStrRef = (ieStrRef) -1;
playedCommandSound = false;
PCStats = NULL;
LastDamage = 0;
LastDamageType = 0;
LastExit = 0;
attackcount = 0;
secondround = 0;
//AttackStance = IE_ANI_ATTACK;
attacksperround = 0;
nextattack = 0;
nextWalk = 0;
lastattack = 0;
InTrap = 0;
PathTries = 0;
TargetDoor = 0;
attackProjectile = NULL;
lastInit = 0;
roundTime = 0;
modalTime = 0;
modalSpellLingering = 0;
panicMode = PANIC_NONE;
nextComment = 0;
nextBored = 0;
inventory.SetInventoryType(INVENTORY_CREATURE);
fxqueue.SetOwner( this );
inventory.SetOwner( this );
if (classcount<0) {
//This block is executed only once, when the first actor is loaded
InitActorTables();
TranslucentShadows = 0;
core->GetDictionary()->Lookup("Translucent Shadows", TranslucentShadows);
//get the needed size to store projectile immunity bitflags in Dwords
ProjectileSize = (core->GetProjectileServer()->GetHighestProjectileNumber()+31)/32;
//allowing 1024 bits (1024 projectiles ought to be enough for everybody)
//the rest of the projectiles would still work, but couldn't be resisted
if (ProjectileSize>32) {
ProjectileSize=32;
}
}
multiclass = 0;
projectileImmunity = (ieDword *) calloc(ProjectileSize,sizeof(ieDword));
AppearanceFlags = 0;
SetDeathVar = IncKillCount = UnknownField = 0;
memset( DeathCounters, 0, sizeof(DeathCounters) );
InParty = 0;
TalkCount = 0;
InteractCount = 0; //numtimesinteracted depends on this
appearance = 0xffffff; //might be important for created creatures
RemovalTime = ~0;
HomeLocation.x = 0;
HomeLocation.y = 0;
Spawned = false;
version = 0;
//these are used only in iwd2 so we have to default them
for(i=0;i<7;i++) {
BaseStats[IE_HATEDRACE2+i]=0xff;
}
//this one is saved only for PC's
ModalState = 0;
//set it to a neutral value
ModalSpell[0] = '*';
LingeringModalSpell[0] = '*';
BackstabResRef[0] = '*';
//this one is not saved
GotLUFeedback = false;
RollSaves();
WMLevelMod = 0;
TicksLastRested = 0;
polymorphCache = NULL;
memset(&wildSurgeMods, 0, sizeof(wildSurgeMods));
AC.SetOwner(this);
ToHit.SetOwner(this);
}
Actor::~Actor(void)
{
unsigned int i;
delete anims;
core->FreeString( LongName );
core->FreeString( ShortName );
delete PCStats;
for (i = 0; i < vvcOverlays.size(); i++) {
if (vvcOverlays[i]) {
delete vvcOverlays[i];
vvcOverlays[i] = NULL;
}
}
for (i = 0; i < vvcShields.size(); i++) {
if (vvcShields[i]) {
delete vvcShields[i];
vvcShields[i] = NULL;
}
}
for (i = 0; i < EXTRA_ACTORCOVERS; i++)
delete extraCovers[i];
delete attackProjectile;
delete polymorphCache;
free(projectileImmunity);
}
void Actor::SetFistStat(ieDword stat)
{
fiststat = stat;
}
void Actor::SetDefaultActions(int qslot, ieByte slot1, ieByte slot2, ieByte slot3)
{
QslotTranslation=qslot;
DefaultButtons[0]=slot1;
DefaultButtons[1]=slot2;
DefaultButtons[2]=slot3;
}
void Actor::SetName(const char* ptr, unsigned char type)
{
size_t len = strlen( ptr ) + 1;
//32 is the maximum possible length of the actor name in the original games
if (len>32) len=33;
if (type!=2) {
LongName = ( char * ) realloc( LongName, len );
memcpy( LongName, ptr, len );
LongName[len-1]=0;
core->StripLine( LongName, len );
}
if (type!=1) {
ShortName = ( char * ) realloc( ShortName, len );
memcpy( ShortName, ptr, len );
ShortName[len-1]=0;
core->StripLine( ShortName, len );
}
}
void Actor::SetName(int strref, unsigned char type)
{
if (type!=2) {
if (LongName) free(LongName);
LongName = core->GetString( strref, IE_STR_REMOVE_NEWLINE );
LongStrRef = strref;
}
if (type!=1) {
if (ShortName) free(ShortName);
ShortName = core->GetString( strref, IE_STR_REMOVE_NEWLINE );
ShortStrRef = strref;
}
}
void Actor::SetAnimationID(unsigned int AnimID)
{
//if the palette is locked, then it will be transferred to the new animation
Palette *recover = NULL;
if (anims) {
if (anims->lockPalette) {
recover = anims->palette[PAL_MAIN];
}
// Take ownership so the palette won't be deleted
if (recover) {
recover->acquire();
}
delete( anims );
}
//hacking PST no palette
if (core->HasFeature(GF_ONE_BYTE_ANIMID) ) {
if ((AnimID&0xf000)==0xe000) {
if (BaseStats[IE_COLORCOUNT]) {
Log(WARNING, "Actor", "Animation ID %x is supposed to be real colored (no recoloring), patched creature", AnimID);
}
BaseStats[IE_COLORCOUNT]=0;
}
}
anims = new CharAnimations( AnimID&0xffff, BaseStats[IE_ARMOR_TYPE]);
if(anims->ResRef[0] == 0) {
delete anims;
anims = NULL;
Log(ERROR, "Actor", "Missing animation for %s", LongName);
return;
}
anims->SetOffhandRef(ShieldRef);
anims->SetHelmetRef(HelmetRef);
anims->SetWeaponRef(WeaponRef);
//if we have a recovery palette, then set it back
assert(anims->palette[PAL_MAIN] == 0);
anims->palette[PAL_MAIN] = recover;
if (recover) {
anims->lockPalette = true;
}
//bird animations are not hindered by searchmap
//only animtype==7 (bird) uses this feature
//this is a hardcoded hack, but works for all engine type
if (anims->GetAnimType()!=IE_ANI_BIRD) {
BaseStats[IE_DONOTJUMP]=0;
} else {
BaseStats[IE_DONOTJUMP]=DNJ_BIRD;
}
SetCircleSize();
anims->SetColors(BaseStats+IE_COLORS);
//Speed is determined by the number of frames in each cycle of its animation
// (beware! GetAnimation has side effects!)
// TODO: we should have a more efficient way to look this up
Animation** anim = anims->GetAnimation(IE_ANI_WALK, 0);
if (anim && anim[0]) {
SetBase(IE_MOVEMENTRATE, anim[0]->GetFrameCount()) ;
} else {
Log(WARNING, "Actor", "Unable to determine movement rate for animation %04x!", AnimID);
}
}
CharAnimations* Actor::GetAnims() const
{
return anims;
}
/** Returns a Stat value (Base Value + Mod) */
ieDword Actor::GetStat(unsigned int StatIndex) const
{
if (StatIndex >= MAX_STATS) {
return 0xdadadada;
}
return Modified[StatIndex];
}
/** Always return a final stat value not partially calculated ones */
ieDword Actor::GetSafeStat(unsigned int StatIndex) const
{
if (StatIndex >= MAX_STATS) {
return 0xdadadada;
}
if (PrevStats) return PrevStats[StatIndex];
return Modified[StatIndex];
}
void Actor::SetCircleSize()
{
const Color *color;
int color_index;
if (!anims)
return;
GameControl *gc = core->GetGameControl();
if (UnselectableTimer) {
color = &magenta;
color_index = 4;
} else if (Modified[IE_STATE_ID] & STATE_PANIC) {
color = &yellow;
color_index = 5;
} else if (Modified[IE_CHECKFORBERSERK]) {
color = &yellow;
color_index = 5;
} else if (gc && gc->dialoghandler->targetID == GetGlobalID() && (gc->GetDialogueFlags()&DF_IN_DIALOG)) {
color = &white;
color_index = 3; //?? made up
} else {
switch (Modified[IE_EA]) {
case EA_PC:
case EA_FAMILIAR:
case EA_ALLY:
case EA_CONTROLLED:
case EA_CHARMED:
case EA_EVILBUTGREEN:
case EA_GOODCUTOFF:
color = &green;
color_index = 0;
break;
case EA_EVILCUTOFF:
color = &yellow;
color_index = 5;
break;
case EA_ENEMY:
case EA_GOODBUTRED:
case EA_CHARMEDPC:
color = &red;
color_index = 1;
break;
default:
color = &cyan;
color_index = 2;
break;
}
}
int csize = anims->GetCircleSize() - 1;
if (csize >= MAX_CIRCLE_SIZE)
csize = MAX_CIRCLE_SIZE - 1;
SetCircle( anims->GetCircleSize(), *color, core->GroundCircles[csize][color_index], core->GroundCircles[csize][(color_index == 0) ? 3 : color_index] );
}
static void ApplyClab_internal(Actor *actor, const char *clab, int level, bool remove)
{
AutoTable table(clab);
if (table) {
int row = table->GetRowCount();
for(int i=0;i<level;i++) {
for (int j=0;j<row;j++) {
const char *res = table->QueryField(j,i);
if (res[0]=='*') continue;
if (!memcmp(res,"AP_",3)) {
if (remove) {
actor->fxqueue.RemoveAllEffects(res+3);
} else {
core->ApplySpell(res+3, actor, actor, 0);
}
}
else if (!memcmp(res,"GA_",3)) {
if (remove) {
actor->spellbook.RemoveSpell(res+3);
} else {
actor->LearnSpell(res+3, LS_MEMO);
}
}
else if (!memcmp(res,"FA_",3)) {//iwd2 only
//memorize these
int x=atoi(res+3);
ieResRef resref;
ResolveSpellName(resref, x);
actor->LearnSpell(resref, LS_MEMO);
}
else if (!memcmp(res,"FS_",3)) {//iwd2 only
//don't memorize these
int x=atoi(res+3);
ieResRef resref;
ResolveSpellName(resref, x);
actor->LearnSpell(resref, 0);
}
else if (!memcmp(res,"RA_",3)) {//iwd2 only
//remove ability
int x=atoi(res+3);
actor->spellbook.RemoveSpell(x);
}
}
}
}
}
#define BG2_KITMASK 0xffffc000
#define KIT_BASECLASS 0x4000
#define KIT_SWASHBUCKLER 0x100000
#define KIT_BARBARIAN 0x40000000
//TODO: make kitlist column 6 stored internally
static ieDword GetKitIndex (ieDword kit, const char *resref="kitlist")
{
int kitindex = 0;
if ((kit&BG2_KITMASK) == KIT_BASECLASS) {
kitindex = kit&0xfff;
}
if (kitindex == 0) {
Holder<TableMgr> tm = gamedata->GetTable(gamedata->LoadTable(resref) );
if (tm) {
kitindex = tm->FindTableValue(6, kit);
if (kitindex < 0) {
kitindex = 0;
}
}
}
return (ieDword)kitindex;
}
static ieDword GetKitUsability(ieDword kit, const char *resref="kitlist")
{
if ((kit&BG2_KITMASK) == KIT_BASECLASS) {
int kitindex = kit&0xfff;
Holder<TableMgr> tm = gamedata->GetTable(gamedata->LoadTable(resref) );
if (tm) {
return strtol(tm->QueryField(kitindex, 6), NULL, 0 );
}
}
if (kit&KIT_BASECLASS) return 0;
return kit;
}
//applies a kit on the character (only bg2)
bool Actor::ApplyKit(bool remove)
{
ieDword kit = GetStat(IE_KIT);
ieDword kitclass = 0;
ieDword row = GetKitIndex(kit);
const char *clab = NULL;
ieDword max = 0;
if (row) {
//kit abilities
Holder<TableMgr> tm = gamedata->GetTable(gamedata->LoadTable("kitlist"));
if (tm) {
kitclass = (ieDword) atoi(tm->QueryField(row, 7));
clab = tm->QueryField(row, 4);
}
}
//multi class
if (multiclass) {
ieDword msk = 1;
for(unsigned int i=1;(i<(unsigned int) classcount) && (msk<=multiclass);i++) {
if (multiclass & msk) {
max = GetLevelInClass(i);
// don't apply/remove the old kit clab if the kit is disabled
if (i==kitclass && !IsDualClassed()) {
ApplyClab(clab, max, remove);
} else {
ApplyClab(classabilities[i], max, remove);
}
}
msk+=msk;
}
return true;
}
//single class
ieDword cls = GetStat(IE_CLASS);
if (cls>=(ieDword) classcount) {
return false;
}
max = GetLevelInClass(cls);
if (kitclass==cls) {
ApplyClab(clab, max, remove);
} else {
ApplyClab(classabilities[cls], max, remove);
}
return true;
}
void Actor::ApplyClab(const char *clab, ieDword max, bool remove)
{
if (clab[0]!='*') {
if (max) {
//singleclass
ApplyClab_internal(this, clab, max, true);
if (!remove) {
ApplyClab_internal(this, clab, max, false);
}
}
}
}
//call this when morale or moralebreak changed
//cannot use old or new value, because it is called two ways
void pcf_morale (Actor *actor, ieDword /*oldValue*/, ieDword /*newValue*/)
{
if ((actor->Modified[IE_MORALE]<=actor->Modified[IE_MORALEBREAK]) && (actor->Modified[IE_MORALEBREAK] != 0) ) {
actor->Panic(core->GetGame()->GetActorByGlobalID(actor->LastAttacker), core->Roll(1,3,0) );
} else if (actor->Modified[IE_STATE_ID]&STATE_PANIC) {
// recover from panic, since morale has risen again
// but only if we have really just recovered, so panic from other
// sources isn't affected
if ((actor->Modified[IE_MORALE]-1 == actor->Modified[IE_MORALEBREAK]) || (actor->Modified[IE_MORALEBREAK] == 0) ) {
actor->SetBaseBit(IE_STATE_ID, STATE_PANIC, false);
}
}
//for new colour
actor->SetCircleSize();
}
void pcf_berserk(Actor *actor, ieDword /*oldValue*/, ieDword /*newValue*/)
{
//needs for new color
actor->SetCircleSize();
}
void pcf_ea (Actor *actor, ieDword /*oldValue*/, ieDword newValue)
{
if (actor->Selected && (newValue>EA_GOODCUTOFF) ) {
core->GetGame()->SelectActor(actor, false, SELECT_NORMAL);
}
actor->SetCircleSize();
}
//this is a good place to recalculate level up stuff
void pcf_level (Actor *actor, ieDword oldValue, ieDword newValue)
{
ieDword sum =
actor->GetFighterLevel()+
actor->GetMageLevel()+
actor->GetThiefLevel()+
actor->GetBarbarianLevel()+
actor->GetBardLevel()+
actor->GetClericLevel()+
actor->GetDruidLevel()+
actor->GetMonkLevel()+
actor->GetPaladinLevel()+
actor->GetRangerLevel()+
actor->GetSorcererLevel();
actor->SetBase(IE_CLASSLEVELSUM,sum);
actor->SetupFist();
if (newValue!=oldValue) {
actor->ApplyKit(false);
}
actor->GotLUFeedback = false;
}
void pcf_class (Actor *actor, ieDword /*oldValue*/, ieDword newValue)
{
//Call forced initbuttons in old style systems, and soft initbuttons
//in case of iwd2. Maybe we need a custom quickslots flag here.
actor->InitButtons(newValue, !iwd2class);
int sorcerer=0;
if (newValue<(ieDword) classcount) {
switch(booktypes[newValue]) {
case 2: sorcerer = 1<<IE_SPELL_TYPE_WIZARD; break; //sorcerer
case 3: sorcerer = 1<<IE_SPELL_TYPE_PRIEST; break; //divine caster with sorc. style spells
case 5: sorcerer = 1<<IE_IWD2_SPELL_SHAPE; break; //divine caster with sorc style shapes (iwd2 druid)
default: break;
}
}
actor->spellbook.SetBookType(sorcerer);
}
void pcf_animid(Actor *actor, ieDword /*oldValue*/, ieDword newValue)
{
actor->SetAnimationID(newValue);
}
static const ieDword fullwhite[7]={ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT,ICE_GRADIENT};
static const ieDword fullstone[7]={STONE_GRADIENT,STONE_GRADIENT,STONE_GRADIENT,STONE_GRADIENT,STONE_GRADIENT,STONE_GRADIENT,STONE_GRADIENT};
void pcf_state(Actor *actor, ieDword /*oldValue*/, ieDword State)
{
if (actor->InParty) core->SetEventFlag(EF_PORTRAIT);
if (State & STATE_PETRIFIED) {
actor->SetLockedPalette(fullstone);
return;
}
if (State & STATE_FROZEN) {
actor->SetLockedPalette(fullwhite);
return;
}
//it is not enough to check the new state
core->GetGame()->Infravision();
actor->UnlockPalette();