-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
1512 lines (1370 loc) · 53.2 KB
/
mod.rs
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
mod decoder;
mod encoder;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::prelude::*;
use bitflags::bitflags;
use derive_more::derive::{Display, Error, From};
use glam::UVec2;
use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
use serde::{Deserialize, Serialize};
pub use decoder::{DecodeError, Decoder};
pub use encoder::{EncodeError, Encoder};
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct ScriptState {
pub program_counter: u32,
/// - 0x13 (19u32) is saved after a cutscene (which could also be just
/// before a battle would start).
/// - 0x3A (58u32) is saved at the victory screen.
pub unknown0: u32,
/// The base address in the script where the campaign should start executing
/// from. In the English version of the game, this is `0x4C3C48`. In the
/// German version of the game, this is `0x4C3D90`. Combine with
/// `execution_offset_index
/// * 4` to get the address to start executing from.
pub base_execution_address: u32,
/// In the English version of the game, this is `0x4CCD28`. In the German
/// version of the game, this is `0x4CCE70`.
pub unknown_address: u32,
/// Initialized to 0 on init.
pub local_variable: u32,
/// Initialized to 1 on init.
pub unknown1: u32,
/// Initialized to 20 on init.
pub stack_pointer: u32,
unknown2: Vec<u8>,
unknown2_hex: Vec<String>, // TODO: Remove, debug only.
unknown2_as_u32s: Vec<u32>, // TODO: Remove, debug only.
/// The offset index to add to the base execution address to get the address
/// to start executing from. To account for alignment, multiply this value
/// by 4 before adding it to the base address.
pub execution_offset_index: u32,
pub unknown3: u32,
/// Initialized to 0 on init.
pub nest_if: u32,
/// Initialized to 0 on init.
pub nest_gosub: u32,
/// Initialized to 0 on init.
pub nest_loop: u32,
pub unknown4: u32,
/// Initialized to the current tick count on init, which is the number of
/// milliseconds since the operating system started. The purpose of this is
/// not yet known.
pub elapsed_millis: u32,
/// Initialized to 0 on init.
pub unknown5: u32,
/// Initialized to 0 on init.
pub unknown6: u32,
unknown7: Vec<u8>,
unknown7_hex: Vec<String>, // TODO: Remove, debug only.
unknown7_as_u32s: Vec<u32>, // TODO: Remove, debug only.
}
impl ScriptState {
/// Returns the address to start executing from.
pub fn execution_address(&self) -> u32 {
self.base_execution_address + self.execution_offset_index * 4
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct SaveGameHeader {
/// The name displayed when loading the save game.
pub display_name: String,
/// The original game writes over the existing display name with the new
/// path but the old bytes are not cleared first. This field is used to
/// store the residual bytes, if there are any. If it's `None` then there
/// are no residual bytes / all bytes are zero after the null-terminated
/// string. If it's `Some`, then it contains the residual bytes, up to, but
/// not including, the last nul-terminated string.
display_name_residual_bytes: Option<Vec<u8>>,
/// The name suggested when saving the game.
pub suggested_display_name: String,
/// The original game writes over the existing suggested display name with
/// the new path but the old bytes are not cleared first. This field is used
/// to store the residual bytes, if there are any. If it's `None` then there
/// are no residual bytes / all bytes are zero after the null-terminated
/// string. If it's `Some`, then it contains the residual bytes, up to, but
/// not including, the last nul-terminated string.
suggested_display_name_residual_bytes: Option<Vec<u8>>,
pub unknown_bool1: bool,
pub unknown_bool2: bool,
script_state_hex: Vec<String>, // TODO: Remove, debug only.
script_state_as_u32s: Vec<u32>, // TODO: Remove, debug only.
/// The script state of the save game. Used by the WHMTG scripting engine to
/// run the next part of the campaign.
pub script_state: ScriptState,
/// Protect Bogenhafen mission played.
pub bogenhafen_mission: bool,
/// Attacked Goblin Camp or helped Ragnar.
pub goblin_camp_or_ragnar: bool,
/// Attacked the goblin camp together with Munz.
pub goblin_camp_mission: bool,
/// Helps Ragnar but mission has not been started.
pub ragnar_mission_pre_battle: bool,
/// Attacked Greenskins in Vingtienne or helped Treeman.
pub vingtienne_or_treeman: bool,
/// Attacked the Greenskins near Vingtienne.
pub vingtienne_mission: bool,
/// Helped the treeman in Loren Lake mission.
pub treeman_mission: bool,
/// Manfred von Carstein defeated.
pub carstein_defeated: bool,
/// Hand of Nagash defeated.
pub hand_of_nagash_defeated: bool,
/// Black Grail defeated.
pub black_grail_defeated: bool,
pub unknown1: u32,
/// Helmgart mission played.
pub helmgart_mission: bool,
/// Helped Ragnar defeat the trolls.
pub ragnar_mission: bool,
/// Talked with King Orion (Woodelf King).
pub loren_king_met: bool,
/// Helped Azkuz moving through the Axebite Pass.
pub axebite_mission: bool,
pub unknown2: u32,
pub unknown3: u32,
pub unknown4: u32,
pub unknown5: u32,
pub unknown6: u32,
pub unknown7: u32,
/// Previous fought battle won.
pub previous_battle_won_1: bool,
/// Previous fought battle won.
pub previous_battle_won_2: bool,
/// Answer for last asked question.
pub previous_answer: u32,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct CutsceneAnimation {
/// Whether the animation is enabled.
pub enabled: bool,
unknown1: u32,
/// The initial position of the animation within the background image.
pub position: UVec2,
/// The path to the sprite sheet file, e.g. "[SPRITES]\m_empbi1.spr".
pub path: String,
unknown2: u32,
unknown3: u32,
/// The number of sprites in the sprite sheet / the number of frames in the
/// animation.
pub sprite_count: u32,
/// The duration, in milliseconds, to display each frame of the animation.
pub frame_duration_millis: u32,
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct SaveGameFooter {
unknown1: Vec<u8>,
unknown1_as_u16s: Vec<u16>, // TODO: Remove, debug only.
unknown1_as_u32s: Vec<u32>, // TODO: Remove, debug only.
/// The path to the background image file, e.g. "[PICTURES]\m_empn.bmp".
pub background_image_path: Option<String>,
/// The original game writes over the existing background image path with
/// the new path but the old bytes are not cleared first. This field is used
/// to store the residual bytes, if there are any. If it's `None` then there
/// are no residual bytes / all bytes are zero after the null-terminated
/// string. If it's `Some`, then it contains the residual bytes, up to, but
/// not including, the last nul-terminated string.
background_image_path_residual_bytes: Option<Vec<u8>>,
// 4 u32s. First is always 0. Third is always one more than second, e.g. we
// see pairs like [0, 1] and [52, 53]. Fourth is always some big number, so
// may not be a u32, but around the first level (as in save games where I
// saved frequently) it seems to be `1551335452` in the English save games,
// but `3011451320` in the German save game, so could be some language
// specific data. It may be u8s and could make up 4 portraits / tracks that
// need to load for the current cutscene.
unknown2: Vec<u32>,
/// A list of animations used on the cutscene screens shown in between
/// battles.
pub cutscene_animations: Vec<CutsceneAnimation>,
unknown3: Vec<u8>,
unknown3_as_u16s: Vec<u16>, // TODO: Remove, debug only.
unknown3_as_u32s: Vec<u32>, // TODO: Remove, debug only.
hex: Vec<String>, // TODO: Remove, debug only.
}
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct Army {
/// An optional save game header if the army is a save game.
pub save_game_header: Option<SaveGameHeader>,
/// An optional save game footer if the army is a save game.
pub save_game_footer: Option<SaveGameFooter>,
/// The army's race.
///
/// This is used in multiplayer mode to group armies by race.
pub race: ArmyRace,
unknown1: [u8; 3], // always seems to be 0, could be padding
/// The index of the name to use when army name is empty.
///
/// This is used to display the army name in multiplayer mode when no army
/// name is set.
pub default_name_index: u16,
/// The name of the army. Displayed in multiplayer mode.
pub name: String,
/// There are some bytes after the null-terminated string. Not sure what
/// they are for.
name_remainder: Vec<u8>,
pub small_banner_path: String,
/// There are some bytes after the null-terminated string. Not sure what
/// they are for.
small_banner_path_remainder: Vec<u8>,
pub small_disabled_banner_path: String,
/// There are some bytes after the null-terminated string. Not sure what
/// they are for.
small_disabled_banner_path_remainder: Vec<u8>,
small_disabled_banner_path_remainder_as_u16s: Vec<u16>, // TODO: Remove, debug only.
small_disabled_banner_path_remainder_as_u32s: Vec<u32>, // TODO: Remove, debug only.
pub large_banner_path: String,
/// There are some bytes after the null-terminated string. Not sure what
/// they are for.
large_banner_path_remainder: Vec<u8>,
large_banner_path_remainder_as_u16s: Vec<u16>, // TODO: Remove, debug only.
large_banner_path_remainder_as_u32s: Vec<u32>, // TODO: Remove, debug only.
/// The amount of gold captured from treasures and earned in the last
/// battle.
pub last_battle_gold: u16,
/// The amount of gold available to the army for buying new units and
/// reinforcements.
pub gold_in_coffers: u16,
/// A list of magic items in the army's inventory.
///
/// Each magic item is an index into the list of magic items. A value of 1
/// means the Grudgebringer Sword is equipped in that slot. A value of 0
/// means the army does not have anything in that slot.
pub magic_items: Vec<u8>,
unknown3: Vec<u8>,
pub regiments: Vec<Regiment>,
}
impl Army {
/// Returns true if the army has any magic items in its inventory.
pub fn any_magic_items(&self) -> bool {
self.magic_items.iter().any(|&item| item != 0)
}
/// Returns a list of all magic items in the army's inventory.
pub fn all_magic_items(&self) -> Vec<u8> {
self.magic_items
.iter()
.filter(|&&item| item != 0)
.copied()
.collect()
}
}
bitflags! {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(opaque))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug, Default, Deserialize, Hash, PartialEq, Serialize))]
pub struct ArmyRace: u8 {
/// Empire army.
const EMPIRE = 0;
/// Multiplayer army.
const MULTIPLAYER = 1 << 0;
/// Greenskins army.
const GREENSKINS = 1 << 1;
/// Undead army.
const UNDEAD = 1 << 2;
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct Regiment {
pub flags: RegimentFlags,
unknown1: [u8; 2],
pub id: u32,
pub mage_class: MageClass,
/// The regiment's maximum level of armor.
pub max_armor: u8,
pub cost: u16,
/// The index into the list of sprite sheet file names found in ENGREL.EXE
/// for the regiment's banner.
pub banner_sprite_sheet_index: u16,
unknown3: [u8; 2],
pub attributes: RegimentAttributes,
/// The profile of the regiment's rank and file units.
pub unit_profile: UnitProfile,
unknown4: u8,
/// The profile of the regiment's leader unit.
///
/// Some of the fields are not used for leader units.
pub leader_profile: UnitProfile,
/// The leader's 3D head ID.
pub leader_head_id: u16,
/// The stats of the regiment's last battle.
pub last_battle_stats: LastBattleStats,
/// A number that represents the regiment's total experience.
///
/// It is a number between 0 and 6000. If experience is <1000 then the
/// regiment has a threat level of 1. If experience >=1000 and <3000 then
/// the regiment has a threat level of 2. If experience >= 3000 and <6000
/// then the regiment has a threat level of 3. If experience >= 6000 then
/// the regiment has a threat level of 4.
pub total_experience: u16,
pub duplicate_id: u8,
/// The regiment's minimum or base level of armor.
///
/// This is displayed as the gold shields in the troop roster.
pub min_armor: u8,
/// The spell book that is equipped to the regiment. A spell book is one of
/// the magic items.
///
/// This is an index into the list of magic items. In the original game, the
/// value is either 22, 23, 24, 25 or 65535.
///
/// A value of 22 means the Bright Book is equipped. A value of 23 means the
/// Ice Book is equipped. A value of 65535 means the regiment does not have
/// a spell book slot. Only mages can equip spell books.
pub spell_book: SpellBook,
/// A list of magic items that are equipped to the regiment.
///
/// Each magic item is an index into the list of magic items. A value of 1
/// means the Grudgebringer Sword is equipped in that slot. A value of 65535
/// means the regiment does not have anything equipped in that slot.
pub magic_items: [u16; 3],
/// A list of spells that the regiment can cast.
///
/// Each spell is an index into the list of magic items unless the value is
/// 0 or 65535. From testing changes to `SPARE9MR.ARM` in the original game,
/// it doesn't seem like this can be changed to a specific set of spells.
/// The changes seem to be ignored. It's possible that a CTL file overrides
/// this value, or for player regiments, the threat level determines the
/// number of spells to provision.
///
/// See `GAMEDATA/1PBAT/B1_04/B104NME.ARM` for an example of all 0s in the
/// spells field.
///
/// See `GAMEDATA/1PBAT/B3_08/B308MRC.ARM` and
/// `GAMEDATA/1PBAT/B3_08/B308NME.ARM` for an example with non-zero values.
pub spells: [u16; 5],
/// The amount of gold captured by the regiment in the last battle. The
/// total amount of gold captured by the army can be calculated by summing
/// the gold captured by each regiment.
pub gold_captured: u16,
pub purchased_armor: u8,
pub max_purchasable_armor: u8,
pub repurchased_unit_count: u8,
pub max_purchasable_unit_count: u8,
pub book_profile: [u8; 4],
}
impl Regiment {
/// The maximum threat rating for a regiment.
pub const MAX_THREAT_RATING: u8 = 4;
/// Returns the display name of the regiment.
///
/// May be empty. The display name ID is the preferred way to get the
/// display name. This is so that the display name can be localized.
#[inline(always)]
pub fn display_name(&self) -> &str {
self.unit_profile.display_name.as_str()
}
/// Returns the display name ID of the regiment.
///
/// The display name ID is used to look up the display name string in the
/// list of display names found in ENGREL.EXE. This allows the display name
/// to be localized.
///
/// This is an index into the list of display names found in ENGREL.EXE.
#[inline(always)]
pub fn display_name_id(&self) -> u16 {
self.unit_profile.display_name_id
}
/// Marks the regiment as active.
pub fn mark_active(&mut self) {
self.flags.insert(RegimentFlags::ACTIVE);
}
/// Forces the regiment to be deployed.
pub fn mark_must_deploy(&mut self) {
self.flags.insert(RegimentFlags::MUST_DEPLOY);
}
/// Returns `true` if the regiment must be deployed.
pub fn must_deploy(&self) -> bool {
self.flags.contains(RegimentFlags::MUST_DEPLOY)
}
/// Returns `true` if the regiment is deployable.
pub fn is_deployable(&self) -> bool {
self.flags.contains(RegimentFlags::ACTIVE)
&& !self.flags.contains(RegimentFlags::NON_DEPLOYABLE)
}
/// Returns the number of units in the regiment that are alive.
#[inline(always)]
pub fn alive_unit_count(&self) -> usize {
self.unit_profile.alive_unit_count as usize
}
/// Returns the maximum number of units allowed in the regiment.
#[inline(always)]
pub fn max_unit_count(&self) -> usize {
self.unit_profile.max_unit_count as usize
}
/// Returns the rank count.
#[inline(always)]
pub fn rank_count(&self) -> usize {
self.unit_profile.rank_count as usize
}
/// A value from 1 to 4, inclusive, that indicates the regiment's threat
/// rating.
#[inline(always)]
pub fn threat_rating(&self) -> u8 {
(self.unit_profile.point_value >> 3) + 1
}
/// Returns `true` if the regiment is a mage.
#[inline(always)]
pub fn is_mage(&self) -> bool {
self.mage_class != MageClass::None
}
/// Returns `true` if the regiment has any magic items equipped.
pub fn any_magic_items(&self) -> bool {
self.magic_items.iter().any(|&item| item != 65535)
}
/// Returns a list of all magic items equipped to the regiment.
pub fn all_magic_items(&self) -> Vec<u16> {
self.magic_items
.iter()
.filter(|&&item| item != 65535)
.copied()
.collect()
}
}
bitflags! {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(opaque))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug, Default, Deserialize, Hash, PartialEq, Serialize))]
pub struct RegimentFlags: u16 {
/// No flags are set. This is the default state.
const NONE = 0;
/// Set if the regiment is active. Active regiments can be deployed to
/// the battlefield. This is used when deciding if the regiment should
/// be shown in the troop roster, or if the regiment is available in the
/// army reserve. Also known as "available for hire".
const ACTIVE = 1 << 0;
/// Set if the regiment was deployed in the last battle. This is used
/// when deciding if the regiment should be shown on the victory screen
/// battle stats roster.
const DEPLOYED_LAST_BATTLE = 1 << 1;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_2 = 1 << 2;
/// Set if the regiment must be deployed to the battlefield. Regiments
/// that must be deployed cannot be taken off the battlefield. The
/// player is not allowed to put them in the army reserve.
const MUST_DEPLOY = 1 << 3;
/// TODO: Not sure what this flag is yet. This is used by almost every
/// regiment across .ARM and save games. Removed this from a regiment
/// and they battled fine and then the flag stayed off after the battle
/// was finished (i.e. it wasn't reinstated after the battle).
const UNKNOWN_REGIMENT_FLAG_4 = 1 << 4;
/// TODO: Not sure what this flag is yet. This is only used in save
/// games and compared to [`RegimentFlags::UNKNOWN_REGIMENT_FLAG_4`],
/// it's only used by a handful of regiments.
const UNKNOWN_REGIMENT_FLAG_5 = 1 << 5;
/// Set if the regiment is non-deployable. Non-deployable regiments
/// cannot be deployed to the battlefield and do not appear in the army
/// reserve. This overrides the [`RegimentFlags::ACTIVE`] flag when
/// deciding if the regiment can be deployed. This is used for cases
/// such as underground battles where artillery regiments like cannons
/// and mortars are not available (you can imagine they stay above
/// ground back at base). Regiments with the [`RegimentFlags::ACTIVE`]
/// flag as well as the [`RegimentFlags::NON_DEPLOYABLE`] flag are shown
/// in the troop roster but cannot be deployed to the battlefield.
const NON_DEPLOYABLE = 1 << 6;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_7 = 1 << 7;
/// Set if the regiment is temporarily in the army. In the troop roster,
/// temporary regiments are shown with a green arrow next to the banner.
const TEMPORARY = 1 << 8;
/// Set if the regiment has departed.
const DEPARTED = 1 << 9;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_10 = 1 << 10;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_11 = 1 << 11;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_12 = 1 << 12;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_13 = 1 << 13;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_14 = 1 << 14;
/// The flag seems to be unused in any .ARM or save games. It's possible
/// it's only set during battle.
const UNKNOWN_REGIMENT_FLAG_15 = 1 << 15;
}
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum MageClass {
#[default]
None = 0,
BaseMage = 2,
OrcAdept = 3,
AdeptMage = 4,
MasterMage = 5,
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum RegimentAlignment {
#[default]
Good = 0,
Neutral = 64,
Evil = 128,
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum RegimentClass {
#[default]
None = 0,
HumanInfantryman = 8,
WoodElfInfantryman = 9,
DwarfInfantryman = 10,
NightGoblinInfantryman = 11,
OrcInfantryman = 12,
UndeadInfantryman = 13,
Townsperson = 14,
Ogre = 15,
HumanCavalryman = 16,
OrcCavalryman = 20,
UndeadCavalryman = 21,
HumanArcher = 24,
WoodElfArcher = 25,
NightGoblinArcher = 27,
OrcArcher = 28,
SkeletonArcher = 29,
HumanArtilleryUnit = 32,
OrcArtilleryUnit = 36,
UndeadArtilleryUnit = 37,
HumanMage = 40,
NightGoblinShaman = 43,
OrcShaman = 44,
EvilMage = 45,
DreadKing = 53,
Monster = 55,
UndeadChariot = 61,
Fanatic = 67,
Unknown1 = 71,
}
impl RegimentClass {
pub fn is_infantry(&self) -> bool {
Into::<u8>::into(*self) >> 3 == Into::<u8>::into(RegimentType::Infantryman)
}
pub fn is_cavalry(&self) -> bool {
Into::<u8>::into(*self) >> 3 == Into::<u8>::into(RegimentType::Cavalryman)
}
pub fn is_archer(&self) -> bool {
Into::<u8>::into(*self) >> 3 == Into::<u8>::into(RegimentType::Archer)
}
pub fn is_artillery(&self) -> bool {
Into::<u8>::into(*self) >> 3 == Into::<u8>::into(RegimentType::ArtilleryUnit)
}
pub fn is_mage(&self) -> bool {
Into::<u8>::into(*self) >> 3 == Into::<u8>::into(RegimentType::Mage)
}
pub fn is_human(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::Human)
}
pub fn is_wood_elf(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::WoodElf)
}
pub fn is_dwarf(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::Dwarf)
}
pub fn is_night_goblin(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::NightGoblin)
}
pub fn is_orc(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::Orc)
}
pub fn is_undead(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::Undead)
}
pub fn is_townsperson(&self) -> bool {
Into::<u8>::into(*self) & 0x07 == Into::<u8>::into(RegimentRace::Townsfolk)
}
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum RegimentType {
#[default]
Unknown,
Infantryman,
Cavalryman,
Archer,
ArtilleryUnit,
Mage,
Monster,
Chariot,
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum RegimentRace {
#[default]
Human,
WoodElf,
Dwarf,
NightGoblin,
Orc,
Undead,
Townsfolk,
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum RegimentMount {
#[default]
None,
Horse,
Boar,
}
bitflags! {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "bevy_reflect", reflect(opaque))]
#[cfg_attr(feature = "bevy_reflect", reflect(Debug, Default, Deserialize, Hash, PartialEq, Serialize))]
pub struct RegimentAttributes: u32 {
const NONE = 0;
/// The regiment never retreats from a fight and the retreat button is
/// disabled.
const NEVER_ROUTS = 1 << 0;
const UNKNOWN_FLAG_2 = 1 << 1;
/// Fear increases the chance that the enemy retreats during combat.
const CAUSES_FEAR = 1 << 2;
/// Stronger version of CAUSES_FEAR.
const CAUSES_TERROR = 1 << 3;
/// Used by elves (unknown use).
const ELF_RACE = 1 << 4;
/// Used by goblins (unknown use).
const GOBLIN_RACE = 1 << 5;
const HATES_GREENSKINS = 1 << 6;
/// Regiment has the same movement speed on every terrain.
const NOT_SLOWED_BY_DIFFICULT_TERRAIN = 1 << 7;
/// Immune against any fear but can still rout.
const IMMUNE_TO_FEAR_CAN_BE_ROUTED = 1 << 8;
/// Slowly heals damage.
const REGENERATES_WOUNDS = 1 << 9;
/// Regiment never regroups when it is retreating.
const NEVER_RALLIES_OR_REGROUPS = 1 << 10;
/// Permanently follows retreating units.
const ALWAYS_PURSUES = 1 << 11;
/// Steam Tank flag. Can't enter close combat anymore.
const ENGINE_OF_WAR_RULE = 1 << 12;
/// Regiment becomes invulnerable.
const INDESTRUCTIBLE = 1 << 13;
const UNKNOWN_FLAG_15 = 1 << 14;
/// Regiment gets lots of additional damage in close combat (used by
/// skeletons).
const SUFFERS_ADDITIONAL_WOUNDS = 1 << 15;
/// If the regiment attacks an enemy using close or ranged combat, the
/// enemy receives additional fear.
const INFLICTING_CASUALTY_CAUSES_FEAR = 1 << 16;
/// Regiment is less resistant against fear.
const COWARDLY = 1 << 17;
/// Regiment dies during retreat.
const DESTROYED_IF_ROUTED = 1 << 18;
/// Suffers additional damage when attacked by fire.
const FLAMMABLE = 1 << 19;
/// Regiment can see everything that isn't blocked by mountains or other
/// obstacles.
const THREE_SIXTY_DEGREE_VISION = 1 << 20;
/// Regiment spawns fanatics if an enemy is near enough.
const SPAWNS_FANATICS = 1 << 21;
/// Used by wraiths (unknown use).
const WRAITH_RACE = 1 << 22;
/// Used by Treeman.
const GIANT = 1 << 23;
/// The goblins on the Trading Post map have this flag set (unknown
/// use).
const GOBLIN_FLAG_TRADING_POST_MAP_ONLY = 1 << 24;
/// Regiment is completely immune against magic attacks.
const IMPERVIOUS_TO_MAGIC = 1 << 25;
/// Same as NEVER_ROUTS but the retreat button is enabled (and ignored).
const NEVER_RETREATS = 1 << 26;
/// Regiment has no item slots. Items can still be assigned.
const NO_ITEM_SLOTS = 1 << 27;
/// Fanatics have this flag.
const FANATICS_FLAG = 1 << 28;
/// Penalty when fighting elves.
const FEARS_ELVES = 1 << 29;
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct LastBattleStats {
/// The number of units in the regiment that were killed in the last battle.
pub unit_killed_count: u16,
unknown1: u16,
/// The number of units the regiment killed in the last battle.
pub kill_count: u16,
/// The regiment's experience gained in the last battle.
pub experience: u16,
}
#[repr(u16)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum SpellBook {
#[default]
None = 65535,
BrightBook = 22,
IceBook = 23,
WaaaghBook = 24,
DarkBook = 25,
}
#[repr(u8)]
#[derive(
Clone, Copy, Debug, Default, Deserialize, IntoPrimitive, PartialEq, Serialize, TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum Weapon {
#[default]
None,
BasicHandWeapon,
TwoHandedWeapon,
Polearm,
Flail,
WightBlade,
}
#[repr(u8)]
#[derive(
Clone,
Copy,
Debug,
Default,
Deserialize,
IntoPrimitive,
PartialEq,
PartialOrd,
Serialize,
TryFromPrimitive,
)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub enum Projectile {
#[default]
None,
ShortBow = 7,
NormalBow = 8,
ElvenBow = 9,
Crossbow = 10,
Pistol = 11,
Cannon = 12,
Mortar = 13,
SteamTankCannon = 14,
RockLobber = 15,
Ballista = 16,
ScreamingSkullCatapult = 17,
}
#[derive(Debug, Display, Error, From)]
pub enum DecodeClassError {
#[error(ignore)]
InvalidType(TryFromPrimitiveError<RegimentType>),
#[error(ignore)]
InvalidRace(TryFromPrimitiveError<RegimentRace>),
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct UnitProfile {
/// The index into the list of sprite sheet file names found in ENGREL.EXE
/// for the unit's sprite sheet.
pub sprite_sheet_index: u16,
/// The display name of the regiment, e.g. "Grudgebringer Cavalry", "Zombies
/// #1", "Imperial Steam Tank".
///
/// May be empty. The display name ID is the preferred way to get the
/// display name. This is so that the display name can be localized.
pub display_name: String,
/// The index into the list of display names found in ENGREL.EXE. This
/// allows the display name to be localized.
pub display_name_id: u16,
/// The regiment's alignment to good or evil.
///
/// - 0x00 (decimal 0) is good.
/// - 0x40 (decimal 64) is neutral.
/// - 0x80 (decimal 128) is evil.
pub alignment: RegimentAlignment,
/// The maximum number of units allowed in the regiment.
pub max_unit_count: u8,
/// The number of units currently alive in the regiment.
pub alive_unit_count: u8,
pub rank_count: u8,
unknown1: Vec<u8>,
pub stats: UnitStats,
pub mount: RegimentMount,
pub armor: u8,
pub weapon: Weapon,
pub class: RegimentClass,
/// A value from 0 to 31, inclusive, that indicates the regiment's threat
/// rating.
///
/// - 0-7: Threat rating 1
/// - 8-15: Threat rating 2
/// - 16-23: Threat rating 3
/// - 24-31: Threat rating 4
///
/// For example, the Dread King has the maximum value of 31 and a threat
/// rating of 4.
///
/// This is set in the `unit_profile`, but 0 in the `leader_profile`.
pub point_value: u8,
pub projectile: Projectile,
unknown2: [u8; 4],
unknown2_a: u16, // TODO: Remove, debug only.
unknown2_b: u16, // TODO: Remove, debug only.
unknown2_as_u32: u32, // TODO: Remove, debug only.
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
pub struct UnitStats {
pub movement: u8,
pub weapon_skill: u8,
pub ballistic_skill: u8,
pub strength: u8,
pub toughness: u8,
pub wounds: u8,
pub initiative: u8,
pub attacks: u8,
pub leadership: u8,
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::{
ffi::{OsStr, OsString},
fs::File,
path::{Path, PathBuf},
};
#[test]
fn test_regiment_threat_rating() {
fn make_regiment(point_value: u8) -> Regiment {
Regiment {
unit_profile: UnitProfile {
point_value,
..Default::default()
},
..Default::default()
}
}
assert_eq!(make_regiment(0).threat_rating(), 1);
assert_eq!(make_regiment(1).threat_rating(), 1);
assert_eq!(make_regiment(7).threat_rating(), 1);
assert_eq!(make_regiment(8).threat_rating(), 2);
assert_eq!(make_regiment(20).threat_rating(), 3);
assert_eq!(make_regiment(31).threat_rating(), 4);
}
#[test]
fn test_regiment_class_is_infantry() {
assert!(RegimentClass::HumanInfantryman.is_infantry());
assert!(!RegimentClass::HumanCavalryman.is_infantry());
assert!(!RegimentClass::HumanArcher.is_infantry());
assert!(!RegimentClass::HumanArtilleryUnit.is_infantry());
assert!(!RegimentClass::HumanMage.is_infantry());
assert!(!RegimentClass::Monster.is_infantry());
assert!(!RegimentClass::Fanatic.is_infantry());
}
#[test]
fn test_regiment_class_is_cavalry() {
assert!(!RegimentClass::HumanInfantryman.is_cavalry());
assert!(RegimentClass::HumanCavalryman.is_cavalry());
assert!(!RegimentClass::HumanArcher.is_cavalry());
assert!(!RegimentClass::HumanArtilleryUnit.is_cavalry());
assert!(!RegimentClass::HumanMage.is_cavalry());
assert!(!RegimentClass::Monster.is_cavalry());
assert!(!RegimentClass::Fanatic.is_cavalry());
}
#[test]
fn test_regiment_class_is_archer() {
assert!(!RegimentClass::HumanInfantryman.is_archer());
assert!(!RegimentClass::HumanCavalryman.is_archer());
assert!(RegimentClass::HumanArcher.is_archer());
assert!(!RegimentClass::HumanArtilleryUnit.is_archer());
assert!(!RegimentClass::HumanMage.is_archer());
assert!(!RegimentClass::Monster.is_archer());
assert!(!RegimentClass::Fanatic.is_archer());
}
#[test]
fn test_regiment_class_is_artillery() {
assert!(!RegimentClass::HumanInfantryman.is_artillery());
assert!(!RegimentClass::HumanCavalryman.is_artillery());
assert!(!RegimentClass::HumanArcher.is_artillery());
assert!(RegimentClass::HumanArtilleryUnit.is_artillery());
assert!(!RegimentClass::HumanMage.is_artillery());
assert!(!RegimentClass::Monster.is_artillery());
assert!(!RegimentClass::Fanatic.is_artillery());
}
#[test]
fn test_regiment_class_is_mage() {
assert!(!RegimentClass::HumanInfantryman.is_mage());
assert!(!RegimentClass::HumanCavalryman.is_mage());
assert!(!RegimentClass::HumanArcher.is_mage());
assert!(!RegimentClass::HumanArtilleryUnit.is_mage());
assert!(RegimentClass::HumanMage.is_mage());
assert!(!RegimentClass::Monster.is_mage());
assert!(!RegimentClass::Fanatic.is_mage());
}
#[test]
fn test_regiment_class_is_human() {
assert!(RegimentClass::HumanInfantryman.is_human());
assert!(RegimentClass::HumanCavalryman.is_human());
assert!(!RegimentClass::WoodElfInfantryman.is_human());
}
#[test]