Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
205 commits
Select commit Hold shift + click to select a range
a8042fe
import: 93 TribesState reimplementations from development
gynt Oct 19, 2026
5387a86
style: resolve four field-walking pointers in TribesState
gynt Oct 19, 2026
e6a1179
types: five Tribe shorts are unsigned
gynt Oct 19, 2026
33a5e0d
reimplement: clearAllTribes 100%, getMajorityArcherTypeEuropeanOrArab…
gynt Oct 19, 2026
121a6d3
types: UnitSelectionDefinedData::BitMaskHelper is unsigned
gynt Oct 19, 2026
a0484b2
reimplement: six TribesState functions, two to 100%
gynt Oct 19, 2026
e8a8986
status: TribesState progress after the import and first two batches
gynt Oct 19, 2026
f15fd21
reimplement: wrong pointer strides and tribe loop shape, four to 100%
gynt Oct 20, 2026
8bc4767
reimplement: restore six loops the import had dropped the exit from
gynt Oct 20, 2026
53f8ad9
types: UnitsState::selectedUnitsBitFlags is a short array
gynt Oct 20, 2026
5f45ab5
reimplement: unitIsSelectedByPlayer to 100%, two more dropped returns
gynt Oct 20, 2026
960fc32
reimplement: four more TribesState functions
gynt Oct 20, 2026
7d877f4
reimplement: the magic-address loops, two to 100%
gynt Oct 20, 2026
e053a22
status: TribesState at 78.35% average, 49 of 113 at 100%
gynt Oct 20, 2026
212f556
reimplement: getUnitIDForIndexInTribe 58.0%, and decode playUnitSelec…
gynt Oct 20, 2026
716d42e
status: TribesState at 78.53% average, 49 of 113 at 100%
gynt Oct 20, 2026
af181e1
reimplement: updateTribeCombatStanceBehavior 57.6%
gynt Oct 20, 2026
36228ae
status: TribesState at 78.57% average, 49 of 113 at 100%
gynt Oct 20, 2026
56c1e20
reimplement: three animal and behaviour-target functions
gynt Oct 20, 2026
8cdb11b
note: giveTribeMoveInstruction's four measured declaration orders
gynt Oct 20, 2026
d5f4490
reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 45.1%
gynt Oct 20, 2026
b40aae2
reimplement: commandUnitsToLocation 42.3%, moveTribeToIndexedNearbyTi…
gynt Oct 20, 2026
b3f2c0e
reimplement: four more to 100% from the comparison forms
gynt Oct 20, 2026
4e95697
status: TribesState at 78.95% average, 53 of 113 at 100%
gynt Oct 20, 2026
b378d3c
reimplement: isTribeUnitBlockedByOtherUnit 80.7%
gynt Oct 20, 2026
74f2e12
reimplement: moveTribeToIndexedNearbyTile 45.9%, and two more heurist…
gynt Oct 20, 2026
6b2a213
reimplement: trySpawnAdditionalWildlifeForTribe 87.2%
gynt Oct 20, 2026
1c58149
style: createTribeForPlayer loses its hand-built tribeState pointer
gynt Oct 20, 2026
8193d39
reimplement: the initial-move-order block returns, in three animal up…
gynt Oct 20, 2026
4754dfc
status: TribesState at 79.30% average, 53 of 113 at 100%
gynt Oct 20, 2026
78742ba
reimplement: createAnimal 46.8%, createTribe's zero guard as an early…
gynt Oct 20, 2026
e492947
style: giveTribeMoveInstruction's x bound is `400 <=`, and its y guar…
gynt Oct 20, 2026
ec9277d
status: TribesState at 79.32% average, 53 of 113 at 100%
gynt Oct 20, 2026
e321adf
reimplement: giveTribeAnInstruction's three `id | x | id != 0` tests
gynt Oct 20, 2026
4fb7879
style: three redundant locals out of giveTribeAnInstruction
gynt Oct 20, 2026
0a80e06
style: two of giveTribeAnInstruction's cross-case gotos become fall-t…
gynt Oct 20, 2026
ec34747
style: two more cross-case goto webs in giveTribeAnInstruction
gynt Oct 20, 2026
a7b4932
style: the last convertible goto web in giveTribeAnInstruction
gynt Oct 20, 2026
aa32e5a
reimplement: spawnQueuedReinforcementWaves 59.6%
gynt Oct 20, 2026
82db04e
status: TribesState at 79.33% average, 53 of 113 at 100%
gynt Oct 20, 2026
fd431a0
reimplement: the local-widening heuristic, measured properly this time
gynt Oct 20, 2026
6671fcc
reimplement: trimTribeToSize 54.1%, spawnDeerLionOrRabbit 68.9%
gynt Oct 20, 2026
cdd796c
style: spawnUnitsForAITribe names its second coordinate
gynt Oct 20, 2026
ebfbbcf
reimplement: spawnUnitsIntoNewTribe 98.3%
gynt Oct 20, 2026
b55016c
style: commandUnitsToLocation's success flag as an early return
gynt Oct 20, 2026
19253e0
reimplement: scatterTribeUnitsRandomly 90.9%
gynt Oct 20, 2026
1002ca6
reimplement: spawnDeerLionOrRabbit 73.0%
gynt Oct 20, 2026
715b9fc
style: early returns in getAvailableTribeID and upgradeTribeArrayLayo…
gynt Oct 20, 2026
c36b010
note: assignAttackTargetsForTribe's goto web is the original's shape
gynt Oct 20, 2026
0362835
status: TribesState at 79.59% average, 53 of 113 at 100%
gynt Oct 20, 2026
c978510
reimplement: playUnitCombatSpeechForTarget's arms, read from the disa…
gynt Oct 20, 2026
90bb108
reimplement: playUnitSelectionSound 50.4% from the disassembly
gynt Oct 20, 2026
94459fb
reimplement: processDeerMoving 44.2% from the disassembly
gynt Oct 20, 2026
7670531
reimplement: the map-editor branch returns, in two more animal updaters
gynt Oct 20, 2026
f100c2c
reimplement: four dropped exits and a ten-element rally array, from t…
gynt Oct 20, 2026
f7afb0b
status: TribesState at 81.25% average, 54 of 113 at 100%
gynt Oct 20, 2026
63a362c
reimplement: createTribe, and a deer tribe that walks a rally array i…
gynt Oct 20, 2026
06232a2
reimplement: a stance the original keeps, and a tribe shuffle that re…
gynt Oct 20, 2026
a631596
reimplement: four more dropped returns in the camel and lion updaters
gynt Oct 20, 2026
a0c0a3f
reimplement: snapshotSelectionTribeAndComputeStance 55.8 -> 85.7%
gynt Oct 20, 2026
7da540b
reimplement: the archer arm's dropped exit and its lost second play
gynt Oct 20, 2026
9720ae0
measure: the Tribe& that made createTribe match ruins its twin
gynt Oct 20, 2026
8fa40c4
reimplement: processDeerMoving 47.4 -> 50.7%, from the reccmp diff
gynt Oct 20, 2026
266cb70
reimplement: drawFlagsAndUnitDestinations 41.8 -> 47.0%
gynt Oct 20, 2026
9090084
reimplement: one idiv in moveTribeToIndexedNearbyTile, not a reciproc…
gynt Oct 20, 2026
cee46c4
reimplement: two more at 100%, from one movsx where we had movzx plus…
gynt Oct 20, 2026
9881cdf
reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 46.2 -> 48.7%
gynt Oct 20, 2026
f19ab57
style: declare at first use in the two files that still needed it
gynt Oct 20, 2026
af86208
reimplement: giveTribeAnInstruction 27.9 -> 51.9%, by its real case o…
gynt Oct 20, 2026
7ae5890
measure: the nested switches' arm order does not pay, only the outer one
gynt Oct 20, 2026
2be1e00
measure: why the Tribe& does not pay in giveTribeAnInstruction either
gynt Oct 21, 2026
08f5c18
reimplement: giveTribeAnInstruction's frame is now exactly the origin…
gynt Oct 21, 2026
cb3a4b8
measure: nested switch arm order never pays, tested three times
gynt Oct 21, 2026
53d2046
measure: declaration order cannot match giveTribeAnInstruction's slot…
gynt Oct 21, 2026
5562578
reimplement: giveTribeAnInstruction 52.0 -> 56.8%, by not rebasing th…
gynt Oct 21, 2026
ffed577
measure: caching the tribe base does not pay, even with the displacem…
gynt Oct 21, 2026
a284692
fix: restore _tribeSize's initialisation, deleted by my own comment edit
gynt Oct 21, 2026
4e4af15
measure: caching the tribe base loses 4.8 points, retested on correct…
gynt Oct 21, 2026
97ed425
measure: the base-caching paradox is register selection, not addressing
gynt Oct 21, 2026
68aa69b
reimplement: three short functions, one of them to 100%
gynt Oct 21, 2026
b6980dd
reimplement: trimTribeToSize 54.1 -> 65.9%, by caching the base after…
gynt Oct 21, 2026
5f75dd1
measure: my discriminator for base-caching was wrong, tested and with…
gynt Oct 21, 2026
7a86c48
reimplement: createTribe 67.2 -> 79.0%, a Tribe& was shifting every d…
gynt Oct 21, 2026
d22c976
reimplement: updateRabbitTribeBehavior 58.9 -> 60.3%, and the base-ca…
gynt Oct 21, 2026
501f11f
fix: revert my own 0x80 cursor error in upgradeTribeArrayLayoutForMap…
gynt Oct 21, 2026
0c83c97
reimplement: playUnitSelectionSound 50.4 -> 62.1%, three case labels …
gynt Oct 21, 2026
a249055
reimplement: updateRabbitTribeBehavior 60.3 -> 63.0%, rally count is …
gynt Oct 21, 2026
8ac1364
fix: computeTribePercentages pinned percentageSomething at 100
gynt Oct 21, 2026
5667061
reimplement: trySpawnAdditionalWildlifeForTribe 87.2 -> 100%, flat gu…
gynt Oct 21, 2026
03d62be
reimplement: predictUnitInterceptPosition 31.1 -> 31.7%, flat guards
gynt Oct 21, 2026
1f44aac
measure: flattening guard chains is not a general win
gynt Oct 21, 2026
9fb7cfd
measure: moveTribeToIndexedNearbyTile's idiv is not reachable by nami…
gynt Oct 21, 2026
e9b754d
status: sync the eight TribesState percentages changed in this pass
gynt Oct 21, 2026
9a04d70
reimplement: importStoredInfoFromSlot0 94.5 -> 100%, processDeerMovin…
gynt Oct 21, 2026
b529b33
reimplement: getSpecificUnitFromTribe 53.5 -> 55.1%, the wrap arm is …
gynt Oct 21, 2026
62ccfa0
reimplement: drawFlagsAndUnitDestinations 47.0 -> 54.7%, computeTribe…
gynt Oct 21, 2026
2752fcd
reimplement: updateTribeUnitAssignments 57.0 -> 75.0%, updateLionWolf…
gynt Oct 21, 2026
4a49c5b
reimplement: giveTribeAnInstruction 56.8 -> 56.9%, and the first inve…
gynt Oct 21, 2026
3633fc9
measure: spawnQueuedReinforcementWaves' difficulty chain is not a switch
gynt Oct 21, 2026
a1bbf42
reimplement: updateAnimalHerdBehaviorState 80.4 -> 100%, snapshot... …
gynt Oct 21, 2026
33d8426
cleanup: playUnitCombatSpeechForTarget's out-param is an int count, n…
gynt Oct 21, 2026
299bba8
reimplement: giveTribeAnInstruction 56.9 -> 57.4%, second switch's ar…
gynt Oct 21, 2026
70b8f84
reimplement: giveTribeAnInstruction 57.4 -> 57.5%, fourth switch's ar…
gynt Oct 21, 2026
0a68863
reimplement: giveTribeAnInstruction 57.5 -> 57.8%, seventh switch's a…
gynt Oct 21, 2026
3d97b93
fix: UT_TUNNELER was on the wrong switch arm in giveTribeAnInstruction
gynt Oct 21, 2026
e99b31d
docs: record the giveTribeAnInstruction switch audit so it is not redone
gynt Oct 21, 2026
6ea3acc
tools: diff_regalloc.py, splitting the gap into register naming and r…
gynt Oct 22, 2026
701f597
measure: spawnQueuedReinforcementWaves addresses tribes through the g…
gynt Oct 22, 2026
921a3bc
reimplement: comparisons written with their operands swapped, +15.0 o…
gynt Oct 22, 2026
227168a
reimplement: the last four swapped comparisons, +8.0; none left in th…
gynt Oct 22, 2026
1851d6b
tools: diff_anchor.py missed cursors into global arrays
gynt Oct 22, 2026
66922a3
reimplement: drawFlagsAndUnitDestinations 54.7 -> 55.9%, the zero arm…
gynt Oct 22, 2026
0ebc914
reimplement: getMajoritySelectedUnitType 75.7 -> 93.1%, widen the typ…
gynt Oct 22, 2026
016ad73
reimplement: computeTribePercentages 46.0 -> 47.2%, and when widening…
gynt Oct 22, 2026
ef5a6bd
reimplement: upgradeTribeArrayLayoutForMapVersion 46.5 -> 49.4%, mems…
gynt Oct 22, 2026
2c151a6
reimplement: upgradeTribeArrayLayoutForMapVersion 49.4 -> 57.5% with …
gynt Oct 22, 2026
a995c4b
fix: an always-true retry test in giveTribeAnInstruction walked off a…
gynt Oct 22, 2026
1a49468
measure: matching instruction counts does not mean matching code
gynt Oct 22, 2026
681e494
reimplement: getMajorityArcherTypeEuropeanOrArabian 69.6 -> 88.2%, de…
gynt Oct 22, 2026
874639b
reimplement: updateTribeRallyFlags 74.7 -> 100%, getTribeAliveStatus …
gynt Oct 22, 2026
95d6c8d
docs: declare_at_use.py said declaration position does not affect cod…
gynt Oct 22, 2026
4039211
measure: re-confirmed the shifted base on updateRabbitTribeBehavior
gynt Oct 22, 2026
3be524d
reimplement: spawnUnitsForAITribe 78.0 -> 100%, the call belongs in b…
gynt Oct 22, 2026
072a306
reimplement: the constant-argument call belongs in each arm, +8.2 ove…
gynt Oct 22, 2026
2a3fe9a
reimplement: playUnitCombatSpeechForTarget 31.0 -> 40.6%, two more ar…
gynt Oct 22, 2026
8938eb1
reimplement: createTribeForPlayer 75.2 -> 86.2%, the loop is top-tested
gynt Oct 22, 2026
4bffc0a
measure: the loop-rotation fix needs the original to strength-reduce too
gynt Oct 22, 2026
91223b2
reimplement: spawnQueuedReinforcementWaves 59.6 -> 60.4%, the startGo…
gynt Oct 22, 2026
87f98d8
reimplement: predictUnitInterceptPosition 34.1 -> 40.1%, the bound is…
gynt Oct 22, 2026
60049bf
reimplement: updateCamelTribeBehavior 71.9 -> 73.1, the constant is 0…
gynt Oct 22, 2026
2540290
reimplement: drawFlagsAndUnitDestinations 55.9 -> 57.6, the constant …
gynt Oct 22, 2026
ed3bc6b
measure: sync the status file for the last three wins
gynt Oct 22, 2026
8afb543
measure: playUnitSelectionSound's two merge-candidate arms cost 5.5 w…
gynt Oct 22, 2026
b664e88
measure: three flat results - the ID local, the zero register, the co…
gynt Oct 22, 2026
237f625
reimplement: sortTribePathDestinationsByCost 29.5 -> 56.2, one array …
gynt Oct 22, 2026
58b8cbf
measure: sortTribePathDestinationsByCost's slot widths cost 1.3, not …
gynt Oct 22, 2026
391f162
measure: sync the status file for sortTribePathDestinationsByCost
gynt Oct 22, 2026
75fb59a
measure: hoisting coordinate reads above their arithmetic is flat in …
gynt Oct 22, 2026
8d07e45
reimplement: predictUnitInterceptPosition's path plan is units[].path…
gynt Oct 22, 2026
66498a6
reimplement: updateCamelTribeBehavior 73.1 -> 76.3, the behaviour tim…
gynt Oct 22, 2026
4fc4ad4
measure: two narrowing changes that cost more in block layout than th…
gynt Oct 22, 2026
192aa10
measure: sync the status file for updateCamelTribeBehavior
gynt Oct 22, 2026
3cd214b
measure: moveTribeToIndexedNearbyTile's loop bound has to stay a literal
gynt Oct 22, 2026
5d0d387
measure: assignAttackTargetsForTribe's materialised flag is cheaper t…
gynt Oct 22, 2026
f216948
reimplement: giveTribeAnInstruction 60.4 -> 60.6, per-arm locals and …
gynt Oct 22, 2026
0fe4a40
reimplement: Tribe::minimumMovementSpeed is a ushort, and four shapes…
gynt Oct 22, 2026
9f92dac
measure: giveTribeMoveInstruction's missing frame slots do not exist
gynt Oct 22, 2026
89ab8c3
measure: the switch arms were inlined helpers, and inlining them back…
gynt Oct 23, 2026
b90a47b
reimplement: playUnitCombatSpeechForTarget 40.6 -> 41.2, the selectio…
gynt Oct 23, 2026
d7a10a2
split: move the 44 fully matching functions to reimpl/TribesState-100
gynt Oct 24, 2026
d24bafb
Adopt the Wilps implementations that measure better here (#18)
gynt Nov 2, 2026
c48093a
Merge origin/main into reimpl/TribesState
gynt Dec 3, 2026
a266352
reimplement: addUnitToTribe 86.7 -> 100, taken from reimpl/TroopValue…
gynt Dec 3, 2026
3082c97
TribesState: follow main's headers after the merge
gynt Dec 3, 2026
a86b3aa
reimplement: createTribe 79.0 -> 100.0
gynt Dec 3, 2026
e7416a6
reimplement: createTribeForPlayer 86.2 -> 100.0
gynt Dec 3, 2026
83db82f
reimplement: getUnitIDForIndexInTribe 58.0 -> 100.0
gynt Dec 3, 2026
81e6070
reimplement: snapshotSelectionTribeAndComputeStance 96.8 -> 100.0
gynt Dec 3, 2026
1a5a397
reimplement: getAvailableTribeID 62.3 -> 85.0
gynt Dec 3, 2026
099b36a
reimplement: getSpecificUnitFromTribe 55.1 -> 64.1
gynt Dec 3, 2026
e50d928
reimplement: playAttackCommandFeedback 89.1 -> 98.0
gynt Dec 3, 2026
28339f8
reimplement: removeUnitFromThisTribeIfInTribe 79.5 -> 100.0
gynt Dec 3, 2026
3b9f015
reimplement: removeUnitFromTribe 67.1 -> 100.0
gynt Dec 3, 2026
babf464
reimplement: trimTribeToSize 70.6 -> 100.0
gynt Dec 3, 2026
ea2aed9
reimplement: upgradeTribeArrayLayoutForMapVersion 72.5 -> 77.5
gynt Dec 3, 2026
d3d1648
TribesState: moveTribeToIndexedNearbyTile returns void
gynt Dec 3, 2026
0ad67a2
reimplement: moveTribeToIndexedNearbyTile 45.9 -> 98.6
gynt Dec 3, 2026
e7033a6
reimplement: getAvailableTribeID 85.0 -> 100.0
gynt Dec 3, 2026
60f52dc
reimplement: spawnDeerLionOrRabbit 74.1 -> 100.0
gynt Dec 3, 2026
9454ad5
reimplement: updateCamelTribeBehavior 76.3 -> 100.0
gynt Dec 3, 2026
5c14ff6
reimplement: updateLionWolfTribeBehavior 72.7 -> 100.0
gynt Dec 3, 2026
615dc13
reimplement: updateRabbitTribeBehavior 63.0 -> 100.0
gynt Dec 3, 2026
fa4e088
reimplement: processDeerMoving 63.3 -> 100.0
gynt Dec 3, 2026
222de88
reimplement: commandUnitsToLocation 42.3 -> 71.4
gynt Dec 3, 2026
a52759b
reimplement: computeTribePercentages 47.2 -> 58.6
gynt Dec 3, 2026
f652c68
reimplement: moveUnitToBehaviorTarget 33.3 -> 37.1
gynt Dec 3, 2026
1e5fcff
TribesState: update stale header comments for computeTribePercentages…
gynt Dec 3, 2026
615e686
reimplement: playUnitMoveSpeech 82.8 -> 100.0
gynt Dec 4, 2026
bbfb316
reimplement: playUnitSelectionSound 65.4 -> 78.0
gynt Dec 4, 2026
ff919c3
reimplement: playUnitCombatSpeechForTarget 41.2 -> 78.2
gynt Dec 4, 2026
c9b10ab
reimplement: moveUnitToBehaviorTarget 37.1 -> 100.0
gynt Dec 4, 2026
906f225
reimplement: upgradeTribeArrayLayoutForMapVersion 77.5 -> 100.0
gynt Dec 4, 2026
0bdda4f
reimplement: isTribeUnitBlockedByOtherUnit 80.7 -> 91.2
gynt Dec 4, 2026
48a10cd
reimplement: applyUnitTopSpeedDelayBasedOnTribeSize 71.7 -> 72.6
gynt Dec 4, 2026
8e78e19
reimplement: assignAttackTargetsForTribe 44.6 -> 69.0
gynt Dec 4, 2026
e49a2e5
reimplement: commandUnitsToLocation 71.4 -> 90.3
gynt Dec 4, 2026
b359e00
assignAttackTargetsForTribe: record the measured load order
gynt Dec 4, 2026
f5150c4
reimplement: createAnimal 46.8 -> 96.1
gynt Dec 4, 2026
72c82e5
reimplement: playUnitSelectionSound 78.0 -> 92.4
gynt Dec 4, 2026
52c5480
reimplement: playUnitCombatSpeechForTarget 78.2 -> 94.4
gynt Dec 4, 2026
2dc099c
reimplement: drawFlagsAndUnitDestinations 57.6 -> 78.1
gynt Dec 4, 2026
e9f69ef
reimplement: updateTribeCombatStanceBehavior 75.1 -> 81.2
gynt Dec 4, 2026
413aafe
reimplement: giveTribeMoveInstruction 30.0 -> 49.7
gynt Dec 4, 2026
bbb9df8
Merge origin/main into reimpl/TribesState
gynt Dec 4, 2026
2f62f9d
Merge origin/main into reimpl/TribesState
gynt Dec 5, 2026
101b856
Tribe::countdown: ushort -> short
gynt Dec 5, 2026
88e71ca
isTribeUnitBlockedByOtherUnit: 91.2% -> 100%
gynt Dec 5, 2026
bf1a4d0
predictUnitInterceptPosition: 84.6% -> 91.4%
gynt Dec 5, 2026
3bbe7e0
updateTribes: structured switches, no gotos (stays 91.2%)
gynt Dec 5, 2026
5757082
TribesState: record the remaining asm differences of four draft funct…
gynt Dec 5, 2026
650fe13
drawFlagsAndUnitDestinations: 78.1% -> 82.5%
gynt Dec 5, 2026
89e18df
giveTribeMoveInstruction: 49.7% -> 52.6%
gynt Dec 5, 2026
836dbc8
assignAttackTargetsForTribe: no goto (stays 69.0%)
gynt Dec 5, 2026
af57a70
updateTribeCombatStanceBehavior: names instead of decompiler locals (…
gynt Dec 5, 2026
db90886
TribesState: record the remaining asm differences of two more draft f…
gynt Dec 5, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenSHC/Map/Units/Tribe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace Map {
short field_0x288; // 0x00000288 length: 2
short unknownBool02; // 0x0000028A length: 2
short unknownBool01; // 0x0000028C length: 2
ushort countdown; // 0x0000028E length: 2
short countdown; // 0x0000028E length: 2
short someUnitArrayIndex; // 0x00000290 length: 2
short someIndex; // 0x00000292 length: 2
short attackWave; // 0x00000294 length: 2
Expand Down
30 changes: 30 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "../TribesState.func.hpp"

#include "OpenSHC/Globals/DAT_UnitSelectionDefinedData.hpp"
#include "OpenSHC/Globals/DAT_UnitsState.hpp"

namespace OpenSHC {
namespace Map {
namespace Units {

// FUNCTION: STRONGHOLDCRUSADER 0x00522590
void TribesState::addUnitToTribe(uint unitID, int tribeID)
{
this->tribes[tribeID].size = this->tribes[tribeID].size + 1;
int const bitFlagsIndex = (int)unitID / 16;
int const bitMaskIndex = (int)unitID % 16;
this->tribes[tribeID].unitSelectionBitMasked[bitFlagsIndex]
|= DAT_UnitSelectionDefinedData::instance.BitMaskHelper[bitMaskIndex];
if (this->tribes[tribeID].selectionTargetUnitID == 0) {
this->tribes[tribeID].selectionTargetUnitID = unitID;
}
DAT_UnitsState::instance.units[unitID].selectionTargetUnitID = this->tribes[tribeID].selectionTargetUnitID;
DAT_UnitsState::instance.units[unitID].tribeID = tribeID;
DAT_UnitsState::instance.units[unitID].tribeUID = this->tribes[tribeID].uid;
DAT_UnitsState::instance.units[unitID].idInTribe = this->tribes[tribeID].size;
MACRO_CALL_MEMBER(TribesState_Func::setWhetherTribeContainsAnyUnits, this)(tribeID);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "../TribesState.func.hpp"

#include "OpenSHC/IO/LowLevelMemory.func.hpp"
#include "OpenSHC/Map/Units/UnitLogicState.hpp"

#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp"
#include "OpenSHC/Globals/DAT_UnitsState.hpp"

namespace OpenSHC {
namespace Map {
namespace Units {

// The pair slot advances for every member while the count only grows for living ones (0x524FCA), so
// dead members leave zero holes and trailing living members are not re-indexed. The swap test is
// signed (jge at 0x525009) and skipped entirely when skipDistanceSort is set.
// FUNCTION: STRONGHOLDCRUSADER 0x00524EF0
void TribesState::applyUnitTopSpeedDelayBasedOnTribeSize(int tribeID, BOOLEnum skipDistanceSort)
{
short* const tribeSize = &this->tribes[tribeID].size;
int i;
int j;
int swapped;
if (*tribeSize >= 40) {
for (i = 0; i < *tribeSize; ++i) {
int const unitID = MACRO_CALL_MEMBER(
OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i);
DAT_UnitsState::instance.units[unitID].topSpeedDelayIndex = (short)(i < 199 ? i : 199);
}
return;
}
this->someUnitIDArrayCount = 0;
MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(
sizeof(this->someUnitIDMovementDistancePair), 0, this->someUnitIDMovementDistancePair);
for (i = 0; i < *tribeSize; ++i) {
int const unitID = MACRO_CALL_MEMBER(
OpenSHC::Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, i);
if (DAT_UnitsState::instance.units[unitID].logicalState == ULS_NORMAL
&& DAT_UnitsState::instance.units[unitID].dying == 0) {
++this->someUnitIDArrayCount;
this->someUnitIDMovementDistancePair[i].unitID = (short)unitID;
if (skipDistanceSort == FALSE) {
this->someUnitIDMovementDistancePair[i].movementDistance
= DAT_UnitsState::instance.units[unitID].movementDistance;
}
}
}
do {
swapped = 0;
for (j = 0; j < this->someUnitIDArrayCount - 1; ++j) {
int const unitID = this->someUnitIDMovementDistancePair[j].unitID;
int const nextUnitID = this->someUnitIDMovementDistancePair[j + 1].unitID;
int const distance = this->someUnitIDMovementDistancePair[j].movementDistance;
int const nextDistance = this->someUnitIDMovementDistancePair[j + 1].movementDistance;
if (skipDistanceSort == FALSE && nextDistance < distance) {
swapped = 1;
this->someUnitIDMovementDistancePair[j].unitID = (short)nextUnitID;
this->someUnitIDMovementDistancePair[j + 1].unitID = (short)unitID;
this->someUnitIDMovementDistancePair[j].movementDistance = (short)nextDistance;
this->someUnitIDMovementDistancePair[j + 1].movementDistance = (short)distance;
}
}
} while (swapped != 0);
for (i = 0; i < this->someUnitIDArrayCount; ++i) {
DAT_UnitsState::instance.units[this->someUnitIDMovementDistancePair[i].unitID].topSpeedDelayIndex
= (short)i;
}
}

}
}
}
235 changes: 235 additions & 0 deletions src/OpenSHC/Map/Units/TribesState/assignAttackTargetsForTribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
#include "../../../Map.func.hpp"

#include "OpenSHC/Map/Units/TribesState.func.hpp"
#include "OpenSHC/Map/Units/TroopValueState.func.hpp"
#include "OpenSHC/Map/Units/UnitsState.func.hpp"
#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp"
#include "OpenSHC/Map/Units/States/UnitState.hpp"
#include "OpenSHC/Map/Units/UnitInstructionType.hpp"
#include "OpenSHC/Map/Units/UnitLogicState.hpp"
#include "OpenSHC/Map/Units/UnitType.hpp"
#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp"

#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp"
#include "OpenSHC/Globals/DAT_PathFindingState.hpp"
#include "OpenSHC/Globals/DAT_TroopValueState.hpp"
#include "OpenSHC/Globals/DAT_UnitsState.hpp"
#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp"

namespace OpenSHC {
namespace Map {
namespace Units {

using Map::Units::SomeTribeBehaviorType;
using Map::Units::UnitInstructionType;
using Map::Units::UnitLogicState;
using Map::Units::UnitType;
using Map::Units::States::UnitState;
using WindowsHelper::Enums::BOOLEnum;

/*
Every target arm loads tile, y and x itself. The compiler merges the identical
arm tails into one copy, which is the shape the original shows. The repeated
loads are what give x, y and tile their registers (ebx, ebp, esi) ahead of
unitID, which the original keeps on the stack. With one shared `found` flag
and the loads written once, unitID took ebx and the function measured 44.6%.
Two differences remain. The merged copy lands after the last arm (0x3f7),
where the original keeps the one after the 0x3f2 call. x and y have
swapped registers. No declaration order of x, y and tile changes that.
Loading tile, x, y instead fixes the swap (71.3%) but costs normalized
match (66.2% -> 63.9%), so the order stays. Putting the shared code
inside the 0x3f2 arm, with the other arms jumping to it, still leaves
the last copy.
*/
// FUNCTION: STRONGHOLDCRUSADER 0x005244D0
BOOLEnum TribesState::assignAttackTargetsForTribe(int tribeID, SomeTribeBehaviorType targetType)
{
int index = 0;
BOOLEnum const allAssassins
= MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeAllAssassins, this)(tribeID);
if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[this->tribes[tribeID].owner] != -1) {
return FALSE;
}
if (this->tribes[tribeID].size <= 0) {
return TRUE;
}
do {
uint x;
uint y;
uint tile;
int const unitID
= MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, this)(tribeID, index);
index = index + 1;
if (DAT_UnitsState::instance.units[unitID].logicalState != Map::Units::ULS_NORMAL
|| DAT_UnitsState::instance.units[unitID].dying != 0
|| DAT_UnitsState::instance.units[unitID].usingTeleport != 0
|| DAT_UnitsState::instance.units[unitID].field_0x413 != 0
|| DAT_UnitsState::instance.units[unitID].moveableUnk == 0) {
continue;
}
if (targetType != Map::Units::STBT_0x3fd) {
UnitStateShort const state = DAT_UnitsState::instance.units[unitID].state.generic;
if (state == Map::Units::States::US_MELEE_ATTACK_WALL
|| state == Map::Units::States::US_MOVE_TO_DESTINATION || state == (UnitState)0x67) {
continue;
}
UnitTypeShort const unitType = DAT_UnitsState::instance.units[unitID].unitType;
if (unitType == Map::Units::UT_E_SPEAR) {
if (state == Map::Units::States::US_AIM_WEAPONUnk) {
continue;
}
} else if (unitType == Map::Units::UT_E_ENGINEER) {
if (state == Map::Units::States::US_RELOAD_WEAPONUnk) {
continue;
}
} else if (unitType == Map::Units::UT_E_LADDER) {
if (state == (UnitState)3) {
continue;
}
} else if (unitType == Map::Units::UT_TUNNELER) {
if (state == (UnitState)2 || state == (UnitState)3
|| state == Map::Units::States::US_RELOAD_WEAPONUnk
|| state == Map::Units::States::US_STAND_UPUnk
|| state == (Map::Units::States::US_STAND_UPUnk | Map::Units::States::US_IDLEUnk)) {
continue;
}
}
}
if (targetType == Map::Units::STBT_0x3f2) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyWalls, DAT_TroopValueState::ptr)(
unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3f4) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestAvailableScalePoint,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
x = DAT_TroopValueState::instance.x;
y = DAT_TroopValueState::instance.y;
tile = DAT_TroopValueState::instance.tile;
DAT_UnitsState::instance.units[unitID].state.generic = (UnitState)0x67;
} else if (targetType == Map::Units::STBT_0x3f3) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findNearestAvailableScalePoint,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3f5) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyBuildingsClosestToUnit,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3fb) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2PeoplValueClosestToUnit,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3fd) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyLord, DAT_TroopValueState::ptr)(
unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3f6) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::findEnemyTowersOrGates,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x413) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x414) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getClosestWideValueBasedOnPlayer,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else if (targetType == Map::Units::STBT_0x3f7) {
if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateTile2MoatValueClosestToUnit,
DAT_TroopValueState::ptr)(unitID)) {
continue;
}
tile = DAT_TroopValueState::instance.tile;
y = DAT_TroopValueState::instance.y;
x = DAT_TroopValueState::instance.x;
} else {
continue;
}
if (targetType != Map::Units::STBT_0x3f4) {
DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION;
if (targetType == Map::Units::STBT_0x414) {
uint const freeTile = MACRO_CALL_MEMBER(
Map::Units::UnitsState_Func::findFreeTileNearby, DAT_UnitsState::ptr)(unitID, tile);
if (freeTile == 0) {
continue;
}
MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(
unitID,
freeTile
- DAT_ViewportRenderState::instance
.translationMatrix[DAT_ViewportRenderState::instance
.tileTranslationMatrix_YComponent[freeTile]]
.addXgetTile,
DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[freeTile], 0);
short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile];
DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY;
DAT_UnitsState::instance.units[unitID].targetID_OR_targetBuildingID = (short)tile;
DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_ATTACK_WALL;
DAT_UnitsState::instance.units[unitID].state.generic
= Map::Units::States::US_MOVE_TO_DESTINATION;
DAT_UnitsState::instance.units[unitID].attackAtTileX = (short)tile
- (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile;
continue;
}
}
short const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile];
DAT_UnitsState::instance.units[unitID].attackAtTileY = tileY;
DAT_UnitsState::instance.units[unitID].targetedBuildingTile = tile;
DAT_UnitsState::instance.units[unitID].attackAtTileX
= (short)tile - (short)DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile;
DAT_UnitsState::instance.units[unitID].unknownDigMoatOrWallAttackFlag1015 = (undefined2)targetType;
if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::stopUnitIfNextToTarget, DAT_UnitsState::ptr)(
unitID)) {
if (allAssassins != FALSE) {
DAT_PathFindingState::instance.allAssassinsUnk = 1;
}
if (!MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)(
unitID, x, y, 0)) {
DAT_UnitsState::instance.units[unitID].logicalState = Map::Units::ULS_REMOVE;
}
}
if (DAT_UnitsState::instance.units[unitID].selectionTargetUnitID == unitID) {
this->tribes[tribeID].targetX = (short)x;
this->tribes[tribeID].targetY = (short)y;
}
} while (index < this->tribes[tribeID].size);
return TRUE;
}

}
}
}
Loading
Loading