diff --git a/AGENTS.md b/AGENTS.md index a7faa81e71..57e5ed5772 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -306,6 +306,52 @@ Diff patterns that were reliable (more in the cheat sheet): independent stores into one object says nothing about the order the fields were assigned in the original source (see the cheat sheet's *Statement Order*). Reordering the body to follow the asm makes the match worse. - Diffs can reveal real bugs in existing reimplementations (wrong constants, wrong strides); fix those. +- An `if (cond) {}` with an empty body is a `goto` the import dropped, and it is almost always a *loop or + function exit*. Thirteen sat in `Map::Units::TroopValueState`, six of them leaving a `do { } while (true);` + that never terminates. Read the original's jump target with `orig_asm.py NAME` - a jump past the loop's + back edge is `break`, a jump into the epilogue is `return` - and fix the exit *before* restyling anything + else: `placeTunnelForEveryTunneler` went 32% -> 52% from three `return`s alone, while the same function + rewritten structurally first went *down*. Where the correct exit costs match because the original shares + one epilogue and we need two, keep the fix and say so in the commit remark. +- A literal game address (`while ((int)p < 0x17802b4)`, `*(byte*)(tile + 0x1ee2998)`) is the original's global, + which `resolve_address.py` turns into `DAT_X::instance.field[i]`. It is only correct while that struct's + reimplementation is off, because an enabled resolver puts the data in our own object, so these are latent + bugs and worth replacing. Replacing an *array base* is free or better; replacing a *loop bound* is not - + a typed pointer bound (`p < &tribes[1251]`) re-anchors the walk and cost 5-11 points in three functions, + where converting the whole loop to an index over `tribes[t]` gained instead. +- **Where a field read sits relative to a call or a test is worth more than almost anything else.** + The decompiler hoists reads to the top of a block; the original reads them where they are used, and + `quick_styles.py` settles it in seconds per variant. Moving one read down past a call took + `recountTotalTroopValue` 27.7% -> 49.5% and `collectArcherUnitsByLocation` 49.4% -> 61.5%; moving one + down past two threshold tests took `queueOilThrowForIdleArchers` 70.7% -> 100%. The same lever runs the + other way for an argument subexpression inside a short-circuit chain, which the original computed + *before* the test: hoisting the `PathConnectionLayer` lookup out of the `&&` took + `assignBehaviorTypeToNearbyTribes` 46.6% -> 68.0%. Read which side of the branch the original's address + arithmetic falls on and follow it. +- `if (x < N) { if (x == N) { ... } ... }` is a three-way test the decompiler flattened wrongly: the inner + arm is unreachable, so whatever it does never happens. The original tests `> N`, then `== N`, then falls + through to the rest. Rebuild it as `if (x < N) { rest } else if (x == N) { arm } else { high }` - the + fall-through arm has to stay first or MSVC lays the blocks out the other way and the match collapses + (20 points on `updateTribeBehaviorBasedOnBehaviorType`, which is also where the unreachable arm was). +- A `je` in the original against a `jne` of ours after the same compare means the two arms of an + `if/else` are the other way round: the arm the original falls into is the one it keeps inline, and the + one it jumps to is the one we should put in the `else`. `scan_arms.py` lists them. Swapping took + `getPlayerNot1AndHasKeep` 50% -> 100%, `addUnitToNewTribe` 84% -> 100% and + `aiControlNonSkirmishUnitMovement` 78% -> 98.6% (two swaps plus a tail call), and it is the same lever as + inverting an `if/else if` chain so its first test matches the original's - `computeAttackWaveTroopComposition` + gained 11 points from testing `< 10` and taking the lower arm there instead of `9 <` and taking the upper. + Read the original rather than trusting the hint: the scan pairs branches loosely and one of its five + suggestions measured worse. +- `pop`s followed by `jmp ` in the original is a tail call, so nothing runs after it. Where the + decompiler shows that callee in the middle of a function, the original returned there: + `aiControlNonSkirmishUnitMovement` called `updateAttackInfoTick` and then carried on, where the original + stops, and fixing it was worth 3.6 points before the arm swaps. +- `expr * 0x177bc` into one of `AttackInfo`'s own `*ValuesArray` members is `playerInfo[player - 1]`: + the per-player `AttackInfoSubElement` array is one-based, and `AttackInfo`'s named copies of those fields + are the unused slot 0 at the same relative offsets (delta 0x301C0). Writing + `playerInfo[playerID - 1].wide2` for `*(int*)((int)wideValuesArray + playerID * 0x177bc + -8)` took seven + of that namespace's "find the nearest X" functions from 45-78% to exactly 100%, and the negative deltas + (-0x10, -0xc, -8, -4) are the four scalars declared before each array. - `(-(uint)(c) & MASK) + BASE` in the decompiler output is a conditional it has already turned into mask-and-add: it is `c ? BASE + MASK : BASE`. Writing the conditional out recovers the same instructions and stops the constants being unreadable - `(-(uint)(d != 1) & 0xffffffce) + 200` is `d == 1 ? 200 : 150`. @@ -410,3 +456,747 @@ When a relevant skill exists, prefer it over this document, as it contains more ## Agent Maintenance If you discover undocumented conventions, missing workflows, or repeated guidance, suggest updates to this guide or the relevant skill. + +- **`reccmp_report.py --run` does not build.** It only re-runs reccmp over the DLL that is already + on disk, so a source edit measured with `--run` alone scores the *previous* build. Run + `build_quiet.py` (check for `BUILD_OK`) first, every time. A suspiciously bit-identical diff - + same percentages and the same `(file.cpp:NN)` line numbers as before the edit - is the symptom. + +- **Switch-arm source order decides the arm layout, including for a jump-table switch.** + `playAttackAlarmSound` went 75% -> 100% purely by moving `case 5:` ahead of `case 2/3/4:`, which + is the order reccmp's printed jump table implies (the arm reached by the lowest `start + N` + offset comes first in the source). Sort the distinct jump-table offsets, map each back to its + arm, and lay the arms out in that order. + +- **Four identical stores beat one shared store.** Where Ghidra nests a chain of tests as + `if (a) { x = K; } else { if (b) { x = K; } else { ... } }`, the original often writes each + failure as its own guard - `if (a) { x = K; goto LAB_tail; }` - repeated per test, with the + success path falling into a label just before the loop's increments. reccmp shows this as the + original carrying several copies of the same `mov dword ptr [reg], K` plus a `jmp`, against a + single copy on our side. `computeSiegeSpotScores` gained 5 points from it (47.4% -> 52.2%); + rewriting the same block's `!x` tests as signed `(int)x < 1` at the same time *cost* 3, so land + the restructure on its own and measure before touching the comparisons. + +- **`b = a + 1; ; a = b;` is one variable, not two.** When a counter is live across a call, + Ghidra sometimes names the pre-call and post-call halves separately, leaving a copy-back that + reads as a no-op (`a = b;` on a path where `b` never changed). reccmp shows the original doing + `add reg, 1` / `mov [esp+N], reg` once and reloading after the call, against our two counters + shuffling between a register and two slots. Merging them into one variable took + `assignMacemenAndKnightsNotFromPlayer1ToTribes` from 41.8% to 52.5%. Check that the two really + are equal at the top of each iteration before merging - initialised together, and every + assignment to one followed by the copy-back. + +- **A store Ghidra invented from a shared constant.** `computeAttackWaveTroopComposition` opened + with `maxMoatAttackTribes = 6;` although every arm of the chain below assigns that field; the + original only materialises 6 for the first `cmp`. If reccmp shows a `mov dword ptr [global], reg` + on our side alone right before a compare against the same constant, and the field is written on + every path, the assignment is not in the original. + +- **`test r8, r8` + `jbe` is `0 < (byte)x`, not `x != '\0'`.** `jbe` after a compare against zero + means the source tested an *unsigned* value with `<` or `<=`; a `!= 0` test gives `je`/`jne` + instead. Writing the guard as `if (0 < (byte)cVar1)` is what reproduces it. Five of those plus a + restored `else` took `decrementTileMap1104` from 79.4% to an exact match. + +- **An unconditional store after a conditional one means a dropped `else`.** Ghidra emitted + `if (off < 80200) { off += 200; } off = 0;` - which makes the increment dead, and our compiler + duly dropped it, so reccmp showed the original carrying a `cmp`/`jge` we had nothing for. The + second store belongs in an `else`. Any tail where the decompiler assigns a field it has just + conditionally updated is worth this check. + +- **Loop shape is not reachable from the source.** `pruneStaleArcherPoints` and its two twins walk + a 199-element array; the original exits on `cmp esi, ` / `jl`, we emit a down-counter + (`mov edi, 0xc7` / `sub edi, 1` / `jne`), and that single transform is the whole 29% they are + missing. A `do/while` on the pointer, a `for`, a `while`, `!=` instead of `<`, an index with the + pointer derived inside, and a hoisted end-pointer local all compile to the *same* down-counter - + measured, not assumed, on three functions in one build. When several spellings of a loop give a + bit-identical diff, the remaining difference is the optimiser's and there is nothing left to try. + +- **Near-miss constants are comparison encodings, and they are worth sweeping for.** Ghidra prints + `x < K+1` where the original compiled `x <= K`, and `K-1 < x` where it compiled `K <= x`; the + reccmp diff shows it as the original's `cmp reg, K` against our `cmp reg, K+-1` on the next line. + Scan every sub-100 diff for adjacent `-cmp`/`+cmp` pairs whose constants differ by a small + amount, then rewrite each condition in the original's form. Seven of them in one build lifted six + functions and cost nothing anywhere. The same scan with the delta restricted to a struct size + (820 for Tribe, 1168 for Unit) finds loop bounds that are off by one whole element. + +- **Comparison operand *order* is visible too, and sweepable the same way.** MSVC emits + `cmp , ` in source order, so `bound <= index` and `index >= bound` - identical in + meaning - produce opposite operand orders. Scan the diffs for an adjacent `-cmp A, B` / `+cmp B, A` + pair and rewrite the condition with the original's left-hand side first: the index before its + bound, the tick before its delay, the cached field before the layer read. Worth 2 points on + `placeTunnelForEveryTunneler`, 2 on `scanForSiegeTentPoints`, 1.4 on `initializeOrAdvanceAttackWave`. + It is not universal - 25 flips in `updateTribeBehaviorBasedOnBehaviorType` changed nothing at all, + because the compiler had already canonicalised those - so measure per function. + +- **An inverted `jcc` means the two arms are the wrong way round - scan for it first.** For every + sub-100 function, pair each `-jcc` in the diff with a `+jcc` within three lines and flag the ones + whose conditions are exact inverses (`je`/`jne`, `jl`/`jge`, `jg`/`jle`, `ja`/`jbe`). Each hit is + an `if`/`else` whose arms the original lays out in the other order, or a guard written with the + opposite sense; rewriting it in the original's sense is usually a few lines and has been the + single most productive sweep here: + + isLessThanPercentageOfTribesInAttackDying 89.6 -> 100.0 (zero case first, with an else) + collectArcherUnitsByLocation 61.5 -> 75.9 (mask test positive, arms swapped) + updateInProgressAttackWave 78.4 -> 84.1 (tent-points arms swapped) + computeAttackWaveTroopComposition 55.3 -> 60.7 (four wall-scale chains swapped) + findNearestDiggableMoatPoint 89.2 -> 94.6 ('(int)tile <= -1', giving jg) + + Beware that the sense also encodes signedness and the exact constant: `!x` gives `jne`, `(int)x + <= 0` gives `jg`, and `(int)x < 1` gives `jge` - three spellings of the same test with three + different branches. Pick the one the original used. + +- **A `(short)` cast written twice in one condition costs a conversion.** `registerSpottedEnemyTile` + cast `EntityLayer[tile]` from short once for the zero test and again as the array index; the + compiler emitted `movzx` + `test ax, ax` + a second `movsx`, where the original does a single + `movsx` into a register it then reuses. Reading it once into an `int` local took that function + from 81.8% to an exact match. Scan each sub-100 source for the same `(short)expr` appearing twice + - the declared-`short`-local form of this is the same bug with a different spelling. It does not + always pay (hoisting `(short)_unitID` in `recomputeAttackInfo` cost 2 points), so measure. + +- **Per-block work on a long function: map its counters, not its instructions.** For a function too + long for reccmp to align (`recomputeAttackInfo`, 981 instructions, ten near-identical per-feature + blocks), the way in is to find a value each block touches exactly once - here the `gate1`, + `town1`, `high1`, `arch1`, ... counters - resolve each to its `instance+N` offset from the + prologue's zero stores, then grep `orig_asm.py NAME --both` for those offsets *in address order*. + That prints the original's block layout directly: + + people lord gate town high2 arch2 wide hack scale moat + + Ours had the owned-building arm as high2/arch2, town, gate - exactly reversed. `swap_arms.py` + reorders an `if`/`else` or `if`/`else if` chain in place (it negates the condition, so behaviour + is unchanged); three calls on the nested chains were worth 2 points. + + **The layout order is not the whole source order, though.** Matching it completely measured + *worse*: moving `moat` last cost 1.3 and swapping the two outer arms another 2.1, because MSVC + outlines some of these blocks rather than emitting them where the source puts them. Reorder one + chain at a time and keep only the chains that measure better. + +- **When the two streams are the same length, stop looking for missing code.** `recomputeAttackInfo` + is 941 instructions against the original's 981, with the same 41 calls in the same order and the + same addressing idioms - so its 30% is an *alignment* score, not a structural gap: the prologue + allocates the player offset to a different register and needs one stack slot more, the streams + desynchronise there, and everything after counts as a mismatch. Things that look like the cause + and are not (all measured, all reverted): un-caching the second neighbour-gather so the layer + pointers reload, inlining three single-use height locals, moving the hoisted `ptr_SpecialAreasArray` + / `ptr_PathConnectionLayer` loads down to their first use, and hoisting a repeated `(short)_unitID`. + Count both streams before spending a day here. + +- **Retry a failed change once the function has moved.** Replacing `this->attackInfo.` with + `DAT_TroopValueState::instance.attackInfo.` in `initializeOrAdvanceAttackWave` measured *worse* + early on (54.8% -> 38.2%) and took it from 58.8% to 88.1% later, after the pointer temp and the + loop bound were fixed. Through `this` every field is a this-relative literal offset that can never + match the original's absolute addressing, so the change was always right; it just could not show + while a larger misalignment sat in front of it. When a change you believe in measures worse, note + it and try it again after the next structural win - and when it still loses, trust the number + (the same substitution in `scanForSiegeTentPoints` has now lost twice, five points each time). + +- **Symbolic loop bounds are not automatically better than Ghidra's literal.** Rewriting + `(int)p < 0x176238c` as `(int)p < (int)&tribes[1250].owner` reproduces the original's symbolic + operand but costs a `lea` to re-anchor the walking pointer, and measured worse on + `placeSiegeTentsAndAssignEngineers`, `recountAttackTroopValue` and + `sortAttackInfoTribeIDArrayBasedOn` (that last one by 14 points). Check the trip count either + way - `&tribes[1250]` and the literal are usually count-equivalent, so the literal is not a bug - + and keep whichever measures better. + +- **Verify a jump table's index -> case mapping against the *data* table.** For a sparse switch MSVC + emits a byte table (value - min -> jump index) alongside the jump table, and reccmp prints both. + The indices run strictly ascending over the case values that are present, and `default` gets the + *last* index - not a slot in the middle. I assumed the default sat at index 12 in + `updateTribeBehaviorBasedOnBehaviorType`, which shifted every case from 0x400 on by one and made + the derived arm order wrong from position 6 onwards; it still gained 9 points, which is how it + went unnoticed. Correcting it gained another 7 and made our layout match the original's exactly. + `arm_layout.py` prints both layouts, so check them rather than trusting the derivation. + +- **`arm_sizes.py` turns a big switch into a short worklist.** Differencing consecutive jump-table + offsets gives each arm's byte size on both sides, so an 800-instruction function at 55% becomes + "these four arms are the wrong length". It is also how you catch a reorder that dropped a + terminator: Ghidra's last arm carries no `break`, so moving it anywhere else makes it fall + through - `arm_sizes.py` showed that arm 13 bytes *short*, and the listing showed the original + ending it with `ret`. + +- **A jcc pair differing only in the equality is an off-by-one in the condition.** `sweep_all.py` + flags `-jg`/`+jge`, `-jl`/`+jle` and the unsigned equivalents over the same compare. Sometimes it + is only an encoding (`cmp x, 0xa` / `jl` against `cmp x, 9` / `jle` mean the same thing - write + the original's form), and sometimes the operands really do differ, so read the compare before + acting. Check that the `cmp` itself is identical on both sides: if the original compares a + register where we compare an immediate, the pairing is noise. + +- **Instruction order inside a basic block is the scheduler's, not the source's.** The original + reads `owner` before its `getValueOfTroopType` call in `recountTotalTroopValue`; moving our read + to match cost *24 points*, because it forces a spill across the call where MSVC otherwise keeps + the unit pointer and re-reads. Same for hoisting the layer pointers in `recomputeAttackInfo`. + Reorder *blocks*, not instructions. + +- **/GL does not rescue an allocator-bound function.** Adding the `pruneStale*` trio and the three + find-slot scans to `cmake/compiler-flags-gl.txt` changed their codegen not at all - all six + measured identical to the byte. Whatever decides the down-counter and the `mov [abs], esi` vs + `eax` choice is not reachable through that flag either, so do not reach for per-file flags as a + way out of register allocation. + +- **A `(char)BVar1` that MSVC can fold is not a way to reuse a known-zero register.** The original's + second `fillMemory_ByteValue` in `pruneStaleArcherPoints` passes the zero already in `eax` + (`push eax`, one byte) where we emit `push 0` (two). Passing `(char)BVar1` instead of `'\0'` - + provably the same value inside that branch - changes nothing, because MSVC const-folds it back. + +### Ghidra's decompilation is the *starting point*, not the target + +`giveLightPitchInstructionToUnitClosestToPitch` (0x51BF70) carries a +`decompilerscript: committed` banner - its Ghidra listing is the same text the +.cpp was seeded from. Three of its differences from our current file were tested +and all three lose: + +| Ghidra's form | ours | delta | +|---|---|---| +| success block nested inside the `maxUnitCount` guard | early `if (!_minUnitID) return 0;` | -6.6 | +| `getPlayerNot1AndHasKeep(&DAT_TroopValueState, ...)` | `this` | -4.6 | +| both together | | -5.0 | + +On a function already improved past its seed, Ghidra re-confirms the seed, not +the original. Check for the `decompilerscript: committed` banner first: when it +is there and our % is already well above where the seed would land, Ghidra has +no new information and the time is better spent on a function whose listing is +fresh. + +### A local that only mirrors a global is really the *enregistered global* + +The three slot scans (`findOrReserveArcherPointSlot`, `getSupportPointIndex`, +`getSiegeIndexForTile`) each had a `_firstFree` local written beside the +`...Next` global and never read for anything but its own zero-test. All three +sat at 76.6% on a single-instruction diff: + + - mov dword ptr [...Next], esi <- original + + mov dword ptr [...Next], eax <- ours + +`mov [abs32], eax` is the 5-byte `A3` form, `esi` needs 6 - and that one byte +shifted every jump displacement in the function, which is where the 23 points +went. MSVC had coalesced our `_firstFree = i; Next = _firstFree;` onto `eax`. + +Deleting the local and testing/assigning the global directly took all three to +**100%** at once: with no call or aliasing store in the loop MSVC enregisters +the global in `esi` and writes through, which is exactly the original's shape. + +Generalisation: when a local exists only to shadow a global, the original +probably had no local at all. Grep for a local assigned next to a global of the +same meaning and whose only other use is a predicate on itself. + +### Cast the loop bound to `int` to stop MSVC strength-reducing a pointer walk + +The `pruneStale*` trio all walked `point < &array[200]`, which MSVC turned into +a down-counter (`mov edi, 0xc7` / `sub edi,1` / `jne`) and an extra live +register; the original keeps the pointer compare (`cmp esi, ` / `jl`). +Writing the bound as + + } while ((int)point < (int)&...array[200]); + +defeats the strength reduction and took all three from 70.7% to **77.1%**. + +This is the first thing to try on any counted pointer loop whose diff shows a +`sub ,1 / jne` where the original has `cmp , `. + +Not reachable (both tried and lost ~19 points each on this trio): +- walking an `int*` at the `tribeID` field offset, as Ghidra prints it, to kill + the per-iteration `lea ecx,[esi-0x10]`. The lea is cheaper than whatever MSVC + does to the field accesses afterwards. +- Ghidra's arm order here is the *inverted* one. The original lays the + `!= someCounter1` (fill) arm first, which is what our file already had; + taking Ghidra's `==`-first order costs the arm layout. + +### Initialise a global *from* the local, not from a bare literal + +`applyTribeBehaviorTypes` opened with + + int iVar2 = 0; + int _index = 0; + DAT_...unknownTribeCounterRelated = 0; + +MSVC saw three independent zeros, coalesced two of the locals into one register +and stored the literal: `xor edx,edx` then `mov [global], edx`. The original +keeps two zeroed registers and stores `esi`. Rewriting the middle store as + + int iVar2 = 0; + DAT_...unknownTribeCounterRelated = iVar2; + int _index = 0; + +took it 95.7% -> **100%**: reading `iVar2` forces MSVC to give it its own +register and store through it, and it fixes the `xor`/store order at the same +time. + +Same family as [the spare local being the enregistered global]: whenever a +global and a local hold the same value, the *data flow between them* is what +pins the register allocation. Spell the dependency out - `global = local;` - +rather than writing the literal twice. + +### Read the local, not the global, once you have cached it + +`placeTunnelEntrances` did + + iVar2 = DAT_TileMapState::instance.placedBuildingID; + if (0 < (int)DAT_TileMapState::instance.placedBuildingID) { + ...buildings[DAT_TileMapState::instance.placedBuildingID].attackWave = ...; + +The original tests the cached value and indexes with a *copy* of it, giving +`mov eax,[global] / test eax,eax / mov ecx,eax / imul ecx,0x32c`; our re-reads +made MSVC pick the opposite registers, which also flipped two `push` operands. +Replacing both re-reads with `iVar2` took it 88.6% -> **100%**. + +This and the two entries above are one rule in three costumes: **the original's +register allocation is a shadow of the source's data flow, so write the data +flow the registers imply.** + +- global written beside a local that mirrors it -> drop the local, use the global + (the three slot scans, 76.6% -> 100%) +- global initialised to a literal a local also holds -> `global = local;` + (`applyTribeBehaviorTypes`, 95.7% -> 100%) +- global cached in a local and then re-read -> read the local + (`placeTunnelEntrances`, 88.6% -> 100%) + +Each is worth a mechanical grep across the namespace. Three of the four known +100% jumps this session came from this family. + +Scheduler-bound, do not retry: `initializeAttackZoneSearch` (77.8%) and +`findEnemyLord` (78.0%) differ only in load placement inside one basic block and +in which register holds the count vs the walk pointer. + +#### ...but the direction is per site, so measure + +Replacing the re-read with the cached local was swept over six functions in one +build (scores are per-function, so one build evaluates them all): + +| function | before | after | +|---|---|---| +| `trySendKnightTribeToAttack` | 62.1 | **100.0** | +| `collectArcherUnitsByLocation` | 75.9 | 63.7 | +| `commandUnitsToMoveToKeep` | 77.6 | 73.7 | +| `executeAttackWaveTargetAssignment` | 88.5 | 82.4 | +| `recountAttackTroopValue` | 75.9 | 65.9 | + +One big win, four clear losses. The rule says *the register allocation implies a +data flow*, not *caching is always right* - sometimes the original really does +re-read. Sweep the candidates, keep the winners, and do it in a single build. + +### sweep_all.py CONST and SIGNED hits drift out of alignment - verify before trusting + +A full sweep after this session's structural wins produced four plausible-looking +constant mismatches. All four were artefacts of the aligner pairing the original's +*next* comparison against ours, and all four lost points when "fixed": + +| reported | reality | +|---|---| +| `computeAttackWaveTroopComposition` orig 0x4 vs ours 0x2 | ours cmp 2 paired with orig's cmp 4 from the *following* `5 <= x` test; `4 < x` -> 66.8 to 65.9 | +| `executeAttackWaveTargetAssignment` orig 0x3 vs ours 0x6 | same drift one test over; `< 3`/`< 7`/`< 13` -> 88.5 to 88.3 | +| `setRallyPointForLaddermenTribe` SIGNED jl/jb | int-casting the bound is exactly neutral here | +| `assignMacemen...` orig 0x12 vs ours 0x13 | `cmp 0x12/jle` and `cmp 0x13/jl` are the same test; MSVC canonicalises `<= 18` back to `< 19`, exactly neutral | + +Before acting on a CONST hit, read both sides' surrounding comparisons and check +the hit is not just our test aligned against the original's neighbour. A CONST +hit is only real if the two tests it pairs are the *same* test. + +Also measured and reverted (the cached-local substitution, wrong direction here): +`scanForArcherPoints` 82.7 -> 77.7, `scanForSiegeTentPoints` 41.7 -> 34.5. + +### A boolean argument that the original *branches* on wants two call sites + +`aiRecomputeAttacks2` passed `(int)(BVar3 != 0)` to `computeAIZoneLayer`, which +MSVC renders branchlessly as `xor eax,eax / setne al`. The original branches: +`je / push 1 / jmp`. Writing the call twice under an `if (BVar3) ... else ...` +with literal `1` and `0` gives the branch and took it 87.6% -> **89.2%**. + +`BVar3 ? 1 : 0` is *not* a substitute - it compiles bit-identically to +`(BVar3 != 0)` (measured, 87.6%). MSVC only branches when the two argument +values reach the call from genuinely different paths, so the call has to be +duplicated. + +Note this cuts the opposite way from `computeAttackWaveTroopComposition`, where +reading `setne` in the original's listing and converting two assignments to +`(x != 0)` form collapsed it from 66.8% to **13.7%** - that `setne` belonged to a +different statement that the aligner had drifted onto. Confirm which statement a +`setne` belongs to before acting on it. + +### Rebase parameter arithmetic onto the derived local that already holds it + +`renderAttackInfoDebugOverlay` computed `xParam = x + 2` once and then wrote +`x + 0x16` (4 sites) and `x + 0x20` (2 sites) for later columns. The original +emits `lea eax,[esi + 0x14]` from the register already holding `xParam`, while +ours re-read the parameter off the frame and added the absolute offset - which +also cost an extra dword of frame (`sub esp,0x3f0` vs `0x3ec`). + +`x + 0x16` is `xParam + 0x14`, same value. Rewriting all six sites relative to +`xParam` took it 82.4% -> **95.8%**, and the rest of the gap is only reccmp +mis-symbolising the literal `0xffffff` (white) as `BuildingsState+424671` plus +the security-cookie names. + +Fourth instance of the data-flow rule. Worth grepping for: two expressions on +the same parameter with different constants, where one of them is already a +named local - the later ones are probably offsets from that local, not from the +parameter. + +#### Sweep result, round two (same substitution, five more functions) + +| function | before | after | kept | +|---|---|---|---| +| `updateTribeBehaviorBasedOnBehaviorType` | 55.0 | **71.3** | yes | +| `sortAttackInfoTribeIDArrayBasedOn` | 74.5 | **83.1** | yes | +| `placeSiegeTentOrTunnelAtSuitableLocation...` | 83.2 | **84.2** | yes | +| `placeTunnelForEveryTunneler` | 70.2 | 56.0 | no | +| `placeSiegeTentsAndAssignEngineers` | 32.6 | 28.1 | no | +| `ifNecessaryThrowOilAutomatically` | 36.5 | 35.9 | no | + +Running tally for "read the cached local, not the global": **5 wins (two of them +to 100%), 8 losses.** It is the single highest-yield substitution found so far +and it is still only slightly better than a coin flip per site, so it must be +swept and measured, never reasoned about. Batch every candidate into one build - +scores are per-function - then revert the losers. + +The scan that finds the candidates: + + local = ; ...and that same expr appears again later + +### `x + -1` and `x - 1` are the same instruction - do not chase it + +`updateTribeBehaviorBasedOnBehaviorType`'s diff shows the original using +`sub eax,1` where ours emits `add eax,-1`, which looks like the Ghidra `+ -1` +idiom leaking into codegen. It is not. Rewriting every `+ -1` as `- 1` across +all twelve sub-100 files that contain it changed **nothing**: all twelve scored +bit-identically. MSVC canonicalises the two forms. + +Keep `+ -1` - it is what the already-100% files in this directory use. + +### Do not reorder the three top-level arms of `updateTribeBehaviorBasedOnBehaviorType` + +Its dispatch is `cmp eax,0x3f2` followed in the original by `jg / je ` +where ours emits a single `jge`, which reads as though the original ordered the +arms high-switch / `== 1010` / low-switch while ours has low / `== 1010` / high. +Rewriting the chain into that order (same bodies, conditions negated) collapsed +it **71.3% -> 21.3%**. + +The two switches are ~76 and ~308 lines; moving either past the other relocates +every jump-table entry and every arm, so the per-arm layout that previous +sessions tuned (see the corrected arm order above) is worth far more than the +dispatch's two-instruction shape. Treat the dispatch `jge`/`jg`+`je` difference +as a 2-instruction cost and leave the arms where they are. + +## Prioritise by starting percentage: local fixes only pay above ~70% + +Tally of every change measured in this session, by the function's starting score: + +**Starting >= 70% - 13 wins, 0 regressions kept:** +slot scans +23.4 (x3, to 100%), `renderAttackInfoDebugOverlay` +13.4, +`trySendKnightTribeToAttack` +37.9 (to 100%), `placeTunnelEntrances` +11.4 +(to 100%), `applyTribeBehaviorTypes` +4.3 (to 100%), `sortAttackInfo` +8.6, +`pruneStale*` +6.4 (x3), `aiRecomputeAttacks2` +1.6, +`placeSiegeTentOrTunnel...` +1.0. + +**Starting < 70% - one win (+16.3), everything else lost:** +`ifNecessaryThrowOilAutomatically` (36.5): cached local -0.6, `< 1`->`<= 0` -6.2, +and -6.3 for that same edit *in isolation at the exact site the diff flagged*. +`placeSiegeTentsAndAssignEngineers` (32.6): cached local -4.5, `<= 0` -4.1. +`computeAttackWaveTroopComposition` (66.8): `setne` -53.1, CONST -0.9. +`recomputeAttackInfo` (30.5): int-cast bound +0.1. + +**Why:** reccmp aligns instruction streams. Below roughly 50-70% the two streams +are already badly out of phase, so a *correct* one-instruction fix re-phases the +alignment somewhere else and the score drops. The `<= 0` edit above is almost +certainly right - the original really does emit `test edi,edi / jg` - and it +still measured -6.3. + +**Consequences:** +1. On a sub-70% function the score cannot validate a local fix. Do not grind + single-site edits there and do not trust a regression as evidence the edit was + wrong. +2. Sub-70% functions need a change large enough to re-phase the whole function - + arm order, a dropped `return`, a restructured loop - the kind of thing found + by reading Ghidra's control flow, not by reading a diff. +3. Spend diff-reading time on the 70-99% band. That is where every win came from. + +## The induction-pointer anchor is not source-controllable - stop trying + +Four functions differ almost entirely in *which struct field* MSVC anchors the +loop's induction pointer on, which shifts every field offset and the loop bound +in the diff and costs roughly 20 points each: + +| function | % | original anchor | ours | +|---|---|---|---| +| `exitSiegeEquipmentForWave` | 77.7 | `TribesState+886` | `+1520` | +| `recountAttackTroopValue` | 75.9 | `TribesState+886` | `+864` | +| `commandUnitsToMoveToKeep` | 77.6 | `UnitsState+2866` | `+2874` | +| `sortAttackInfoTribeIDArrayBasedOn` | 83.1 | `TribesState+1025064` | anchor-0x18 | + +The decisive evidence is `recountAttackTroopValue`: both sides access exactly +the same three fields (884, 864, 912), so MSVC had a free choice of base and +simply picked differently. The anchor is chosen downstream of register pressure +and instruction order, not from anything spellable in the source. + +Tried and failed: +- hoisting the read of the field the original anchors on, so it is the first + access (`exitSiegeEquipmentForWave`, exactly neutral - MSVC sinks the load back + into the branch) +- walking an explicit `int*`/`short*` at the original's offset (`pruneStale*`, + -19 points; also -12 on a second shape) +- writing the loop bound symbolically instead of as a literal + (`sortAttackInfo`, -2.5 raw, twice) + +Treat these four as regalloc-bound. Their remaining ~20 points each are not +reachable from C++. + +Also neutral, do not retry: moving `placeTunnelForEveryTunneler`'s zero +initialisations after the `if (!local_c)` test to stop MSVC reusing a zeroed +register as the comparison operand. MSVC reorders them back. + +## `recomputeAttackInfo` (0x51E300): what it actually is, and why diffs mislead + +Ghidra's control-flow analysis: 4124 bytes, 997 instructions, 143 basic blocks, +cyclomatic complexity 144, 11 loops, **42 calls, 1 return**. Our file has exactly +42 `MACRO_CALL_MEMBER` and exactly 1 `return`, and 9 loops. So the function is +*not* missing code or returns - the call and exit structure already match. + +Two real fixes found by reading the first non-register divergence (30.5% -> 31.0%): +- `((2999 < zoneSize) - 1 & 6) + 4` -> `3000 <=`, which is what produces the + original's `cmp 0xbb8 / setge` instead of our `cmp 0xbb7 / setg`. +- the `_isSolitary` if/else arms were swapped; the original lays the + `someDistanceLimit = 4` arm as the fallthrough (`je` over it), ours jumped to it. + +**The large asymmetric runs in its diff are aligner artifacts, not missing code.** +The diff shows three separate `orig 29 insns / ours 0` runs and one +`orig 3 / ours 37`, which look like whole blocks we failed to write. They are not: +the block in question (`mov eax,[TileMapState+5589560] / shl eax,5 / ...`) occurs +40 times across the function and *most of its occurrences match*. When a block +repeats many times reccmp pairs the copies arbitrarily, so one copy matches and +another shows as wholly absent. + +Consequence: do not read this function's diff for missing-code leads. What is +left is (a) register allocation - the original keeps `_unitID` in `si` and the +loop index in `ebp` across the main loop while ours spills both to the frame - +and (b) the pairing noise above. Combined with the sub-70% aligner problem +documented earlier, its diff cannot validate a local change either. If this +function is to be moved, it needs Ghidra's control flow read block by block +against the source, not reccmp. + +# The Ghidra reading: what it establishes, and how to use Ghidra here + +## Every decompiled function in this namespace is the seed our .cpp came from + +`get_plate_comment` on all seven functions checked (`recomputeAttackInfo`, +`ifNecessaryThrowOilAutomatically`, `commandUnitsToMoveToKeep`, +`exitSiegeEquipmentForWave`, `pruneStaleArcherPoints`, +`findOrReserveArcherPointSlot`, `giveLightPitchInstructionToUnitClosestToPitch`) +returns the identical banner: + + decompilerscript: committed: 2025-01-30 21:57:43.216000 + +One bulk script run. So **Ghidra's decompiled C is the text our files were +generated from** - it cannot contain information our files lack, and on a file +already tuned past the seed, following it *undoes* that tuning. Measured on +`ifNecessaryThrowOilAutomatically`: rewriting the body to mirror Ghidra exactly +(its variable set, its `param_1`-as-size-index and `sVar2`-as-y reuse, its merged +guard) scored **36.5% -> 29.2%**. + +Worse, the decompiler actively misleads on control flow: it renders two +*sequential early returns* as one `||` chain. Ghidra prints + + if ((iVar9 < 1) || (0x50 < ...closestEnemyMicroDistance) || (...)) return 0; + +but the disassembly shows `test edi,edi / jg 0xa` - a 10-byte hop over an inline +epilogue, i.e. genuinely two separate `if (...) return 0;` statements, which is +what our file already had. Merging them cost 7.3 points on its own. + +## What Ghidra *is* good for: the disassembly and the control-flow metrics + +Those come from the binary, not the script. `analyze_control_flow` gives a +structural checksum worth running before touching a low-scoring function: + +| function | calls (Ghidra/ours) | instrs (orig/ours) | delta | +|---|---|---|---| +| `recomputeAttackInfo` | 42 / 42 | 981 / 941 | -40 | +| `assignMacemenAndKnights...` | 8 / 8 | 213 / 213 | 0 | +| `placeSiegeTentsAndAssignEngineers` | 5 / 5 | 211 / 224 | +13 | +| `scanForSiegeTentPoints` | 2 / 2 | 193 / 200 | +7 | +| `ifNecessaryThrowOilAutomatically` | 4 / 4 | 176 / 169 | -7 | +| `recountTotalTroopValue` | 1 / 1 | 48 / 48 | -3 | + +**Call counts match exactly on every function.** And diffing the *set of global +offsets* each side touches (`I::instance+N` in the reccmp diff) finds +**zero** offsets the original reads or writes that our side does not - so no +data access is missing either. + +Conclusion: the big low-% functions are not missing logic. Their gap is +instruction selection and ordering. For `recomputeAttackInfo` the mnemonic +histogram of the gap is `movzx +25 / mov -41 / add -17 / jge -12 / jg +7` - +narrow-unsigned loads where the original loads dwords, and comparison polarity. +Our 16-bit temps are already declared 2-byte (`undefined2`, `ushort`), so the +extra `movzx` come from MSVC promoting spilled 16-bit temps, not from the source +types. Not obviously source-addressable. + +### Checks worth running, in order, before editing a low-scoring function +1. `get_plate_comment` - if it carries the bulk banner, do not read the + decompiled C as evidence; read `disassemble_function`. +2. `analyze_control_flow` - compare `calls` against our `MACRO_CALL_MEMBER` + count. A mismatch is real missing/extra code and is worth a lot. + `returns` and `loops_detected` are *not* comparable to source `return`/loop + counts (they count merged epilogues and back-edges). +3. The global-offset set difference above - catches missing data access. +4. Instruction-count delta - a large positive delta means we do extra work. + Count only lines matching `[a-z]` in the diff; blank lines otherwise inflate + it (this produced a bogus "+26" on oil before correction). + +# The instruction-surplus ranking: the technique that works below 70% + +The reccmp diff gives both sides' instruction totals, so no Ghidra call is +needed: `orig = matched + orig-only`, `ours = matched + ours-only`. Totals are +**pairing-independent**, so unlike every other reading of the diff this number +survives the aligner being out of phase. A positive delta is real extra work, +and removing real work moves the score even on a sub-50% function - which is the +one thing local encoding fixes cannot do there. + +Exclude `0xcc`, `nop` and the `lea esp,[esp]` / `mov edi,edi` alignment fillers +or the ranking is wrong: `updateTribeBehaviorBasedOnBehaviorType` has 12 `int3` +padding bytes counted as original instructions. + +Results from the first pass of this ranking: + +| function | delta | fix | % | +|---|---|---|---| +| `placeSiegeTentsAndAssignEngineers` | +18 | redundant reload in the pop loop | 32.6 -> **44.1** | +| `computeSiegeSpotScores` | +27 | `-(uint)(cond) & mask` -> ternary | 63.2 -> **66.4** | +| `placeTunnelForEveryTunneler` | +17 | hoist a loop-invariant store | 70.2 -> **75.7** | + +## The two shapes that paid + +**A store inside a loop whose target is only read after it.** `placeSiegeTents` +re-read `tribeIDArraySize` at the end of a loop body that the loop condition +then re-read anyway; `placeTunnelForEveryTunneler` assigned +`_nTunnels = _nTunnelers` every iteration. One extra load/store per iteration +each. + +**A 0-or-N mask written as arithmetic on a bool.** `-(uint)(cond) & 9999` and +`(cond) - 1 & 9999` both make MSVC materialise the bool as 0/1 then negate it +back into a mask: + + orig: cmp bl,byte[..] / sbb edx,edx / and edx,0x270f + ours: cmp bl,byte[..] / sbb edx,edx / neg edx / neg edx / and edx,0x270f + +`cond ? N : 0` lowers straight from `sbb` to `and`. **But only where the +comparison feeds `sbb`**: the same rewrite on the two `(cond) - 1 & N` sites in +`computeAttackWaveTroopComposition` and `recomputeAttackInfo`, where there is no +memory compare, lost 0.3 each. Check for the `neg` pair before rewriting. + +## Do not delete a loop store without tracing the gotos + +`recomputeAttackInfo:325` sets `_someLimit = 1` inside a loop and `_someLimit` is +read only at line 596, which looks dead. It is not: the `goto LAB_0051f316` on +the next line jumps *past* the line-590 assignment that would otherwise +overwrite it, so line 325 supplies the value actually passed to +`recomputeALGPathFindingTileMapUnk`. Deleting it would have been a behaviour bug. + +## `updateTribeBehaviorBasedOnBehaviorType`: one stack slot, 33 instructions + +Its +46 surplus is almost entirely one cause, and it is precisely quantified: + + ret orig 32 ours 32 <- exits match exactly + pop orig 128 ours 160 <- exactly one extra pop per exit + +Ours opens with an extra `push ecx` (the MSVC idiom for allocating one 4-byte +slot) and every one of the 32 epilogues carries a matching `pop ecx`. The +original keeps `this` in `ebx` (`mov ebx,ecx`); we spill it +(`mov [esp+0x10],ecx`). So our source holds **exactly one more value live than +the original**, and finding it is worth 33 instructions. + +Not the cause (measured): the `uVar7` local, loaded at line 73 and read only at +line 381, whose live range spans the whole switch. Inlining it to its use site +is safe (the only writers of `attackWaveRetargetCount` are in other functions +not reachable on that path) but left `pop` at 160 and cost 10.1 points. + +## `updateTribeBehaviorBasedOnBehaviorType`: the stack slot is removable, and it is not worth it + +Follow-up to the 33-instruction lead above. The extra slot is caused by our +source keeping **three** values live across the switch dispatch where the +original keeps none: + +- `_playerID_7` (`currentPlayerSlotID`, cached line 72, used at lines 111-122 and 270) +- `uVar7` (`attackWaveRetargetCount`, cached line 73, used only at line 381) +- `_attackWave_2` (cached line 60, used at lines 61, 64 and 164) + +Removing any **one** of them does nothing - MSVC simply spills a different one +(measured: `uVar7` alone -10.1, `_playerID_7` alone -14.4, both left `pop` at 160). +Removing **all three** does fix it exactly: + + before: pop orig 128 ours 160 surplus +46 + after: pop orig 128 ours 128 surplus +9 + +So the diagnosis was right and the slot is reachable. But it is still a net loss +(**71.3% -> 56.4%**) because matched instructions fall from ~566 to 401: pushing +the three reads down into the arms re-phases the arm bodies, and that costs far +more than the 33 prologue/epilogue instructions it saves. + +Conclusion: this function's +46 surplus is **not** worth collecting. Note also +that the original reads `currentPlayerSlotID` twice and `unknownAttackRelatedUpdateCounter` +twice - both already match our counts - and `sVar3` is a *reused* variable +(reassigned at line 82), not a long-lived cache, so it is not a candidate either. + +Generalisation: the instruction-surplus ranking finds real extra work, but +removing it only pays when the removal is **local** - a dead store in a loop, a +redundant reload, a mask spelled as bool arithmetic. When the only way to remove +the work is to move code across basic blocks, the alignment loss dominates. +Check that a surplus fix is local before attempting it. + +# Decoding a jump table with ghidra-mcp: the one reliable structural lever + +Ghidra's decompiled C is the seed (see above), but its **jump tables are ground +truth and arm layout is source-controllable**. This is the one structural thing +worth reading Ghidra for. + +## Procedure + +1. `disassemble_function ` and find the dispatch, e.g. + `JMP dword ptr [ECX*0x4 + 0x51c7d8]`. A preceding + `MOVZX ECX,byte ptr [ECX + ]` means there is a byte index table at `` + as well as the dword jump table. +2. `disassemble_bytes` the few instructions before it to get `min` from the + `SUB EAX,` / `ADD EAX,-` and the default from the `JA` target. +3. `read_memory <4 * (max-min+1)>` and decode little-endian dwords. +4. Entry *i* is value `i + min`. Sort the distinct body addresses ascending - + that is the **arm layout order**, which is set by the order the arms appear in + the source. + +## Result: `ifNecessaryThrowOilAutomatically` 36.5% -> 37.80% + +The building-type switch lays its arms out `7, 5, 4` +(`MOV [ESP+0x1c],0x7` at 0051c69a, `0x5` at 0051c6a4, `0x4` at 0051c6ae); our +source had them `4, 5, 7`. Reversing the source order matched it. + +## Result: `updateTribeBehaviorBasedOnBehaviorType`'s low switch is already correct + +Worth recording so nobody re-derives it. Table at **0x51E240**, `min = 1`, +default = **0x51E22C** (the `JA` target): + +| value | target | meaning | +|---|---|---| +| 1 | 0051d9e0 | body | +| 2 | 0051e22c | **default** | +| 3 | 0051d882 | body | +| 4 | 0051e237 | bare epilogue - do nothing | +| 5 | 0051d8e3 | body | +| 6 | 0051e237 | bare epilogue - do nothing | +| 7 | 0051d932 | body | +| 8 | 0051d9b5 | body | + +So our `case 4: case 6: break;` is right, having no `case 2` is right (it falls +to `default`), and the arm layout `3, 5, 7, 8, 1, default` already matches. +Moving the empty `4/6` arm after `default` to mirror 0x51e237 sitting after +0x51e22c is exactly neutral - MSVC points an empty arm at the shared exit +wherever it appears in the source. + +## `getValueOfTroopType`: the last 3.2% is cold-block placement + +Its tail is two separate compares both jumping *forward* to one shared +`XOR EAX,EAX / RET`: + + 0051c3d2: CMP ECX,0x16 / JL 0x0051c3e6 + 0051c3d7: CMP ECX,0x1e / JG 0x0051c3e6 + 0051c3dc: MOV EAX,[ECX*0x4 + 0x6172a0] / RET 0x4 + 0051c3e6: XOR EAX,EAX / RET 0x4 + +Three source spellings were measured. `&&` and a nested `if` both get fused into +a range check (`LEA EDX,[ECX-0x16] / CMP EDX,8 / JA`), losing 0.4. Two `goto`s to +a trailing label keeps the two compares but MSVC still places the cold +`xor/ret` *first* (`jl 0x5 / jle 0x5` instead of `jl 0xf / jg 0xa`), scoring the +same 96.8%. No lever found for cold-block placement. diff --git a/cmake/compiler-flags-gl.txt b/cmake/compiler-flags-gl.txt index 17882af542..1ee57d17ec 100644 --- a/cmake/compiler-flags-gl.txt +++ b/cmake/compiler-flags-gl.txt @@ -450,6 +450,19 @@ src/OpenSHC/Map/Units/UpdateJester.cpp src/OpenSHC/Map/Units/UpdateSlave.cpp src/OpenSHC/Map/Units/UpdateHealer.cpp src/OpenSHC/Map/Units/UpdateSpearman.cpp +src/OpenSHC/Map/Units/TroopValueState/collectArcherUnitsByLocation.cpp +src/OpenSHC/Map/Units/TroopValueState/getPlayerNot1AndHasKeep.cpp +src/OpenSHC/Map/Units/TroopValueState/getValueOfTroopType.cpp +src/OpenSHC/Map/Units/TroopValueState/recountTotalTroopValue.cpp +src/OpenSHC/Map/Units/TroopValueState/ifNecessaryThrowOilAutomatically.cpp +src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp +src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp +src/OpenSHC/Map/Units/TribesState/setWhetherTribeContainsAnyUnits.cpp +src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp +src/OpenSHC/Map/Units/TroopValueState/scanForArcherPoints.cpp +src/OpenSHC/Map/Units/TroopValueState/placeTunnelForEveryTunneler.cpp +src/OpenSHC/Map/Units/TroopValueState/giveLightPitchInstructionToUnitClosestToPitch.cpp +src/OpenSHC/Map/Units/TroopValueState/computeSiegeSpotScores.cpp src/OpenSHC/Map/Units/UpdateSlinger.cpp src/OpenSHC/Map/Units/UpdateWoodcutter.cpp src/OpenSHC/Map/Units/UpdateFletcher.cpp diff --git a/src/OpenSHC/Audio/SFX/SFXState/playUnitSpeech.cpp b/src/OpenSHC/Audio/SFX/SFXState/playUnitSpeech.cpp index 7b18583c41..fb9a3ffe58 100644 --- a/src/OpenSHC/Audio/SFX/SFXState/playUnitSpeech.cpp +++ b/src/OpenSHC/Audio/SFX/SFXState/playUnitSpeech.cpp @@ -181,7 +181,7 @@ namespace Audio { return; } - int numberOfVariations = DAT_SpeechDefinedData::instance.UnitSpeech[actionID].numberOfVariations; + int const numberOfVariations = DAT_SpeechDefinedData::instance.UnitSpeech[actionID].numberOfVariations; if (1 < numberOfVariations) { /* Calculate action speech variation. */ variationIndex = DAT_SpeechDefinedData::instance.UnitSpeech[actionID].variationCounter[_soundSet]++; diff --git a/src/OpenSHC/Audio/mss/SoundSystem/playRandomAmbientMusic.cpp b/src/OpenSHC/Audio/mss/SoundSystem/playRandomAmbientMusic.cpp index 134ff0ce0b..9ed45ee894 100644 --- a/src/OpenSHC/Audio/mss/SoundSystem/playRandomAmbientMusic.cpp +++ b/src/OpenSHC/Audio/mss/SoundSystem/playRandomAmbientMusic.cpp @@ -189,7 +189,7 @@ namespace Audio { } else if (0 < DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] .totalEnemyUnitsCount - && DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated == 0) { + && !DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated) { MACRO_CALL_MEMBER(SoundSystem_Func::playRandomMusic02, this)(1); MACRO_CALL_MEMBER(SoundSystem_Func::playBattleGloryMusicIfConditionsMet, this)(); } @@ -260,12 +260,13 @@ namespace Audio { && 30000 < timeGetTime() - DAT_SoundEffectsHelperData1::instance.DAT_SomeSoundTime2) { MACRO_CALL_MEMBER(Random::RNG_Func::nextRandomNumber1, SEC_RNG::ptr)(); int const randomNumber = SEC_RNG::instance.currentNumber1 % 3; - if (randomNumber == 0) + if (randomNumber == 0) { DAT_SoundEffectsHelperData1::instance.battleMoodState = 1; - else if (randomNumber == 1) + } else if (randomNumber == 1) { DAT_SoundEffectsHelperData1::instance.battleMoodState = 2; - else + } else { DAT_SoundEffectsHelperData1::instance.battleMoodState = 0; + } } } diff --git a/src/OpenSHC/Global/WinMain.cpp b/src/OpenSHC/Global/WinMain.cpp index f4512aff4c..e41048d4b6 100644 --- a/src/OpenSHC/Global/WinMain.cpp +++ b/src/OpenSHC/Global/WinMain.cpp @@ -172,8 +172,8 @@ int __stdcall Global::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPST // The original passes the AICState as this to the empty function ((void(__fastcall*)(OpenSHC::AI::AICState*))MACRO_CALL(OpenSHC::Global_Func::DoNothing))(DAT_AICState::ptr); - int mainMenuBorderHeight = DAT_WindowAndDirectDraw::instance.mainMenuBorderHeight; - int mainMenuBorderWidth = DAT_WindowAndDirectDraw::instance.mainMenuBorderWidth; + int const mainMenuBorderHeight = DAT_WindowAndDirectDraw::instance.mainMenuBorderHeight; + int const mainMenuBorderWidth = DAT_WindowAndDirectDraw::instance.mainMenuBorderWidth; Menu* loadingMenu = DAT_MenuHandlerState::instance.currentMenu; loadingMenu->yPosition = mainMenuBorderHeight; loadingMenu->xPosition = mainMenuBorderWidth; @@ -241,8 +241,9 @@ int __stdcall Global::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPST */ tagMSG windowMessage; if (PeekMessageA(&windowMessage, NULL, 0, 0, 0)) { - if (!GetMessageA(&windowMessage, NULL, 0, 0)) + if (!GetMessageA(&windowMessage, NULL, 0, 0)) { break; // Break the game loop + } TranslateMessage(&windowMessage); DispatchMessageA(&windowMessage); } else { @@ -442,7 +443,7 @@ int __stdcall Global::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPST && MACRO_CALL_MEMBER( OpenSHC::Game::GameCore_Func::getAreWeInAInGameMenu, DAT_GameCore::ptr)()) { if (TIME_ReceivedMessage_2::instance) { - int pausedDuration = timeGetTime() - TIME_ReceivedMessage_2::instance; + int const pausedDuration = timeGetTime() - TIME_ReceivedMessage_2::instance; TIME_ReceivedMessage_2::instance = 0; DAT_GameCore::instance.timeSum_2 = DAT_GameCore::instance.timeSum_2 + pausedDuration; } @@ -450,7 +451,7 @@ int __stdcall Global::WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPST TIME_ReceivedMessage_2::instance = timeGetTime(); } if (TIME_ReceivedMessage_1::instance) { - int unfocusedDuration = timeGetTime() - TIME_ReceivedMessage_1::instance; + int const unfocusedDuration = timeGetTime() - TIME_ReceivedMessage_1::instance; TIME_ReceivedMessage_1::instance = 0; TIME_Sum_1::instance = TIME_Sum_1::instance + unfocusedDuration; } diff --git a/src/OpenSHC/IO/FilePackager/readMapOrSavFile.cpp b/src/OpenSHC/IO/FilePackager/readMapOrSavFile.cpp index 2bb8f2ed12..a9949ea0f0 100644 --- a/src/OpenSHC/IO/FilePackager/readMapOrSavFile.cpp +++ b/src/OpenSHC/IO/FilePackager/readMapOrSavFile.cpp @@ -45,7 +45,7 @@ namespace IO { char* _mapName = MACRO_CALL_MEMBER(ResourceManager_Func::getFileNameOfCurrentActiveResource, DAT_ResourceManager::ptr)(); - int _fileDescriptor = MACRO_CALL(OS_Func::_ucrt_open)(_mapName, _O_BINARY, 0); + int const _fileDescriptor = MACRO_CALL(OS_Func::_ucrt_open)(_mapName, _O_BINARY, 0); if (_fileDescriptor == -1) { MACRO_CALL(OS_Func::_free_base)(_previewOrDescriptionUnk); MACRO_CALL(OS_Func::_free_base)(this->tempMem); diff --git a/src/OpenSHC/Input/MouseState/drawMouseBasedBox.cpp b/src/OpenSHC/Input/MouseState/drawMouseBasedBox.cpp index 17d373e78a..b15c526e0d 100644 --- a/src/OpenSHC/Input/MouseState/drawMouseBasedBox.cpp +++ b/src/OpenSHC/Input/MouseState/drawMouseBasedBox.cpp @@ -20,10 +20,10 @@ namespace Input { MACRO_CALL_MEMBER(MouseState_Func::resetMouseCursorState, this)(); return; } - int left = this->previousSelectionBox.left; - int right = this->previousSelectionBox.right; - int top = this->previousSelectionBox.top; - int bottom = this->previousSelectionBox.bottom; + int const left = this->previousSelectionBox.left; + int const right = this->previousSelectionBox.right; + int const top = this->previousSelectionBox.top; + int const bottom = this->previousSelectionBox.bottom; DAT_PencilRenderCore::instance.surfaceTarget = Rendering::Enums::RT_MAP_GAME; MACRO_CALL_MEMBER(UI::Rendering::PencilRenderCore_Func::drawBorderBox, DAT_PencilRenderCore::ptr)( diff --git a/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp new file mode 100644 index 0000000000..b5a1a61132 --- /dev/null +++ b/src/OpenSHC/Map/Units/TribesState/addUnitToTribe.cpp @@ -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); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState.asm.hpp b/src/OpenSHC/Map/Units/TroopValueState.asm.hpp new file mode 100644 index 0000000000..1e31c94044 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState.asm.hpp @@ -0,0 +1,138 @@ +#pragma once + +#include + +#include "OpenSHC/Map/TileMapState.hpp" + +/** + Hand-written assembly macros for OpenSHC::Map::Units::TroopValueState. + + Only sequences that MSVC provably will not emit from C++ belong here. Every + macro reproduces one byte-identical run from the original binary; the C++ + equivalent is kept as a comment above it so the behaviour stays readable. + + A resolver global cannot be named inside an __asm operand (`::` is not + parseable there), and a base passed in as a pointer would assemble as + register-indirect addressing where the original uses absolute. So the + namespace below re-declares the resolver's own address as unqualified + enumerators, which the inline assembler accepts as compile-time constants. + The address comes from the project's address table and the field offsets from + the generated struct, so no literal is repeated here. + + The `ds:` override is load bearing: a bracketed constant expression on its + own assembles as an immediate, which would load the field's address instead + of its value. + */ + +namespace OpenSHC { +namespace Map { + namespace Units { + + // The same address DAT_TileMapState.hpp hands to MACRO_STRUCT_RESOLVER, + // so with the struct resolvers enabled these are the original's own + // addresses. Unqualified on purpose: __asm cannot parse `::`. + enum { + ASM_TileMapState = Address::SHC_3BB0A8C1_0x01A93208, + + ASM_TileMapState_DAT_SomeY = ASM_TileMapState + offsetof(TileMapState, DAT_SomeY), + ASM_TileMapState_DAT_SomeTile = ASM_TileMapState + offsetof(TileMapState, DAT_SomeTile), + ASM_TileMapState_ptr_MovementDirectionTranslationMatrix + = ASM_TileMapState + offsetof(TileMapState, ptr_MovementDirectionTranslationMatrix), + ASM_TileMapState_ptr_PathConnectionLayer + = ASM_TileMapState + offsetof(TileMapState, ptr_PathConnectionLayer), + ASM_TileMapState_ptr_SpecialAreasArray + = ASM_TileMapState + offsetof(TileMapState, ptr_SpecialAreasArray), + }; + + } +} +} + +/** + Shared prologue of both neighbour fills. Leaves + + edx = specialAreasArray + esi = &pathConnectionLayer[DAT_SomeTile] (a copy is pushed for the body) + edi = row DAT_SomeY of the movement direction translation matrix + + Both inputs are read back out of the globals, which is what makes these + sequences unreachable from C++: MSVC forwards the value it just stored and + folds the repeated address arithmetic, so it emits neither the reloads nor + the doubling-by-two-adds that the original uses. + */ +#define MACRO_ASM_LOAD_CARDINAL_BASES() \ + __asm mov edi, dword ptr ds:[ASM_TileMapState_ptr_MovementDirectionTranslationMatrix] \ + __asm mov esi, dword ptr ds:[ASM_TileMapState_ptr_PathConnectionLayer] \ + __asm mov edx, dword ptr ds:[ASM_TileMapState_ptr_SpecialAreasArray] \ + __asm mov eax, dword ptr ds:[ASM_TileMapState_DAT_SomeTile] \ + __asm shl eax, 1 \ + __asm add esi, eax \ + __asm push esi \ + __asm mov eax, dword ptr ds:[ASM_TileMapState_DAT_SomeY] \ + __asm shl eax, 5 \ + __asm add edi, eax + +/** + Fill the four even slots of `specialAreasArray` with the path connection + values of the four cardinal neighbours of `DAT_SomeTile`. + + C++ equivalent, with the bases as named by MACRO_ASM_LOAD_CARDINAL_BASES: + + pSpecialAreas[2] = pConnection[1]; + pSpecialAreas[6] = pConnection[-1]; + pSpecialAreas[0] = pConnection[pDirection[0]]; + pSpecialAreas[4] = pConnection[pDirection[4]]; + + Clobbers eax, ebx, ecx, edx, esi, edi. + */ +#define MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS() \ + MACRO_ASM_LOAD_CARDINAL_BASES() \ + __asm mov bx, word ptr [esi + 2] \ + __asm mov ax, word ptr [esi - 2] \ + __asm mov word ptr [edx + 4], bx \ + __asm mov word ptr [edx + 0xC], ax \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov bx, word ptr [esi] \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov ax, word ptr [esi] \ + __asm mov word ptr [edx], bx \ + __asm mov word ptr [edx + 8], ax + +/** + Fill the four odd slots of `specialAreasArray` with the path connection + values either side of the two diagonal neighbours of `DAT_SomeTile`. + + Same prologue and the same unreachable-from-C++ reasoning; only the tile each + pair is taken around, and the slots written, differ. + + C++ equivalent: + + pSpecialAreas[1] = pConnection[pDirection[0] + 1]; + pSpecialAreas[7] = pConnection[pDirection[0] - 1]; + pSpecialAreas[3] = pConnection[pDirection[4] + 1]; + pSpecialAreas[5] = pConnection[pDirection[4] - 1]; + + Clobbers eax, ebx, ecx, edx, esi, edi. + */ +#define MACRO_ASM_FILL_DIAGONAL_PATH_CONNECTIONS() \ + MACRO_ASM_LOAD_CARDINAL_BASES() \ + __asm mov ecx, dword ptr [edi] \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov bx, word ptr [esi + 2] \ + __asm mov ax, word ptr [esi - 2] \ + __asm mov word ptr [edx + 2], bx \ + __asm mov word ptr [edx + 0xE], ax \ + __asm mov ecx, dword ptr [edi + 0x10] \ + __asm pop esi \ + __asm add esi, ecx \ + __asm add esi, ecx \ + __asm mov bx, word ptr [esi + 2] \ + __asm mov ax, word ptr [esi - 2] \ + __asm mov word ptr [edx + 6], bx \ + __asm mov word ptr [edx + 0xA], ax diff --git a/src/OpenSHC/Map/Units/TroopValueState.func.hpp b/src/OpenSHC/Map/Units/TroopValueState.func.hpp index 8efc162c35..dd4c39862c 100644 --- a/src/OpenSHC/Map/Units/TroopValueState.func.hpp +++ b/src/OpenSHC/Map/Units/TroopValueState.func.hpp @@ -53,7 +53,7 @@ namespace Map { false, Address::SHC_3BB0A8C1_0x00518870, &TroopValueState::applyTribeBehaviorTypes) applyTribeBehaviorTypes; - MACRO_FUNCTION_RESOLVER(void (TroopValueState::*)(SomeTribeBehaviorType, undefined4, short, short), false, + MACRO_FUNCTION_RESOLVER(void (TroopValueState::*)(SomeTribeBehaviorType, undefined4, int, int), false, Address::SHC_3BB0A8C1_0x00518930, &TroopValueState::assignBehaviorTypeToNearbyTribes) assignBehaviorTypeToNearbyTribes; diff --git a/src/OpenSHC/Map/Units/TroopValueState.hpp b/src/OpenSHC/Map/Units/TroopValueState.hpp index 7d44016868..bf1a85b265 100644 --- a/src/OpenSHC/Map/Units/TroopValueState.hpp +++ b/src/OpenSHC/Map/Units/TroopValueState.hpp @@ -66,7 +66,7 @@ namespace Map { SomeTribeBehaviorType tribeBehaviorType, SomeTribeBehaviorType tribeBehaviorType2, int off1, int off2); void assignBehaviorTypeToNearbyTribes( - SomeTribeBehaviorType param_1, undefined4 param_2, short param_3, short param_4); + SomeTribeBehaviorType tribeBehaviorType, undefined4 param_2, int upperLimitBase, int upperLimitStep); void assignBehaviorTypeAndLinkSupportTribe( int param_1, int param_2, SomeTribeBehaviorType param_3, undefined4 param_4, undefined4 param_5); diff --git a/src/OpenSHC/Map/Units/TroopValueState/addUnitToNewTribe.cpp b/src/OpenSHC/Map/Units/TroopValueState/addUnitToNewTribe.cpp new file mode 100644 index 0000000000..e010957f7f --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/addUnitToNewTribe.cpp @@ -0,0 +1,48 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BC70 + void TroopValueState::addUnitToNewTribe(int unitID, int attackWave, AITribeType tribeType, int playerID) + { + int tribeID; + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY) { + tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribeForPlayer, DAT_TribesState::ptr)( + playerID); + } else { + int wavePlayerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackWave]; + if (!wavePlayerID) { + wavePlayerID = 2; + } + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + wavePlayerID = 2; + } + tribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)( + wavePlayerID, 0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_6; + } + DAT_TribesState::instance.tribes[tribeID].tribeType = tribeType; + DAT_TribesState::instance.tribes[tribeID].attackWave = attackWave; + DAT_TribesState::instance.tribes[tribeID].attackInfo_someCounter1 = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)(unitID, tribeID); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/advanceAttackWaveStaging.cpp b/src/OpenSHC/Map/Units/TroopValueState/advanceAttackWaveStaging.cpp new file mode 100644 index 0000000000..abb1030056 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/advanceAttackWaveStaging.cpp @@ -0,0 +1,39 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D690 + void TroopValueState::advanceAttackWaveStaging(int attackID) + { + DAT_TroopValueState::instance.attackInfo.attackAlarmPlayed = FALSE; + DAT_TroopValueState::instance.attackInfo.value3Array01[attackID] = Map::Enums::AWS_PREPARING_SIEGE; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] = 0; + MACRO_CALL_MEMBER(Game::GameStateStructures_Func::nof_fpoints_locationFinder, DAT_GameState::ptr)(); + ++DAT_TroopValueState::instance.attackInfo.someCounter1; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 2, 100000, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fe, Map::Units::STBT_5, 0, 50); + if (DAT_TroopValueState::instance.attackInfo.casDis >= DAT_AttackInfoDefinedData::instance + .AICasDisThreshold[DAT_TroopValueState::instance.attackInfo.attacker]) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_5, Map::Units::STBT_1, 0, 40); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/aiControlNonSkirmishUnitMovement.cpp b/src/OpenSHC/Map/Units/TroopValueState/aiControlNonSkirmishUnitMovement.cpp new file mode 100644 index 0000000000..0c81f94995 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/aiControlNonSkirmishUnitMovement.cpp @@ -0,0 +1,119 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Audio/mss/SoundSystem.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Enums/AttackWaveStateInt.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00520F70 + void TroopValueState::aiControlNonSkirmishUnitMovement() + { + MACRO_CALL_MEMBER(TroopValueState_Func::decrementTileMap1104, this)(); + if (DAT_GameCore::instance.gameMode_2 == Game::GM_EDITOR + || DAT_GameCore::instance.gameMode_2 == Game::GM_SKIRMISH_AND_MULTIPLAYER + || DAT_GameCore::instance.gameMode_2 == Game::GM_SIEGE_THAT) { + return; + } + if (DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id + <= 0) { + MACRO_CALL_MEMBER(TroopValueState_Func::updateAttackInfoTick, this)(); + return; + } + MACRO_CALL_MEMBER(TroopValueState_Func::recountAttackTroopValue, this)(FALSE); + MACRO_CALL_MEMBER(TroopValueState_Func::computeAttackWaveTroopComposition, this)(); + if (DAT_TroopValueState::instance.attackInfo.aiTroops == 0) { + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle = 0; + } + int const holdBackTimer + = DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .aiHoldBackTicks; + if (holdBackTimer != -1000) { + if (holdBackTimer == 0) { + BOOLEnum waveComplete = TRUE; + for (int wave = 0; wave < countof(DAT_TroopValueState::instance.attackInfo.nof_tribes); ++wave) { + if (DAT_TroopValueState::instance.attackInfo.nof_tribes[wave] != 0 + && DAT_TroopValueState::instance.attackInfo.value3Array01[wave] != Map::Enums::AWS_ENDED) { + waveComplete = FALSE; + break; + } + } + if (waveComplete) { + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .aiHoldBackTicks = 600; + } + } else if (holdBackTimer > 0) { + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .aiHoldBackTicks = holdBackTimer - 1; + if (holdBackTimer - 1 == 0) { + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .aiHoldBackTicks = -1000; + } + } else if (holdBackTimer < 0) { + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .aiHoldBackTicks = holdBackTimer + 1; + } + } + DAT_TroopValueState::instance.attackInfo.value3Array01[0] = Map::Enums::AWS_GATHERING; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[0] = 0; + for (int wave = 1; wave < countof(DAT_TroopValueState::instance.attackInfo.nof_tribes); ++wave) { + DAT_TroopValueState::instance.attackInfo.attacker + = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[wave]; + if (DAT_TroopValueState::instance.attackInfo.attacker == 1) { + continue; + } + if (DAT_TroopValueState::instance.attackInfo.attacker <= 0 + && !MACRO_CALL_MEMBER(TroopValueState_Func::searchTribeWithProperties, this)(wave)) { + continue; + } + DAT_GameState::instance.playerDataArray[DAT_TroopValueState::instance.attackInfo.attacker] + .attackedPlayerID = 1; + if (DAT_TroopValueState::instance.attackInfo.nof_tribes[wave] != 0) { + DAT_TroopValueState::instance.attackInfo.index = wave; + int const waveAge = ++DAT_TroopValueState::instance.attackInfo.someIntArray2[wave - 1]; + if (DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated && waveAge > 10 + && DAT_TroopValueState::instance.attackInfo.value3Array01[wave] != Map::Enums::AWS_ENDED) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[wave] = 0; + DAT_TroopValueState::instance.attackInfo.value3Array01[wave] = Map::Enums::AWS_ENDED; + MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::handleBattleEndMusicTransition, DAT_SoundSystemState::ptr)(); + } + Map::Enums::AttackWaveStateInt const waveState + = DAT_TroopValueState::instance.attackInfo.value3Array01[wave]; + if (waveState == Map::Enums::AWS_GATHERING) { + MACRO_CALL_MEMBER(TroopValueState_Func::initializeOrAdvanceAttackWave, this)(wave); + } else if (waveState == Map::Enums::AWS_ADVANCING) { + MACRO_CALL_MEMBER(TroopValueState_Func::advanceAttackWaveStaging, this)(wave); + } else if (waveState == Map::Enums::AWS_PREPARING_SIEGE) { + MACRO_CALL_MEMBER(TroopValueState_Func::updateInProgressAttackWave, this)(wave); + } else if (waveState == Map::Enums::AWS_ASSIGNING_TARGETS) { + MACRO_CALL_MEMBER(TroopValueState_Func::executeAttackWaveTargetAssignment, this)(wave); + } else if (waveState == Map::Enums::AWS_DELAYED_TARGET_ASSIGNMENT) { + MACRO_CALL_MEMBER(TroopValueState_Func::executeDelayedAttackWaveTargetAssignment, this)(wave); + } else if (waveState == Map::Enums::AWS_5Unk) { + } else if (waveState == Map::Enums::AWS_ENDED) { + MACRO_CALL_MEMBER(TroopValueState_Func::updateActiveAttackWaveState, this)(wave); + } + } else { + DAT_TroopValueState::instance.attackInfo.value3Array01[wave] = Map::Enums::AWS_GATHERING; + } + DAT_TroopValueState::instance.attackInfo.nof_tribes[wave] = 0; + DAT_TroopValueState::instance.attackInfo.unknownByteArray02[wave] = 0; + } + DAT_TroopValueState::instance.attackInfo.attackWaveSweepActive = 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks.cpp b/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks.cpp new file mode 100644 index 0000000000..e010b5dc2c --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks.cpp @@ -0,0 +1,81 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Util/Timing/Stopwatch.func.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnknownStopwatch.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005202B0 + void TroopValueState::aiRecomputeAttacks(int retryCount, int attackID) + { + int const playerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID]; + MACRO_CALL_MEMBER(Util::Timing::Stopwatch_Func::start, DAT_UnknownStopwatch::ptr)(); + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].currentPlayerID_OR_attackedPlayerID_OR_one + = DAT_GameSynchronyState::instance.currentPlayerSlotID; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TileMapState::instance.AIZoneLayer), 0, DAT_TileMapState::instance.AIZoneLayer); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TileMapState::instance.AIInfoLayer), 0, DAT_TileMapState::instance.AIInfoLayer); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recountAttackTroopValue, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::computeAttackWaveTroopComposition, this)(); + int const defaultSignpostTile = DAT_GameState::instance.mapAndTime.signpostEntryData[0].tile; + int signpostTile + = DAT_GameState::instance.mapAndTime + .signpostEntryData[DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackID]] + .tile; + if (signpostTile <= 0) { + DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackID] = 0; + signpostTile = defaultSignpostTile; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeAIZoneLayer, DAT_PathFindingState::ptr)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, 1, + DAT_TileMapState::instance.PathConnectionLayer[signpostTile]); + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::updateWalkLayerAndAIPathCostLayer, + DAT_PathFindingState::ptr)(60, 0, 0, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::initializeAttackZoneSearch, this)(attackID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recomputeTargetedBuildingTilesArray, this)( + DAT_TroopValueState::instance.attackInfo.attacker); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recomputeAttackInfo, this)( + DAT_TroopValueState::instance.attackInfo.attacker, + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::computeSiegeSpotScores, this)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, + DAT_TroopValueState::instance.attackInfo.attacker); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setScale3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setTown2, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailablePeopleValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableLordValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setGate2, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setWide3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setMoat3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableHighValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableArcherValueSlots, this)(TRUE); + if (DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle <= 0) { + MACRO_CALL_MEMBER(Game::GameStateStructures_Func::calculateAttackVectorsToCampFireOfPlayer, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID); + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle = 1; + } + ++DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle; + if (1 < DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle) { + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle = 0; + } + MACRO_CALL_MEMBER(Util::Timing::Stopwatch_Func::stop, DAT_UnknownStopwatch::ptr)(); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks2.cpp b/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks2.cpp new file mode 100644 index 0000000000..f5c3cde6cb --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/aiRecomputeAttacks2.cpp @@ -0,0 +1,105 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Util/Timing/Stopwatch.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnknownStopwatch.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005200A0 + void TroopValueState::aiRecomputeAttacks2(BOOLEnum scanTentPoints, int attackID) + { + int const playerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID]; + BOOLEnum canKeepReachSignpost = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::canKeepReachSignpostZone, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, playerID, attackID); + MACRO_CALL_MEMBER(Util::Timing::Stopwatch_Func::start, DAT_UnknownStopwatch::ptr)(); + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].currentPlayerID_OR_attackedPlayerID_OR_one + = DAT_GameSynchronyState::instance.currentPlayerSlotID; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TileMapState::instance.AIZoneLayer), 0, DAT_TileMapState::instance.AIZoneLayer); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TileMapState::instance.AIInfoLayer), 0, DAT_TileMapState::instance.AIInfoLayer); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recountAttackTroopValue, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::computeAttackWaveTroopComposition, this)(); + int const _defaultSignpostTile = DAT_GameState::instance.mapAndTime.signpostEntryData[0].tile; + DAT_TroopValueState::instance.attackInfo.field127625_0x42744 = 0; + int _signpostTile + = DAT_GameState::instance.mapAndTime + .signpostEntryData[DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackID]] + .tile; + if (_signpostTile <= 0) { + DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackID] = 0; + _signpostTile = _defaultSignpostTile; + } + int const zone = DAT_TileMapState::instance.PathConnectionLayer[_signpostTile]; + if (canKeepReachSignpost) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeAIZoneLayer, + DAT_PathFindingState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, 1, zone); + } else { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeAIZoneLayer, + DAT_PathFindingState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, 0, zone); + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::updateWalkLayerAndAIPathCostLayer, + DAT_PathFindingState::ptr)(90, 0, 0, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::initializeAttackZoneSearch, this)(attackID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recomputeTargetedBuildingTilesArray, this)( + DAT_TroopValueState::instance.attackInfo.attacker); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recomputeAttackInfo, this)( + DAT_TroopValueState::instance.attackInfo.attacker, + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::computeSiegeSpotScores, this)( + DAT_GameSynchronyState::instance.currentPlayerSlotID, + DAT_TroopValueState::instance.attackInfo.attacker); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setScale3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setTown2, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailablePeopleValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableLordValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setGate2, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setWide3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setMoat3, this)(TRUE, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableHighValueSlots, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::countAvailableArcherValueSlots, this)(TRUE); + if (DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle <= 0) { + MACRO_CALL_MEMBER(Game::GameStateStructures_Func::calculateAttackVectorsToCampFireOfPlayer, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID); + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle = 1; + } + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle + = DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle + 1; + if (1 < DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle) { + DAT_TroopValueState::instance.attackInfo.attackVectorRecomputeThrottle = 0; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::expandAIZoneLayerStage1, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::expandAIZoneLayerStage2, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::expandAIZoneLayerStage3, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::clearAIInfoLayerOutsideStartZone, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::scanForArcherPoints, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::pruneStaleArcherPoints, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::scanForSupportPoints, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::pruneStaleSupportPoints, this)(); + if (scanTentPoints) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::scanForSiegeTentPoints, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::pruneStaleTentPoints, this)(); + } + MACRO_CALL_MEMBER(Util::Timing::Stopwatch_Func::stop, DAT_UnknownStopwatch::ptr)(); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/applyTribeBehaviorTypes.cpp b/src/OpenSHC/Map/Units/TroopValueState/applyTribeBehaviorTypes.cpp new file mode 100644 index 0000000000..74500340c8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/applyTribeBehaviorTypes.cpp @@ -0,0 +1,42 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00518870 + void TroopValueState::applyTribeBehaviorTypes( + SomeTribeBehaviorType tribeBehaviorType, SomeTribeBehaviorType tribeBehaviorType2, int off1, int off2) + { + int assignedCount = 0; + DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated = assignedCount; + for (int index = 0; index < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; ++index) { + int const tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[index]; + if (11 <= DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[index]) { + if (tribeBehaviorType2 != 0) { + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = tribeBehaviorType2; + } + } else { + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = (index + 1) * off2 + off1; + short const someCounter1 = DAT_TroopValueState::instance.attackInfo.someCounter1; + DAT_TribesState::instance.tribes[tribeID].someCounter1 = assignedCount; + ++assignedCount; + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = tribeBehaviorType; + DAT_TribesState::instance.tribes[tribeID].attackInfo_someCounter1 = someCounter1; + DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated = assignedCount; + } + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeAndLinkSupportTribe.cpp b/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeAndLinkSupportTribe.cpp new file mode 100644 index 0000000000..b5d1671e92 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeAndLinkSupportTribe.cpp @@ -0,0 +1,84 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" + +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00518A10 + void TroopValueState::assignBehaviorTypeAndLinkSupportTribe( + int param_1, int param_2, SomeTribeBehaviorType param_3, undefined4 param_4, undefined4 param_5) + { + if (param_2 <= 0) { + param_2 = 2; + } + for (int _tribeID = 1; _tribeID < 1250; ++_tribeID) { + if (DAT_TribesState::instance.tribes[_tribeID].owner == param_2) { + DAT_TribesState::instance.tribes[_tribeID].tribeID = 0; + } + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; ++i) { + int _tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[i]; + // The original stops at the first tribe that is too far along or still linked; it does not skip it. + if (11 <= DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[i]) { + return; + } + int _supportTribeID = (int)DAT_TribesState::instance.tribes[_tribeID].supportTribeID; + if (_supportTribeID + && DAT_TribesState::instance.tribes[_tribeID].uid2 + == DAT_TribesState::instance.tribes[_supportTribeID].uid) { + return; + } + // The original stores the update limit first, and both searches jump straight to the link. + DAT_TribesState::instance.tribes[_tribeID].someUpdateUpperLimit + = ((short)i + 1) * (short)param_5 + (short)param_4; + DAT_TribesState::instance.tribes[_tribeID].tribeBehaviorType = (undefined2)param_3; + DAT_TribesState::instance.tribes[_tribeID].attackInfo_someCounter1 + = (short)DAT_TroopValueState::instance.attackInfo.someCounter1; + DAT_TribesState::instance.tribes[_tribeID].unknownAttackRelatedUpdateCounter = 0; + for (_supportTribeID = 1; _supportTribeID < 1250; ++_supportTribeID) { + if (DAT_TribesState::instance.tribes[_supportTribeID].tribeState != 0 + && DAT_TribesState::instance.tribes[_supportTribeID].tribeState != 3 + && DAT_TribesState::instance.tribes[_supportTribeID].owner + == DAT_TribesState::instance.tribes[_tribeID].owner + && DAT_TribesState::instance.tribes[_supportTribeID].tribeID == 0 + && (DAT_TribesState::instance.tribes[_supportTribeID].tribeType == ((AITribeType)0x16) + || (DAT_TribesState::instance.tribes[_supportTribeID].tribeType == ((AITribeType)0x17)))) { + goto link; + } + } + for (_supportTribeID = 1; _supportTribeID < 1250; ++_supportTribeID) { + if (DAT_TroopValueState::instance.attackInfo.value3Array01[param_1] + == Map::Enums::AWS_DELAYED_TARGET_ASSIGNMENT) { + break; + } + if (DAT_TribesState::instance.tribes[_supportTribeID].tribeState != 0 + && DAT_TribesState::instance.tribes[_supportTribeID].tribeState != 3 + && DAT_TribesState::instance.tribes[_supportTribeID].owner + == DAT_TribesState::instance.tribes[_tribeID].owner + && DAT_TribesState::instance.tribes[_supportTribeID].tribeID == 0 + && (DAT_TribesState::instance.tribes[_supportTribeID].tribeType == AI::Tribes::AITT_ARCHERS + || (DAT_TribesState::instance.tribes[_supportTribeID].tribeType + == AI::Tribes::AITT_CROSSBOWMEN))) { + goto link; + } + } + continue; + link: + DAT_TribesState::instance.tribes[_tribeID].supportTribeID = (short)_supportTribeID; + DAT_TribesState::instance.tribes[_tribeID].uid2 = DAT_TribesState::instance.tribes[_supportTribeID].uid; + DAT_TribesState::instance.tribes[_supportTribeID].tribeID = (short)_tribeID; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeToNearbyTribes.cpp b/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeToNearbyTribes.cpp new file mode 100644 index 0000000000..307459478f --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/assignBehaviorTypeToNearbyTribes.cpp @@ -0,0 +1,49 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00518930 + void TroopValueState::assignBehaviorTypeToNearbyTribes( + SomeTribeBehaviorType tribeBehaviorType, undefined4 param_2, int upperLimitBase, int upperLimitStep) + { + DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated = 0; + for (int index = 0; index < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; ++index) { + int const tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[index]; + int const area = DAT_TileMapState::instance.PathConnectionLayer[DAT_UnitsState::instance + .units[DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID] + .tile]; + if (DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[index] < 11 + && MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, area, + DAT_TroopValueState::instance.attackInfo.someArea, 1)) { + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + = (index + 1) * upperLimitStep + upperLimitBase; + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = tribeBehaviorType; + DAT_TribesState::instance.tribes[tribeID].attackInfo_someCounter1 + = DAT_TroopValueState::instance.attackInfo.someCounter1; + DAT_TribesState::instance.tribes[tribeID].someCounter1 + = DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated; + DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated++; + if (DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated >= 5) { + return; + } + } + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp b/src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp new file mode 100644 index 0000000000..2e04e7c4bc --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/assignMacemenAndKnightsNotFromPlayer1ToTribes.cpp @@ -0,0 +1,137 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051CCB0 + void TroopValueState::assignMacemenAndKnightsNotFromPlayer1ToTribes() + { + int const playerID = DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID; + int macemenCount = 0; + int knightCount = 0; + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; unitID++) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_INVISIBLE + || !MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[unitID].owner)) { + continue; + } + if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_MACE) { + macemenCount = macemenCount + 1; + } else if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_KNIGHT) { + knightCount = knightCount + 1; + } else { + continue; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, DAT_TribesState::ptr)( + unitID, DAT_UnitsState::instance.units[unitID].tribeID); + } + int macemenLimit; + if (macemenCount > 40) { + macemenLimit = 10; + } else if (macemenCount > 20) { + macemenLimit = 8; + } else { + macemenLimit = 5; + } + int knightLimit; + if (knightCount > 18) { + knightLimit = 6; + } else if (knightCount > 12) { + knightLimit = 4; + } else { + knightLimit = 2; + } + if (macemenCount <= 0) { + DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 0; + } else if (macemenCount <= macemenLimit) { + DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 1; + } else { + DAT_TroopValueState::instance.attackInfo.macemenTribeCount = macemenCount / macemenLimit + 1; + if (DAT_TroopValueState::instance.attackInfo.macemenTribeCount > 100) { + DAT_TroopValueState::instance.attackInfo.macemenTribeCount = 100; + } + } + if (knightCount <= 0) { + DAT_TroopValueState::instance.attackInfo.knightTribeCount = 0; + } else if (knightCount <= knightLimit) { + DAT_TroopValueState::instance.attackInfo.knightTribeCount = 1; + } else { + DAT_TroopValueState::instance.attackInfo.knightTribeCount = knightCount / knightLimit + 1; + if (DAT_TroopValueState::instance.attackInfo.knightTribeCount > 100) { + DAT_TroopValueState::instance.attackInfo.knightTribeCount = 100; + } + } + int macemenUnitID = 0; + int tribeIndex; + for (tribeIndex = 0; tribeIndex < DAT_TroopValueState::instance.attackInfo.macemenTribeCount; + tribeIndex++) { + int const tribeID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(playerID, 0); + DAT_TroopValueState::instance.attackInfo.macemenTribeArray[tribeIndex] = tribeID; + DAT_TribesState::instance.tribes[tribeID].tribeType = AI::Tribes::AITT_MACEMEN; + for (int assigned = 0; assigned < macemenLimit;) { + macemenUnitID = macemenUnitID + 1; + if (DAT_UnitsState::instance.units[macemenUnitID].logicalState != Map::Units::ULS_INVISIBLE + && MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[macemenUnitID].owner) + && DAT_UnitsState::instance.units[macemenUnitID].unitType == Map::Units::UT_E_MACE) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)( + macemenUnitID, tribeID); + macemenCount = macemenCount - 1; + if (macemenCount <= 0) { + break; + } + assigned = assigned + 1; + } + } + if (macemenCount <= 0) { + break; + } + } + int knightUnitID = 0; + for (tribeIndex = 0; tribeIndex < DAT_TroopValueState::instance.attackInfo.knightTribeCount; tribeIndex++) { + int const tribeID + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(playerID, 0); + DAT_TroopValueState::instance.attackInfo.knightTribeArray[tribeIndex] = tribeID; + DAT_TribesState::instance.tribes[tribeID].tribeType = AI::Tribes::AITT_KNIGHTS; + for (int assigned = 0; assigned < knightLimit;) { + knightUnitID = knightUnitID + 1; + if (DAT_UnitsState::instance.units[knightUnitID].logicalState != Map::Units::ULS_INVISIBLE + && MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[knightUnitID].owner) + && DAT_UnitsState::instance.units[knightUnitID].unitType == Map::Units::UT_E_KNIGHT) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)( + knightUnitID, tribeID); + knightCount = knightCount - 1; + if (knightCount <= 0) { + return; + } + assigned = assigned + 1; + } + } + if (knightCount <= 0) { + break; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/assignTribeToSupportPoint.cpp b/src/OpenSHC/Map/Units/TroopValueState/assignTribeToSupportPoint.cpp new file mode 100644 index 0000000000..544e0062e0 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/assignTribeToSupportPoint.cpp @@ -0,0 +1,42 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B190 + int TroopValueState::assignTribeToSupportPoint(int x, int y, int tribeID) + { + int const pointIndex = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findSupportPointIndex, + DAT_PathFindingState::ptr)(200, x, y, tribeID); + if (pointIndex) { + short const oldPointIndex = DAT_TribesState::instance.tribes[tribeID].supportPointIndex; + DAT_TribesState::instance.tribes[tribeID].oldSupportPointIndex = oldPointIndex; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[oldPointIndex].tribeID = 0; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[oldPointIndex].tribeUID = 0; + DAT_TribesState::instance.tribes[tribeID].supportPointIndex = pointIndex; + int const pointY = DAT_TroopValueState::instance.attackInfo.supportPointsArray[pointIndex].y; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[pointIndex].tribeUID + = DAT_TribesState::instance.tribes[tribeID].uid; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[pointIndex].tribeID = tribeID; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[pointIndex].three = 3; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + DAT_TroopValueState::instance.attackInfo.supportPointsArray[pointIndex].x, pointY, 0, 0, + Map::Units::Instructions::UMSE_0); + return pointIndex; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/buildRallyPointsFromSiegeUnits.cpp b/src/OpenSHC/Map/Units/TroopValueState/buildRallyPointsFromSiegeUnits.cpp new file mode 100644 index 0000000000..19587e162a --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/buildRallyPointsFromSiegeUnits.cpp @@ -0,0 +1,64 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00519690 + void TroopValueState::buildRallyPointsFromSiegeUnits(int tribeID) + { + DAT_TribesState::instance.tribes[tribeID].rallyPointCount = 0; + for (int siegeTribeID = 1; siegeTribeID < countof(DAT_TribesState::instance.tribes); ++siegeTribeID) { + if (!DAT_TribesState::instance.tribes[siegeTribeID].tribeState) { + continue; + } + BOOLEnum const fullIDIsMinus1 + = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::isFullIDEqualsToMinus1, DAT_GameState::ptr)( + DAT_TribesState::instance.tribes[siegeTribeID].owner); + if (!fullIDIsMinus1) { + continue; + } + if (DAT_TribesState::instance.tribes[siegeTribeID].tribeType != AI::Tribes::AITT_CATAPULT + && DAT_TribesState::instance.tribes[siegeTribeID].tribeType != AI::Tribes::AITT_TREBUCHET) { + continue; + } + int const unitID = DAT_TribesState::instance.tribes[siegeTribeID].selectionTargetUnitID; + if (DAT_UnitsState::instance.units[unitID].stoneAmmunition <= 0) { + continue; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeNextRallyPointDestination, + DAT_PathFindingState::ptr)( + -1, DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y); + DAT_TribesState::instance.tribes[tribeID] + .rallyPointArray[DAT_TribesState::instance.tribes[tribeID].rallyPointCount][0] + = DAT_PathFindingState::instance.ALG_ResultX; + DAT_TribesState::instance.tribes[tribeID] + .rallyPointArray[DAT_TribesState::instance.tribes[tribeID].rallyPointCount][1] + = DAT_PathFindingState::instance.ALG_ResultY; + ++DAT_TribesState::instance.tribes[tribeID].rallyPointCount; + } + if (DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex < 0) { + DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex = 0; + } + if (DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex + >= DAT_TribesState::instance.tribes[tribeID].rallyPointCount) { + DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex = 0; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/calculateArch2ClosestToTribeTargetUnit.cpp b/src/OpenSHC/Map/Units/TroopValueState/calculateArch2ClosestToTribeTargetUnit.cpp new file mode 100644 index 0000000000..7ec2573cf8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/calculateArch2ClosestToTribeTargetUnit.cpp @@ -0,0 +1,58 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AB60 + int TroopValueState::calculateArch2ClosestToTribeTargetUnit(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int minDistance = 1000; + int bestIndex = -1; + int i = 0; + if (0 < DAT_TroopValueState::instance.attackInfo.arch2) { + for (; i < DAT_TroopValueState::instance.attackInfo.arch2; ++i) { + if (DAT_BuildingsState::instance + .buildings[DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[i].buildingID] + .unknownCounterTo10000_0x2b4 + >= 9999 + || DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[i].unitID) { + continue; + } + int const fromYPosition = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[i].tile2]; + int const fromXPosition = DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[bestIndex].unitID = unitID; + return DAT_TroopValueState::instance.attackInfo.arch2ValuesArray[bestIndex].tile2; + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/calculateHigh2ClostestToTribeTargetUnit.cpp b/src/OpenSHC/Map/Units/TroopValueState/calculateHigh2ClostestToTribeTargetUnit.cpp new file mode 100644 index 0000000000..f0858b391a --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/calculateHigh2ClostestToTribeTargetUnit.cpp @@ -0,0 +1,59 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A9F0 + int TroopValueState::calculateHigh2ClostestToTribeTargetUnit(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int minDistance = 1000; + int bestIndex = -1; + int i = 0; + if (0 < DAT_TroopValueState::instance.attackInfo.high2) { + for (; i < DAT_TroopValueState::instance.attackInfo.high2; ++i) { + if (DAT_BuildingsState::instance + .buildings[DAT_TroopValueState::instance.attackInfo.high2ValuesArray[i].buildingID] + .unknownCounterTo10000_0x2b4 + >= 9999 + || DAT_TroopValueState::instance.attackInfo.high2ValuesArray[i].unitID != 0) { + continue; + } + int const fromYPosition = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.high2ValuesArray[i].tile2]; + int const fromXPosition = DAT_TroopValueState::instance.attackInfo.high2ValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.high2ValuesArray[i].tile; + bestIndex = i; + } + } + if (bestIndex <= -1) { + return 0; + } + DAT_TroopValueState::instance.attackInfo.high2ValuesArray[bestIndex].unitID = unitID; + return DAT_TroopValueState::instance.attackInfo.high2ValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/calculateTile2MoatValueClosestToUnit.cpp b/src/OpenSHC/Map/Units/TroopValueState/calculateTile2MoatValueClosestToUnit.cpp new file mode 100644 index 0000000000..09112d12c8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/calculateTile2MoatValueClosestToUnit.cpp @@ -0,0 +1,58 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A780 + int TroopValueState::calculateTile2MoatValueClosestToUnit(int unitID) + { + int const owner = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 100000; + int bestIndex = -1; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moat2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].score + >= Map::Enums::SSS_UNUSABLE_THRESHOLD + || DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].unitID != 0) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + int const distance = DAT_DirectionAlgorithmState::instance.distanceHigh + + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].score * 8; + DAT_DirectionAlgorithmState::instance.distanceHigh = distance; + if (distance < minDistance) { + minDistance = distance; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[bestIndex].unitID + = unitID; + return DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].moatValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/calculateTile2PeoplValueClosestToUnit.cpp b/src/OpenSHC/Map/Units/TroopValueState/calculateTile2PeoplValueClosestToUnit.cpp new file mode 100644 index 0000000000..837966ce7c --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/calculateTile2PeoplValueClosestToUnit.cpp @@ -0,0 +1,52 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A140 + int TroopValueState::calculateTile2PeoplValueClosestToUnit(int unitID) + { + int minDistance = 1000; + int bestIndex = -1; + int i = 0; + if (0 < DAT_TroopValueState::instance.attackInfo.people2) { + for (; i < DAT_TroopValueState::instance.attackInfo.people2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.peopleValuesArray[i].unitID != 0) { + continue; + } + int const fromYPosition = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.peopleValuesArray[i].tile2]; + int const fromXPosition = DAT_TroopValueState::instance.attackInfo.peopleValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.peopleValuesArray[i].tile; + bestIndex = i; + } + } + if (bestIndex <= -1) { + return 0; + } + DAT_TroopValueState::instance.attackInfo.peopleValuesArray[bestIndex].unitID = unitID; + return DAT_TroopValueState::instance.attackInfo.peopleValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/claimArcherAttackPoint.cpp b/src/OpenSHC/Map/Units/TroopValueState/claimArcherAttackPoint.cpp new file mode 100644 index 0000000000..26345b9fc7 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/claimArcherAttackPoint.cpp @@ -0,0 +1,50 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AFF0 + int TroopValueState::claimArcherAttackPoint(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int pointIndex = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::tribeHasActiveLaddermanUnit, DAT_TribesState::ptr)(tribeID); + if (!pointIndex) { + pointIndex = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findArcherRelatedAttackInfoIndex, + DAT_PathFindingState::ptr)( + 200, DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, tribeID); + if (!pointIndex) { + return 0; + } + short const oldPointIndex = DAT_TribesState::instance.tribes[tribeID].archerRelated; + int const tribeUID = DAT_TribesState::instance.tribes[tribeID].uid; + DAT_TribesState::instance.tribes[tribeID].archerRelated2 = oldPointIndex; + DAT_TroopValueState::instance.attackInfo.archerPointArray[oldPointIndex].tribeID = 0; + DAT_TroopValueState::instance.attackInfo.archerPointArray[oldPointIndex].tribeUID = 0; + int const pointY = DAT_TroopValueState::instance.attackInfo.archerPointArray[pointIndex].y; + DAT_TroopValueState::instance.attackInfo.archerPointArray[pointIndex].tribeUID = tribeUID; + int const pointX = DAT_TroopValueState::instance.attackInfo.archerPointArray[pointIndex].x; + DAT_TribesState::instance.tribes[tribeID].archerRelated = pointIndex; + DAT_TroopValueState::instance.attackInfo.archerPointArray[pointIndex].tribeID = tribeID; + DAT_TroopValueState::instance.attackInfo.archerPointArray[pointIndex].claimCooldown = 3; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, pointX, pointY, 0, 0, Map::Units::Instructions::UMSE_0); + return pointIndex; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/collectArcherUnitsByLocation.cpp b/src/OpenSHC/Map/Units/TroopValueState/collectArcherUnitsByLocation.cpp new file mode 100644 index 0000000000..2a026588ec --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/collectArcherUnitsByLocation.cpp @@ -0,0 +1,87 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051CA50 + void TroopValueState::collectArcherUnitsByLocation() + { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2b57c), 0, + DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2b57c); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.tileArray_0x2bd50), 0, + DAT_TroopValueState::instance.attackInfo.tileArray_0x2bd50); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2c070), 0, + DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2c070); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.uidArray_0x2c200), 0, + DAT_TroopValueState::instance.attackInfo.uidArray_0x2c200); + DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c = 0; + DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520 = 0; + for (int _unitID = 1; _unitID < DAT_UnitsState::instance.maxUnitCount; ++_unitID) { + if (DAT_UnitsState::instance.units[_unitID].logicalState != Map::Units::ULS_INVISIBLE + && MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[_unitID].owner) + && (DAT_UnitsState::instance.units[_unitID].unitType == Map::Units::UT_E_ARCHER + || DAT_UnitsState::instance.units[_unitID].unitType == Map::Units::UT_E_XBOW)) { + if (DAT_TileMapState::instance.LogicLayer[DAT_UnitsState::instance.units[_unitID].tile] + & (L_KEEP_NON_MANOR_HOUSE | L_WALL_OR_GATEHOUSE)) { + if (DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520 + < countof(DAT_TroopValueState::instance.attackInfo.tileArray_0x2bd50)) { + int const _tribeID = DAT_UnitsState::instance.units[_unitID].tribeID; + if (_tribeID == 0 || DAT_TribesState::instance.tribes[_tribeID].isRallyingUnk == 0) { + DAT_TroopValueState::instance.attackInfo + .tileArray_0x2bd50[DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520] + = DAT_UnitsState::instance.units[_unitID].tile; + DAT_TroopValueState::instance.attackInfo + .uidArray_0x2c200[DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520] + = DAT_UnitsState::instance.units[_unitID].uid; + DAT_TroopValueState::instance.attackInfo + .unitIDArray_0x2c070[DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520] + = (short)_unitID; + DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520 + = DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520 + 1; + } + } + } else { + if (DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c + < countof(DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2b57c)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, DAT_TribesState::ptr)( + _unitID, DAT_UnitsState::instance.units[_unitID].tribeID); + DAT_TroopValueState::instance.attackInfo + .unitIDArray_0x2b57c[DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c] + = (short)_unitID; + DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c + = DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c + 1; + } + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/commandUnitsToMoveToKeep.cpp b/src/OpenSHC/Map/Units/TroopValueState/commandUnitsToMoveToKeep.cpp new file mode 100644 index 0000000000..17b64949dc --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/commandUnitsToMoveToKeep.cpp @@ -0,0 +1,123 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Buildings/BuildingTypeShort.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Buildings::BuildingType; + using Map::Buildings::BuildingTypeShort; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051C800 + void TroopValueState::commandUnitsToMoveToKeep() + { + uint y1; + uint x1; + int _playerID = DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID; + uint _keep = DAT_GameState::instance.playerDataArray[_playerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id; + if (0 < (int)_keep) { + BuildingTypeShort _buildingType = DAT_BuildingsState::instance.buildings[_keep].buildingType; + if (_buildingType == Map::Buildings::BT_MANORHOUSE) { + x1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].x + 3; + y1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].y + 8; + } else if (_buildingType == Map::Buildings::BT_STONEKEEP) { + x1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].x + 3; + y1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].y + 3; + } else if (_buildingType == Map::Buildings::BT_STRONGHOLD) { + x1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].x + 3; + y1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].y + 3; + } else if (_buildingType == Map::Buildings::BT_KEEPFOUR) { + x1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].x + 4; + y1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].y + 4; + } else if (_buildingType == Map::Buildings::BT_KEEPFIVE) { + x1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].x + 5; + y1 = (int)(short)DAT_BuildingsState::instance.buildings[_keep].y + 5; + } else { + return; + } + int _swordsTribe = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)( + DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID, 0); + DAT_TribesState::instance.tribes[_swordsTribe].tribeType = AI::Tribes::AITT_SWORDSMEN; + int _archersTribe + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(_playerID, 0); + DAT_TribesState::instance.tribes[_archersTribe].tribeType = AI::Tribes::AITT_ARCHERS; + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_INVISIBLE) { + continue; + } + if (!MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[unitID].owner)) { + continue; + } + if (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_MACE + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_SWORD + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_SPEAR) { + if (DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_ARCHER + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_XBOW) { + continue; + } + if (DAT_TileMapState::instance.LogicLayer[DAT_UnitsState::instance.units[unitID].tile] + & 0x10000100U) { + continue; + } + } + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, x1, y1); + if (DAT_DirectionAlgorithmState::instance.distanceHigh >= 30) { + continue; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::removeUnitFromTribe, DAT_TribesState::ptr)( + unitID, DAT_UnitsState::instance.units[unitID].tribeID); + if (DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_SWORD) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)( + unitID, _swordsTribe); + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)( + unitID, _archersTribe); + } + } + if (DAT_TribesState::instance.tribes[_swordsTribe].size <= 0) { + DAT_TribesState::instance.tribes[_swordsTribe].tribeState = 3; + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + _swordsTribe, x1, y1 + 1, 0, 0, Map::Units::Instructions::UMSE_0); + } + if (DAT_TribesState::instance.tribes[_archersTribe].size <= 0) { + DAT_TribesState::instance.tribes[_archersTribe].tribeState = 3; + } else { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + _archersTribe, x1, y1, 0, 0, Map::Units::Instructions::UMSE_0); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/computeAttackWaveTroopComposition.cpp b/src/OpenSHC/Map/Units/TroopValueState/computeAttackWaveTroopComposition.cpp new file mode 100644 index 0000000000..53e1d74613 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/computeAttackWaveTroopComposition.cpp @@ -0,0 +1,204 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519310 + void TroopValueState::computeAttackWaveTroopComposition() + { + if (DAT_TroopValueState::instance.attackInfo.attackTribeCount < 6) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen; + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 1; + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 1; + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 1; + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 1; + DAT_TroopValueState::instance.attackInfo.maxLadderTribes = 0; + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 0; + DAT_TroopValueState::instance.attackInfo.maxEngineerTribes + = DAT_TroopValueState::instance.attackInfo.engineers; + } else if (DAT_TroopValueState::instance.attackInfo.attackTribeCount < 10) { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 0) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 0; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 1) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 1; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen >= 3) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 2; + } else { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen - 1; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore != 0) { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 2; + } else { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 1; + } + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen > 4) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore != 0) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 1; + } else { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 0; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 0) { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 1; + } else { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 0; + } + DAT_TroopValueState::instance.attackInfo.maxLadderTribes = 2; + if (DAT_TroopValueState::instance.attackInfo.laddermen <= 2) { + DAT_TroopValueState::instance.attackInfo.maxLadderTribes + = DAT_TroopValueState::instance.attackInfo.laddermen; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore != 0) { + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 1; + } else { + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 0; + } + DAT_TroopValueState::instance.attackInfo.maxEngineerTribes + = DAT_TroopValueState::instance.attackInfo.engineers; + } else if (DAT_TroopValueState::instance.attackInfo.attackTribeCount < 20) { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 0) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 0; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 1) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 1; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen >= 4) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen - 1; + } + DAT_TroopValueState::instance.attackInfo.maxLadderTribes = 4; + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 1) { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen > 6) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 4; + } else { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 3; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 4) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 0) { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 2; + } else { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 1; + } + if (DAT_TroopValueState::instance.attackInfo.laddermen <= 4) { + DAT_TroopValueState::instance.attackInfo.maxLadderTribes + = DAT_TroopValueState::instance.attackInfo.laddermen; + } + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 1; + DAT_TroopValueState::instance.attackInfo.maxEngineerTribes + = DAT_TroopValueState::instance.attackInfo.engineers; + } else if (DAT_TroopValueState::instance.attackInfo.attackTribeCount < 30) { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 0) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 0; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 1) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 1; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen >= 4) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen - 1; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 4) { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 4; + } else { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 10) { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen > 5) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 4; + } else { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 2; + } + } else { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 6; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 8) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 5; + } else if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 4) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 4; + } else { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 2; + } + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 2; + DAT_TroopValueState::instance.attackInfo.maxLadderTribes = 5; + if (DAT_TroopValueState::instance.attackInfo.laddermen <= 5) { + DAT_TroopValueState::instance.attackInfo.maxLadderTribes + = DAT_TroopValueState::instance.attackInfo.laddermen; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 4) { + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 2; + } else { + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 1; + } + DAT_TroopValueState::instance.attackInfo.maxEngineerTribes + = DAT_TroopValueState::instance.attackInfo.engineers; + } else { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 0) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 0; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen <= 1) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 1; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen >= 5) { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes = 4; + } else { + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen - 1; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 5) { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 5; + } else { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen > 12) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 8; + } else if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen > 6) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 5; + } else { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes = 3; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 10) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 6; + } else if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 8) { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 5; + } else { + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes = 3; + } + DAT_TroopValueState::instance.attackInfo.maxLadderTribes = 6; + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore > 0) { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 3; + } else { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes = 2; + } + if (DAT_TroopValueState::instance.attackInfo.laddermen <= 6) { + DAT_TroopValueState::instance.attackInfo.maxLadderTribes + = DAT_TroopValueState::instance.attackInfo.laddermen; + } + DAT_TroopValueState::instance.attackInfo.maxTunnelerTribes = 2; + DAT_TroopValueState::instance.attackInfo.maxEngineerTribes + = DAT_TroopValueState::instance.attackInfo.engineers; + } + if (DAT_TroopValueState::instance.attackInfo.assassinTribeCount + > DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes) { + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes + = DAT_TroopValueState::instance.attackInfo.assassinTribeCount; + } + DAT_TroopValueState::instance.attackInfo.maxLordAttackTribes = 3; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/computeSiegeSpotScores.cpp b/src/OpenSHC/Map/Units/TroopValueState/computeSiegeSpotScores.cpp new file mode 100644 index 0000000000..0acac15b94 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/computeSiegeSpotScores.cpp @@ -0,0 +1,186 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519990 + void TroopValueState::computeSiegeSpotScores(int attackedPlayerID, int attackingPlayerID) + { + if (DAT_GameState::instance.playerDataArray[attackedPlayerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .id + == 0) { + return; + } + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].someMinimumDistance = 10000; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hack2; ++i) { + int const cost + = DAT_TileMapState::instance + .SEC_PathfindingCostTileMap1105[attackedPlayerID][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .hackValuesArray[i] + .tile]; + if (cost + < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].someMinimumDistance) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].someMinimumDistance + = cost; + } + } + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hack3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hack2; ++i) { + int const tile = DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1] + .hackValuesArray[i] + .tile; + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const cost = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[attackedPlayerID][tile]; + if (MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::ifAnyUnitOnSameTileIsLadderInRightDirection, + DAT_UnitsState::ptr)(tile, y)) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hackValuesArray[i].score + = 9999; + } else if (cost <= 0) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hackValuesArray[i].score + = 9999; + } else { + int const minimumDistance + = DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1] + .someMinimumDistance; + if (minimumDistance >= 50) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1] + .hackValuesArray[i] + .score = 9999; + } else if (cost >= minimumDistance + 15) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1] + .hackValuesArray[i] + .score = 9999; + } else { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].hack3++; + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1] + .hackValuesArray[i] + .score = cost; + } + } + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].scale2; + ++i) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].scaleValuesArray[i].score + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .scaleValuesArray[i] + .tile] + > 5 + ? 9999 + : 0; + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].town2; ++i) { + int const cost + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .townValuesArray[i] + .tile]; + if (cost <= 0) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].townValuesArray[i].score + = 100; + } else { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].townValuesArray[i].score + = cost; + } + } + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].field22_0x3ea4 = 10000; + for (int i = 0; + i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].some1000ArrayIndex_2; + ++i) { + int const cost + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .gateValuesArray[i] + .tile]; + if (cost < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].field22_0x3ea4) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].field22_0x3ea4 = cost; + } + } + for (int i = 0; + i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].some1000ArrayIndex_2; + ++i) { + int const cost + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .gateValuesArray[i] + .tile]; + if (DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].field22_0x3ea4 > 10) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].gateValuesArray[i].score + = cost > 20 ? 9999 : 0; + } else { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].gateValuesArray[i].score + = cost > 10 ? 9999 : 0; + } + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].wide2; ++i) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].wideValuesArray[i].score + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .wideValuesArray[i] + .tile] + > 5 + ? 9999 + : 0; + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moat2; ++i) { + int const cost + = DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one][DAT_TroopValueState::instance.attackInfo + .playerInfo[attackingPlayerID - 1] + .moatValuesArray[i] + .tile]; + if (cost <= 0) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = 9999; + } else if (cost <= 10) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost; + } else if (cost <= 15) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost * 2; + } else if (cost <= 20) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost * 3; + } else if (cost <= 25) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost * 4; + } else if (cost <= 50) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost * 5; + } else if (cost <= 70) { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = cost * 6; + } else { + DAT_TroopValueState::instance.attackInfo.playerInfo[attackingPlayerID - 1].moatValuesArray[i].score + = 9999; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/countAvailableArcherValueSlots.cpp b/src/OpenSHC/Map/Units/TroopValueState/countAvailableArcherValueSlots.cpp index 523cd0eddc..5e405a05aa 100644 --- a/src/OpenSHC/Map/Units/TroopValueState/countAvailableArcherValueSlots.cpp +++ b/src/OpenSHC/Map/Units/TroopValueState/countAvailableArcherValueSlots.cpp @@ -8,7 +8,7 @@ namespace Map { namespace Units { // FUNCTION: STRONGHOLDCRUSADER 0x0051AB10 - void TroopValueState::countAvailableArcherValueSlots(int includeOccupied) + void TroopValueState::countAvailableArcherValueSlots(BOOLEnum includeOccupied) { DAT_TroopValueState::instance.attackInfo.arch3 = 0; for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.arch2; ++i) { diff --git a/src/OpenSHC/Map/Units/TroopValueState/countAvailableHighValueSlots.cpp b/src/OpenSHC/Map/Units/TroopValueState/countAvailableHighValueSlots.cpp index 7d7b670c97..9c6ce62355 100644 --- a/src/OpenSHC/Map/Units/TroopValueState/countAvailableHighValueSlots.cpp +++ b/src/OpenSHC/Map/Units/TroopValueState/countAvailableHighValueSlots.cpp @@ -8,7 +8,7 @@ namespace Map { namespace Units { // FUNCTION: STRONGHOLDCRUSADER 0x0051A9A0 - void TroopValueState::countAvailableHighValueSlots(int includeOccupied) + void TroopValueState::countAvailableHighValueSlots(BOOLEnum includeOccupied) { DAT_TroopValueState::instance.attackInfo.high3 = 0; for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.high2; ++i) { diff --git a/src/OpenSHC/Map/Units/TroopValueState/countAvailableLordValueSlots.cpp b/src/OpenSHC/Map/Units/TroopValueState/countAvailableLordValueSlots.cpp index b2066a48e1..55ba543430 100644 --- a/src/OpenSHC/Map/Units/TroopValueState/countAvailableLordValueSlots.cpp +++ b/src/OpenSHC/Map/Units/TroopValueState/countAvailableLordValueSlots.cpp @@ -7,7 +7,7 @@ namespace Map { namespace Units { // FUNCTION: STRONGHOLDCRUSADER 0x0051A100 - void TroopValueState::countAvailableLordValueSlots(int includeOccupied) + void TroopValueState::countAvailableLordValueSlots(BOOLEnum includeOccupied) { DAT_TroopValueState::instance.attackInfo.lord2 = 0; for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.lord3; ++i) { diff --git a/src/OpenSHC/Map/Units/TroopValueState/countAvailablePeopleValueSlots.cpp b/src/OpenSHC/Map/Units/TroopValueState/countAvailablePeopleValueSlots.cpp new file mode 100644 index 0000000000..63df8a10b6 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/countAvailablePeopleValueSlots.cpp @@ -0,0 +1,24 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A0C0 + void TroopValueState::countAvailablePeopleValueSlots(BOOLEnum includeOccupied) + { + DAT_TroopValueState::instance.attackInfo.people3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.people2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.peopleValuesArray[i].unitID == 0 || includeOccupied) { + ++DAT_TroopValueState::instance.attackInfo.people3; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/decideAndExecuteTribeAttackAction.cpp b/src/OpenSHC/Map/Units/TroopValueState/decideAndExecuteTribeAttackAction.cpp new file mode 100644 index 0000000000..762d0c3e74 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/decideAndExecuteTribeAttackAction.cpp @@ -0,0 +1,79 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D2B0 + void TroopValueState::decideAndExecuteTribeAttackAction(int tribeID) + { + int const targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int const enemyBuildingID + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findNearestEnemyBuildingWithinDistance, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, 15); + int const enemyUnitID + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findNearestEnemyUnitWithinDistance, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, 10); + if (enemyBuildingID && (SEC_RNG::instance.currentNumber2 & 1)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)(tribeID, + Map::Units::UIT_ATTACK_BUILDING, enemyBuildingID, + DAT_BuildingsState::instance.buildings[enemyBuildingID].uid, 0); + return; + } + if (enemyUnitID && (SEC_RNG::instance.currentNumber2 & 1)) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)(tribeID, + Map::Units::UIT_UNIT_ATTACK_UNIT, enemyUnitID, DAT_UnitsState::instance.units[enemyUnitID].uid, 0); + return; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)( + tribeID, Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk, 0, 0, 0); + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 1); + int targetTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findBestAttackTileByPathCost, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, DAT_UnitsState::instance.units[targetUnitID].owner, 6); + if (targetTile <= 0) { + targetTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findBestAttackTileByPathCost, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, DAT_UnitsState::instance.units[targetUnitID].owner, + 30); + if (targetTile <= 0) { + targetTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findBestAttackTileByPathCost, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, + DAT_UnitsState::instance.units[targetUnitID].owner, 50); + } + } + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 0); + if (0 < targetTile) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + targetTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[targetTile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[targetTile], 0, 0, + Map::Units::Instructions::UMSE_0); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/decrementTileMap1104.cpp b/src/OpenSHC/Map/Units/TroopValueState/decrementTileMap1104.cpp new file mode 100644 index 0000000000..2d4eaeb662 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/decrementTileMap1104.cpp @@ -0,0 +1,37 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D5D0 + void TroopValueState::decrementTileMap1104() + { + for (int offset = 0; offset < 200; offset += 5) { + for (int i = 0; i < 5; ++i) { + uchar const value + = DAT_TileMapState::instance + .SEC_TileMap1104[DAT_TroopValueState::instance.attackInfo.tilemapOffset + offset + i]; + if (0 < value) { + DAT_TileMapState::instance + .SEC_TileMap1104[DAT_TroopValueState::instance.attackInfo.tilemapOffset + offset + i] + = value - 1; + } + } + } + if (DAT_TroopValueState::instance.attackInfo.tilemapOffset < MC_MAP_TILES_COUNT - 200) { + DAT_TroopValueState::instance.attackInfo.tilemapOffset += 200; + } else { + DAT_TroopValueState::instance.attackInfo.tilemapOffset = 0; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/executeAttackWaveTargetAssignment.cpp b/src/OpenSHC/Map/Units/TroopValueState/executeAttackWaveTargetAssignment.cpp new file mode 100644 index 0000000000..9d075d50a3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/executeAttackWaveTargetAssignment.cpp @@ -0,0 +1,289 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode2; + using Map::MapType2; + using Map::Units::SomeTribeBehaviorType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005205A0 + void TroopValueState::executeAttackWaveTargetAssignment(int param_1) + { + int _tribeSizeSumLimit; + int _siegeArcherBudget = 0; + int _siegeHackBudget = 0; + int _playerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[param_1]; + if ((DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk) + && (DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE)) { + _playerID = 2; + _siegeArcherBudget = 5; + _siegeHackBudget = -1; + } + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] + = DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] + 1; + if (DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] < DAT_AttackInfoDefinedData::instance + .AIAttackWaveTargetDelay[DAT_TroopValueState::instance.attackInfo.attacker]) { + return; + } + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] = 0; + DAT_TroopValueState::instance.attackInfo.value10++; + BOOLEnum _canReachSignpostZone = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::canKeepReachSignpostZone, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, _playerID, param_1); + if ((_canReachSignpostZone) && (DAT_TroopValueState::instance.attackInfo.attacker != 8)) { + DAT_TroopValueState::instance.attackInfo.value3Array01[param_1] + = Map::Enums::AWS_DELAYED_TARGET_ASSIGNMENT; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] = 10000; + DAT_TroopValueState::instance.attackInfo.attackWaveSignpostShorts[param_1] = 0; + return; + } + DAT_TroopValueState::instance.attackInfo.someCounter1 + = DAT_TroopValueState::instance.attackInfo.someCounter1 + 1; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::aiRecomputeAttacks2, this)(FALSE, param_1); + if (DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].field22_0x3ea4 + < DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].someMinimumDistance) { + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes + = DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes + 1; + } + if (DAT_TroopValueState::instance.attackInfo.attacker == 8) { + if (10 <= DAT_TroopValueState::instance.attackInfo.someCounter1) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] = 0; + DAT_TroopValueState::instance.attackInfo.value3Array01[param_1] = Map::Enums::AWS_ENDED; + MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::handleBattleEndMusicTransition, DAT_SoundSystemState::ptr)(); + return; + } + _canReachSignpostZone = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::canKeepReachSignpostZone, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, _playerID, param_1); + if (_canReachSignpostZone + && DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].gate2 != 0) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x3f6); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f6, Map::Units::STBT_1, 0, 10); + } + _tribeSizeSumLimit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].town3; + if (_tribeSizeSumLimit) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, _tribeSizeSumLimit, Map::Units::STBT_0x3f5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f5, Map::Units::STBT_1, 0, 0x14); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + 1000, DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].hack3, + Map::Units::STBT_0x3f2); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f2, Map::Units::STBT_1, 0, 10); + return; + } + DAT_TroopValueState::instance.attackInfo.reservedTroopBudget = 0; + if (DAT_TroopValueState::instance.attackInfo.catapults) { + DAT_TroopValueState::instance.attackInfo.reservedTroopBudget + = DAT_AttackInfoDefinedData::instance + .AIReservedTroopBudget[DAT_TroopValueState::instance.attackInfo.attacker]; + if (DAT_TroopValueState::instance.attackInfo.zoneSize < 3000) { + DAT_TroopValueState::instance.attackInfo.reservedTroopBudget + = DAT_TroopValueState::instance.attackInfo.reservedTroopBudget + 10; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1, 10000, Map::Units::STBT_0x401); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x401, Map::Units::STBT_1, 0, 0x32); + } + if ((DAT_TroopValueState::instance.attackInfo.lord2) + && (2 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget)) { + if (DAT_TroopValueState::instance.attackInfo.attacker == 4) { + DAT_TroopValueState::instance.attackInfo.maxLordAttackTribes + = DAT_TroopValueState::instance.attackInfo.maxLordAttackTribes * 2; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxLordAttackTribes, 1000, Map::Units::STBT_0x3fd); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::assignBehaviorTypeToNearbyTribes, this)( + Map::Units::STBT_0x3fd, 1, 0, 1); + } + if (DAT_TroopValueState::instance.attackInfo.attacker == 3) { + DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].town3 + = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].town3 * 2; + } + int _limit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].town3; + if ((_limit) + && (2 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxTownAttackTribes, _limit, Map::Units::STBT_0x3f5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f5, Map::Units::STBT_1, 0, 0x14); + } + _canReachSignpostZone + = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::canKeepReachSignpostZoneViaPathfinder, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, _playerID, param_1); + if (_canReachSignpostZone) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxWallScaleTribes, 10000, Map::Units::STBT_0x3fa); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_7, Map::Units::STBT_1, 10, 0x14); + } + if (0 < DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].moat3 + && 2 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget) { + if (DAT_TroopValueState::instance.attackInfo.attacker == 4) { + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes + = DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes / 2; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxMoatAttackTribes, + DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].moat3, Map::Units::STBT_0x3f7); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f7, Map::Units::STBT_1, 10, 10); + DAT_TroopValueState::instance.attackInfo.tribesToSortCount + = DAT_TroopValueState::instance.attackInfo.unknownTribeCounterRelated; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x418); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x418, Map::Units::STBT_1, 0x14, 0x46); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x419); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x419, Map::Units::STBT_1, 0x14, 0x46); + if (DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].gate2 != 0) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 2, 10000, Map::Units::STBT_0x41a); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x41a, Map::Units::STBT_1, 10, 0x1e); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x41b); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x41b, Map::Units::STBT_1, 0x14, 0x14); + if ((0 < DAT_TroopValueState::instance.attackInfo.archerPoints) + && (DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget + > _siegeArcherBudget + 1)) { + DAT_TroopValueState::instance.attackInfo.attackWaveRetargetCount + = DAT_TroopValueState::instance.attackInfo.attackWaveRetargetCount + 1; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, DAT_TroopValueState::instance.attackInfo.archerPoints, 10000, Map::Units::STBT_0x3fc); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fc, Map::Units::STBT_1, 0, 5); + } + if (DAT_TroopValueState::instance.attackInfo.people3) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxPeopleAttackTribes, + DAT_TroopValueState::instance.attackInfo.people3, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fb, Map::Units::STBT_1, 5, 0x1e); + } + if (DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].gate2 != 0 + && 3 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + DAT_TroopValueState::instance.attackInfo.maxGateAttackTribes, 10000, Map::Units::STBT_0x3f6); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f6, Map::Units::STBT_1, 0, 10); + } + _limit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].wide3; + if ((_limit) + && (3 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, _limit, 10000, Map::Units::STBT_0x413); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x413, Map::Units::STBT_1, 0, 0x28); + } + _limit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].wide3; + if (_limit) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, _limit, 10000, Map::Units::STBT_0x414); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x414, Map::Units::STBT_1, 0, 0x28); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x411); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::assignBehaviorTypeAndLinkSupportTribe, this)( + param_1, _playerID, Map::Units::STBT_0x411, 0, 0x14); + if ((5 < DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_L) + && (2 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget)) { + _tribeSizeSumLimit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].scale3; + if (1 < _tribeSizeSumLimit) { + if (_tribeSizeSumLimit <= 2) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1, 10000, Map::Units::STBT_0x3f4); + } else if (_tribeSizeSumLimit <= 6) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 2, 10000, Map::Units::STBT_0x3f4); + } else if (_tribeSizeSumLimit <= 12) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 3, 10000, Map::Units::STBT_0x3f4); + } else { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 4, 10000, Map::Units::STBT_0x3f4); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f4, Map::Units::STBT_5, 0, 0x19); + } + if (DAT_TroopValueState::instance.attackInfo.tribesToSortCount) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, DAT_TroopValueState::instance.attackInfo.tribesToSortCount, 10000, + Map::Units::STBT_0x3f4); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fc, Map::Units::STBT_1, 0, 0x14); + } + } + if (DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget + > _siegeHackBudget + 4) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)(param_1, + 1000, DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].hack3, + Map::Units::STBT_0x3f2); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f2, ((SomeTribeBehaviorType)0), 0, 10); + } + _tribeSizeSumLimit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].moat3; + if ((0 < _tribeSizeSumLimit) + && (4 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, _tribeSizeSumLimit, Map::Units::STBT_0x3f7); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f7, Map::Units::STBT_1, 10, 5); + } + if (DAT_TroopValueState::instance.attackInfo.knights + && DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].gate2 != 0 + && 3 < DAT_TroopValueState::instance.attackInfo.value10 + - DAT_TroopValueState::instance.attackInfo.reservedTroopBudget) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x417); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f6, Map::Units::STBT_1, 0, 10); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + param_1, 1000, 10000, Map::Units::STBT_0x41e); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x41e, Map::Units::STBT_1, 10, 0x28); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/executeDelayedAttackWaveTargetAssignment.cpp b/src/OpenSHC/Map/Units/TroopValueState/executeDelayedAttackWaveTargetAssignment.cpp new file mode 100644 index 0000000000..08a7e660a5 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/executeDelayedAttackWaveTargetAssignment.cpp @@ -0,0 +1,115 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Game::GameMode2; + using Map::MapType2; + using Map::Units::SomeTribeBehaviorType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00520CD0 + void TroopValueState::executeDelayedAttackWaveTargetAssignment(int attackID) + { + int _playerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID]; + if ((DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk) + && (DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE)) { + _playerID = 2; + } + DAT_TroopValueState::instance.attackInfo.delayedWaveAssignmentCount + = DAT_TroopValueState::instance.attackInfo.delayedWaveAssignmentCount + 1; + BOOLEnum _canReachSignpostZone = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::canKeepReachSignpostZone, + DAT_GameState::ptr)(DAT_GameSynchronyState::instance.currentPlayerSlotID, _playerID, attackID); + if (!_canReachSignpostZone) { + DAT_TroopValueState::instance.attackInfo.value3Array01[attackID] = Map::Enums::AWS_ASSIGNING_TARGETS; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] = 0; + return; + } + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] + = DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] + 1; + if (400 <= DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID]) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] = 0; + DAT_TroopValueState::instance.attackInfo.someCounter1 + = DAT_TroopValueState::instance.attackInfo.someCounter1 + 1; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::aiRecomputeAttacks, this)( + DAT_TroopValueState::instance.attackInfo.attackWaveSignpostShorts[attackID]++, attackID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_0x41b); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x41b, Map::Units::STBT_1, 50, 50); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_0x418); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x418, Map::Units::STBT_1, 20, 70); + if ((DAT_TroopValueState::instance.attackInfo.people3) + && (10 <= DAT_TroopValueState::instance.attackInfo.attackTribeCount)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1, DAT_TroopValueState::instance.attackInfo.people3, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fb, Map::Units::STBT_1, 5, 15); + } + if (DAT_TroopValueState::instance.attackInfo.lord2) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, DAT_TroopValueState::instance.attackInfo.maxLordAttackTribes, 1000, + Map::Units::STBT_0x3fd); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::assignBehaviorTypeToNearbyTribes, this)( + Map::Units::STBT_0x3fd, 1, 0, 5); + } + if (DAT_TroopValueState::instance.attackInfo.high3) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, DAT_TroopValueState::instance.attackInfo.high3, 10000, Map::Units::STBT_0x3f8); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f8, Map::Units::STBT_1, 20, 10); + } + if (DAT_TroopValueState::instance.attackInfo.arch3) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, DAT_TroopValueState::instance.attackInfo.arch3, 10000, Map::Units::STBT_0x3f9); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f9, Map::Units::STBT_1, 20, 10); + } + if (DAT_TroopValueState::instance.attackInfo.people3) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 2, DAT_TroopValueState::instance.attackInfo.people3, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fb, Map::Units::STBT_1, 0, 10); + } + int tribeSizeSumLimit = DAT_TroopValueState::instance.attackInfo.playerInfo[_playerID - 1].town3; + if (tribeSizeSumLimit) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1, tribeSizeSumLimit, Map::Units::STBT_0x3f5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f5, Map::Units::STBT_1, 20, 10); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_0x411); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::assignBehaviorTypeAndLinkSupportTribe, this)( + attackID, _playerID, Map::Units::STBT_0x411, 0, 10); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_7); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_7, Map::Units::STBT_1, 0, 5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 1000, 10000, Map::Units::STBT_0x416); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x416, Map::Units::STBT_1, 0, 5); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/exitSiegeEquipmentForWave.cpp b/src/OpenSHC/Map/Units/TroopValueState/exitSiegeEquipmentForWave.cpp new file mode 100644 index 0000000000..518596ca78 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/exitSiegeEquipmentForWave.cpp @@ -0,0 +1,52 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" + +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::States::UnitState; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BD10 + void TroopValueState::exitSiegeEquipmentForWave(int wave) + { + for (int _tribeID = 1; _tribeID < 1250; ++_tribeID) { + if (DAT_TribesState::instance.tribes[_tribeID].tribeState + && DAT_TribesState::instance.tribes[_tribeID].attackWave == wave + && (DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x13 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x14 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x15 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x16 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x17 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x18)) { + for (int _tribeUnitIndex = 0; _tribeUnitIndex < DAT_TribesState::instance.tribes[_tribeID].size;) { + int const _unitID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::getUnitIDForIndexInTribe, + DAT_TribesState::ptr)(_tribeID, _tribeUnitIndex); + _tribeUnitIndex++; + if (DAT_UnitsState::instance.units[_unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[_unitID].dying == 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, + DAT_TribesState::ptr)(_tribeID, Map::Units::UIT_EXIT_SIEGE_EQUIPMENT, _unitID, + DAT_UnitsState::instance.units[_unitID].uid, 0); + DAT_UnitsState::instance.units[_unitID].state.generic = Map::Units::States::US_DISAPPEAR; + } + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage1.cpp b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage1.cpp new file mode 100644 index 0000000000..b24b25612f --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage1.cpp @@ -0,0 +1,74 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Enums/AIInfoFlag.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051ACE0 + void TroopValueState::expandAIZoneLayerStage1() + { + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] != Map::Enums::AIF_RING_1) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + for (int direction = 0; direction < 4; ++direction) { + if (!(DAT_TileMapState::instance.AIInfoLayer[tile + + DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2]] + & Map::Enums::AIF_ANY_RING)) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(6, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.xOffset + + x, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.yOffset + + y, + Map::Enums::AIF_RING_2); + break; + } + } + } + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] == Map::Enums::AIF_RING_2) { + DAT_TileMapState::instance.AIInfoLayer[tile] = Map::Enums::AIF_RING_1; + } + } + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] != Map::Enums::AIF_RING_1) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + for (int direction = 0; direction < 4; ++direction) { + if (!(DAT_TileMapState::instance.AIInfoLayer[tile + + DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2]] + & Map::Enums::AIF_ANY_RING)) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(10, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.xOffset + + x, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.yOffset + + y, + Map::Enums::AIF_RING_2); + break; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage2.cpp b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage2.cpp new file mode 100644 index 0000000000..e64a5e3bb9 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage2.cpp @@ -0,0 +1,45 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Enums/AIInfoFlag.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AE00 + void TroopValueState::expandAIZoneLayerStage2() + { + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] != Map::Enums::AIF_RING_2) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + for (int direction = 0; direction < 4; ++direction) { + if (DAT_TileMapState::instance + .AIInfoLayer[tile + DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2]] + & Map::Enums::AIF_ANY_RING) { + continue; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(15, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2].int_.xOffset + + x, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2].int_.yOffset + + y, + Map::Enums::AIF_RING_3); + break; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage3.cpp b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage3.cpp new file mode 100644 index 0000000000..e8b1648a14 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/expandAIZoneLayerStage3.cpp @@ -0,0 +1,46 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Enums/AIInfoFlag.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TerrainDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AE90 + void TroopValueState::expandAIZoneLayerStage3() + { + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] != Map::Enums::AIF_RING_3) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + for (int direction = 0; direction < 4; ++direction) { + if (!(DAT_TileMapState::instance.AIInfoLayer[tile + + DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2]] + & Map::Enums::AIF_ANY_RING)) { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(15, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.xOffset + + x, + DAT_TerrainDefinedData::instance.clockwiseCardinalTranslationMatrix[direction * 2] + .int_.yOffset + + y, + Map::Enums::AIF_RING_4); + break; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findEnemyBuildingsClosestToUnit.cpp b/src/OpenSHC/Map/Units/TroopValueState/findEnemyBuildingsClosestToUnit.cpp new file mode 100644 index 0000000000..446d3c8cf9 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findEnemyBuildingsClosestToUnit.cpp @@ -0,0 +1,56 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519FB0 + int TroopValueState::findEnemyBuildingsClosestToUnit(int unitID) + { + int const owner = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 1000; + int bestIndex = -1; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].town2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[i].unitID) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + int const distance = DAT_DirectionAlgorithmState::instance.distanceHigh + + (100 - DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[i].score) + * 2; + DAT_DirectionAlgorithmState::instance.distanceHigh = distance; + if (distance < minDistance) { + minDistance = distance; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[bestIndex].unitID + = unitID; + return DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].townValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findEnemyLord.cpp b/src/OpenSHC/Map/Units/TroopValueState/findEnemyLord.cpp new file mode 100644 index 0000000000..308fc7fd88 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findEnemyLord.cpp @@ -0,0 +1,41 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A220 + int TroopValueState::findEnemyLord(int unitID) + { + int i = 0; + if (0 < DAT_TroopValueState::instance.attackInfo.lord3) { + do { + if (DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].unitID == 0) { + this->y = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].tile2]; + this->x = DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[this->y].addXgetTile; + this->tile = DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].tile; + DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].unitID = unitID; + return DAT_TroopValueState::instance.attackInfo.lordValuesArray[i].tile2; + } + i++; + } while (i < DAT_TroopValueState::instance.attackInfo.lord3); + this->y = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.lordValuesArray[0].tile2]; + this->x = DAT_TroopValueState::instance.attackInfo.lordValuesArray[0].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[this->y].addXgetTile; + this->tile = DAT_TroopValueState::instance.attackInfo.lordValuesArray[0].tile; + return DAT_TroopValueState::instance.attackInfo.lordValuesArray[0].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findEnemyTowersOrGates.cpp b/src/OpenSHC/Map/Units/TroopValueState/findEnemyTowersOrGates.cpp new file mode 100644 index 0000000000..40b9a7c5d8 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findEnemyTowersOrGates.cpp @@ -0,0 +1,88 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A330 + int TroopValueState::findEnemyTowersOrGates(int unitID) + { + int const owner = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 1000; + int bestIndex = -1; + int i; + for (i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].some1000ArrayIndex_2; ++i) { + if (Map::Enums::SSS_UNUSABLE_THRESHOLD + <= DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].score + || DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].unitID) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + int const bestTile + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[bestIndex].tile2; + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[bestIndex].unitID + = unitID; + return bestTile; + } + for (i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].some1000ArrayIndex_2; ++i) { + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].unitID = 0; + } + minDistance = 1000; + for (i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].some1000ArrayIndex_2; ++i) { + if (Map::Enums::SSS_UNUSABLE_THRESHOLD + <= DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].score + || DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].unitID) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + return DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].gateValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findEnemyWalls.cpp b/src/OpenSHC/Map/Units/TroopValueState/findEnemyWalls.cpp new file mode 100644 index 0000000000..ba7e860850 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findEnemyWalls.cpp @@ -0,0 +1,58 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519CC0 + int TroopValueState::findEnemyWalls(int unitID) + { + int const owner = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 1000; + int bestIndex = -1; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hack2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].score + >= Map::Enums::SSS_UNUSABLE + || DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].unitID) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + int const distance = DAT_DirectionAlgorithmState::instance.distanceHigh + + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].score * 8; + DAT_DirectionAlgorithmState::instance.distanceHigh = distance; + if (distance < minDistance) { + minDistance = distance; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[bestIndex].unitID + = unitID; + return DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].hackValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findNearestAvailableScalePoint.cpp b/src/OpenSHC/Map/Units/TroopValueState/findNearestAvailableScalePoint.cpp new file mode 100644 index 0000000000..e28bf623a1 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findNearestAvailableScalePoint.cpp @@ -0,0 +1,59 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519E40 + int TroopValueState::findNearestAvailableScalePoint(int unitID) + { + int const playerID = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 1000; + int bestIndex = -1; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale2; ++i) { + if (Map::Enums::SSS_UNUSABLE_THRESHOLD + <= DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].score + || DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].unitID + != 0) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + if (DAT_DirectionAlgorithmState::instance.distanceHigh < minDistance) { + minDistance = DAT_DirectionAlgorithmState::instance.distanceHigh; + this->y = fromYPosition; + this->x = fromXPosition; + this->tile + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].tile; + bestIndex = i; + } + } + if (-1 < bestIndex) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[bestIndex].unitID + = unitID; + return DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .scaleValuesArray[bestIndex] + .tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findNearestDiggableMoatPoint.cpp b/src/OpenSHC/Map/Units/TroopValueState/findNearestDiggableMoatPoint.cpp new file mode 100644 index 0000000000..b50f43b105 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findNearestDiggableMoatPoint.cpp @@ -0,0 +1,34 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A8A0 + int TroopValueState::findNearestDiggableMoatPoint(int unitID) + { + int const moatTile = MACRO_CALL_MEMBER(Map::TileMapState_Func::findNearestFriendlyMoatTileForDigging, + DAT_TileMapState::ptr)(DAT_UnitsState::instance.units[unitID].owner, unitID, 2); + if (moatTile <= -1) { + return 0; + } + int const result = MACRO_CALL_MEMBER(Map::TileMapState_Func::setXYBasedOnMoatID, DAT_TileMapState::ptr)( + moatTile, 2, DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y); + if (result <= 0) { + return 0; + } + this->x = DAT_TileMapState::instance.ALG_MoatXResult; + this->y = DAT_TileMapState::instance.ALG_MoatYResult; + this->tile = DAT_TileMapState::instance.someMoatTile; + return result; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findOrReserveArcherPointSlot.cpp b/src/OpenSHC/Map/Units/TroopValueState/findOrReserveArcherPointSlot.cpp new file mode 100644 index 0000000000..27ce49f841 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findOrReserveArcherPointSlot.cpp @@ -0,0 +1,29 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AF20 + int TroopValueState::findOrReserveArcherPointSlot(int tile) + { + DAT_TroopValueState::instance.attackInfo.archerPointsNext = 0; + for (int i = 1; i < countof(DAT_TroopValueState::instance.attackInfo.archerPointArray) - 1; ++i) { + if (DAT_TroopValueState::instance.attackInfo.archerPointArray[i].tile == tile) { + return i; + } + if (DAT_TroopValueState::instance.attackInfo.archerPointArray[i].tile == 0 + && !DAT_TroopValueState::instance.attackInfo.archerPointsNext) { + DAT_TroopValueState::instance.attackInfo.archerPointsNext = i; + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/findTribeWithMoatAttackBehavior.cpp b/src/OpenSHC/Map/Units/TroopValueState/findTribeWithMoatAttackBehavior.cpp new file mode 100644 index 0000000000..9b3ea12cac --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/findTribeWithMoatAttackBehavior.cpp @@ -0,0 +1,41 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A920 + BOOLEnum TroopValueState::findTribeWithMoatAttackBehavior() + { + for (int tribeID = 1; tribeID < countof(DAT_TribesState::instance.tribes); ++tribeID) { + if (DAT_TribesState::instance.tribes[tribeID].tribeState == 0) { + continue; + } + BOOLEnum const fullIDIsMinus1 + = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::isFullIDEqualsToMinus1, DAT_GameState::ptr)( + DAT_TribesState::instance.tribes[tribeID].owner); + if (fullIDIsMinus1 + && DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType == Map::Units::STBT_0x3f7) { + this->x = DAT_TribesState::instance.tribes[tribeID].targetX; + this->y = DAT_TribesState::instance.tribes[tribeID].targetY; + DAT_TroopValueState::instance.attackInfo.field127625_0x42744 = tribeID; + return TRUE; + } + } + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/getClosestWideValueBasedOnPlayer.cpp b/src/OpenSHC/Map/Units/TroopValueState/getClosestWideValueBasedOnPlayer.cpp new file mode 100644 index 0000000000..09ddb117df --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/getClosestWideValueBasedOnPlayer.cpp @@ -0,0 +1,75 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameState.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 { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A5B0 + int TroopValueState::getClosestWideValueBasedOnPlayer(int unitID) + { + int const owner = DAT_UnitsState::instance.units[unitID].owner; + int minDistance = 1000; + int bestIndex = -1; + int const attackedPlayerID = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1] + .currentPlayerID_OR_attackedPlayerID_OR_one; + int const destinationXPosition = DAT_GameState::instance.playerDataArray[attackedPlayerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry; + int const destinationYPosition = DAT_GameState::instance.playerDataArray[attackedPlayerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .yEntry; + int i = 0; + if (0 < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wide2) { + for (; i < DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wide2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[i].score + >= Map::Enums::SSS_UNUSABLE_THRESHOLD + || DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[i].unitID) { + continue; + } + int const fromYPosition + = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent + [DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[i].tile2]; + int const fromXPosition + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[i].tile2 + - DAT_ViewportRenderState::instance.translationMatrix[fromYPosition].addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, fromXPosition, fromYPosition); + int distance = DAT_DirectionAlgorithmState::instance.distanceHigh; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)( + destinationXPosition, destinationYPosition, fromXPosition, fromYPosition); + distance += DAT_DirectionAlgorithmState::instance.distanceHigh; + if (distance < minDistance) { + this->y = fromYPosition; + this->x = fromXPosition; + this->tile + = DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[i].tile; + minDistance = distance; + bestIndex = i; + } + } + if (bestIndex <= -1) { + return 0; + } + DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[bestIndex].unitID + = unitID; + return DAT_TroopValueState::instance.attackInfo.playerInfo[owner - 1].wideValuesArray[bestIndex].tile2; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/getPlayerNot1AndHasKeep.cpp b/src/OpenSHC/Map/Units/TroopValueState/getPlayerNot1AndHasKeep.cpp new file mode 100644 index 0000000000..40c6d9770d --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/getPlayerNot1AndHasKeep.cpp @@ -0,0 +1,38 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BDE0 + BOOLEnum TroopValueState::getPlayerNot1AndHasKeep(int playerID) + { + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + if (playerID == 1) { + return TRUE; + } + } else if (playerID != 1 + && 0 < DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id) { + return TRUE; + } + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/getSiegeIndexForTile.cpp b/src/OpenSHC/Map/Units/TroopValueState/getSiegeIndexForTile.cpp new file mode 100644 index 0000000000..c44b7fbe82 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/getSiegeIndexForTile.cpp @@ -0,0 +1,29 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B240 + int TroopValueState::getSiegeIndexForTile(int tile) + { + DAT_TroopValueState::instance.attackInfo.tentPointsNext = 0; + for (int i = 1; i < countof(DAT_TroopValueState::instance.attackInfo.tentPointsValues) - 1; ++i) { + if (DAT_TroopValueState::instance.attackInfo.tentPointsValues[i].tile == tile) { + return i; + } + if (DAT_TroopValueState::instance.attackInfo.tentPointsValues[i].tile == 0 + && !DAT_TroopValueState::instance.attackInfo.tentPointsNext) { + DAT_TroopValueState::instance.attackInfo.tentPointsNext = i; + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/getSupportPointIndex.cpp b/src/OpenSHC/Map/Units/TroopValueState/getSupportPointIndex.cpp new file mode 100644 index 0000000000..82b4707475 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/getSupportPointIndex.cpp @@ -0,0 +1,29 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B0C0 + int TroopValueState::getSupportPointIndex(int tile) + { + DAT_TroopValueState::instance.attackInfo.supportPointsNext = 0; + for (int i = 1; i < countof(DAT_TroopValueState::instance.attackInfo.supportPointsArray) - 1; ++i) { + if (DAT_TroopValueState::instance.attackInfo.supportPointsArray[i].tile == tile) { + return i; + } + if (DAT_TroopValueState::instance.attackInfo.supportPointsArray[i].tile == 0 + && !DAT_TroopValueState::instance.attackInfo.supportPointsNext) { + DAT_TroopValueState::instance.attackInfo.supportPointsNext = i; + } + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/getValueOfTroopType.cpp b/src/OpenSHC/Map/Units/TroopValueState/getValueOfTroopType.cpp new file mode 100644 index 0000000000..806206285e --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/getValueOfTroopType.cpp @@ -0,0 +1,52 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_RenderingDefinedData.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051C360 + int TroopValueState::getValueOfTroopType(UnitType unitType) + { + switch (unitType) { + case Map::Units::UT_E_MONK: + return DAT_RenderingDefinedData::instance.MONK; + case Map::Units::UT_TUNNELER: + return DAT_RenderingDefinedData::instance.TUNNELER; + case Map::Units::UT_A_ARCHER: + return DAT_RenderingDefinedData::instance.A_ARCHER; + case Map::Units::UT_A_SLAVE: + return DAT_RenderingDefinedData::instance.SLAVE; + case Map::Units::UT_A_SLINGER: + return DAT_RenderingDefinedData::instance.SLINGER; + case Map::Units::UT_A_ASSASSIN: + return DAT_RenderingDefinedData::instance.ASSASSIN; + case Map::Units::UT_A_HARCHER: + return DAT_RenderingDefinedData::instance.HARCHER; + case Map::Units::UT_A_SWORDSMAN: + return DAT_RenderingDefinedData::instance.A_SWORDSMAN; + case Map::Units::UT_A_FIRETHROWER: + return DAT_RenderingDefinedData::instance.FIRETHROWER; + case Map::Units::UT_S_FBALLISTA: + return DAT_RenderingDefinedData::instance.S_FBALLISTA; + case Map::Units::UT_S_CATAPULT: + return DAT_RenderingDefinedData::instance.CATAPULT; + } + if (unitType < Map::Units::UT_E_ARCHER) { + return 0; + } + if (unitType <= Map::Units::UT_E_ENGINEER) { + return DAT_RenderingDefinedData::instance.NoRushTicks[unitType + Map::Units::UT_TUNNELER]; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/giveLightPitchInstructionToUnitClosestToPitch.cpp b/src/OpenSHC/Map/Units/TroopValueState/giveLightPitchInstructionToUnitClosestToPitch.cpp new file mode 100644 index 0000000000..6dac5d6a99 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/giveLightPitchInstructionToUnitClosestToPitch.cpp @@ -0,0 +1,96 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.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_EntityState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + using Map::Units::States::UnitState; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BF70 + BOOLEnum TroopValueState::giveLightPitchInstructionToUnitClosestToPitch(int tile) + { + BOOLEnum isAIPlayerWithKeep; + int _shot; + int _ditchID + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getPitchDitchIDForTile, DAT_TileMapState::ptr)(tile); + int _minUnitID = 0; + int _minDistance = 100000000; + int _unitID = 1; + if (1 < DAT_UnitsState::instance.maxUnitCount) { + Unit* _pUnit = &DAT_UnitsState::instance.units[1]; + do { + if (_pUnit->logicalState != Map::Units::ULS_INVISIBLE + && (isAIPlayerWithKeep = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)((int)_pUnit->owner), + isAIPlayerWithKeep) + && (_pUnit->unitType == Map::Units::UT_E_ARCHER + || (_pUnit->unitType == Map::Units::UT_A_ARCHER)) + && !_pUnit->dying && _pUnit->field_0x40d != false + && _pUnit->targetingType != Map::Units::UIT_LIGHT_PITCH) { + int const _ditchY = (int)DAT_TileMapState::instance.pitchDitches[_ditchID].y; + int const _ditchX = (int)DAT_TileMapState::instance.pitchDitches[_ditchID].x; + int const _unitY = _ditchY - _pUnit->y; + int const _unitX = _ditchX - _pUnit->x; + int _distancePyth = _unitX * _unitX + _unitY * _unitY; + if (_distancePyth <= 2500 + && (_shot = MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::arrowShootingRelated, + DAT_EntityState::ptr)((int)_pUnit->microXPosition, (int)(_pUnit->microYPosition), + (int)(_pUnit->buildingHeight + 30 + _pUnit->terrainOrClimbHeight), _ditchX * 8 + 4, + _ditchY * 8 + 4, + (int)(DAT_TileMapState::instance + .HeightLayer[DAT_TileMapState::instance.pitchDitches[_ditchID].tile])), + 0 < _shot) + && _distancePyth < _minDistance) { + _minDistance = _distancePyth; + _minUnitID = _unitID; + } + } + _unitID++; + _pUnit++; + } while (_unitID < DAT_UnitsState::instance.maxUnitCount); + } + if (!_minUnitID) { + return 0; + } + DAT_UnitsState::instance.units[_minUnitID].shootTargetMicroX + = DAT_TileMapState::instance.pitchDitches[_ditchID].x * 8 + 4; + DAT_UnitsState::instance.units[_minUnitID].shootTargetMicroY + = DAT_TileMapState::instance.pitchDitches[_ditchID].y * 8 + 4; + DAT_UnitsState::instance.units[_minUnitID].shootTargetZ + = DAT_TileMapState::instance.HeightLayer[DAT_TileMapState::instance.pitchDitches[_ditchID].tile]; + DAT_UnitsState::instance.units[_minUnitID].targetID_OR_targetBuildingID = (short)_ditchID; + int _ditchUID = DAT_TileMapState::instance.pitchDitches[_ditchID].uid; + DAT_UnitsState::instance.units[_minUnitID].shootTargetedUnit = -1; + DAT_UnitsState::instance.units[_minUnitID] + .targetedUnitUIDUnk_OR_someAppearTileUnk_OR_buildingUID_OR_pitchDitchUID_OR_entityUID = _ditchUID; + DAT_UnitsState::instance.units[_minUnitID].field_0x3c5 = 0x22; + DAT_UnitsState::instance.units[_minUnitID].targetingType = Map::Units::UIT_LIGHT_PITCH; + DAT_UnitsState::instance.units[_minUnitID].field_0x3f8 = 0; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::makeUnitStopWalkingByClearingPathProgressState, + DAT_UnitsState::ptr)(_minUnitID); + DAT_UnitsState::instance.units[_minUnitID].state.generic = Map::Units::States::US_FIRE_WEAPONUnk; + DAT_UnitsState::instance.units[_minUnitID].field_0x40e = true; + return 1; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/ifNecessaryThrowOilAutomatically.cpp b/src/OpenSHC/Map/Units/TroopValueState/ifNecessaryThrowOilAutomatically.cpp new file mode 100644 index 0000000000..b6b68243df --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/ifNecessaryThrowOilAutomatically.cpp @@ -0,0 +1,136 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.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::UnitInstructionType; + using Map::Units::Behavior::UnitStanceEnum; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051C570 + BOOLEnum TroopValueState::ifNecessaryThrowOilAutomatically(int unitID) + { + int const tribeID = DAT_UnitsState::instance.units[unitID].tribeID; + int searchRadius = 4; + if (DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight > 110) { + searchRadius = 5; + } + if (tribeID <= 0) { + return FALSE; + } + if (DAT_UnitsState::instance.units[unitID].closestEnemyMicroDistance > 80) { + return FALSE; + } + int const playerID = DAT_UnitsState::instance.units[unitID].owner; + if (DAT_GameSynchronyState::instance.currentPlayerFullIDArray[playerID] != -1 + && DAT_TribesState::instance.tribes[tribeID].unitStance == Map::Units::Behavior::USE_STAND_GROUND) { + return FALSE; + } + DAT_UnitsState::instance.units[unitID].updateTickTracker + = DAT_UnitsState::instance.units[unitID].updateTickTracker + 1; + if (DAT_UnitsState::instance.units[unitID].updateTickTracker < 8) { + return FALSE; + } + DAT_UnitsState::instance.units[unitID].updateTickTracker = 0; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeTotalUnitsWithinDistance, + DAT_PathFindingState::ptr)(playerID, 0, 0, DAT_UnitsState::instance.units[unitID].tile, searchRadius); + if (DAT_TribesState::instance.tribes[tribeID].unitStance == Map::Units::Behavior::USE_AGGRESSIVE) { + if (DAT_PathFindingState::instance.ALGO_TotalTroopCount < 1) { + return FALSE; + } + } else if (DAT_PathFindingState::instance.ALGO_TotalTroopCount <= 3 + && DAT_PathFindingState::instance.ALGO_TotalTroopValue < 20) { + return FALSE; + } + int const targetUnitID = DAT_PathFindingState::instance.field_0x64; + if (targetUnitID == 0) { + return FALSE; + } + short const buildingID + = DAT_TileMapState::instance.BuildingLayer[DAT_UnitsState::instance.units[targetUnitID].tile]; + if (buildingID != 0) { + int buildingSize = 0; + switch (DAT_BuildingsState::instance.buildings[buildingID].buildingType) { + case Map::Buildings::BT_CAMPGROUND: + buildingSize = 7; + break; + case Map::Buildings::BT_PARADEGROUND: + case Map::Buildings::BT_PARADEGROUND2: + case Map::Buildings::BT_PARADEGROUND3: + case Map::Buildings::BT_PARADEGROUND4: + case Map::Buildings::BT_PARADEGROUND5: + buildingSize = 5; + break; + case Map::Buildings::BT_CAMPFIRE: + buildingSize = 4; + break; + default: + break; + } + if (buildingSize != 0) { + int tileIndex = 0; + int const buildingX = DAT_BuildingsState::instance.buildings[buildingID].x; + int const buildingY = DAT_BuildingsState::instance.buildings[buildingID].y; + int friendlyTroopValue = 0; + int enemyTroopValue = 0; + do { + MACRO_CALL_MEMBER(Map::TileMapState_Func::getBuildingSizeIndexMappingData, + DAT_TileMapState::ptr)(tileIndex, buildingSize); + for (int tileUnitID = DAT_TileMapState::instance + .UnitLayer[DAT_ViewportRenderState::instance + .translationMatrix[DAT_TileMapState::instance.buildingY + buildingY] + .addXgetTile + + DAT_TileMapState::instance.buildingX + buildingX]; + tileUnitID != 0; + tileUnitID = DAT_UnitsState::instance.units[tileUnitID].nextUnitOnTheSameTile) { + if (DAT_UnitsState::instance.units[tileUnitID].isSelectable_OR_matchTime == 0) { + continue; + } + if (DAT_GameState::instance.mapAndTime + .playerTeams[DAT_UnitsState::instance.units[unitID].owner] + != DAT_GameState::instance.mapAndTime + .playerTeams[DAT_UnitsState::instance.units[tileUnitID].owner]) { + enemyTroopValue += MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getValueOfTroopType, DAT_TroopValueState::ptr)( + (Map::Units::UnitType)DAT_UnitsState::instance.units[tileUnitID].unitType); + } else { + friendlyTroopValue += MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getValueOfTroopType, DAT_TroopValueState::ptr)( + (Map::Units::UnitType)DAT_UnitsState::instance.units[tileUnitID].unitType); + } + } + tileIndex++; + } while (tileIndex < DAT_TileMapState::instance.constructionTileCount); + if (friendlyTroopValue > enemyTroopValue) { + return FALSE; + } + } + } + DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_THROW_OIL; + DAT_UnitsState::instance.units[unitID].attackAtTileX = DAT_UnitsState::instance.units[targetUnitID].x; + DAT_UnitsState::instance.units[unitID].attackAtTileY = DAT_UnitsState::instance.units[targetUnitID].y; + DAT_UnitsState::instance.units[unitID].animationFrame = 4; + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/initializeAttackWaveSlot.cpp b/src/OpenSHC/Map/Units/TroopValueState/initializeAttackWaveSlot.cpp new file mode 100644 index 0000000000..bed30cf94d --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/initializeAttackWaveSlot.cpp @@ -0,0 +1,50 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00518180 + void TroopValueState::initializeAttackWaveSlot(int param_1, int param_2) + { + DAT_TroopValueState::instance.attackInfo.value3Array01[param_1] = Map::Enums::AWS_GATHERING; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[param_1] = 0; + DAT_TroopValueState::instance.attackInfo.someIntArray2[param_1 - 1] = 0; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.someSinglePlayerScore[param_1]), '\0', + &DAT_TroopValueState::instance.attackInfo.someSinglePlayerScore[param_1]); + DAT_TroopValueState::instance.attackInfo.aiTickPhase = 0; + DAT_TroopValueState::instance.attackInfo.counter = 0x10; + DAT_TroopValueState::instance.attackInfo.archerPointCountPrev = 0xffffffff; + DAT_TroopValueState::instance.attackInfo.delayedWaveAssignmentCount = 0; + DAT_TroopValueState::instance.attackInfo.buildingDamageEventCount = 0; + DAT_TroopValueState::instance.attackInfo.enemyUnitsInsideCastleCount = 0; + for (int _player = 0; _player < 8; ++_player) { + DAT_TroopValueState::instance.attackInfo.playerInfo[_player].field22_0x3ea4 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[_player].field_0x3ea8 = 0; + } + int _randomSignpost = MACRO_CALL_MEMBER( + Game::GameStateStructures_Func::pickRandomAccessibleSignpostEntry, DAT_GameState::ptr)(); + int _activeSignposts + = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::countActiveSignposts, DAT_GameState::ptr)(); + if ((param_2 <= _activeSignposts) && (param_2)) { + DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[param_1] = param_2 - 1; + } else { + DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[param_1] = _randomSignpost; + } + DAT_TroopValueState::instance.attackInfo.field_0x469d4 + = DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[param_1]; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/initializeAttackZoneSearch.cpp b/src/OpenSHC/Map/Units/TroopValueState/initializeAttackZoneSearch.cpp new file mode 100644 index 0000000000..8876dbef8f --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/initializeAttackZoneSearch.cpp @@ -0,0 +1,38 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519850 + void TroopValueState::initializeAttackZoneSearch(int attackID) + { + DAT_TroopValueState::instance.attackInfo.biggestZone = 0; + int const tile + = DAT_GameState::instance.mapAndTime + .signpostEntryData[DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackID]] + .tile; + DAT_TroopValueState::instance.attackInfo.startZone = DAT_TileMapState::instance.AIZoneLayer[tile]; + DAT_TroopValueState::instance.attackInfo.startCon = DAT_TileMapState::instance.PathConnectionLayer[tile]; + DAT_TroopValueState::instance.attackInfo.zoneSize + = DAT_PathFindingState::instance.zoneSizesArray[DAT_TileMapState::instance.PathConnectionLayer[tile]]; + DAT_TroopValueState::instance.attackInfo.unknownOne_0x20f90 = 1; + DAT_TroopValueState::instance.attackInfo.keepCon + = DAT_TileMapState::instance.PathConnectionLayer[DAT_GameState::instance + .playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .tileEntry]; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/initializeOrAdvanceAttackWave.cpp b/src/OpenSHC/Map/Units/TroopValueState/initializeOrAdvanceAttackWave.cpp new file mode 100644 index 0000000000..af032cb0f2 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/initializeOrAdvanceAttackWave.cpp @@ -0,0 +1,63 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/WildlifeState.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/Map/Enums/TribeState.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_WildlifeState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051FF90 + void TroopValueState::initializeOrAdvanceAttackWave(int attackWave) + { + if (DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] == 0) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] = 1; + DAT_TroopValueState::instance.attackInfo.pendingAttackWaveCount++; + DAT_TroopValueState::instance.attackInfo.someCounter1 = 1; + DAT_TroopValueState::instance.attackInfo.attackAlarmPlayed = FALSE; + DAT_TroopValueState::instance.attackInfo.attackWaveRetargetCount = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recountAttackTroopValue, this)(TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::computeAttackWaveTroopComposition, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::initializeAttackZoneSearch, this)(attackWave); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recountTotalTroopValue, this)(); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackWave, 2, 100000, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fe, Map::Units::STBT_1, 0, 50); + DAT_TroopValueState::instance.attackInfo.casDis = MACRO_CALL_MEMBER( + Map::WildlifeState_Func::getDistanceFromAttackSignpostToZoneProximity, DAT_WildlifeState::ptr)(); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyTribeBehaviorType, DAT_TribesState::ptr)( + attackWave, Map::Units::STBT_3); + } + if ((char)DAT_TroopValueState::instance.attackInfo.unknownByteArray02[attackWave] + >= DAT_TroopValueState::instance.attackInfo.nof_tribes[attackWave] - 5) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave]++; + if (DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] + >= DAT_AttackInfoDefinedData::instance + .AIAttackWaveAdvanceDelay[DAT_TroopValueState::instance.attackInfo.attacker]) { + if (DAT_TroopValueState::instance.attackInfo.attacker == 8) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::playAttackAlarmSound, this)(); + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] = 10000; + DAT_TroopValueState::instance.attackInfo.value3Array01[attackWave] + = Map::Enums::AWS_ASSIGNING_TARGETS; + DAT_TroopValueState::instance.attackInfo.value10 = 0; + } else { + DAT_TroopValueState::instance.attackInfo.value3Array01[attackWave] = Map::Enums::AWS_ADVANCING; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] = 0; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/isAttackWaveComplete.cpp b/src/OpenSHC/Map/Units/TroopValueState/isAttackWaveComplete.cpp index a4b1afeb07..83dbaac9b1 100644 --- a/src/OpenSHC/Map/Units/TroopValueState/isAttackWaveComplete.cpp +++ b/src/OpenSHC/Map/Units/TroopValueState/isAttackWaveComplete.cpp @@ -1,4 +1,5 @@ #include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" #include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" #include "OpenSHC/Globals/DAT_TroopValueState.hpp" @@ -12,9 +13,9 @@ namespace Map { // FUNCTION: STRONGHOLDCRUSADER 0x00518350 BOOLEnum TroopValueState::isAttackWaveComplete() { - for (int i = 0; i < 50; ++i) { + for (int i = 0; i < countof(DAT_TroopValueState::instance.attackInfo.nof_tribes); ++i) { if (DAT_TroopValueState::instance.attackInfo.nof_tribes[i] != 0 - && DAT_TroopValueState::instance.attackInfo.value3Array01[i] != 6) { + && DAT_TroopValueState::instance.attackInfo.value3Array01[i] != Map::Enums::AWS_ENDED) { return FALSE; } } diff --git a/src/OpenSHC/Map/Units/TroopValueState/isLessThanPercentageOfTribesInAttackDying.cpp b/src/OpenSHC/Map/Units/TroopValueState/isLessThanPercentageOfTribesInAttackDying.cpp new file mode 100644 index 0000000000..a9ad10db58 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/isLessThanPercentageOfTribesInAttackDying.cpp @@ -0,0 +1,46 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00518BB0 + BOOLEnum TroopValueState::isLessThanPercentageOfTribesInAttackDying(int attackID, int leDyingPerc) + { + int dying = 0; + int living = 0; + for (int tribeID = 1; tribeID < countof(DAT_TribesState::instance.tribes); ++tribeID) { + if (!DAT_TribesState::instance.tribes[tribeID].tribeState + || DAT_TribesState::instance.tribes[tribeID].tribeState == 3 + || DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[DAT_TribesState::instance.tribes[tribeID].owner] + != -1 + || DAT_TribesState::instance.tribes[tribeID].attackWave != attackID) { + continue; + } + int const aliveStatus = MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::getTribeAliveStatus, DAT_TribesState::ptr)(tribeID); + if (aliveStatus < 50) { + ++living; + } else { + ++dying; + } + } + int dyingPercentage; + if (dying + living == 0) { + dyingPercentage = 100; + } else { + dyingPercentage = (dying * 100) / (dying + living); + } + return dyingPercentage >= leDyingPerc; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/moveTowardsParticularUnits.cpp b/src/OpenSHC/Map/Units/TroopValueState/moveTowardsParticularUnits.cpp new file mode 100644 index 0000000000..c5fb432e86 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/moveTowardsParticularUnits.cpp @@ -0,0 +1,44 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D510 + void TroopValueState::moveTowardsParticularUnits(int tribeID) + { + int const selectedUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int targetUnitID = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findDistanceOrThreatLevelToUnitUnk, DAT_PathFindingState::ptr)( + DAT_TribesState::instance.tribes[tribeID].owner, DAT_UnitsState::instance.units[selectedUnitID].x, + DAT_UnitsState::instance.units[selectedUnitID].y, 80); + if (!targetUnitID) { + targetUnitID + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findDistanceOrThreatLevelToUnitUnk, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, + DAT_UnitsState::instance.units[selectedUnitID].x, + DAT_UnitsState::instance.units[selectedUnitID].y, 200); + if (!targetUnitID) { + return; + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)( + tribeID, Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk, 0, 0, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, 0, 0, + Map::Units::Instructions::UMSE_0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/moveTribeToNearbyGatehouse.cpp b/src/OpenSHC/Map/Units/TroopValueState/moveTribeToNearbyGatehouse.cpp new file mode 100644 index 0000000000..e57c9d8c71 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/moveTribeToNearbyGatehouse.cpp @@ -0,0 +1,46 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D450 + BOOLEnum TroopValueState::moveTribeToNearbyGatehouse(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int gatehouseID = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getGatehouseNearSomethingUnk, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 80); + if (!gatehouseID) { + gatehouseID = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::getGatehouseNearSomethingUnk, + DAT_PathFindingState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 200); + if (!gatehouseID) { + return FALSE; + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)( + tribeID, Map::Units::UIT_NO_INSTRUCTION_OR_MOVEUnk, 0, 0, 0); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + DAT_BuildingsState::instance.buildings[gatehouseID].someX, + DAT_BuildingsState::instance.buildings[gatehouseID].someY, 0, 0, Map::Units::Instructions::UMSE_0); + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/moveTribeToReachableNearbyTile.cpp b/src/OpenSHC/Map/Units/TroopValueState/moveTribeToReachableNearbyTile.cpp new file mode 100644 index 0000000000..a76ac78185 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/moveTribeToReachableNearbyTile.cpp @@ -0,0 +1,53 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D1C0 + void TroopValueState::moveTribeToReachableNearbyTile(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 1); + int tile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, DAT_UnitsState::instance.units[unitID].owner, 12, 0); + if (tile <= 0) { + tile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, DAT_UnitsState::instance.units[unitID].owner, 30, 0); + if (tile <= 0) { + tile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[unitID].x, + DAT_UnitsState::instance.units[unitID].y, DAT_UnitsState::instance.units[unitID].owner, 50, 0); + } + } + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 0); + if (0 < tile) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile], 0, 0, + Map::Units::Instructions::UMSE_0); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentAtAttackAngle.cpp b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentAtAttackAngle.cpp new file mode 100644 index 0000000000..bd409aed84 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentAtAttackAngle.cpp @@ -0,0 +1,56 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B310 + int TroopValueState::placeSiegeTentAtAttackAngle(int tribeID, MappersEnum commandBuildingType) + { + int const targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int const attackLocationIndex + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::algFindAttackAngle, + DAT_PathFindingState::ptr)(200, DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, tribeID); + if (attackLocationIndex) { + short const previousSiegeIndex = DAT_TribesState::instance.tribes[tribeID].siegeIndexValue1; + int const tribeUID = DAT_TribesState::instance.tribes[tribeID].uid; + DAT_TribesState::instance.tribes[tribeID].siegeIndexValue2 = previousSiegeIndex; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[previousSiegeIndex].tribeID = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[previousSiegeIndex].tribeUID = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[attackLocationIndex].tribeUID = tribeUID; + int const y = DAT_TroopValueState::instance.attackInfo.tentPointsValues[attackLocationIndex].y; + int const x = DAT_TroopValueState::instance.attackInfo.tentPointsValues[attackLocationIndex].x; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[attackLocationIndex].tribeID = tribeID; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[attackLocationIndex].three = 3; + int const playerID = DAT_TribesState::instance.tribes[tribeID].owner; + DAT_TribesState::instance.tribes[tribeID].siegeIndexValue1 = attackLocationIndex; + MACRO_CALL_MEMBER(Map::TileMapState_Func::placeBuilding, DAT_TileMapState::ptr)( + playerID, x - 1, y - 1, commandBuildingType, 3, 15); + int const buildingID = DAT_TileMapState::instance.placedBuildingID; + DAT_BuildingsState::instance.buildings[DAT_TileMapState::instance.placedBuildingID].attackWave + = DAT_TribesState::instance.tribes[tribeID].attackWave; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)(tribeID, + Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED, buildingID, + DAT_BuildingsState::instance.buildings[buildingID].uid, 0); + return attackLocationIndex; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers.cpp b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers.cpp new file mode 100644 index 0000000000..80d2497e63 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers.cpp @@ -0,0 +1,78 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B420 + BOOLEnum TroopValueState::placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers( + int tribeID, MappersEnum commandBuildingType, uint strategicDistance, UnitInstructionType instruction) + { + uint x; + uint y; + int const targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + dword targetTile = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::findAppropriateLocationForSiegeTent, DAT_PathFindingState::ptr)( + 100, DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, + tribeID, strategicDistance, DAT_TribesState::instance.tribes[tribeID].owner); + if (!targetTile) { + targetTile + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findAppropriateLocationForSiegeTent, + DAT_PathFindingState::ptr)(200, DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, tribeID, strategicDistance, + DAT_TribesState::instance.tribes[tribeID].owner); + if (!targetTile) { + return FALSE; + } + } + int const tileY = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[targetTile]; + x = (targetTile - DAT_ViewportRenderState::instance.translationMatrix[tileY].addXgetTile) - 1; + y = tileY - 1; + if (x > MC_MAP_XY_LIMIT_INCLUSIVE) { + } else if (y > MC_MAP_XY_LIMIT_INCLUSIVE) { + } else if (DAT_ViewportRenderState::instance.DAT_BinaryTileMap400x400[y * MC_MAP_XY_LIMIT + x] == 0) { + DAT_TileMapState::instance.buildingPlacementFail = TRUE; + return FALSE; + } else { + if (DAT_TileMapState::instance + .LogicLayer[DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile + x] + & 0x4a5014b1) { + DAT_TileMapState::instance.buildingPlacementFail = TRUE; + return FALSE; + } + MACRO_CALL_MEMBER(Map::TileMapState_Func::placeBuilding, DAT_TileMapState::ptr)( + DAT_TribesState::instance.tribes[tribeID].owner, x, y, commandBuildingType, 3, 0xf); + if (DAT_TileMapState::instance.buildingPlacementFail) { + return FALSE; + } + int const buildingID = DAT_TileMapState::instance.placedBuildingID; + DAT_BuildingsState::instance.buildings[buildingID].attackWave + = DAT_TribesState::instance.tribes[tribeID].attackWave; + DAT_BuildingsState::instance.buildings[buildingID].unknownSiegeTentRelated01 = 2; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)( + tribeID, instruction, buildingID, DAT_BuildingsState::instance.buildings[buildingID].uid, 0); + return TRUE; + } + DAT_TileMapState::instance.buildingPlacementFail = TRUE; + return FALSE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp new file mode 100644 index 0000000000..00e595a51e --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/placeSiegeTentsAndAssignEngineers.cpp @@ -0,0 +1,154 @@ +#include "../../../Map.func.hpp" + +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/TroopValueState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/INT_ARRAY_00b4d0f0.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Game::GameMode2; + using Map::MapType2; + using Map::Units::UnitInstructionType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B710 + void TroopValueState::placeSiegeTentsAndAssignEngineers(int param_1, int param_2) + { + int owner = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[param_1]; + if (!owner) { + owner = 2; + } + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + owner = 2; + } + int engineerTotal = 0; + DAT_TroopValueState::instance.attackInfo.createTribeAmount = 0; + for (int k = 0; k <= 4; k++) { + Commands::MappersEnum const buildingType = DAT_AttackInfoDefinedData::instance.field_0xc8[k]; + int const engineers = INT_ARRAY_00b4d0f0::instance[k]; + int const count = (char)DAT_TroopValueState::instance.attackInfo.someSinglePlayerScore[param_1][k]; + for (int n = 0; n < count; n++) { + DAT_TroopValueState::instance.attackInfo + .engineerBuildQueue[DAT_TroopValueState::instance.attackInfo.createTribeAmount] + .engineerBuildingType = buildingType; + DAT_TroopValueState::instance.attackInfo + .engineerBuildQueue[DAT_TroopValueState::instance.attackInfo.createTribeAmount] + .engineerCount = engineers; + DAT_TroopValueState::instance.attackInfo.createTribeAmount + = DAT_TroopValueState::instance.attackInfo.createTribeAmount + 1; + engineerTotal = engineerTotal + engineers; + if (DAT_TroopValueState::instance.attackInfo.createTribeAmount >= 250) { + break; + } + } + } + int const count = (char)DAT_TroopValueState::instance.attackInfo.someSinglePlayerScore[param_1][8]; + Commands::MappersEnum const buildingType = DAT_AttackInfoDefinedData::instance.field_0xc8[8]; + int const engineers = INT_ARRAY_00b4d0f0::instance[8]; + for (int n = 0; n < count; n++) { + if (DAT_TroopValueState::instance.attackInfo.createTribeAmount >= 250) { + break; + } + DAT_TroopValueState::instance.attackInfo + .engineerBuildQueue[DAT_TroopValueState::instance.attackInfo.createTribeAmount] + .engineerBuildingType = buildingType; + DAT_TroopValueState::instance.attackInfo + .engineerBuildQueue[DAT_TroopValueState::instance.attackInfo.createTribeAmount] + .engineerCount = engineers; + DAT_TroopValueState::instance.attackInfo.createTribeAmount + = DAT_TroopValueState::instance.attackInfo.createTribeAmount + 1; + engineerTotal = engineerTotal + engineers; + } + int tribeID = 1; + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize = 0; + for (; tribeID < 1250; tribeID++) { + if (DAT_TribesState::instance.tribes[tribeID].tribeState + && DAT_TribesState::instance.tribes[tribeID].owner == owner + && DAT_TribesState::instance.tribes[tribeID].attackWave == param_1 + && DAT_TribesState::instance.tribes[tribeID].tribeType == AI::Tribes::AITT_ENGINEERS) { + DAT_TroopValueState::instance.attackInfo + .tribeIDArray[DAT_TroopValueState::instance.attackInfo.tribeIDArraySize] = tribeID; + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize + = DAT_TroopValueState::instance.attackInfo.tribeIDArraySize + 1; + } + } + if (!DAT_TroopValueState::instance.attackInfo.tribeIDArraySize) { + return; + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; i++) { + tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[i]; + while (DAT_TribesState::instance.tribes[tribeID].size && engineerTotal > 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::popUnitFromTribe, DAT_TribesState::ptr)(tribeID); + engineerTotal = engineerTotal - 1; + } + } + int unitID = 0; + for (int q = 0; q < DAT_TroopValueState::instance.attackInfo.createTribeAmount; q++) { + int remaining = DAT_TroopValueState::instance.attackInfo.engineerBuildQueue[q].engineerCount; + int const newTribeID = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, + DAT_TribesState::ptr)(DAT_TribesState::instance.tribes[tribeID].owner, 0); + DAT_TribesState::instance.tribes[newTribeID].tribeType = AI::Tribes::AITT_ENGINEERS; + DAT_TribesState::instance.tribes[newTribeID].tribeBehaviorType = 1040; + DAT_TribesState::instance.tribes[newTribeID].attackWave + = DAT_TribesState::instance.tribes[tribeID].attackWave; + DAT_TribesState::instance.tribes[newTribeID].attackInfo_someCounter1 + = DAT_TribesState::instance.tribes[tribeID].attackInfo_someCounter1; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + for (;;) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].owner == owner + && !DAT_UnitsState::instance.units[unitID].dying + && DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_E_ENGINEER + && !DAT_UnitsState::instance.units[unitID].tribeID) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)( + unitID, newTribeID); + remaining = remaining - 1; + if (remaining <= 0) { + break; + } + } + unitID = unitID + 1; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + } + unitID = unitID + 1; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + if (param_2) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::placeSiegeTentAtAttackAngle, this)(newTribeID, + DAT_TroopValueState::instance.attackInfo.engineerBuildQueue[q].engineerBuildingType); + } else { + MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::placeSiegeTentOrTunnelAtSuitableLocationAndAssignEngineers, + this)(newTribeID, + DAT_TroopValueState::instance.attackInfo.engineerBuildQueue[q].engineerBuildingType, 0, + Map::Units::UIT_CONSTRUCT_SIEGE_EQUIPMENTOIL_DUTYENGINEERRELATED); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/placeTunnelEntrances.cpp b/src/OpenSHC/Map/Units/TroopValueState/placeTunnelEntrances.cpp new file mode 100644 index 0000000000..27745f3b11 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/placeTunnelEntrances.cpp @@ -0,0 +1,57 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitInstructionType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B570 + int TroopValueState::placeTunnelEntrances(int tribeID) + { + int const unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + int const tentPointIndex = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::algFindAttackAngle, DAT_PathFindingState::ptr)( + 200, DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, tribeID); + if (tentPointIndex) { + short const previousSiegeIndex = DAT_TribesState::instance.tribes[tribeID].siegeIndexValue1; + int const tribeUID = DAT_TribesState::instance.tribes[tribeID].uid; + DAT_TribesState::instance.tribes[tribeID].siegeIndexValue2 = previousSiegeIndex; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[previousSiegeIndex].tribeID = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[previousSiegeIndex].tribeUID = 0; + int const tentPointY = DAT_TroopValueState::instance.attackInfo.tentPointsValues[tentPointIndex].y; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[tentPointIndex].tribeUID = tribeUID; + int const playerID = DAT_TribesState::instance.tribes[tribeID].owner; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[tentPointIndex].tribeID = tribeID; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[tentPointIndex].three = 3; + int const tentPointX = DAT_TroopValueState::instance.attackInfo.tentPointsValues[tentPointIndex].x; + DAT_TribesState::instance.tribes[tribeID].siegeIndexValue1 = tentPointIndex; + MACRO_CALL_MEMBER(Map::TileMapState_Func::placeBuilding, DAT_TileMapState::ptr)( + playerID, tentPointX - 1, tentPointY - 1, Commands::M_MAPPER_TUNNEL_CONSTRUCTION, 3, 15); + int const buildingID = DAT_TileMapState::instance.placedBuildingID; + if (buildingID <= 0) { + return 0; + } + DAT_BuildingsState::instance.buildings[buildingID].attackWave + = DAT_TribesState::instance.tribes[tribeID].attackWave; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeAnInstruction, DAT_TribesState::ptr)( + tribeID, UIT_DIG_TUNNEL, buildingID, DAT_BuildingsState::instance.buildings[buildingID].uid, 0); + return tentPointIndex; + } + return 0; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/placeTunnelForEveryTunneler.cpp b/src/OpenSHC/Map/Units/TroopValueState/placeTunnelForEveryTunneler.cpp new file mode 100644 index 0000000000..d1459eaeac --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/placeTunnelForEveryTunneler.cpp @@ -0,0 +1,106 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using Game::GameMode2; + using Map::MapType2; + using Map::Units::SomeTribeBehaviorType; + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BA30 + void TroopValueState::placeTunnelForEveryTunneler(int attackWave) + { + int _playerID = (char)DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackWave]; + int _nTunnelers = 0; + if (!_playerID) { + _playerID = 2; + } + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + _playerID = 2; + } + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].owner == _playerID + && !DAT_UnitsState::instance.units[unitID].dying + && DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_TUNNELER) { + _nTunnelers++; + } + } + int _tribeID = 1; + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize = 0; + for (; _tribeID < 1250; ++_tribeID) { + if (DAT_TribesState::instance.tribes[_tribeID].tribeState + && DAT_TribesState::instance.tribes[_tribeID].owner == _playerID + && DAT_TribesState::instance.tribes[_tribeID].attackWave == attackWave + && DAT_TribesState::instance.tribes[_tribeID].tribeType == AI::Tribes::AITT_TUNNELERS) { + DAT_TroopValueState::instance.attackInfo + .tribeIDArray[DAT_TroopValueState::instance.attackInfo.tribeIDArraySize] = _tribeID; + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize + = DAT_TroopValueState::instance.attackInfo.tribeIDArraySize + 1; + } + } + if (!DAT_TroopValueState::instance.attackInfo.tribeIDArraySize) { + return; + } + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; ++i) { + _tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[i]; + while (DAT_TribesState::instance.tribes[_tribeID].size) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::popUnitFromTribe, DAT_TribesState::ptr)(_tribeID); + } + } + // The new tribes copy their wave and counter from the last tunneler tribe emptied above. + int unitID = 0; + for (int n = 0; n < _nTunnelers; ++n) { + int const _tribe + = MACRO_CALL_MEMBER(Map::Units::TribesState_Func::createTribe, DAT_TribesState::ptr)(_playerID, 0); + DAT_TribesState::instance.tribes[_tribe].tribeType = AI::Tribes::AITT_TUNNELERS; + DAT_TribesState::instance.tribes[_tribe].tribeBehaviorType = Map::Units::STBT_0x415; + DAT_TribesState::instance.tribes[_tribe].attackWave + = DAT_TribesState::instance.tribes[_tribeID].attackWave; + DAT_TribesState::instance.tribes[_tribe].attackInfo_someCounter1 + = DAT_TribesState::instance.tribes[_tribeID].attackInfo_someCounter1; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + while (!(DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].owner == _playerID + && !DAT_UnitsState::instance.units[unitID].dying + && DAT_UnitsState::instance.units[unitID].unitType == Map::Units::UT_TUNNELER + && !DAT_UnitsState::instance.units[unitID].tribeID)) { + ++unitID; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::addUnitToTribe, DAT_TribesState::ptr)(unitID, _tribe); + unitID++; + if (unitID >= DAT_UnitsState::instance.maxUnitCount) { + return; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::placeTunnelEntrances, this)(_tribe); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/playAttackAlarmSound.cpp b/src/OpenSHC/Map/Units/TroopValueState/playAttackAlarmSound.cpp new file mode 100644 index 0000000000..593098f863 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/playAttackAlarmSound.cpp @@ -0,0 +1,44 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Audio/MSS/SoundSystem.func.hpp" +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/string-macros.hpp" + +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_SoundSystemState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B6C0 + void TroopValueState::playAttackAlarmSound() + { + MACRO_CALL_MEMBER( + Audio::MSS::SoundSystem_Func::playBattleGloryMusicIfConditionsMet, DAT_SoundSystemState::ptr)(); + switch (DAT_TroopValueState::instance.attackInfo.attacker) { + case 5: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playOnSpeechSfxStream, DAT_SFXState::ptr)( + S_DRUMHORN_WAV_005ABB58); + break; + case 2: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playOnSpeechSfxStream, DAT_SFXState::ptr)( + S_BATTLEHORN_WAV_005A77D8); + break; + case 3: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playOnSpeechSfxStream, DAT_SFXState::ptr)( + S_BATTLEHORN_WAV_005A77D8); + break; + case 4: + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playOnSpeechSfxStream, DAT_SFXState::ptr)( + S_BATTLEHORN_WAV_005A77D8); + break; + } + DAT_TroopValueState::instance.attackInfo.attackAlarmPlayed = TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/processSpottedEnemyTiles.cpp b/src/OpenSHC/Map/Units/TroopValueState/processSpottedEnemyTiles.cpp new file mode 100644 index 0000000000..cf56da1d1b --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/processSpottedEnemyTiles.cpp @@ -0,0 +1,49 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/MapConstants.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051F950 + void TroopValueState::processSpottedEnemyTiles() + { + for (int i = 0; i < countof(DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles); ++i) { + int const tile = DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].tile; + if (tile <= 0) { + continue; + } + if (MC_MAP_TILES_COUNT <= tile) { + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].tile = 0; + } + if (DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].value == 100) { + BOOLEnum const shouldLightPitch + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::shouldLightPitchBasedOnTroopValue, this)( + tile, DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID, + DAT_TroopValueState::instance.attackInfo.playerID_0x2c850); + if (shouldLightPitch) { + MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::giveLightPitchInstructionToUnitClosestToPitch, this)( + tile); + } + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].value = 99; + } else { + int const countdown = DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].value - 1; + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].value = countdown; + if (countdown <= 0) { + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[i].tile = 0; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/pruneStaleArcherPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleArcherPoints.cpp new file mode 100644 index 0000000000..01ed65e495 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleArcherPoints.cpp @@ -0,0 +1,47 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051AF70 + void TroopValueState::pruneStaleArcherPoints() + { + for (int slot = 1; slot < countof(DAT_TroopValueState::instance.attackInfo.archerPointArray) - 1; ++slot) { + if (DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].lastSeenCounter + != DAT_TroopValueState::instance.attackInfo.someCounter1) { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.archerPointArray[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.archerPointArray[slot]); + } else { + if (DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].tribeID != 0 + && MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::tribeCorrespondsWithUID, DAT_TribesState::ptr)( + DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].tribeID, + DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].tribeUID) + == FALSE) { + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.archerPointArray[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.archerPointArray[slot]); + } + if (DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].claimCooldown > 0) { + DAT_TroopValueState::instance.attackInfo.archerPointArray[slot].claimCooldown--; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/pruneStaleSupportPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleSupportPoints.cpp new file mode 100644 index 0000000000..76d4967d11 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleSupportPoints.cpp @@ -0,0 +1,44 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B110 + void TroopValueState::pruneStaleSupportPoints() + { + for (int slot = 1; slot < countof(DAT_TroopValueState::instance.attackInfo.supportPointsArray) - 1; + ++slot) { + if (DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].someCounter + != DAT_TroopValueState::instance.attackInfo.someCounter1) { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot]); + } else { + if (DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].tribeID != 0 + && MACRO_CALL_MEMBER(TribesState_Func::tribeCorrespondsWithUID, DAT_TribesState::ptr)( + DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].tribeID, + DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].tribeUID) + == FALSE) { + MACRO_CALL_MEMBER( + OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot]); + } + if (DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].three > 0) { + DAT_TroopValueState::instance.attackInfo.supportPointsArray[slot].three--; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/pruneStaleTentPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleTentPoints.cpp new file mode 100644 index 0000000000..bf18f43d52 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/pruneStaleTentPoints.cpp @@ -0,0 +1,45 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051B290 + void TroopValueState::pruneStaleTentPoints() + { + // The original walks slots 1..1999 (end 0x17A52B4): the tent point table really has 2000 entries, + // tentPointsValues is declared with only 216. + for (int slot = 1; slot < countof(DAT_TroopValueState::instance.attackInfo.tentPointsValues) - 1; ++slot) { + if (DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].someCounter + != DAT_TroopValueState::instance.attackInfo.someCounter1) { + MACRO_CALL_MEMBER(OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot]); + } else { + if (DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].tribeID != 0 + && MACRO_CALL_MEMBER(TribesState_Func::tribeCorrespondsWithUID, DAT_TribesState::ptr)( + DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].tribeID, + DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].tribeUID) + == FALSE) { + MACRO_CALL_MEMBER( + OpenSHC::IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot]), 0, + &DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot]); + } + if (DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].three > 0) { + DAT_TroopValueState::instance.attackInfo.tentPointsValues[slot].three--; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/queueOilThrowForIdleArchers.cpp b/src/OpenSHC/Map/Units/TroopValueState/queueOilThrowForIdleArchers.cpp new file mode 100644 index 0000000000..5c0fb104a1 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/queueOilThrowForIdleArchers.cpp @@ -0,0 +1,58 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.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_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051C470 + void TroopValueState::queueOilThrowForIdleArchers() + { + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_INVISIBLE) { + continue; + } + BOOLEnum const isAIPlayerWithKeep + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)( + DAT_UnitsState::instance.units[unitID].owner); + if (!isAIPlayerWithKeep || DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_E_ENGINEER + || DAT_UnitsState::instance.units[unitID].dying != 0 + || DAT_UnitsState::instance.units[unitID].state.generic != Map::Units::States::US_SIT_DOWNUnk) { + continue; + } + int radius = 3; + if (110 < DAT_UnitsState::instance.units[unitID].terrainOrClimbHeight) { + radius = 5; + } + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeTotalUnitsWithinDistance, + DAT_PathFindingState::ptr)(DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID, 0, 0, + DAT_UnitsState::instance.units[unitID].tile, radius); + if (DAT_PathFindingState::instance.ALGO_TotalTroopValue < 10 + || DAT_PathFindingState::instance.ALGO_TotalTroopCount < 2 + || !DAT_PathFindingState::instance.field_0x64) { + continue; + } + int const targetUnitID = DAT_PathFindingState::instance.field_0x64; + DAT_UnitsState::instance.units[unitID].targetingType = Map::Units::UIT_THROW_OIL; + DAT_UnitsState::instance.units[unitID].attackAtTileX = DAT_UnitsState::instance.units[targetUnitID].x; + DAT_UnitsState::instance.units[unitID].attackAtTileY = DAT_UnitsState::instance.units[targetUnitID].y; + DAT_UnitsState::instance.units[unitID].animationFrame = 4; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/recomputeAttackInfo.cpp b/src/OpenSHC/Map/Units/TroopValueState/recomputeAttackInfo.cpp new file mode 100644 index 0000000000..eb3245ec77 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/recomputeAttackInfo.cpp @@ -0,0 +1,840 @@ +#include "../TroopValueState.asm.hpp" +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/TileMapState.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Buildings/BuildingTypeShort.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingDefinedData.hpp" +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.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 Game::GameMode; + using Map::Buildings::BuildingType; + using Map::Buildings::BuildingTypeShort; + using Map::Units::UnitType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051E300 + void TroopValueState::recomputeAttackInfo(int playerID, int attackedPlayerID) + { + int _tileDefaultHeight; + undefined2 _pathConnectionValue; + BOOLEnum _teamsDifferent; + uint _neighbourTotalHeight; + int (*_pDirectionRow)[8]; + int _specialArea; + int _candidateTile; + uint _totalHeight; + int _tile; + int _directionStep; + int* _pValueCount; + int _direction; + BOOLEnum _isSolitary; + uint _defaultHeight; + int _directionMin1; + BuildingTypeShort _buildingType; + int _y; + ushort _unitID; + uint _logic; + ushort _buildingID; + int _someY; + int _someLimit; + ushort _area; + int _startCon; + _isSolitary = DAT_GameSynchronyState::instance.currentGameMode == Game::GM_SOLITARY; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack2 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale2 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].stone1 = 0; + DAT_TroopValueState::instance.attackInfo.high1 = 0; + DAT_TroopValueState::instance.attackInfo.arch1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gate1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town1 = 0; + DAT_TroopValueState::instance.attackInfo.people1 = 0; + DAT_TroopValueState::instance.attackInfo.lord1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].some1000ArrayIndex_2 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide2 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town2 = 0; + DAT_TroopValueState::instance.attackInfo.people2 = 0; + DAT_TroopValueState::instance.attackInfo.lord3 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat1 = 0; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat2 = 0; + DAT_TroopValueState::instance.attackInfo.high2 = 0; + DAT_TroopValueState::instance.attackInfo.arch2 = 0; + DAT_TroopValueState::instance.attackInfo.scaleZone = 1000; + DAT_TroopValueState::instance.attackInfo.someArea = 0; + DAT_TroopValueState::instance.attackInfo.field127541_0x2c85c = 0; + if (!_isSolitary) { + DAT_TroopValueState::instance.attackInfo.someDistanceLimit = 4; + } else { + DAT_TroopValueState::instance.attackInfo.someDistanceLimit + = ((3000 <= DAT_TroopValueState::instance.attackInfo.zoneSize) - 1 & 6) + 4; + } + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hackValuesArray), '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hackValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 16016, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(16000, '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 16016, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(16000, '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].townValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 4992, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 16000, '\0', (void*)((int)(DAT_TroopValueState::instance.attackInfo.peopleValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 14672, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + 16000, '\0', (void*)((int)(DAT_TroopValueState::instance.attackInfo.lordValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 16016, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(16000, '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gateValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 10992, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(16000, '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wideValuesArray))); + // FIXME:: this clears 16000 bytes where the member is 8992, so sizeof would change the clear; + // the constant stays until the header is corrected. + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)(16000, '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moatValuesArray))); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.high2ValuesArray), '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.high2ValuesArray))); + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.arch2ValuesArray), '\0', + (void*)((int)(DAT_TroopValueState::instance.attackInfo.arch2ValuesArray))); + _startCon = DAT_TroopValueState::instance.attackInfo.startCon; + _tile = 0; + do { + _unitID = DAT_TileMapState::instance.UnitLayer[_tile]; + _logic = DAT_TileMapState::instance.LogicLayer[_tile]; + if (_unitID || (_logic & 0x50000700)) { + _buildingID = DAT_TileMapState::instance.BuildingLayer[_tile]; + if (_buildingID) { + int _buildingID_2 = (int)(short)_buildingID; + _buildingType = DAT_BuildingsState::instance.buildings[_buildingID_2].buildingType; + if (_buildingType == Map::Buildings::BT_GATEHOUSELARGE) { + int _buildingHeight_GHL + = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(_buildingID_2); + _defaultHeight + = _buildingHeight_GHL + (uint)DAT_TileMapState::instance.DefaultHeightLayer[_tile]; + } else if (_buildingType == Map::Buildings::BT_GATEHOUSESMALL) { + int _buildingHeight_GHS + = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(_buildingID_2); + _defaultHeight + = _buildingHeight_GHS + (uint)DAT_TileMapState::instance.DefaultHeightLayer[_tile]; + } else { + int _buildingHeight_else + = MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::getBuildingHeightForBuildingID, + DAT_BuildingsState::ptr)(_buildingID_2); + _defaultHeight = _buildingHeight_else + (uint)DAT_TileMapState::instance.HeightLayer[_tile]; + } + } else { + _defaultHeight = (uint)DAT_TileMapState::instance.HeightLayer[_tile]; + } + if (_unitID) { + _teamsDifferent = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::getTeamsDifferent, + DAT_GameState::ptr)((int)DAT_UnitsState::instance.units[(short)_unitID].owner, playerID); + if (_teamsDifferent) { + /* + path cost is zero + */ + if (DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[attackedPlayerID][_tile] + <= 0) { + continue; + } + if ((short)DAT_TileMapState::instance.PathConnectionLayer[_tile] == _startCon + && DAT_UnitsState::instance.units[(short)_unitID].isStalked == 0 + && DAT_UnitsState::instance.units[(short)_unitID].isSelectable_OR_matchTime != 0) { + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeY + = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + DAT_TroopValueState::instance.attackInfo.people1 + = DAT_TroopValueState::instance.attackInfo.people1 + 1; + MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS(); + _directionStep = 0; + do { + if (DAT_TileMapState::instance.specialAreasArray[_directionStep] == _startCon) { + if ((999 < DAT_TroopValueState::instance.attackInfo.people2) + || (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, + this)(_tile), + _teamsDifferent)) { + break; + } + uint _height1 = MACRO_CALL_MEMBER( + Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)( + DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile); + _y = DAT_TileMapState::instance.DAT_SomeY; + if ((int)_defaultHeight <= (int)(_height1 + 0x10) + && (int)(_height1 - 0x10) <= (int)_defaultHeight) { + DAT_TroopValueState::instance.attackInfo + .peopleValuesArray[DAT_TroopValueState::instance.attackInfo.people2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo + .peopleValuesArray[DAT_TroopValueState::instance.attackInfo.people2] + .tile2 = DAT_TileMapState::instance + .directionTranslationMatrix[_y][_directionStep] + + _tile; + DAT_TroopValueState::instance.attackInfo.people2 + = DAT_TroopValueState::instance.attackInfo.people2 + 1; + } + } + _directionStep += 2; + } while (_directionStep < 8); + } + if (DAT_UnitsState::instance.units[(short)_unitID].unitType == Map::Units::UT_LORD) { + DAT_TroopValueState::instance.attackInfo.someArea + = (int)(short)DAT_TileMapState::instance.PathConnectionLayer[_tile]; + DAT_TroopValueState::instance.attackInfo.lord1 + = DAT_TroopValueState::instance.attackInfo.lord1 + 1; + if (DAT_TroopValueState::instance.attackInfo.someArea + == DAT_TroopValueState::instance.attackInfo.startCon) { + DAT_TileMapState::instance.DAT_SomeY + = (int) + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + _candidateTile = 0; + _pDirectionRow = DAT_TileMapState::instance.directionTranslationMatrix + + DAT_TileMapState::instance.DAT_SomeY; + _directionStep = DAT_TroopValueState::instance.attackInfo.lord3; + do { + int _neighbourTile = (*_pDirectionRow)[0] + _tile; + if (DAT_TileMapState::instance.PathConnectionLayer[_neighbourTile] != 0) { + if (999 < _directionStep) { + break; + } + DAT_TroopValueState::instance.attackInfo.lordValuesArray[_directionStep] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo + .lordValuesArray[DAT_TroopValueState::instance.attackInfo.lord3] + .tile2 = _neighbourTile; + _directionStep = DAT_TroopValueState::instance.attackInfo.lord3 + 1; + DAT_TroopValueState::instance.attackInfo.lord3 = _directionStep; + } + _candidateTile++; + _pDirectionRow = (int (*)[8])(*_pDirectionRow + 1); + } while (_candidateTile < 8); + } + } + } + } + undefined* _pPathConnectionBase = (undefined*)(DAT_TileMapState::instance.ptr_SpecialAreasArray); + undefined* _pSpecialAreasBase = (undefined*)(DAT_TileMapState::instance.ptr_PathConnectionLayer); + if ((!_buildingID) + || (DAT_BuildingsState::instance.buildings[(short)_buildingID].owner != attackedPlayerID)) { + if (!(_logic & 0x300) || (_logic & 2)) { + if ((_logic & 0x40000000) + && 0 < DAT_TileMapState::instance + .SEC_PathfindingCostTileMap1105[attackedPlayerID][_tile]) { + _tileDefaultHeight = DAT_TileMapState::instance.DefaultHeightLayer[_tile]; + _pValueCount = &DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat1; + *_pValueCount = (int)((undefined*)(*_pValueCount + 1)); + _pPathConnectionBase = (undefined*)(DAT_TileMapState::instance.ptr_SpecialAreasArray); + _pSpecialAreasBase = (undefined*)(DAT_TileMapState::instance.ptr_PathConnectionLayer); + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeY + = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS(); + _directionStep = 0; + do { + _candidateTile = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)((int)(DAT_TileMapState::instance.specialAreasArray[_directionStep])), + 0); + if (_candidateTile) { + if (999 < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .moat2 + || (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, + this)(_tile), + _teamsDifferent)) { + break; + } + _candidateTile + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + if (!(DAT_TileMapState::instance.LogicLayer[_candidateTile] & 0x10000100U) + && (_totalHeight + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_candidateTile), + _someY = DAT_TileMapState::instance.DAT_SomeY, + _tileDefaultHeight <= (int)(_totalHeight + 0x10) + && ((int)(_totalHeight - 0x10) <= _tileDefaultHeight))) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .moatValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .moat2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .moatValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .moat2] + .tile2 = DAT_TileMapState::instance + .directionTranslationMatrix[_someY][_directionStep] + + _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat2 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .moat2 + + 1; + if (DAT_TileMapState::instance.AIZoneLayer[_tile] != 0) { + _directionStep + = DAT_ViewportRenderState::instance.translationMatrix[_someY] + .addXgetTile; + _someLimit = 1; + // Duplicated from the gate scan; _someLimit is 1 here rather than + // someDistanceLimit. + DAT_TileMapState::instance.DAT_SomeX = _tile - _directionStep; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(_someLimit, + (uint)(DAT_TileMapState::instance.DAT_SomeX), (uint)(_someY), 1); + } + break; + } + } + _directionStep += 2; + } while (_directionStep < 8); + } + } else if (0 + < DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[attackedPlayerID][_tile] + && (DAT_TileMapState::instance.WallOwnerLayer[_tile] & 7) + 1 == attackedPlayerID + && (_totalHeight = (uint)DAT_TileMapState::instance.DefaultHeightLayer[_tile], + 0x14 < (int)(DAT_TileMapState::instance.HeightLayer[_tile] - _totalHeight)) + && !_buildingID) { + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeY + = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS(); + MACRO_ASM_FILL_DIAGONAL_PATH_CONNECTIONS(); + for (_direction = 0; _direction < 8; _direction += 2) { + dword _toArea = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)((int)(DAT_TileMapState::instance.specialAreasArray[_direction])), 0); + if (_toArea) { + _directionMin1 = _direction - 1; + if (_directionMin1 < 0) { + _directionMin1 = 7; + } + _directionStep = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)(( + int)((int)DAT_TileMapState::instance.specialAreasArray[_direction + 1])), + 0); + if (_directionStep + && (_directionStep + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)(( + int)(DAT_TileMapState::instance.specialAreasArray[_directionMin1])), + 0), + _directionStep != 0) + && !(DAT_TileMapState::instance.AIInfoLayer[_tile] & 0x20)) { + if (999 < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .wide2 + || (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, + this)(_tile), + _teamsDifferent)) { + break; + } + _directionStep + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_direction] + + _tile; + if (!(DAT_TileMapState::instance.LogicLayer[_directionStep] & 0x10000100U) + && (_neighbourTotalHeight + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_directionStep), + (int)_totalHeight <= (int)(_neighbourTotalHeight + 0x10) + && ((int)(_neighbourTotalHeight - 0x10) <= (int)_totalHeight))) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .wideValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .wide2] + .tile = _tile; + _directionStep = DAT_TileMapState::instance.DAT_SomeY; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .wideValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .wide2] + .tile2 + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_direction] + + _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide2 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .wide2 + + 1; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide1 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .wide1 + + 1; + DAT_TileMapState::instance.DAT_SomeX = _tile + - DAT_ViewportRenderState::instance.translationMatrix[_directionStep] + .addXgetTile; + MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::updateAIZoneWithFloodFill0x20, + DAT_PathFindingState::ptr)(4, + (uint)(DAT_TileMapState::instance.DAT_SomeX), (uint)(_directionStep)); + continue; + } + } + } + } + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].stone1 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].stone1 + 1; + _directionStep = 0; + do { + _candidateTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)((int)(DAT_TileMapState::instance.specialAreasArray[_directionStep])), 0); + if (_candidateTile) { + if (999 < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack2 + || (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, this)( + _tile), + _teamsDifferent)) { + break; + } + _candidateTile + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + if (!(DAT_TileMapState::instance.LogicLayer[_candidateTile] & 0x10000100U) + && (_neighbourTotalHeight + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_candidateTile), + _candidateTile = DAT_TileMapState::instance.DAT_SomeY, + (int)_totalHeight <= (int)(_neighbourTotalHeight + 0x10) + && ((int)(_neighbourTotalHeight - 0x10) <= (int)_totalHeight))) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .hackValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .hack2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .hackValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .hack2] + .tile2 = DAT_TileMapState::instance + .directionTranslationMatrix[_candidateTile][_directionStep] + + _tile; + _directionStep + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack2; + DAT_TileMapState::instance.AIZoneLayer[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .hackValuesArray[_directionStep] + .tile2] = 10; + _tileDefaultHeight = DAT_TileMapState::instance.AIZoneLayer[_tile]; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack2 + = _directionStep + 1; + if (_tileDefaultHeight) { + DAT_TileMapState::instance.DAT_SomeX = _tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_TileMapState::instance.DAT_SomeY] + .addXgetTile; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)( + DAT_TroopValueState::instance.attackInfo.someDistanceLimit, + (uint)(DAT_TileMapState::instance.DAT_SomeX), + (uint)(DAT_TileMapState::instance.DAT_SomeY), 1); + } + break; + } + } + _directionStep += 2; + } while (_directionStep < 8); + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack1 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].hack1 + 1; + if (!(_logic & 0x400000)) { + _directionStep = (int)(char)DAT_TileMapState::instance.AIZoneLayer[_tile]; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale1 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale1 + 1; + if ((_directionStep) + && (_directionStep < DAT_TroopValueState::instance.attackInfo.scaleZone)) { + DAT_TroopValueState::instance.attackInfo.scaleZone = _directionStep; + } + _directionStep = 0; + do { + _candidateTile = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo.startCon)), + (dword)((int)(DAT_TileMapState::instance.specialAreasArray[_directionStep])), + 0); + if (_candidateTile) { + if (999 < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .scale2 + || (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, + this)(_tile), + _teamsDifferent)) { + break; + } + _candidateTile + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + if (!(DAT_TileMapState::instance.LogicLayer[_candidateTile] & 0x10000100U) + && (_neighbourTotalHeight + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_candidateTile), + (int)_totalHeight <= (int)(_neighbourTotalHeight + 0x10) + && ((int)(_neighbourTotalHeight - 0x10) <= (int)_totalHeight))) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .scaleValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .scale2] + .tile = _tile; + _someY = DAT_TileMapState::instance.DAT_SomeY; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .scaleValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .scale2] + .tile2 + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale2 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .scale2 + + 1; + // Duplicated from the gate scan: the original shared one tail block between + // the two scans. + if (DAT_TileMapState::instance.AIZoneLayer[_tile] != 0) { + _directionStep + = DAT_ViewportRenderState::instance.translationMatrix[_someY] + .addXgetTile; + _someLimit = DAT_TroopValueState::instance.attackInfo.someDistanceLimit; + DAT_TileMapState::instance.DAT_SomeX = _tile - _directionStep; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(_someLimit, + (uint)(DAT_TileMapState::instance.DAT_SomeX), (uint)(_someY), 1); + } + break; + } + } + _directionStep += 2; + } while (_directionStep < 8); + } + } + } else { + int _buildingTypeInt + = (int)(short)DAT_BuildingsState::instance.buildings[(short)_buildingID].buildingType; + _totalHeight = (uint)DAT_TileMapState::instance.DefaultHeightLayer[_tile]; + if (DAT_BuildingDefinedData::instance.IsGateOrTowerArray[_buildingTypeInt]) { + if (0 + < DAT_TileMapState::instance.SEC_PathfindingCostTileMap1105[attackedPlayerID][_tile]) { + _pValueCount = &DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gate1; + *_pValueCount = (int)((undefined*)(*_pValueCount + 1)); + _pPathConnectionBase = (undefined*)(DAT_TileMapState::instance.ptr_SpecialAreasArray); + _pSpecialAreasBase = (undefined*)(DAT_TileMapState::instance.ptr_PathConnectionLayer); + DAT_TileMapState::instance.DAT_SomeY + = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeX = _tile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_TileMapState::instance.DAT_SomeY] + .addXgetTile; + MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS(); + _directionStep = 0; + // The decompiler encoded this scan as a label plus a fragment after the tile + // loop: its "break" was the back edge and falling off the end left the scan. + for (_directionStep = 0; _directionStep < 8; _directionStep += 2) { + if (DAT_TileMapState::instance.specialAreasArray[_directionStep] + != DAT_TroopValueState::instance.attackInfo.startCon) { + continue; + } + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .some1000ArrayIndex_2 + < 1000 + && (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::getTileInTargetedBuildingTiles, this)( + _tile), + !_teamsDifferent)) { + _candidateTile + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + if ((DAT_TileMapState::instance.LogicLayer[_candidateTile] & 0x10000100U) + || (_neighbourTotalHeight + = MACRO_CALL_MEMBER(Map::TileMapState_Func::getTotalHeightAtTile, + DAT_TileMapState::ptr)(_candidateTile), + (int)(_neighbourTotalHeight + 0x10) < (int)_totalHeight + || ((int)_totalHeight < (int)(_neighbourTotalHeight - 0x10)))) { + continue; + } + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .gateValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .some1000ArrayIndex_2] + .tile = _tile; + _someY = DAT_TileMapState::instance.DAT_SomeY; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .gateValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .some1000ArrayIndex_2] + .tile2 + = DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .some1000ArrayIndex_2 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .some1000ArrayIndex_2 + + 1; + if (DAT_TileMapState::instance.AIZoneLayer[_tile] != 0) { + _directionStep = DAT_ViewportRenderState::instance.translationMatrix[_someY] + .addXgetTile; + _someLimit = DAT_TroopValueState::instance.attackInfo.someDistanceLimit; + DAT_TileMapState::instance.DAT_SomeX = _tile - _directionStep; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func:: + recomputeALGPathFindingTileMapUnk, + DAT_PathFindingState::ptr)(_someLimit, + (uint)(DAT_TileMapState::instance.DAT_SomeX), (uint)(_someY), 1); + } + } + break; + } + } + } else { + if (DAT_BuildingDefinedData::instance.BuildingTypeHasHealth[_buildingTypeInt] != 0) { + if (!(DAT_TileMapState::instance.LogicLayer[_tile] & 0xf000000U)) { + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeY + = (int) + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + _pValueCount + = &DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town1; + *_pValueCount = (int)((undefined*)(*_pValueCount + 1)); + _pPathConnectionBase + = (undefined*)(DAT_TileMapState::instance.ptr_SpecialAreasArray); + _pSpecialAreasBase + = (undefined*)(DAT_TileMapState::instance.ptr_PathConnectionLayer); + MACRO_ASM_FILL_CARDINAL_PATH_CONNECTIONS(); + _directionStep = 0; + do { + if (DAT_TileMapState::instance.specialAreasArray[_directionStep] + == DAT_TroopValueState::instance.attackInfo.startCon) { + if (999 < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .town2 + || (_teamsDifferent + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func:: + getTileInTargetedBuildingTiles, + this)(_tile), + _teamsDifferent)) { + break; + } + uint _heightTown = MACRO_CALL_MEMBER( + Map::TileMapState_Func::getTotalHeightAtTile, DAT_TileMapState::ptr)( + DAT_TileMapState::instance + .directionTranslationMatrix[DAT_TileMapState::instance.DAT_SomeY] + [_directionStep] + + _tile); + _candidateTile = DAT_TileMapState::instance.DAT_SomeY; + if ((int)_totalHeight <= (int)(_heightTown + 0x10) + && (int)(_heightTown - 0x10) <= (int)_totalHeight) { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .townValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .town2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .townValuesArray[DAT_TroopValueState::instance.attackInfo + .playerInfo[playerID - 1] + .town2] + .tile2 + = DAT_TileMapState::instance + .directionTranslationMatrix[_candidateTile][_directionStep] + + _tile; + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town2 + = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1] + .town2 + + 1; + break; + } + } + _directionStep += 2; + } while (_directionStep < 8); + } + } else { + if ((_logic & 0x10000000)) { + _area = DAT_TileMapState::instance.PathConnectionLayer[_tile]; + if (0 < DAT_TileMapState::instance + .SEC_PathfindingCostTileMap1105[attackedPlayerID][_tile]) { + int _buildingID_3 = (int)DAT_TileMapState::instance.BuildingLayer[_tile]; + _teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::attackInfoHasHigh2Building, this)( + _buildingID_3); + if ((!_teamsDifferent) + && (_teamsDifferent = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::attackInfoHasArch2Building, this)( + _buildingID_3), + _pPathConnectionBase + = (undefined*)DAT_TileMapState::instance.ptr_SpecialAreasArray, + _pSpecialAreasBase + = (undefined*)DAT_TileMapState::instance.ptr_AIZoneLayer, + !_teamsDifferent)) { + DAT_TileMapState::instance.DAT_SomeTile = _tile; + DAT_TileMapState::instance.DAT_SomeY + = (int)DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[_tile]; + _pValueCount = (int*)((char*)DAT_TileMapState::instance + .ptr_MovementDirectionTranslationMatrix + + DAT_TileMapState::instance.DAT_SomeY * 0x10); + _pathConnectionValue + = *(undefined2*)(((char*)DAT_TileMapState::instance.ptr_AIZoneLayer) + + _tile - 1); + *(undefined2*)(((char*)DAT_TileMapState::instance.ptr_SpecialAreasArray) + + 2) + = *(undefined2*)(((char*)DAT_TileMapState::instance.ptr_AIZoneLayer) + + _tile + 1); + *(undefined2*)(_pPathConnectionBase + 6) = _pathConnectionValue; + _pathConnectionValue + = *(undefined2*)(_pSpecialAreasBase + _pValueCount[4] + _tile); + *(undefined2*)_pPathConnectionBase + = *(undefined2*)(_pSpecialAreasBase + *_pValueCount + _tile); + *(undefined2*)(_pPathConnectionBase + 4) = _pathConnectionValue; + _specialArea = 0; + short* _pSpecialArea = DAT_TileMapState::instance.specialAreasArray; + do { + if (*_pSpecialArea != 0) { + _specialArea = (int)*_pSpecialArea; + } + _pSpecialArea += 2; + } while ((int)_pSpecialArea + < (int)&DAT_TileMapState::instance.specialAreasArray[8]); + if (0 < _specialArea) { + DAT_BuildingsState::instance.buildings[_buildingID_3] + .unknownCounterTo10000_0x2b4 = (short)_specialArea; + _teamsDifferent = MACRO_CALL_MEMBER( + Map::Buildings::BuildingsState_Func::hasHumanPlayerUnitsOnBuilding, + DAT_BuildingsState::ptr)(_buildingID_3); + if (_teamsDifferent) { + DAT_TroopValueState::instance.attackInfo.high1 + = DAT_TroopValueState::instance.attackInfo.high1 + 1; + if (DAT_TroopValueState::instance.attackInfo.high2 < 1000 + && (_directionStep = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo + .startCon)), + (dword)((int)((short)_area)), 0), + _directionStep != 0)) { + DAT_TroopValueState::instance.attackInfo + .high2ValuesArray[DAT_TroopValueState::instance.attackInfo + .high2] + .buildingID = _buildingID_3; + DAT_TroopValueState::instance.attackInfo + .high2ValuesArray[DAT_TroopValueState::instance.attackInfo + .high2] + .tile2 = _tile; + DAT_TroopValueState::instance.attackInfo + .high2ValuesArray[DAT_TroopValueState::instance.attackInfo + .high2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo.high2 + = DAT_TroopValueState::instance.attackInfo.high2 + 1; + } + } else { + DAT_TroopValueState::instance.attackInfo.arch1 + = DAT_TroopValueState::instance.attackInfo.arch1 + 1; + if (DAT_TroopValueState::instance.attackInfo.arch2 < 1000 + && (_directionStep = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func:: + calculateCanPlayerUnitsNavigateToAreaFromArea, + DAT_PathFindingState::ptr)(playerID, + (dword)((int)(DAT_TroopValueState::instance.attackInfo + .startCon)), + (dword)((int)((short)_area)), 0), + _directionStep != 0)) { + DAT_TroopValueState::instance.attackInfo + .arch2ValuesArray[DAT_TroopValueState::instance.attackInfo + .arch2] + .buildingID = _buildingID_3; + DAT_TroopValueState::instance.attackInfo + .arch2ValuesArray[DAT_TroopValueState::instance.attackInfo + .arch2] + .tile2 = _tile; + DAT_TroopValueState::instance.attackInfo + .arch2ValuesArray[DAT_TroopValueState::instance.attackInfo + .arch2] + .tile = _tile; + DAT_TroopValueState::instance.attackInfo.arch2 + = DAT_TroopValueState::instance.attackInfo.arch2 + 1; + } + } + } + } + } + } + } + } + } + } + _tile++; + if (0x13a0f < _tile) { + return; + } + } while (true); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/recomputeTargetedBuildingTilesArray.cpp b/src/OpenSHC/Map/Units/TroopValueState/recomputeTargetedBuildingTilesArray.cpp new file mode 100644 index 0000000000..de7dae0628 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/recomputeTargetedBuildingTilesArray.cpp @@ -0,0 +1,43 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x005198C0 + void TroopValueState::recomputeTargetedBuildingTilesArray(int playerID) + { + DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArraySize = 0; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArray), 0, + DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArray); + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + if (DAT_UnitsState::instance.units[unitID].logicalState == Map::Units::ULS_NORMAL + && DAT_UnitsState::instance.units[unitID].isSelectable_OR_matchTime + && DAT_UnitsState::instance.units[unitID].owner == playerID + && DAT_UnitsState::instance.units[unitID].targetedBuildingTile + && DAT_UnitsState::instance.units[unitID].unitType != Map::Units::UT_A_SLAVE) { + DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArray[DAT_TroopValueState::instance + .attackInfo.targetedBuildingTilesArraySize] + = DAT_UnitsState::instance.units[unitID].targetedBuildingTile; + ++DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArraySize; + if (countof(DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArray) + <= DAT_TroopValueState::instance.attackInfo.targetedBuildingTilesArraySize) { + break; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/recomputeTotalTroopValueOfTroopsNearKeep.cpp b/src/OpenSHC/Map/Units/TroopValueState/recomputeTotalTroopValueOfTroopsNearKeep.cpp new file mode 100644 index 0000000000..4841ffca6b --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/recomputeTotalTroopValueOfTroopsNearKeep.cpp @@ -0,0 +1,57 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051FA70 + void TroopValueState::recomputeTotalTroopValueOfTroopsNearKeep() + { + for (int player = 0; + player < countof(DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueOfTroopsNearKeep); + ++player) { + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueOfTroopsNearKeep[player] = 0; + } + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + Unit* const unit = &DAT_UnitsState::instance.units[unitID]; + if (unit->logicalState == Map::Units::ULS_INVISIBLE || unit->dying + || !unit->isSelectable_OR_matchTime) { + continue; + } + UnitTypeShort const troopType = unit->unitType; + if (troopType == Map::Units::UT_E_ENGINEER || troopType == Map::Units::UT_TUNNELER + || troopType == Map::Units::UT_E_LADDER) { + continue; + } + int const playerID = unit->owner; + MACRO_CALL_MEMBER(Map::Navigation::DirectionAlgorithmState_Func::setAxisBasedDistanceResult, + DAT_DirectionAlgorithmState::ptr)(DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .xEntry, + DAT_GameState::instance.playerDataArray[playerID] + .buildingEntryInfos[Game::Player::Enums::BEIE_CAMPGROUND] + .yEntry, + unit->x, unit->y); + if (DAT_DirectionAlgorithmState::instance.distanceHigh <= 40) { + int const troopValue = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getValueOfTroopType, + this)((Map::Units::UnitType)unit->unitType); + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueOfTroopsNearKeep[playerID] + += troopValue; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/recountAttackTroopValue.cpp b/src/OpenSHC/Map/Units/TroopValueState/recountAttackTroopValue.cpp new file mode 100644 index 0000000000..fb28a0fe30 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/recountAttackTroopValue.cpp @@ -0,0 +1,182 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/IO/LowLevelMemory.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/AI/Tribes/AIVUnitTypeTribeArrayOffset.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_LowLevelMemory.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051FB90 + void TroopValueState::recountAttackTroopValue(BOOLEnum param_1) + { + int const counter = param_1 ? 0x10 : DAT_TroopValueState::instance.attackInfo.counter; + DAT_TroopValueState::instance.attackInfo.counter = counter + 1; + if (16 <= DAT_TroopValueState::instance.attackInfo.counter) { + DAT_TroopValueState::instance.attackInfo.counter = 0; + DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated = FALSE; + DAT_TroopValueState::instance.attackInfo.size = 0; + DAT_TroopValueState::instance.attackInfo.aiTroops = 0; + DAT_TroopValueState::instance.attackInfo.attackTribeCount = 0; + DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen = 0; + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore = 0; + DAT_TroopValueState::instance.attackInfo.engineers = 0; + DAT_TroopValueState::instance.attackInfo.laddermen = 0; + DAT_TroopValueState::instance.attackInfo.assassinTribeCount = 0; + DAT_TroopValueState::instance.attackInfo.catapults = 0; + DAT_TroopValueState::instance.attackInfo.knights = 0; + DAT_TroopValueState::instance.attackInfo.ranged = 0; + MACRO_CALL_MEMBER(IO::LowLevelMemory_Func::fillMemory_ByteValue, DAT_LowLevelMemory::ptr)( + sizeof(DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType), '\0', + (void*)((int)(&DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType))); + for (int _tribeID = 1; _tribeID < 1250; ++_tribeID) { + if (DAT_TribesState::instance.tribes[_tribeID].tribeState != 0) { + BOOLEnum fullIDIsMinus1 + = MACRO_CALL_MEMBER(Game::GameStateStructures_Func::isFullIDEqualsToMinus1, + DAT_GameState::ptr)(DAT_TribesState::instance.tribes[_tribeID].owner); + if (!fullIDIsMinus1) { + if (DAT_TribesState::instance.tribes[_tribeID].owner + == DAT_GameSynchronyState::instance.currentPlayerSlotID) { + DAT_TroopValueState::instance.attackInfo.size + = DAT_TroopValueState::instance.attackInfo.size + + DAT_TribesState::instance.tribes[_tribeID].size; + } + } else { + ((int*)&DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType) + [(short)DAT_TribesState::instance.tribes[_tribeID].tribeType] + = ((int*)&DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType) + [(short)DAT_TribesState::instance.tribes[_tribeID].tribeType] + + DAT_TribesState::instance.tribes[_tribeID].size; + if (DAT_TribesState::instance.tribes[_tribeID].tribeType == AI::Tribes::AITT_ENGINEERS) { + DAT_TroopValueState::instance.attackInfo.engineers + = DAT_TroopValueState::instance.attackInfo.engineers + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_LADDERMEN) { + DAT_TroopValueState::instance.attackInfo.laddermen + = DAT_TroopValueState::instance.attackInfo.laddermen + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType != (AITribeType)0x13 + && DAT_TribesState::instance.tribes[_tribeID].tribeType != (AITribeType)0x14 + && DAT_TribesState::instance.tribes[_tribeID].tribeType != (AITribeType)0x15) { + if (DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x16) { + if (0 < DAT_UnitsState::instance + .units[DAT_TribesState::instance.tribes[_tribeID].selectionTargetUnitID] + .stoneAmmunition) { + DAT_TroopValueState::instance.attackInfo.catapults + = DAT_TroopValueState::instance.attackInfo.catapults + 1; + } + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x17) { + if (0 < DAT_UnitsState::instance + .units[DAT_TribesState::instance.tribes[_tribeID].selectionTargetUnitID] + .stoneAmmunition) { + DAT_TroopValueState::instance.attackInfo.catapults + = DAT_TroopValueState::instance.attackInfo.catapults + 1; + } + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + != AI::Tribes::AITT_TUNNELERS + && ((short)DAT_TribesState::instance.tribes[_tribeID].tribeType < 0xd + || (0x19 <= (short)DAT_TribesState::instance.tribes[_tribeID].tribeType))) { + DAT_TroopValueState::instance.attackInfo.aiTroops + = DAT_TroopValueState::instance.attackInfo.aiTroops + + DAT_TribesState::instance.tribes[_tribeID].size; + DAT_TroopValueState::instance.attackInfo.attackTribeCount + = DAT_TroopValueState::instance.attackInfo.attackTribeCount + 1; + if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_SPEARMEN) { + DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_MACEMEN) { + DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen + = DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == (AITribeType)0x1a) { + DAT_TroopValueState::instance.attackInfo.assassinTribeCount + = DAT_TroopValueState::instance.attackInfo.assassinTribeCount + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_PIKEMEN) { + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + = DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_SWORDSMEN) { + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + = DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::OFFSET_CROSSBOWMAN) { + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + = DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == (AITribeType)0x1c) { + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + = DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_KNIGHTS) { + DAT_TroopValueState::instance.attackInfo.knights + = DAT_TroopValueState::instance.attackInfo.knights + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == (AI::Tribes::AITT_SWORDSMEN | AI::Tribes::AITT_LADDERMEN)) { + DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + = DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore + 1; + } else if (DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_ARCHERS + || DAT_TribesState::instance.tribes[_tribeID].tribeType + == AI::Tribes::AITT_CROSSBOWMEN + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x19 + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x1d + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x1b + || DAT_TribesState::instance.tribes[_tribeID].tribeType == (AITribeType)0x1f) { + DAT_TroopValueState::instance.attackInfo.ranged + = DAT_TroopValueState::instance.attackInfo.ranged + 1; + } + } + } + } + } + } + if (DAT_TroopValueState::instance.attackInfo.knights) { + if (DAT_TroopValueState::instance.attackInfo.spearmenAndMacemen) { + DAT_TroopValueState::instance.attackInfo.knights = 0; + } + if (DAT_TroopValueState::instance.attackInfo.pikemenSwordsmenAndMore) { + DAT_TroopValueState::instance.attackInfo.knights = 0; + } + } + int _limit = 6; + if (!DAT_GameState::instance.mapAndTime.difficulty) { + _limit = 3; + } + if ((DAT_TroopValueState::instance.attackInfo.aiTroops <= 0) + || (DAT_TroopValueState::instance.attackInfo.attackTribeCount <= 0)) { + DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated = TRUE; + } + if ((DAT_TroopValueState::instance.attackInfo.size + > DAT_TroopValueState::instance.attackInfo.aiTroops * 3) + && (DAT_TroopValueState::instance.attackInfo.aiTroops < _limit)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::recountTotalTroopValue, this)(); + if (DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[2] + + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[3] + + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[4] + + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[5] + < 30) { + DAT_TroopValueState::instance.attackInfo.lowTroopValueRelated = TRUE; + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/recountTotalTroopValue.cpp b/src/OpenSHC/Map/Units/TroopValueState/recountTotalTroopValue.cpp new file mode 100644 index 0000000000..67668cabba --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/recountTotalTroopValue.cpp @@ -0,0 +1,42 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::UnitLogicState; + using Map::Units::UnitType; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051F9C0 + void TroopValueState::recountTotalTroopValue() + { + for (int _player = 0; + _player < countof(DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray); ++_player) { + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[_player] = 0; + } + for (int _unitID = 1; _unitID < DAT_UnitsState::instance.maxUnitCount; ++_unitID) { + if (DAT_UnitsState::instance.units[_unitID].logicalState != Map::Units::ULS_INVISIBLE + && !DAT_UnitsState::instance.units[_unitID].dying + && DAT_UnitsState::instance.units[_unitID].isSelectable_OR_matchTime + && DAT_UnitsState::instance.units[_unitID].unitType != Map::Units::UT_E_ENGINEER + && DAT_UnitsState::instance.units[_unitID].unitType != Map::Units::UT_TUNNELER + && DAT_UnitsState::instance.units[_unitID].unitType != Map::Units::UT_E_LADDER) { + int const _playerID = DAT_UnitsState::instance.units[_unitID].owner; + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[_playerID] + = DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[_playerID] + + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getValueOfTroopType, this)( + (Map::Units::UnitType)DAT_UnitsState::instance.units[_unitID].unitType); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/redeployArchersToDefensivePositions.cpp b/src/OpenSHC/Map/Units/TroopValueState/redeployArchersToDefensivePositions.cpp new file mode 100644 index 0000000000..210919b954 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/redeployArchersToDefensivePositions.cpp @@ -0,0 +1,55 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/LogicHelpers/Logic1.hpp" +#include "OpenSHC/Map/Units/States/UnitState.hpp" + +#include "OpenSHC/Globals/DAT_TileMapState.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::LogicHelpers::L_KEEP_NON_MANOR_HOUSE; + using Map::LogicHelpers::L_WALL_OR_GATEHOUSE; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051CBD0 + void TroopValueState::redeployArchersToDefensivePositions() + { + if (DAT_TroopValueState::instance.attackInfo.unitsSentToKeepFlag + || DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c <= 0) { + return; + } + for (int index = 0; index < DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2c520; ++index) { + int const postedUnitID = DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2c070[index]; + if (postedUnitID + && DAT_UnitsState::instance.units[postedUnitID].uid + == DAT_TroopValueState::instance.attackInfo.uidArray_0x2c200[index]) { + continue; + } + int const tile = DAT_TroopValueState::instance.attackInfo.tileArray_0x2bd50[index]; + if (!(DAT_TileMapState::instance.LogicLayer[tile] & (L_KEEP_NON_MANOR_HOUSE | L_WALL_OR_GATEHOUSE))) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + --DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c; + int const unitID + = DAT_TroopValueState::instance.attackInfo + .unitIDArray_0x2b57c[DAT_TroopValueState::instance.attackInfo.unitIDIndex_0x2bd4c]; + int const rowStartTile = DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + DAT_TroopValueState::instance.attackInfo.uidArray_0x2c200[index] + = DAT_UnitsState::instance.units[unitID].uid; + DAT_TroopValueState::instance.attackInfo.unitIDArray_0x2c070[index] = unitID; + DAT_UnitsState::instance.units[unitID].state.generic = Map::Units::States::US_MOVE_TO_DESTINATION; + MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::setDestinationForUnit, DAT_UnitsState::ptr)( + unitID, tile - rowStartTile, y, 0); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/registerSpottedEnemyTile.cpp b/src/OpenSHC/Map/Units/TroopValueState/registerSpottedEnemyTile.cpp new file mode 100644 index 0000000000..d3cf0d4764 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/registerSpottedEnemyTile.cpp @@ -0,0 +1,58 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Game/GameMode.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Entities/EntityType.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BE60 + void TroopValueState::registerSpottedEnemyTile(int enemyTile) + { + if (DAT_GameSynchronyState::instance.currentGameMode != Game::GM_SOLITARY + || 0 < DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id) { + return; + } + int const unitID = DAT_TileMapState::instance.UnitLayer[enemyTile]; + if (unitID <= 0 + || DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[DAT_UnitsState::instance.units[unitID].owner] + == -1 + || DAT_UnitsState::instance.units[unitID].isSelectable_OR_matchTime == 0) { + return; + } + int const entityID = DAT_TileMapState::instance.EntityLayer[enemyTile]; + if (entityID && DAT_EntityState::instance.entityArray[entityID].entityType == Map::Entities::ET_FIRE) { + return; + } + int freeSlot = -1; + for (int slot = 0; slot < countof(DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles); ++slot) { + int const tile = DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[slot].tile; + if (tile == enemyTile) { + return; + } + if (!tile && freeSlot == -1) { + freeSlot = slot; + } + } + if (-1 < freeSlot) { + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[freeSlot].tile = enemyTile; + DAT_TroopValueState::instance.attackInfo.spottedEnemyTiles[freeSlot].value = 100; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/renderAttackInfoDebugOverlay.cpp b/src/OpenSHC/Map/Units/TroopValueState/renderAttackInfoDebugOverlay.cpp new file mode 100644 index 0000000000..87aa686859 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/renderAttackInfoDebugOverlay.cpp @@ -0,0 +1,206 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/OS.func.hpp" +#include "OpenSHC/Text/TextManager.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Rendering/Colors/ColorsEnum.hpp" +#include "OpenSHC/Text/TextAlignment.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" +#include "OpenSHC/string-literals.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TextManagerObject.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/MSVC_SecurityCookie.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00518C50 + void TroopValueState::renderAttackInfoDebugOverlay(int x, int y, int width, int height) + { + char textBuffer[1000]; + uint const securityCookie = MSVC_SecurityCookie::instance ^ (uint)textBuffer; + int const column1X = x + 2; + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_Biggest_zone__005abb48, column1X, y, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.biggestZone, column1X, y, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + int const column2X = column1X + 10; + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_Start_zone__005abb38, column2X, y, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.startZone, column2X, y, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + int rowY = y + 16; + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_Scale_zone__005abb28, column1X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.scaleZone, column1X, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_keep_POS__005abb1c, column2X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .yEntry, + column2X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_0080FF, 18, TRUE, 0); + rowY = y + 32; + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_nof_fpoints__005abb0c, column1X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.nof_fpoints, column1X, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_inv_count__005abb00, column2X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.inv_count, column2X, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_nof_tribes__005abaf0, column1X + 20, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, + 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.nof_tribes[DAT_TroopValueState::instance.attackInfo.inv_count], + column1X + 20, rowY, Text::TTA_LEFT, Rendering::Colors::COL_0080FF, 18, TRUE, 0); + rowY = y + 48; + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_Start_con__005abae4, column1X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.startCon, column1X, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_Keep_con__005abad8, column2X, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.keepCon, column2X, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_cas_dis__005abacc, column1X + 20, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.casDis, column1X + 20, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + s_zone_size__005abac0, column1X + 30, rowY, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, TRUE, 0); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderNumberToScreen2, DAT_TextManagerObject::ptr)( + DAT_TroopValueState::instance.attackInfo.zoneSize, column1X + 30, rowY, Text::TTA_LEFT, + Rendering::Colors::COL_0080FF, 18, TRUE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Hack_d_d_d__Scale_d_d_d__005aba94, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .hack1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .hack2, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .hack3, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .scale1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .scale2, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .scale3, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .stone1); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 64, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Town_d_d__Gate_d_d__Moat_d__005aba60, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .town1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .town3, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .gate1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .gate2, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .moat1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .moat3, + DAT_TroopValueState::instance.attackInfo.lord1, DAT_TroopValueState::instance.attackInfo.lord2); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 80, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_High_d_d_d__Arch_d_d_d__P_005aba2c, + DAT_TroopValueState::instance.attackInfo.high1, DAT_TroopValueState::instance.attackInfo.high2, + DAT_TroopValueState::instance.attackInfo.high3, DAT_TroopValueState::instance.attackInfo.arch1, + DAT_TroopValueState::instance.attackInfo.arch2, DAT_TroopValueState::instance.attackInfo.arch3, + DAT_TroopValueState::instance.attackInfo.people1, DAT_TroopValueState::instance.attackInfo.people2, + DAT_TroopValueState::instance.attackInfo.people3); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 96, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Wide_d_d_d__005aba1c, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .wide1, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .wide2, + DAT_TroopValueState::instance.attackInfo + .playerInfo[DAT_TroopValueState::instance.attackInfo.attacker - 1] + .wide3); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 112, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Archer_points_d__Next_d_005aba00, + DAT_TroopValueState::instance.attackInfo.archerPoints, + DAT_TroopValueState::instance.attackInfo.archerPointsNext); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 128, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Support_points_d__Next_d_005ab9e4, + DAT_TroopValueState::instance.attackInfo.supportPoints, + DAT_TroopValueState::instance.attackInfo.supportPointsNext); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 144, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Tent_points_d__Next_d_005ab9c8, + DAT_TroopValueState::instance.attackInfo.tentPoints, + DAT_TroopValueState::instance.attackInfo.tentPointsNext); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 160, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_ai_troops__A_d_X_d_S_d_P_d_M_005ab988, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_A, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_X, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_S, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_P, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_M, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_S2, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_K, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_L, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_E, + DAT_TroopValueState::instance.attackInfo.aiTribeSizesPerTribeType.aiTroops_T); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 176, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_killed_d_lost_d_AI_troops_d_005ab968, + DAT_GameState::instance.playerDataArray[1].troopsKilled, + DAT_GameState::instance.playerDataArray[1].troopsLost, + DAT_TroopValueState::instance.attackInfo.aiTroops); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 192, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + MACRO_CALL(OS_Func::_sprintf)(textBuffer, s_Points___d___d___d___d___d___d____005ab93c, + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[0], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[1], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[2], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[3], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[4], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[5], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[6], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[7], + DAT_TroopValueState::instance.attackInfo.playerTotalTroopValueArray[8]); + MACRO_CALL_MEMBER(Text::TextManager_Func::renderTextToScreen, DAT_TextManagerObject::ptr)( + textBuffer, column1X, y + 208, Text::TTA_LEFT, Rendering::Colors::COL_FFFFFF, 18, FALSE, 0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/scanForArcherPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/scanForArcherPoints.cpp new file mode 100644 index 0000000000..3eda976431 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/scanForArcherPoints.cpp @@ -0,0 +1,84 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051F340 + void TroopValueState::scanForArcherPoints() + { + int _searchRadius; + DAT_TroopValueState::instance.attackInfo.archerPoints = 0; + if ((int)DAT_TroopValueState::instance.attackInfo.archerPointCountPrev < 0) { + _searchRadius = 8; + } else if ((int)DAT_TroopValueState::instance.attackInfo.archerPointCountPrev < 2) { + _searchRadius = 5; + } else if ((int)DAT_TroopValueState::instance.attackInfo.archerPointCountPrev < 4) { + _searchRadius = 6; + } else if ((int)DAT_TroopValueState::instance.attackInfo.archerPointCountPrev < 8) { + _searchRadius = 7; + } else if ((int)DAT_TroopValueState::instance.attackInfo.archerPointCountPrev < 20) { + _searchRadius = 8; + } else { + _searchRadius = 10; + } + for (int tile = 0; tile < 80400; ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] == '') { + uint const y = (uint)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + for (int _direction = 0; _direction < 4; ++_direction) { + int const neighbour + = DAT_TileMapState::instance.directionTranslationMatrix[y][_direction * 2] + tile; + if ((DAT_TileMapState::instance.AIInfoLayer[neighbour] & 0xf) == 4 + && !(DAT_TileMapState::instance.RandomLayer[neighbour] & 3)) { + uint const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + BOOLEnum hasArcherZoneFlag + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findAIZoneWithFlags, + DAT_PathFindingState::ptr)(_searchRadius, x, y, 0x10); + if (!hasArcherZoneFlag) { + DAT_TileMapState::instance.AIInfoLayer[tile] + = DAT_TileMapState::instance.AIInfoLayer[tile] | 0x10; + int _existingPointIndex = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findOrReserveArcherPointSlot, this)(tile); + int const _nextPointIndex = DAT_TroopValueState::instance.attackInfo.archerPointsNext; + if (_existingPointIndex) { + DAT_TroopValueState::instance.attackInfo.archerPointArray[_existingPointIndex] + .lastSeenCounter = DAT_TroopValueState::instance.attackInfo.someCounter1; + } else { + DAT_TroopValueState::instance.attackInfo + .archerPointArray[DAT_TroopValueState::instance.attackInfo.archerPointsNext] + .x = x; + DAT_TroopValueState::instance.attackInfo.archerPointArray[_nextPointIndex].y = y; + DAT_TroopValueState::instance.attackInfo.archerPointArray[_nextPointIndex].tile + = tile; + DAT_TroopValueState::instance.attackInfo.archerPointArray[_nextPointIndex] + .lastSeenCounter = DAT_TroopValueState::instance.attackInfo.someCounter1; + } + DAT_TroopValueState::instance.attackInfo.archerPoints + = DAT_TroopValueState::instance.attackInfo.archerPoints + 1; + if (200 <= DAT_TroopValueState::instance.attackInfo.archerPoints) { + return; + } + break; + } + } + } + } + } + DAT_TroopValueState::instance.attackInfo.archerPointCountPrev + = DAT_TroopValueState::instance.attackInfo.archerPoints; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/scanForSiegeTentPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/scanForSiegeTentPoints.cpp new file mode 100644 index 0000000000..d1fc26b3f1 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/scanForSiegeTentPoints.cpp @@ -0,0 +1,141 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051F5C0 + void TroopValueState::scanForSiegeTentPoints() + { + DAT_TroopValueState::instance.attackInfo.tentPoints = 0; + for (int _candidateTile = 0; _candidateTile < 80400; ++_candidateTile) { + if (DAT_TileMapState::instance.AIInfoLayer[_candidateTile] != 8) { + continue; + } + // Counts the flat, free and reachable tiles around the candidate: 8 neighbours, 8 each. + int _flatTileCount = 0; + if (DAT_TileMapState::instance.UnitLayer[_candidateTile] != 0) { + continue; + } + int const _y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_candidateTile]; + int const _baseHeight = DAT_TileMapState::instance.HeightLayer[_candidateTile]; + for (int _outerDirection = 0; _outerDirection < 8; _outerDirection++) { + int const _neighbourTile + = DAT_TileMapState::instance.directionTranslationMatrix[_y][_outerDirection] + _candidateTile; + int _height = DAT_TileMapState::instance.HeightLayer[_neighbourTile]; + if (_height > _baseHeight + 6 || _height < _baseHeight - 6 + || DAT_TileMapState::instance.UnitLayer[_neighbourTile] != 0 + || (short)DAT_TileMapState::instance.PathConnectionLayer[_neighbourTile] + != DAT_TroopValueState::instance.attackInfo.startCon + || (DAT_TileMapState::instance.LogicLayer[_neighbourTile] & 0xa0002400U)) { + break; + } + int _inner; + uint _logic; + for (int _innerDirection = 0; _innerDirection < 8; _innerDirection += 4) { + _inner = DAT_TileMapState::instance.directionTranslationMatrix[_y][_innerDirection] + + _neighbourTile; + _height = DAT_TileMapState::instance.HeightLayer[_inner]; + if (_height > _baseHeight + 8 || _height < _baseHeight - 8 + || DAT_TileMapState::instance.UnitLayer[_inner] != 0 + || (short)DAT_TileMapState::instance.PathConnectionLayer[_inner] + != DAT_TroopValueState::instance.attackInfo.startCon + || (DAT_TileMapState::instance.LogicLayer[_inner] & 0xa0002400U)) { + break; + } + _inner = DAT_TileMapState::instance.directionTranslationMatrix[_y][_innerDirection + 1] + + _neighbourTile; + _height = DAT_TileMapState::instance.HeightLayer[_inner]; + if (_height > _baseHeight + 8 || _height < _baseHeight - 8 + || DAT_TileMapState::instance.UnitLayer[_inner] != 0 + || (short)DAT_TileMapState::instance.PathConnectionLayer[_inner] + != DAT_TroopValueState::instance.attackInfo.startCon + || ((_logic = DAT_TileMapState::instance.LogicLayer[_inner]) & 0x2000)) { + _flatTileCount += 1; + break; + } + if ((_logic & 0x20000000) || (int)_logic < 0 || (_logic & 0x400)) { + _flatTileCount += 1; + break; + } + _inner = DAT_TileMapState::instance.directionTranslationMatrix[_y][_innerDirection + 2] + + _neighbourTile; + _height = DAT_TileMapState::instance.HeightLayer[_inner]; + if (_height > _baseHeight + 8 || _height < _baseHeight - 8 + || DAT_TileMapState::instance.UnitLayer[_inner] != 0 + || (short)DAT_TileMapState::instance.PathConnectionLayer[_inner] + != DAT_TroopValueState::instance.attackInfo.startCon + || ((_logic = DAT_TileMapState::instance.LogicLayer[_inner]) & 0x2000)) { + _flatTileCount += 2; + break; + } + if ((_logic & 0x20000000) || (int)_logic < 0 || (_logic & 0x400)) { + _flatTileCount += 2; + break; + } + _inner = DAT_TileMapState::instance.directionTranslationMatrix[_y][_innerDirection + 3] + + _neighbourTile; + _height = DAT_TileMapState::instance.HeightLayer[_inner]; + if (_height > _baseHeight + 8 || _height < _baseHeight - 8 + || DAT_TileMapState::instance.UnitLayer[_inner] != 0 + || (short)DAT_TileMapState::instance.PathConnectionLayer[_inner] + != DAT_TroopValueState::instance.attackInfo.startCon + || ((_logic = DAT_TileMapState::instance.LogicLayer[_inner]) & 0x2000)) { + _flatTileCount += 3; + break; + } + if ((_logic & 0x20000000) || (int)_logic < 0 || (_logic & 0x400)) { + _flatTileCount += 3; + break; + } + _flatTileCount += 4; + } + } + if (_flatTileCount < 64) { + continue; + } + int const _x = _candidateTile - DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile; + if (MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findAIZoneWithFlags, + DAT_PathFindingState::ptr)(6, _x, _y, 0x80)) { + continue; + } + DAT_TileMapState::instance.AIInfoLayer[_candidateTile] |= 0x80; + int const _sIndex + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getSiegeIndexForTile, this)(_candidateTile); + if (_sIndex) { + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_sIndex].someCounter + = DAT_TroopValueState::instance.attackInfo.someCounter1; + } else { + int const _index = DAT_TroopValueState::instance.attackInfo.tentPointsNext; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].x = _x; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].y = _y; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].tile = _candidateTile; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].someCounter + = DAT_TroopValueState::instance.attackInfo.someCounter1; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].zero = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].three = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].tribeUID = 0; + DAT_TroopValueState::instance.attackInfo.tentPointsValues[_index].tribeID = 0; + } + DAT_TroopValueState::instance.attackInfo.tentPoints + = DAT_TroopValueState::instance.attackInfo.tentPoints + 1; + if (2000 <= DAT_TroopValueState::instance.attackInfo.tentPoints) { + return; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/scanForSupportPoints.cpp b/src/OpenSHC/Map/Units/TroopValueState/scanForSupportPoints.cpp new file mode 100644 index 0000000000..79a7f4acc4 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/scanForSupportPoints.cpp @@ -0,0 +1,74 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Enums/AIInfoFlag.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TileMapState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051F4B0 + void TroopValueState::scanForSupportPoints() + { + DAT_TroopValueState::instance.attackInfo.supportPoints = 0; + for (int tile = 0; tile < countof(DAT_TileMapState::instance.AIInfoLayer); ++tile) { + if (DAT_TileMapState::instance.AIInfoLayer[tile] != Map::Enums::AIF_RING_1) { + continue; + } + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[tile]; + for (int direction = 0; direction < 4; ++direction) { + if ((DAT_TileMapState::instance + .AIInfoLayer[DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2] + + tile] + & Map::Enums::AIF_ANY_RING) + != Map::Enums::AIF_RING_2 + || (DAT_TileMapState::instance + .RandomLayer[DAT_TileMapState::instance.directionTranslationMatrix[y][direction * 2] + + tile] + & 3)) { + continue; + } + int const x = tile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + BOOLEnum const hasSupportZoneFlag + = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::findAIZoneWithFlags, + DAT_PathFindingState::ptr)(8, x, y, Map::Enums::AIF_SUPPORT_POINT); + if (hasSupportZoneFlag) { + continue; + } + DAT_TileMapState::instance.AIInfoLayer[tile] |= Map::Enums::AIF_SUPPORT_POINT; + int const existingPointIndex + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getSupportPointIndex, this)(tile); + int const nextPointIndex = DAT_TroopValueState::instance.attackInfo.supportPointsNext; + if (existingPointIndex) { + DAT_TroopValueState::instance.attackInfo.supportPointsArray[existingPointIndex].someCounter + = DAT_TroopValueState::instance.attackInfo.someCounter1; + } else { + DAT_TroopValueState::instance.attackInfo + .supportPointsArray[DAT_TroopValueState::instance.attackInfo.supportPointsNext] + .x = x; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[nextPointIndex].y = y; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[nextPointIndex].tile = tile; + DAT_TroopValueState::instance.attackInfo.supportPointsArray[nextPointIndex].someCounter + = DAT_TroopValueState::instance.attackInfo.someCounter1; + } + ++DAT_TroopValueState::instance.attackInfo.supportPoints; + if (200 <= DAT_TroopValueState::instance.attackInfo.supportPoints) { + return; + } + break; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/searchTribeWithProperties.cpp b/src/OpenSHC/Map/Units/TroopValueState/searchTribeWithProperties.cpp new file mode 100644 index 0000000000..d33f51e6aa --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/searchTribeWithProperties.cpp @@ -0,0 +1,49 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x005182A0 + BOOLEnum TroopValueState::searchTribeWithProperties(int attackID) + { + if (DAT_TroopValueState::instance.attackInfo.nof_tribes[attackID] == 0) { + return FALSE; + } + int owner = 0; + for (int tribeID = 1; tribeID < countof(DAT_TribesState::instance.tribes); ++tribeID) { + if (DAT_TribesState::instance.tribes[tribeID].tribeState == 2 + && DAT_TribesState::instance.tribes[tribeID].owner + && DAT_TribesState::instance.tribes[tribeID].owner + != DAT_GameSynchronyState::instance.currentPlayerSlotID) { + owner = DAT_TribesState::instance.tribes[tribeID].owner; + break; + } + } + if (!owner) { + DAT_TroopValueState::instance.attackInfo.nof_tribes[attackID] = 0; + DAT_TroopValueState::instance.attackInfo.unknownByteArray02[attackID] = 0; + return FALSE; + } + DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID] = owner; + DAT_TroopValueState::instance.attackInfo.attacker + = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID]; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] = 10000; + DAT_TroopValueState::instance.attackInfo.value3Array01[attackID] = Map::Enums::AWS_ASSIGNING_TARGETS; + DAT_TroopValueState::instance.attackInfo.value10 = 10; + return TRUE; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/sendAttackingPatrolTribeToComputedDestination.cpp b/src/OpenSHC/Map/Units/TroopValueState/sendAttackingPatrolTribeToComputedDestination.cpp new file mode 100644 index 0000000000..aeb71663b0 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/sendAttackingPatrolTribeToComputedDestination.cpp @@ -0,0 +1,57 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/DAT_ViewportRenderState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D0D0 + void TroopValueState::sendAttackingPatrolTribeToComputedDestination(int tribeID) + { + int const targetUnitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 1); + int destinationTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, DAT_UnitsState::instance.units[targetUnitID].owner, 12, + 1); + if (destinationTile <= 0) { + destinationTile = MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, + DAT_PathFindingState::ptr)(DAT_UnitsState::instance.units[targetUnitID].x, + DAT_UnitsState::instance.units[targetUnitID].y, DAT_UnitsState::instance.units[targetUnitID].owner, + 30, 1); + if (destinationTile <= 0) { + destinationTile = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathfindingRelated49ff20, DAT_PathFindingState::ptr)( + DAT_UnitsState::instance.units[targetUnitID].x, DAT_UnitsState::instance.units[targetUnitID].y, + DAT_UnitsState::instance.units[targetUnitID].owner, 50, 1); + } + } + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::updatePathLinkageTileMap, DAT_BuildingsState::ptr)( + 0); + if (0 < destinationTile) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + destinationTile + - DAT_ViewportRenderState::instance + .translationMatrix[DAT_ViewportRenderState::instance + .tileTranslationMatrix_YComponent[destinationTile]] + .addXgetTile, + DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[destinationTile], 0, 0, + Map::Units::Instructions::UMSE_0); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setGate2.cpp b/src/OpenSHC/Map/Units/TroopValueState/setGate2.cpp new file mode 100644 index 0000000000..e6e326426c --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setGate2.cpp @@ -0,0 +1,29 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A2D0 + void TroopValueState::setGate2(BOOLEnum includeAll, int playerID) + { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gate2 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].some1000ArrayIndex_2; + ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gateValuesArray[i].score + < Map::Enums::SSS_UNUSABLE_THRESHOLD + && (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gateValuesArray[i].unitID == 0 + || includeAll)) { + ++DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].gate2; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setMoat3.cpp b/src/OpenSHC/Map/Units/TroopValueState/setMoat3.cpp new file mode 100644 index 0000000000..8c4b52df6c --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setMoat3.cpp @@ -0,0 +1,28 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A720 + void TroopValueState::setMoat3(BOOLEnum includeAll, int playerID) + { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moatValuesArray[i].score + < Map::Enums::SSS_UNUSABLE_THRESHOLD + && (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moatValuesArray[i].unitID == 0 + || includeAll)) { + ++DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].moat3; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setRallyPointForLaddermenTribe.cpp b/src/OpenSHC/Map/Units/TroopValueState/setRallyPointForLaddermenTribe.cpp new file mode 100644 index 0000000000..91b5f9c9bb --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setRallyPointForLaddermenTribe.cpp @@ -0,0 +1,50 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using AI::Tribes::AITribeType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00519790 + void TroopValueState::setRallyPointForLaddermenTribe(int laddermenTribeID) + { + int laddermenTribeCount = 0; + for (int tribeID = 1; tribeID < countof(DAT_TribesState::instance.tribes); ++tribeID) { + if (DAT_TribesState::instance.tribes[tribeID].tribeState != 0) { + if (MACRO_CALL_MEMBER(Game::GameStateStructures_Func::isFullIDEqualsToMinus1, DAT_GameState::ptr)( + DAT_TribesState::instance.tribes[tribeID].owner) + && DAT_TribesState::instance.tribes[tribeID].tribeType == AI::Tribes::AITT_LADDERMEN) { + ++laddermenTribeCount; + if (laddermenTribeCount + == DAT_TribesState::instance.tribes[laddermenTribeID].someCounter1 + 1) { + int const _unitID = DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeNextRallyPointDestination, + DAT_PathFindingState::ptr)(-1, (int)(DAT_UnitsState::instance.units[_unitID].x), + (int)(DAT_UnitsState::instance.units[_unitID].y)); + short const _rallyPointY = (short)DAT_PathFindingState::instance.ALG_ResultY; + DAT_TribesState::instance.tribes[laddermenTribeID].rallyPointArray[0][0] + = (short)DAT_PathFindingState::instance.ALG_ResultX; + DAT_TribesState::instance.tribes[laddermenTribeID].rallyPointArray[0][1] = _rallyPointY; + return; + } + } + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setScale3.cpp b/src/OpenSHC/Map/Units/TroopValueState/setScale3.cpp new file mode 100644 index 0000000000..46563c2f19 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setScale3.cpp @@ -0,0 +1,29 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519DE0 + void TroopValueState::setScale3(BOOLEnum includeAll, int playerID) + { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].score + < Map::Enums::SSS_UNUSABLE_THRESHOLD + && (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scaleValuesArray[i].unitID + == 0 + || includeAll)) { + ++DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].scale3; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setTown2.cpp b/src/OpenSHC/Map/Units/TroopValueState/setTown2.cpp new file mode 100644 index 0000000000..6c0a33ed32 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setTown2.cpp @@ -0,0 +1,25 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x00519F50 + void TroopValueState::setTown2(BOOLEnum includeAll, int playerID) + { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].townValuesArray[i].unitID == 0 + || includeAll) { + ++DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town3; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/setWide3.cpp b/src/OpenSHC/Map/Units/TroopValueState/setWide3.cpp new file mode 100644 index 0000000000..4a45859bc5 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/setWide3.cpp @@ -0,0 +1,28 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/SiegeSpotScore.hpp" + +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051A550 + void TroopValueState::setWide3(BOOLEnum includeAll, int playerID) + { + DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide3 = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide2; ++i) { + if (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wideValuesArray[i].score + < Map::Enums::SSS_UNUSABLE_THRESHOLD + && (DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wideValuesArray[i].unitID == 0 + || includeAll)) { + ++DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].wide3; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/shouldLightPitchBasedOnTroopValue.cpp b/src/OpenSHC/Map/Units/TroopValueState/shouldLightPitchBasedOnTroopValue.cpp new file mode 100644 index 0000000000..fc856e5c06 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/shouldLightPitchBasedOnTroopValue.cpp @@ -0,0 +1,25 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" + +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051BF20 + BOOLEnum TroopValueState::shouldLightPitchBasedOnTroopValue(int tile, int playerID, int param_3) + { + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeTotalUnitsWithinDistance, + DAT_PathFindingState::ptr)(playerID, 1, 1, tile, 10); + int const nearbyTroopValue = DAT_PathFindingState::instance.ALGO_TotalTroopValue; + MACRO_CALL_MEMBER(Map::Navigation::PathFindingState_Func::computeTotalUnitsWithinDistance, + DAT_PathFindingState::ptr)(playerID, 0, 1, tile, 6); + return DAT_PathFindingState::instance.ALGO_TotalTroopValue > nearbyTroopValue * 2; + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/sortAttackInfoTribeIDArrayBasedOn.cpp b/src/OpenSHC/Map/Units/TroopValueState/sortAttackInfoTribeIDArrayBasedOn.cpp new file mode 100644 index 0000000000..eca5e61aa0 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/sortAttackInfoTribeIDArrayBasedOn.cpp @@ -0,0 +1,240 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/AI/Tribes/AITribeTypeInt.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + + // FUNCTION: STRONGHOLDCRUSADER 0x00518380 + void TroopValueState::sortAttackInfoTribeIDArrayBasedOn( + int attackWave, int shrinkSize, int tribeSizeSumLimit, SomeTribeBehaviorType someTribeTypeIdentifier) + { + AI::Tribes::AITribeTypeInt const* _pPriority; + AI::Tribes::AITribeTypeInt const* _tribeTypePriority; + if (someTribeTypeIdentifier == 1010) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x21c; + } else if (someTribeTypeIdentifier == 1011) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x44c; + } else if (someTribeTypeIdentifier == 1012) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x35c; + } else if (someTribeTypeIdentifier == 1018) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x4ec; + } else if (someTribeTypeIdentifier == 1040) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x3ac; + } else if (someTribeTypeIdentifier == 1045) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x3fc; + } else if (someTribeTypeIdentifier == 7) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x12c; + } else if (someTribeTypeIdentifier == 5) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x17c; + } else if (someTribeTypeIdentifier == 1014) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x26c; + } else if (someTribeTypeIdentifier == 1047) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x99c; + } else if (someTribeTypeIdentifier == 1043) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x2bc; + } else if (someTribeTypeIdentifier == 1044) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x30c; + } else if (someTribeTypeIdentifier == 1013) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x53c; + } else if (someTribeTypeIdentifier == 1019) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x58c; + } else if (someTribeTypeIdentifier == 1025) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x5dc; + } else if (someTribeTypeIdentifier == 1021) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x62c; + } else if (someTribeTypeIdentifier == 1046) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x67c; + } else if (someTribeTypeIdentifier == 1020) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x7bc; + } else if (someTribeTypeIdentifier == 1049) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x80c; + } else if (someTribeTypeIdentifier == 1050) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x71c; + } else if (someTribeTypeIdentifier == 1051) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x71c; + } else if (someTribeTypeIdentifier == 1048) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x85c; + } else if (someTribeTypeIdentifier == 1054) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x8fc; + } else if (someTribeTypeIdentifier == 1042) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x8ac; + } else if (someTribeTypeIdentifier == 1016) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x6cc; + } else if (someTribeTypeIdentifier == 1017) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x76c; + } else if (someTribeTypeIdentifier == 1015) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x49c; + } else if (someTribeTypeIdentifier == 1041) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x94c; + } else if (someTribeTypeIdentifier == 1023) { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x1cc; + } else { + _tribeTypePriority = DAT_AttackInfoDefinedData::instance.field_0x17c; + } + for (int i = 0; i < 50; ++i) { + DAT_TroopValueState::instance.attackInfo.tribeIDArray[i] = 0; + DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[i] = 12; + } + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize = 0; + for (int tribeID = 1; tribeID < countof(DAT_TribesState::instance.tribes); ++tribeID) { + if (DAT_TribesState::instance.tribes[tribeID].tribeState + && DAT_TribesState::instance.tribes[tribeID].tribeState != 3 + && DAT_GameSynchronyState::instance + .currentPlayerFullIDArray[DAT_TribesState::instance.tribes[tribeID].owner] + == -1 + && DAT_TribesState::instance.tribes[tribeID].attackWave == attackWave + && DAT_TribesState::instance.tribes[tribeID].attackInfo_someCounter1 + != DAT_TroopValueState::instance.attackInfo.someCounter1 + && DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType != 1040 + && DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType != 1045) { + DAT_TroopValueState::instance.attackInfo + .tribeIDArray[DAT_TroopValueState::instance.attackInfo.tribeIDArraySize] = tribeID; + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize++; + } + } + if (shrinkSize <= 0 || tribeSizeSumLimit <= 0) { + return; + } + BOOLEnum swapped; + do { + int index = 0; + swapped = FALSE; + if (DAT_TroopValueState::instance.attackInfo.tribeIDArraySize - 1 <= 0) { + break; + } + do { + int const current = DAT_TroopValueState::instance.attackInfo.tribeIDArray[index]; + int const next = DAT_TroopValueState::instance.attackInfo.tribeIDArray[index + 1]; + int const currentTribeType = DAT_TribesState::instance.tribes[current].tribeType; + int const nextTribeType = DAT_TribesState::instance.tribes[next].tribeType; + int currentValue; + int nextValue; + currentValue = 0; + _pPriority = _tribeTypePriority + 2; + do { + if (_pPriority[-2] == currentTribeType) { + break; + } + if (_pPriority[-1] == currentTribeType) { + currentValue++; + break; + } + if (*_pPriority == currentTribeType) { + currentValue += 2; + break; + } + if (_pPriority[1] == currentTribeType) { + currentValue += 3; + break; + } + if (_pPriority[2] == currentTribeType) { + currentValue += 4; + break; + } + if (_pPriority[3] == currentTribeType) { + currentValue += 5; + break; + } + currentValue += 6; + _pPriority += 6; + } while (currentValue < 12); + nextValue = 0; + _pPriority = _tribeTypePriority + 2; + do { + if (_pPriority[-2] == nextTribeType) { + break; + } + if (_pPriority[-1] == nextTribeType) { + nextValue++; + break; + } + if (*_pPriority == nextTribeType) { + nextValue += 2; + break; + } + if (_pPriority[1] == nextTribeType) { + nextValue += 3; + break; + } + if (_pPriority[2] == nextTribeType) { + nextValue += 4; + break; + } + if (_pPriority[3] == nextTribeType) { + nextValue += 5; + break; + } + nextValue += 6; + _pPriority += 6; + } while (nextValue < 12); + if (nextValue < currentValue) { + swapped = TRUE; + DAT_TroopValueState::instance.attackInfo.tribeIDArray[index] = next; + DAT_TroopValueState::instance.attackInfo.tribeIDArray[index + 1] = current; + } + index++; + } while (index < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize - 1); + } while (swapped); + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; i++) { + int const tribeType + = DAT_TribesState::instance.tribes[DAT_TroopValueState::instance.attackInfo.tribeIDArray[i]] + .tribeType; + int value; + value = 0; + _pPriority = _tribeTypePriority + 2; + do { + if (_pPriority[-2] == tribeType) { + break; + } + if (_pPriority[-1] == tribeType) { + value++; + break; + } + if (*_pPriority == tribeType) { + value += 2; + break; + } + if (_pPriority[1] == tribeType) { + value += 3; + break; + } + if (_pPriority[2] == tribeType) { + value += 4; + break; + } + if (_pPriority[3] == tribeType) { + value += 5; + break; + } + value += 6; + _pPriority += 6; + } while (value < 12); + DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[i] = value; + } + int tribeSizeSum = 0; + for (int i = 0; i < DAT_TroopValueState::instance.attackInfo.tribeIDArraySize; i++) { + int const tribeID = DAT_TroopValueState::instance.attackInfo.tribeIDArray[i]; + if (tribeSizeSum > tribeSizeSumLimit) { + DAT_TroopValueState::instance.attackInfo.tribeRelatedArrayValue0UpTo12[i] = 12; + } + tribeSizeSum += DAT_TribesState::instance.tribes[tribeID].size; + } + if (DAT_TroopValueState::instance.attackInfo.tribeIDArraySize > shrinkSize) { + DAT_TroopValueState::instance.attackInfo.tribeIDArraySize = shrinkSize; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/trySendKnightTribeToAttack.cpp b/src/OpenSHC/Map/Units/TroopValueState/trySendKnightTribeToAttack.cpp new file mode 100644 index 0000000000..847fc28c1b --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/trySendKnightTribeToAttack.cpp @@ -0,0 +1,50 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D020 + void TroopValueState::trySendKnightTribeToAttack() + { + int const playerID = DAT_TroopValueState::instance.attackInfo.playerID_0x2c850; + if (DAT_TroopValueState::instance.attackInfo.knightTribeCount <= 0 + || DAT_TroopValueState::instance.attackInfo.attackInfoTickWrapCount < 10 + || (DAT_TroopValueState::instance.attackInfo.attackInfoTickWrapCount < 100 + && DAT_GameState::instance.playerDataArray[playerID].previousSiegeWeaponsCount <= 0) + || (SEC_RNG::instance.currentNumber2 & 7)) { + return; + } + int const siegeWeaponsCount = DAT_GameState::instance.playerDataArray[playerID].previousSiegeWeaponsCount; + --DAT_TroopValueState::instance.attackInfo.knightTribeCount; + int const tribeID = DAT_TroopValueState::instance.attackInfo + .knightTribeArray[DAT_TroopValueState::instance.attackInfo.knightTribeCount]; + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + int unitID; + if (siegeWeaponsCount) { + unitID = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::findActiveSiegeEngineForTribe, DAT_UnitsState::ptr)(playerID); + } else { + unitID = MACRO_CALL_MEMBER(Map::Units::UnitsState_Func::getLivingSelectableUnit, DAT_UnitsState::ptr)( + playerID); + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)(tribeID, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, 0, 0, + Map::Units::Instructions::UMSE_0); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/trySendMacemenTribeToAttack.cpp b/src/OpenSHC/Map/Units/TroopValueState/trySendMacemenTribeToAttack.cpp new file mode 100644 index 0000000000..d1bfcad9c0 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/trySendMacemenTribeToAttack.cpp @@ -0,0 +1,49 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Map/Units/UnitsState.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" +#include "OpenSHC/Globals/SEC_RNG.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051CF90 + void TroopValueState::trySendMacemenTribeToAttack() + { + int const playerID = DAT_TroopValueState::instance.attackInfo.playerID_0x2c850; + if (DAT_TroopValueState::instance.attackInfo.macemenTribeCount <= 0 + || DAT_TroopValueState::instance.attackInfo.attackInfoTickWrapCount < 4 + || (SEC_RNG::instance.currentNumber2 & 7)) { + return; + } + BOOLEnum const isNotCampaignMission = DAT_GameCore::instance.gameMode_2 != Game::GM_CAMPAIGN_MISSION; + --DAT_TroopValueState::instance.attackInfo.macemenTribeCount; + int const macemenTribeID + = DAT_TroopValueState::instance.attackInfo + .macemenTribeArray[DAT_TroopValueState::instance.attackInfo.macemenTribeCount]; + DAT_TribesState::instance.tribes[macemenTribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + if (isNotCampaignMission || 15 <= DAT_GameCore::instance.missionNumber1to20) { + int const unitID = MACRO_CALL_MEMBER( + Map::Units::UnitsState_Func::getLivingSelectableUnit, DAT_UnitsState::ptr)(playerID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + macemenTribeID, DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + 0, 0, Map::Units::Instructions::UMSE_0); + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/updateActiveAttackWaveState.cpp b/src/OpenSHC/Map/Units/TroopValueState/updateActiveAttackWaveState.cpp new file mode 100644 index 0000000000..920382bc2c --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/updateActiveAttackWaveState.cpp @@ -0,0 +1,53 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Buildings/BuildingsState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D730 + void TroopValueState::updateActiveAttackWaveState(int attackWave) + { + int playerID = DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackWave]; + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + playerID = 2; + } + int const ticker = DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave]; + int const nextTicker = ticker + 1; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] = nextTicker; + if (0 < ticker) { + if (100 < nextTicker) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::applyTribeBehaviorType, DAT_TribesState::ptr)( + attackWave, Map::Units::STBT_0x400); + } + if (1000 < DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave]) { + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackWave] = 0; + } + return; + } + ++DAT_TroopValueState::instance.attackInfo.someCounter1; + MACRO_CALL_MEMBER(Map::Buildings::BuildingsState_Func::removeSiegeBuildings, DAT_BuildingsState::ptr)( + attackWave, playerID); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::exitSiegeEquipmentForWave, this)(attackWave); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackWave, 1000, 10000, Map::Units::STBT_0x3ff); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3ff, Map::Units::STBT_1, 0, 10); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/updateArcherBrazierProximityFlags.cpp b/src/OpenSHC/Map/Units/TroopValueState/updateArcherBrazierProximityFlags.cpp new file mode 100644 index 0000000000..cd2a8b0f21 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/updateArcherBrazierProximityFlags.cpp @@ -0,0 +1,42 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Entities/EntityState.func.hpp" +#include "OpenSHC/Map/Units/UnitLogicState.hpp" +#include "OpenSHC/Map/Units/UnitType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_EntityState.hpp" +#include "OpenSHC/Globals/DAT_UnitsState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051C2D0 + void TroopValueState::updateArcherBrazierProximityFlags() + { + for (int unitID = 1; unitID < DAT_UnitsState::instance.maxUnitCount; ++unitID) { + Unit* const unit = &DAT_UnitsState::instance.units[unitID]; + if (unit->logicalState == Map::Units::ULS_INVISIBLE) { + continue; + } + BOOLEnum const isAIPlayerWithKeep + = MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::getPlayerNot1AndHasKeep, this)(unit->owner); + if (!isAIPlayerWithKeep) { + continue; + } + if ((unit->unitType == Map::Units::UT_E_ARCHER || unit->unitType == Map::Units::UT_A_ARCHER) + && !unit->dying) { + BOOLEnum const isBrazierNearby = MACRO_CALL_MEMBER(Map::Entities::EntityState_Func::isBrazierNearby, + DAT_EntityState::ptr)(unit->x, unit->y, unit->buildingHeight + unit->terrainOrClimbHeight); + unit->field_0x40d = isBrazierNearby != 0; + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/updateAttackInfoTick.cpp b/src/OpenSHC/Map/Units/TroopValueState/updateAttackInfoTick.cpp new file mode 100644 index 0000000000..ec9c28f971 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/updateAttackInfoTick.cpp @@ -0,0 +1,72 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Game/GameStateStructures.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Units.func.hpp" +#include "OpenSHC/Game/GameMode2.hpp" +#include "OpenSHC/Map/MapType2.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_MapPropertiesState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051FE80 + void TroopValueState::updateAttackInfoTick() + { + if (DAT_GameCore::instance.gameMode_2 == Game::GM_BUILDERUnk + && DAT_MapPropertiesState::instance.SEC_U3_MapType2_1 == Map::MT_SIEGE) { + DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID = 1; + DAT_TroopValueState::instance.attackInfo.playerID_0x2c850 = 2; + } else { + DAT_TroopValueState::instance.attackInfo.pitchRelatedPlayerID + = MACRO_CALL(Map::Units_Func::FindFirstOpponentWithKeep)(); + DAT_TroopValueState::instance.attackInfo.playerID_0x2c850 = 1; + } + if (!DAT_TroopValueState::instance.attackInfo.aiTickPhase) { + DAT_TroopValueState::instance.attackInfo.unitsSentToKeepFlag = FALSE; + DAT_TroopValueState::instance.attackInfo.attackInfoTickWrapCount + = DAT_TroopValueState::instance.attackInfo.aiTickPhase; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::collectArcherUnitsByLocation, this)(); + MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::assignMacemenAndKnightsNotFromPlayer1ToTribes, this)(); + } else if (DAT_TroopValueState::instance.attackInfo.aiTickPhase == 1) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::updateArcherBrazierProximityFlags, this)(); + } else if (DAT_TroopValueState::instance.attackInfo.aiTickPhase != 50 + && DAT_TroopValueState::instance.attackInfo.aiTickPhase != 100) { + if (DAT_TroopValueState::instance.attackInfo.aiTickPhase == 70) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::trySendMacemenTribeToAttack, this)(); + } else if (DAT_TroopValueState::instance.attackInfo.aiTickPhase == 60) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::trySendKnightTribeToAttack, this)(); + } + } + if (!(DAT_TroopValueState::instance.attackInfo.aiTickPhase & 0xf)) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::queueOilThrowForIdleArchers, this)(); + } + if ((DAT_TroopValueState::instance.attackInfo.aiTickPhase & 0xf) == 8) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::redeployArchersToDefensivePositions, this)(); + } + ++DAT_TroopValueState::instance.attackInfo.aiTickPhase; + if (100 < DAT_TroopValueState::instance.attackInfo.aiTickPhase) { + ++DAT_TroopValueState::instance.attackInfo.attackInfoTickWrapCount; + DAT_TroopValueState::instance.attackInfo.aiTickPhase = 2; + } + BOOLEnum const canMoveFromKeep = MACRO_CALL_MEMBER( + Game::GameStateStructures_Func::unitsCanMoveFromKeepOfPlayerToAnotherArea, DAT_GameState::ptr)(1); + if (canMoveFromKeep && !DAT_TroopValueState::instance.attackInfo.unitsSentToKeepFlag) { + DAT_TroopValueState::instance.attackInfo.unitsSentToKeepFlag = TRUE; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::commandUnitsToMoveToKeep, this)(); + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::processSpottedEnemyTiles, this)(); + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/updateInProgressAttackWave.cpp b/src/OpenSHC/Map/Units/TroopValueState/updateInProgressAttackWave.cpp new file mode 100644 index 0000000000..4bbcdea768 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/updateInProgressAttackWave.cpp @@ -0,0 +1,79 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Audio/SFX/SFXState.func.hpp" +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Enums/AttackWaveState.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_AttackInfoDefinedData.hpp" +#include "OpenSHC/Globals/DAT_GameCore.hpp" +#include "OpenSHC/Globals/DAT_SFXState.hpp" +#include "OpenSHC/Globals/DAT_TroopValueState.hpp" + +namespace OpenSHC { +namespace Map { + namespace Units { + + using Map::Units::SomeTribeBehaviorType; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x00520450 + void TroopValueState::updateInProgressAttackWave(int attackID) + { + // The original reads the wave's player here, before any of the calls below. + int const playerID = (char)DAT_TroopValueState::instance.attackInfo.attackWavePlayerIDArray[attackID]; + BOOLEnum isAttackMostlyAlive = MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::isLessThanPercentageOfTribesInAttackDying, this)(attackID, (int)(50)); + if (isAttackMostlyAlive && !DAT_TroopValueState::instance.attackInfo.attackAlarmPlayed) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::playAttackAlarmSound, this)(); + } + if (!DAT_TroopValueState::instance.attackInfo.attackAlarmPlayed) { + return; + } + if (DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] == 0) { + DAT_TroopValueState::instance.attackInfo.someCounter1 + = DAT_TroopValueState::instance.attackInfo.someCounter1 + 1; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::aiRecomputeAttacks2, this)(TRUE, attackID); + if (0 < DAT_TroopValueState::instance.attackInfo.tentPoints) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::placeSiegeTentsAndAssignEngineers, this)( + attackID, TRUE); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::placeTunnelForEveryTunneler, this)(attackID); + } else { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::placeSiegeTentsAndAssignEngineers, this)( + attackID, FALSE); + } + int tribeSizeSumLimit = DAT_TroopValueState::instance.attackInfo.playerInfo[playerID - 1].town3; + if (tribeSizeSumLimit) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 2, tribeSizeSumLimit, Map::Units::STBT_0x3f5); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3f5, Map::Units::STBT_1, 0, 0x14); + } + if (DAT_TroopValueState::instance.attackInfo.people3) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::sortAttackInfoTribeIDArrayBasedOn, this)( + attackID, 2, DAT_TroopValueState::instance.attackInfo.people3, Map::Units::STBT_0x3fb); + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::applyTribeBehaviorTypes, this)( + Map::Units::STBT_0x3fb, Map::Units::STBT_1, 5, 0x1e); + } + } + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID]++; + if (DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] + >= DAT_AttackInfoDefinedData::instance + .AttackWaveDurationPerAttacker[DAT_TroopValueState::instance.attackInfo.attacker]) { + BOOLEnum isMission39 = DAT_GameCore::instance.missionNumber1to20 == 39; + DAT_TroopValueState::instance.attackInfo.attackWaveTicker[attackID] = 10000; + DAT_TroopValueState::instance.attackInfo.value3Array01[attackID] = Map::Enums::AWS_ASSIGNING_TARGETS; + DAT_TroopValueState::instance.attackInfo.value10 = 0; + if ((isMission39) && (DAT_TroopValueState::instance.attackInfo.attacker == 4)) { + /* + "we are the macemen" + */ + MACRO_CALL_MEMBER(Audio::SFX::SFXState_Func::playWAVSFX, DAT_SFXState::ptr)("Mace_s4.wav"); + } + } + } + + } +} +} diff --git a/src/OpenSHC/Map/Units/TroopValueState/updateTribeBehaviorBasedOnBehaviorType.cpp b/src/OpenSHC/Map/Units/TroopValueState/updateTribeBehaviorBasedOnBehaviorType.cpp new file mode 100644 index 0000000000..af16a5ffc3 --- /dev/null +++ b/src/OpenSHC/Map/Units/TroopValueState/updateTribeBehaviorBasedOnBehaviorType.cpp @@ -0,0 +1,453 @@ +#include "../TroopValueState.func.hpp" + +#include "OpenSHC/Map.func.hpp" +#include "OpenSHC/Map/Navigation/DirectionAlgorithmState.func.hpp" +#include "OpenSHC/Map/Navigation/PathFindingState.func.hpp" +#include "OpenSHC/Map/Units/TribesState.func.hpp" +#include "OpenSHC/Game/Player/Enums/BuildingEntryInfoEnum.hpp" +#include "OpenSHC/Map/Buildings/BuildingType.hpp" +#include "OpenSHC/Map/Buildings/BuildingTypeShort.hpp" +#include "OpenSHC/Map/Units/Behavior/UnitStanceEnum.hpp" +#include "OpenSHC/Map/Units/Instructions/UnitMatchSpeedEnum.hpp" +#include "OpenSHC/Map/Units/SomeTribeBehaviorType.hpp" +#include "OpenSHC/WindowsHelper/Enums/BOOLEnum.hpp" + +#include "OpenSHC/Globals/DAT_BuildingsState.hpp" +#include "OpenSHC/Globals/DAT_DirectionAlgorithmState.hpp" +#include "OpenSHC/Globals/DAT_GameState.hpp" +#include "OpenSHC/Globals/DAT_GameSynchronyState.hpp" +#include "OpenSHC/Globals/DAT_PathFindingState.hpp" +#include "OpenSHC/Globals/DAT_TribesState.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::Buildings::BuildingType; + using Map::Buildings::BuildingTypeShort; + using Map::Units::SomeTribeBehaviorType; + using Map::Units::Behavior::UnitStanceEnum; + using Map::Units::Instructions::UnitMatchSpeedEnum; + using WindowsHelper::Enums::BOOLEnum; + + // FUNCTION: STRONGHOLDCRUSADER 0x0051D7F0 + void TroopValueState::updateTribeBehaviorBasedOnBehaviorType(int tribeID) + { + short const tribeWave = DAT_TribesState::instance.tribes[tribeID].attackWave; + if (tribeWave && tribeWave < DAT_TroopValueState::instance.attackInfo.index) { + DAT_TribesState::instance.tribes[tribeID].attackWave + = (short)DAT_TroopValueState::instance.attackInfo.index; + } + int const attackWave = DAT_TribesState::instance.tribes[tribeID].attackWave; + DAT_TroopValueState::instance.attackInfo.nof_tribes[attackWave]++; + if (DAT_TribesState::instance.tribes[tribeID].percentageMovingUnk <= 0) { + DAT_TroopValueState::instance.attackInfo.unknownByteArray02[attackWave]++; + } + int const behaviorType = DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType; + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter++; + short const updateCounter = DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter; + DAT_TribesState::instance.tribes[tribeID].field_0x2ae = 0; + switch (behaviorType) { + case Map::Units::STBT_3: + if (MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::stopTribeMovementAndCheckIdle, DAT_TribesState::ptr)(tribeID)) { + int const signpost + = DAT_TroopValueState::instance.attackInfo + .unknownSignpostRelatedArray[DAT_TribesState::instance.tribes[tribeID].attackWave]; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, + DAT_GameState::instance.mapAndTime + .unitMoveDestinationXYPairs[signpost][DAT_TribesState::instance.tribes[tribeID].someIndex] + .x, + DAT_GameState::instance.mapAndTime + .unitMoveDestinationXYPairs[signpost][DAT_TribesState::instance.tribes[tribeID].someIndex] + .y, + 0, 0, Map::Units::Instructions::UMSE_0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_4; + } + return; + case Map::Units::STBT_4: + case Map::Units::STBT_6: + return; + case Map::Units::STBT_5: + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, + DAT_GameState::instance.mapAndTime + .somePairArray[DAT_TribesState::instance.tribes[tribeID].someIndex] + .x, + DAT_GameState::instance.mapAndTime + .somePairArray[DAT_TribesState::instance.tribes[tribeID].someIndex] + .y, + 0, 0, Map::Units::Instructions::UMSE_0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_6; + } + return; + case Map::Units::STBT_7: + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + int vector; + for (vector = 0; vector < 49; ++vector) { + if (DAT_GameState::instance.mapAndTime + .attackVectors[DAT_GameSynchronyState::instance.currentPlayerSlotID][vector] + .tribeID + == 0) { + DAT_GameState::instance.mapAndTime + .attackVectors[DAT_GameSynchronyState::instance.currentPlayerSlotID][vector] + .tribeID = tribeID; + break; + } + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, + DAT_GameState::instance.mapAndTime + .attackVectors[DAT_GameSynchronyState::instance.currentPlayerSlotID][vector] + .x, + DAT_GameState::instance.mapAndTime + .attackVectors[DAT_GameSynchronyState::instance.currentPlayerSlotID][vector] + .y, + 0, 0, Map::Units::Instructions::UMSE_0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_8; + } + return; + case Map::Units::STBT_8: + if (DAT_UnitsState::instance.units[DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID] + .totalSizeOfPathPlan + < 25) { + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + } + return; + case Map::Units::STBT_1: + if (updateCounter >= 30) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + if (MACRO_CALL_MEMBER(Map::Units::TribesState_Func::isTribeUnitBlockedByOtherUnit, + DAT_TribesState::ptr)(tribeID)) { + MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::moveTribeToNearbyClearTile, DAT_TribesState::ptr)(tribeID); + } + } + return; + case Map::Units::STBT_0x3f2: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3f2); + } + return; + case Map::Units::STBT_0x3ff: + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + int const signpost + = DAT_TroopValueState::instance.attackInfo.unknownSignpostRelatedArray[attackWave]; + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, DAT_GameState::instance.mapAndTime.signpostsMapEdge[signpost][0].x, + DAT_GameState::instance.mapAndTime.signpostsMapEdge[signpost][0].y, 0, 0, + Map::Units::Instructions::UMSE_0); + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_0x400; + } + return; + case Map::Units::STBT_0x3f6: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3f6); + } + return; + case Map::Units::STBT_0x413: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x413); + } + return; + case Map::Units::STBT_0x414: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x414); + } + return; + case Map::Units::STBT_0x3f5: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_STAND_GROUND; + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3f5); + } + return; + case Map::Units::STBT_0x3fb: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3fb); + } + return; + case Map::Units::STBT_0x401: + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + if (MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::allUnitsReachedTheirDestination, DAT_TribesState::ptr)(tribeID)) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::buildRallyPointsFromSiegeUnits, this)(tribeID); + short const rallyPointCount = DAT_TribesState::instance.tribes[tribeID].rallyPointCount; + int rallyPoint = DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex + 1; + if (rallyPoint >= rallyPointCount) { + rallyPoint = 0; + } + int const rallyX = DAT_TribesState::instance.tribes[tribeID].rallyPointArray[rallyPoint][0]; + int const rallyY = DAT_TribesState::instance.tribes[tribeID].rallyPointArray[rallyPoint][1]; + DAT_TribesState::instance.tribes[tribeID].currentRallyPointIndex = (short)rallyPoint; + if (rallyPointCount > 1 + && DAT_UnitsState::instance + .units[DAT_TribesState::instance.tribes[tribeID].selectionTargetUnitID] + .closestEnemyMicroDistance + > 0x60) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, rallyX, rallyY, 0, 0, Map::Units::Instructions::UMSE_0); + } + } + return; + case Map::Units::STBT_0x3fe: + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + return; + case Map::Units::STBT_0x3fd: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3fd); + } + return; + case Map::Units::STBT_0x416: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && MACRO_CALL_MEMBER( + Map::Units::TribesState_Func::stopTribeMovementAndCheckIdle, DAT_TribesState::ptr)(tribeID)) { + int const keep + = DAT_GameState::instance.playerDataArray[DAT_GameSynchronyState::instance.currentPlayerSlotID] + .buildingEntryInfos[Game::Player::Enums::BEIE_KEEP] + .id; + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + if (keep <= 0) { + return; + } + int x; + int y; + BuildingTypeShort const keepType = DAT_BuildingsState::instance.buildings[keep].buildingType; + if (keepType == Map::Buildings::BT_MANORHOUSE) { + x = DAT_BuildingsState::instance.buildings[keep].x + 3; + y = DAT_BuildingsState::instance.buildings[keep].y + 8; + } else if (keepType == Map::Buildings::BT_STONEKEEP) { + x = DAT_BuildingsState::instance.buildings[keep].x + 3; + y = DAT_BuildingsState::instance.buildings[keep].y + 3; + } else if (keepType == Map::Buildings::BT_STRONGHOLD) { + x = DAT_BuildingsState::instance.buildings[keep].x + 3; + y = DAT_BuildingsState::instance.buildings[keep].y + 3; + } else if (keepType == Map::Buildings::BT_KEEPFOUR) { + x = DAT_BuildingsState::instance.buildings[keep].x + 4; + y = DAT_BuildingsState::instance.buildings[keep].y + 4; + } else if (keepType == Map::Buildings::BT_KEEPFIVE) { + x = DAT_BuildingsState::instance.buildings[keep].x + 5; + y = DAT_BuildingsState::instance.buildings[keep].y + 5; + } else { + return; + } + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, x, y, 0, 0, Map::Units::Instructions::UMSE_0); + DAT_TribesState::instance.tribes[tribeID].unitStance = Map::Units::Behavior::USE_AGGRESSIVE; + } + return; + case Map::Units::STBT_0x3f8: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateHigh2ClostestToTribeTargetUnit, this)( + tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, this->x, this->y, 0, 0, Map::Units::Instructions::UMSE_0); + } + return; + case Map::Units::STBT_0x3f9: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::calculateArch2ClosestToTribeTargetUnit, this)( + tribeID); + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, this->x, this->y, 0, 0, Map::Units::Instructions::UMSE_0); + } + return; + case Map::Units::STBT_0x3f7: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3f7); + } + return; + case Map::Units::STBT_0x418: + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::sendAttackingPatrolTribeToComputedDestination, this)(tribeID); + } + return; + case Map::Units::STBT_0x41e: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::moveTribeToReachableNearbyTile, this)(tribeID); + } + return; + case Map::Units::STBT_0x419: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::decideAndExecuteTribeAttackAction, this)( + tribeID); + } + return; + case Map::Units::STBT_0x41a: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::moveTribeToNearbyGatehouse, this)(tribeID); + } + return; + case Map::Units::STBT_0x41b: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::moveTowardsParticularUnits, this)(tribeID); + } + return; + case Map::Units::STBT_0x3fc: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + DAT_TribesState::instance.tribes[tribeID].unitStance + = (UnitStanceEnum)(DAT_TroopValueState::instance.attackInfo.attackWaveRetargetCount > 2); + if (DAT_TribesState::instance.tribes[tribeID].countdown2 <= 0) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::claimArcherAttackPoint, this)(tribeID); + } + } + return; + case Map::Units::STBT_0x412: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + if (DAT_TribesState::instance.tribes[tribeID].countdown2 <= 0) { + if (!MACRO_CALL_MEMBER( + Map::Units::TroopValueState_Func::findTribeWithMoatAttackBehavior, this)()) { + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::claimArcherAttackPoint, this)(tribeID); + return; + } + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::assignTribeToSupportPoint, this)( + this->x, this->y, tribeID); + } + } + return; + case Map::Units::STBT_0x406: + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + MACRO_CALL_MEMBER(Map::Units::TroopValueState_Func::setRallyPointForLaddermenTribe, this)(tribeID); + if (DAT_TribesState::instance.tribes[tribeID].countdown2 <= 0) { + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, DAT_TribesState::instance.tribes[tribeID].rallyPointArray[0][0], + DAT_TribesState::instance.tribes[tribeID].rallyPointArray[0][1], 0, 0, + Map::Units::Instructions::UMSE_0); + } + } + return; + case Map::Units::STBT_0x3f3: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + } + return; + case Map::Units::STBT_0x3f4: + if (DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit + && updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit = 0; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::assignAttackTargetsForTribe, DAT_TribesState::ptr)( + tribeID, Map::Units::STBT_0x3f4); + } + return; + case Map::Units::STBT_0x411: { + // The original falls back to the plain behaviour and stops here when the support tribe is gone. + int const supportTribeID = DAT_TribesState::instance.tribes[tribeID].supportTribeID; + if (!supportTribeID + || DAT_TribesState::instance.tribes[tribeID].uid2 + != DAT_TribesState::instance.tribes[supportTribeID].uid) { + DAT_TribesState::instance.tribes[tribeID].tribeBehaviorType = Map::Units::STBT_1; + return; + } + if (updateCounter >= DAT_TribesState::instance.tribes[tribeID].someUpdateUpperLimit) { + DAT_TribesState::instance.tribes[tribeID].unknownAttackRelatedUpdateCounter = 0; + int const unitID = DAT_TribesState::instance.tribes[supportTribeID].selectionTargetUnitID; + int const targetTile = MACRO_CALL_MEMBER( + Map::Navigation::PathFindingState_Func::pathFindingRelated, DAT_PathFindingState::ptr)(2, + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, + DAT_UnitsState::instance.units[unitID].siegeTargetPlayerID); + if (targetTile > 0) { + int const y = DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[targetTile]; + int const x = targetTile - DAT_ViewportRenderState::instance.translationMatrix[y].addXgetTile; + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::giveTribeMoveInstruction, DAT_TribesState::ptr)( + tribeID, x, y, 0, 0, Map::Units::Instructions::UMSE_0); + MACRO_CALL_MEMBER( + Map::Navigation::DirectionAlgorithmState_Func::calculatePreferredRelativeOrientation, + DAT_DirectionAlgorithmState::ptr)( + DAT_UnitsState::instance.units[unitID].x, DAT_UnitsState::instance.units[unitID].y, x, y); + DAT_TribesState::instance.tribes[tribeID].orientation + = (short)DAT_DirectionAlgorithmState::instance.orientation; + } + } + return; + } + case Map::Units::STBT_0x400: + return; + default: + MACRO_CALL_MEMBER(Map::Units::TribesState_Func::updateTribeCombatStanceBehavior, DAT_TribesState::ptr)( + tribeID); + return; + } + } + + } +} +} diff --git a/src/OpenSHC/Map/WallAndPitchState/confirmAndQueueDestructionCommand.cpp b/src/OpenSHC/Map/WallAndPitchState/confirmAndQueueDestructionCommand.cpp index 24536bcd45..f0d38fbfb3 100644 --- a/src/OpenSHC/Map/WallAndPitchState/confirmAndQueueDestructionCommand.cpp +++ b/src/OpenSHC/Map/WallAndPitchState/confirmAndQueueDestructionCommand.cpp @@ -25,7 +25,7 @@ namespace Map { case 0: { int id = this->id; DAT_GameSynchronyState::instance.DAT_GameCommandParam0 = id; - int uid = DAT_BuildingsState::instance.buildings[id].uid; + int const uid = DAT_BuildingsState::instance.buildings[id].uid; DAT_GameSynchronyState::instance.DAT_GameCommandParam1 = 100; DAT_GameSynchronyState::instance.DAT_GameCommandParam2 = uid; MACRO_CALL_MEMBER(OpenSHC::Synchrony::GameSynchronyState_Func::queueCommand, diff --git a/src/OpenSHC/Map/WallAndPitchState/destroyWall.cpp b/src/OpenSHC/Map/WallAndPitchState/destroyWall.cpp index 5fa02dd2a3..d9f7490cbf 100644 --- a/src/OpenSHC/Map/WallAndPitchState/destroyWall.cpp +++ b/src/OpenSHC/Map/WallAndPitchState/destroyWall.cpp @@ -28,15 +28,15 @@ namespace Map { DAT_BuildingsState::ptr)(playerID, OpenSHC::Game::Resources::RT_WOOD, amount); } for (int i = 0; i < count; i++) { - int _tile = this->receivedWallPlacementInfoArray[i].tile_OR_pitchID; - int _y = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; + int const _tile = this->receivedWallPlacementInfoArray[i].tile_OR_pitchID; + int const _y = (int)DAT_ViewportRenderState::instance.tileTranslationMatrix_YComponent[_tile]; DAT_TileMapState::instance.DamageLayer[_tile] = this->receivedWallPlacementInfoArray[i].damage; DAT_TileMapState::instance.HeightLayer[_tile] = this->receivedWallPlacementInfoArray[i].height; DAT_TileMapState::instance.LogicLayer[_tile] &= ~(LogicHelpers::L_WALL_OR_GATEHOUSE | LogicHelpers::L_CRENEL | LogicHelpers::L_STAIRS | LogicHelpers::L_UNKNOWN_WALL_RELATED | LogicHelpers::L_BOULDERS | LogicHelpers::L_PEBBLES | LogicHelpers::L_CRENEL_VARIATIONUnk); DAT_TileMapState::instance.LogicLayer[_tile] |= this->receivedWallPlacementInfoArray[i].logic; - int _tileDelta = _tile - DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile; + int const _tileDelta = _tile - DAT_ViewportRenderState::instance.translationMatrix[_y].addXgetTile; MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updatePathLinkagesInAllEightDirections, DAT_PathFindingState::ptr)(_y, _tile); MACRO_CALL_MEMBER(OpenSHC::Map::Navigation::PathFindingState_Func::updateWalkAndPathLayer, diff --git a/src/OpenSHC/Text/FontSizeClass/renderCharUnk.cpp b/src/OpenSHC/Text/FontSizeClass/renderCharUnk.cpp index 63e4faa067..8dfdf1e033 100644 --- a/src/OpenSHC/Text/FontSizeClass/renderCharUnk.cpp +++ b/src/OpenSHC/Text/FontSizeClass/renderCharUnk.cpp @@ -17,7 +17,7 @@ namespace Text { int FontSizeClass::renderCharUnk(byte charId, int xPos, int yPosUnk, int bgr24, int blendStrengthUnk) { int _xPos = xPos; - int _yPos = yPosUnk; + int const _yPos = yPosUnk; Rendering::Enums::RenderTarget renderTargetTemp = (Rendering::Enums::RenderTarget)DAT_TextureRenderCoreObject::instance.drawBufferChoiceValue; diff --git a/status/addresses-SHC-3BB0A8C1.txt b/status/addresses-SHC-3BB0A8C1.txt index 47b2ecfdf6..4ca595d01b 100644 --- a/status/addresses-SHC-3BB0A8C1.txt +++ b/status/addresses-SHC-3BB0A8C1.txt @@ -45739,43 +45739,43 @@ SHC_3BB0A8C1_0x0051812A | 0.0% | Pending SHC_3BB0A8C1_0x00518130 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00518180 | 0.0% | Pending +SHC_3BB0A8C1_0x00518180 | 96.9% | zero-store order (scheduler) -SHC_3BB0A8C1_0x005182A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005182A0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00518350 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00518380 | 0.0% | Pending +SHC_3BB0A8C1_0x00518380 | 93.5% | the original enters the bubble-sort loop through a jump and keeps the sort index in edi SHC_3BB0A8C1_0x005185C0 | 0.0% | Pending SHC_3BB0A8C1_0x00518690 | 0.0% | Pending -SHC_3BB0A8C1_0x00518870 | 0.0% | Pending +SHC_3BB0A8C1_0x00518870 | 100.0% | Reimplemented SHC_3BB0A8C1_0x005188A0 | 0.0% | Pending -SHC_3BB0A8C1_0x00518930 | 0.0% | Pending +SHC_3BB0A8C1_0x00518930 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00518A10 | 0.0% | Pending +SHC_3BB0A8C1_0x00518A10 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00518BB0 | 0.0% | Pending +SHC_3BB0A8C1_0x00518BB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00518C50 | 0.0% | Pending +SHC_3BB0A8C1_0x00518C50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519310 | 0.0% | Pending +SHC_3BB0A8C1_0x00519310 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519690 | 0.0% | Pending +SHC_3BB0A8C1_0x00519690 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519790 | 0.0% | Pending +SHC_3BB0A8C1_0x00519790 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519850 | 0.0% | Pending +SHC_3BB0A8C1_0x00519850 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005198C0 | 0.0% | Pending +SHC_3BB0A8C1_0x005198C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00519960 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519990 | 0.0% | Pending +SHC_3BB0A8C1_0x00519990 | 96.4% | GL; base/index reg order SHC_3BB0A8C1_0x005199E0 | 0.0% | Pending @@ -45789,53 +45789,53 @@ SHC_3BB0A8C1_0x00519BF0 | 0.0% | Pending SHC_3BB0A8C1_0x00519C40 | 0.0% | Pending -SHC_3BB0A8C1_0x00519CC0 | 0.0% | Pending +SHC_3BB0A8C1_0x00519CC0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519DE0 | 0.0% | Pending +SHC_3BB0A8C1_0x00519DE0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519E40 | 0.0% | Pending +SHC_3BB0A8C1_0x00519E40 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519F50 | 0.0% | Pending +SHC_3BB0A8C1_0x00519F50 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00519FB0 | 0.0% | Pending +SHC_3BB0A8C1_0x00519FB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A0C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A0C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051A100 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A140 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A140 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A220 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A220 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A2D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A2D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A330 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A330 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051A490 | 0.0% | Pending -SHC_3BB0A8C1_0x0051A550 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A550 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A5B0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A5B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051A630 | 0.0% | Pending -SHC_3BB0A8C1_0x0051A720 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A720 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A780 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A780 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A8A0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A8A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A920 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A920 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051A9A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051A9F0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051A9F0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051AA40 | 0.0% | Pending SHC_3BB0A8C1_0x0051AB10 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051AB60 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AB60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051ABB0 | 0.0% | Pending @@ -45843,47 +45843,47 @@ SHC_3BB0A8C1_0x0051AC80 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051ACB0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051ACE0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051ACE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051ACF0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051AE00 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AE00 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051AE90 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AE90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051AF20 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AF20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051AF70 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AF70 | 80.5% | Known-zero push lost in loop SHC_3BB0A8C1_0x0051AF80 | 0.0% | Pending -SHC_3BB0A8C1_0x0051AFF0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051AFF0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051B0C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B0C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051B110 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B110 | 80.5% | Known-zero push lost in loop SHC_3BB0A8C1_0x0051B120 | 0.0% | Pending -SHC_3BB0A8C1_0x0051B190 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B190 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051B240 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B240 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051B290 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B290 | 80.5% | Known-zero push lost in loop SHC_3BB0A8C1_0x0051B2A0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051B310 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B310 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051B420 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B420 | 97.9% | the original computes y with the constant-1 register (sub ecx, esi) -SHC_3BB0A8C1_0x0051B570 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B570 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051B680 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051B690 | 0.0% | Pending -SHC_3BB0A8C1_0x0051B6C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B6C0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051B700 | 0.0% | Pending @@ -45893,7 +45893,7 @@ SHC_3BB0A8C1_0x0051B708 | 0.0% | Pending SHC_3BB0A8C1_0x0051B70C | 0.0% | Pending -SHC_3BB0A8C1_0x0051B710 | 0.0% | Pending +SHC_3BB0A8C1_0x0051B710 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051B770 | 0.0% | Pending @@ -45901,31 +45901,31 @@ SHC_3BB0A8C1_0x0051B890 | 0.0% | Pending SHC_3BB0A8C1_0x0051B900 | 0.0% | Pending -SHC_3BB0A8C1_0x0051BA30 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BA30 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051BA90 | 0.0% | Pending -SHC_3BB0A8C1_0x0051BC70 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BC70 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051BD10 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BD10 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051BDE0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BDE0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051BE20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051BE60 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BE60 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051BEE0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051BF20 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BF20 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051BF70 | 0.0% | Pending +SHC_3BB0A8C1_0x0051BF70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051C1A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051C2D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051C2D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051C360 | 0.0% | Pending +SHC_3BB0A8C1_0x0051C360 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051C3EC | 0.0% | Pending @@ -46097,9 +46097,9 @@ SHC_3BB0A8C1_0x0051C463 | 0.0% | Pending SHC_3BB0A8C1_0x0051C464 | 0.0% | Pending -SHC_3BB0A8C1_0x0051C470 | 0.0% | Pending +SHC_3BB0A8C1_0x0051C470 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051C570 | 0.0% | Pending +SHC_3BB0A8C1_0x0051C570 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051C710 | 0.0% | Pending @@ -46129,45 +46129,45 @@ SHC_3BB0A8C1_0x0051C7EF | 0.0% | Pending SHC_3BB0A8C1_0x0051C7F0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051C800 | 0.0% | Pending +SHC_3BB0A8C1_0x0051C800 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051CA50 | 0.0% | Pending +SHC_3BB0A8C1_0x0051CA50 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051CAE0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051CBD0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051CBD0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051CC00 | 0.0% | Pending -SHC_3BB0A8C1_0x0051CCB0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051CCB0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051CE50 | 0.0% | Pending -SHC_3BB0A8C1_0x0051CF90 | 0.0% | Pending +SHC_3BB0A8C1_0x0051CF90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D020 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D020 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D0D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D0D0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D1C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D1C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D2B0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D2B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D450 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D450 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D510 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D510 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D5D0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D5D0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051D5E0 | 0.0% | Pending SHC_3BB0A8C1_0x0051D680 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D690 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D690 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D730 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D730 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051D7F0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051D7F0 | 97.5% | counter load copy (ushort) SHC_3BB0A8C1_0x0051D960 | 0.0% | Pending @@ -46327,7 +46327,7 @@ SHC_3BB0A8C1_0x0051E2F2 | 0.0% | Pending SHC_3BB0A8C1_0x0051E2F3 | 0.0% | Pending -SHC_3BB0A8C1_0x0051E300 | 0.0% | Pending +SHC_3BB0A8C1_0x0051E300 | 42.8% | zero in ebx not edi SHC_3BB0A8C1_0x0051E4D0 | 0.0% | Pending @@ -46339,45 +46339,45 @@ SHC_3BB0A8C1_0x0051EDF0 | 0.0% | Pending SHC_3BB0A8C1_0x0051F320 | 0.0% | Pending -SHC_3BB0A8C1_0x0051F340 | 0.0% | Pending +SHC_3BB0A8C1_0x0051F340 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051F3D0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051F4B0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051F4B0 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051F4F0 | 0.0% | Pending -SHC_3BB0A8C1_0x0051F5C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051F5C0 | 46.5% | startCon enregistered in ebx; SHC_3BB0A8C1_0x0051F5E0 | 0.0% | Pending SHC_3BB0A8C1_0x0051F630 | 0.0% | Pending -SHC_3BB0A8C1_0x0051F950 | 0.0% | Pending +SHC_3BB0A8C1_0x0051F950 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051F9C0 | 0.0% | Pending +SHC_3BB0A8C1_0x0051F9C0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051FA70 | 0.0% | Pending +SHC_3BB0A8C1_0x0051FA70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x0051FB80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051FB90 | 0.0% | Pending +SHC_3BB0A8C1_0x0051FB90 | 96.7% | const 1 range start (esi) -SHC_3BB0A8C1_0x0051FE80 | 0.0% | Pending +SHC_3BB0A8C1_0x0051FE80 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x0051FF90 | 0.0% | Pending +SHC_3BB0A8C1_0x0051FF90 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005200A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005200A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005202B0 | 0.0% | Pending +SHC_3BB0A8C1_0x005202B0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00520450 | 0.0% | Pending +SHC_3BB0A8C1_0x00520450 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x005205A0 | 0.0% | Pending +SHC_3BB0A8C1_0x005205A0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00520CD0 | 0.0% | Pending +SHC_3BB0A8C1_0x00520CD0 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00520F70 | 0.0% | Pending +SHC_3BB0A8C1_0x00520F70 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00521180 | 100.0% | Reimplemented @@ -46791,7 +46791,7 @@ SHC_3BB0A8C1_0x00522520 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522550 | 100.0% | Reimplemented -SHC_3BB0A8C1_0x00522590 | 0.0% | Pending +SHC_3BB0A8C1_0x00522590 | 100.0% | Reimplemented SHC_3BB0A8C1_0x00522630 | 0.0% | Pending @@ -67547,7 +67547,7 @@ SHC_3BB0A8C1_0x0057B9BE | 0.0% | Pending SHC_3BB0A8C1_0x0057B9C0 | 100.0% | Reimplemented, only names differ: the original's FLAG_JokeAIMessageArray element addresses carry no symbol -SHC_3BB0A8C1_0x0057BE10 | 0.0% | Pending +SHC_3BB0A8C1_0x0057BE10 | 97.2% | noreturn _exit: extra add esp SHC_3BB0A8C1_0x0057C815 | 0.0% | Pending diff --git a/tools/compiler-internals/reg_scores.py b/tools/compiler-internals/reg_scores.py index 80ae2d56b0..16ae10894e 100644 --- a/tools/compiler-internals/reg_scores.py +++ b/tools/compiler-internals/reg_scores.py @@ -377,7 +377,8 @@ def why_register(invocation): """ q = BS + '"' costs = ', '.join('poi(' + COST_ARRAY + '+' + hex(4 * n) + ')' for n in range(1, 9)) - cmd = ('.printf ' + q + 'PICK reg=%d costs=%d,%d,%d,%d,%d,%d,%d,%d ' + q + ', @ebx, ' + costs + '; ' + cmd = ('.printf ' + q + 'PICK reg=%d key=%d wt=%d id=%x costs=%d,%d,%d,%d,%d,%d,%d,%d ' + q + ', @ebx, ' + 'poi(@eax+0xc), poi(@eax+0x40), poi(@eax+0x44), ' + costs + '; ' '.if (poi(poi(@eax)) != 0) { da poi(poi(poi(@eax))+4)+1 L28 } ' '.else { .printf ' + q + '(temp)' + BS + BS + 'n' + q + ' }; gc') lines = ['sxe ld:c2', 'g', 'r $t0=0', @@ -388,20 +389,22 @@ def why_register(invocation): 'g', 'q', ''] txt = run(lines, 'why' + str(invocation)) rows = [] - for m in re.finditer(r'PICK reg=(-?\d+) costs=([\d,-]+) (?:\(temp\)|[0-9a-f]{8}\s+"([^"]*)")', txt): - rows.append((int(m.group(1)), [int(x) for x in m.group(2).split(',')], m.group(3) or '(temp)')) + for m in re.finditer(r'PICK reg=(-?\d+) key=(-?\d+) wt=(-?\d+) id=([0-9a-f]+) costs=([\d,-]+) ' + r'(?:\(temp\)|[0-9a-f]{8}\s+"([^"]*)")', txt): + rows.append((int(m.group(1)), [int(x) for x in m.group(5).split(',')], + '%s k=%s w=%s #%s' % (m.group(6) or '(temp)', m.group(2), m.group(3), m.group(4)))) if not rows: print('no register assignment seen in invocation %d' % invocation) return print('') - print('%-22s %-5s %s' % ('candidate', 'got', 'cost per register (lowest wins)')) - print('%-22s %-5s %s' % ('', '', ' '.join('%-7s' % REGNAMES[n] for n in range(1, 9)))) + print('%-34s %-5s %s' % ('candidate', 'got', 'cost per register (lowest wins)')) + print('%-34s %-5s %s' % ('', '', ' '.join('%-7s' % REGNAMES[n] for n in range(1, 9)))) for reg, cost, name in rows: cells = [] for n in range(1, 9): c = str(cost[n - 1]) cells.append('%-7s' % (c if n != reg else '[' + c + ']')) - print('%-22s %-5s %s' % (name, REGNAMES.get(reg, reg), ' '.join(cells))) + print('%-34s %-5s %s' % (name, REGNAMES.get(reg, reg), ' '.join(cells))) print('') print('The bracketed cost is the register that was taken. To move a candidate elsewhere,') print('that register needs a cost below the bracketed one. Cost is built from interference') diff --git a/tools/reimplementation-helper/batch/arm_layout.py b/tools/reimplementation-helper/batch/arm_layout.py new file mode 100644 index 0000000000..c4315408e8 --- /dev/null +++ b/tools/reimplementation-helper/batch/arm_layout.py @@ -0,0 +1,44 @@ +"""Print the original's and our switch-arm layout order, from reccmp's jump tables. + +A jump table's entries are in case order (MSVC assigns indices by ascending case value and +gives `default` the last one), so sorting the entries by offset gives the order the arms are +laid out in - which for MSVC is the source order, modulo arms it chooses to move. + +usage: arm_layout.py FUNCTION TABLE_INDEX case,case,... (cases ascending, no default) +""" +import re +import subprocess +import sys + +func, which, cases = sys.argv[1], int(sys.argv[2]), [int(c, 0) for c in sys.argv[3].split(",")] +out = subprocess.run(["python", "tools/reimplementation-helper/batch/reccmp_report.py", "diff", func], + capture_output=True, text=True).stdout.split("\n") +tables, cur, seen = [], None, 0 +for l in out: + if l.strip().endswith("Jump table:"): + seen += 1 + cur = {"-": [], "+": []} + tables.append(cur) + continue + if cur is None: + continue + m = re.match(r"^([-+ ])start \+ (0x[0-9a-f]+)", l) + if not m: + if l.strip() and "start +" not in l: + cur = None + continue + sign, off = m.group(1), int(m.group(2), 16) + if sign == " ": + cur["-"].append(off); cur["+"].append(off) + else: + cur[sign].append(off) + +t = tables[which] +names = ["%x" % c for c in cases] + ["default"] +for side, label in (("-", "orig"), ("+", "ours")): + offs = t[side] + if len(offs) != len(names): + print("%s: %d entries, expected %d - alignment unreliable" % (label, len(offs), len(names))) + continue + order = sorted(range(len(offs)), key=lambda i: offs[i]) + print("%s: %s" % (label, ", ".join(names[i] for i in order))) diff --git a/tools/reimplementation-helper/batch/arm_sizes.py b/tools/reimplementation-helper/batch/arm_sizes.py new file mode 100644 index 0000000000..606d484816 --- /dev/null +++ b/tools/reimplementation-helper/batch/arm_sizes.py @@ -0,0 +1,68 @@ +"""Per-arm byte sizes of a jump-table switch, original vs ours, from reccmp's tables. + +The jump-table entries are in case order, so sorting them by offset gives both the arm layout +and - by differencing consecutive offsets - each arm's size. That turns "this 800-instruction +function is 55%" into a short list of arms that are actually the wrong length. + +usage: arm_sizes.py FUNCTION TABLE_INDEX case,case,... (cases ascending, no default) +""" +import re +import subprocess +import sys + +func, which = sys.argv[1], int(sys.argv[2]) +cases = [int(c, 0) for c in sys.argv[3].split(",")] +names = ["%x" % c for c in cases] + ["default"] + +out = subprocess.run(["python", "tools/reimplementation-helper/batch/reccmp_report.py", "diff", func], + capture_output=True, text=True).stdout.split("\n") +tables, cur = [], None +for l in out: + if l.strip().endswith("Jump table:"): + cur = {"-": [], "+": []} + tables.append(cur) + continue + if cur is None: + continue + m = re.match(r"^([-+ ])start \+ (0x[0-9a-f]+)", l) + if not m: + if l.strip() and "start +" not in l: + cur = None + continue + sign, off = m.group(1), int(m.group(2), 16) + if sign == " ": + cur["-"].append(off); cur["+"].append(off) + else: + cur[sign].append(off) + +t = tables[which] +for side in ("-", "+"): + if len(t[side]) != len(names): + sys.exit("%s side has %d entries, expected %d" % (side, len(t[side]), len(names))) + + +def layout_and_sizes(offs): + order = sorted(range(len(offs)), key=lambda i: offs[i]) + sz = {} + for k in range(len(order) - 1): + sz[names[order[k]]] = offs[order[k + 1]] - offs[order[k]] + sz[names[order[-1]]] = None + return [names[i] for i in order], sz + + +lo, so = layout_and_sizes(t["-"]) +lu, su = layout_and_sizes(t["+"]) +print("layout identical: %s" % (lo == lu)) +if lo != lu: + print(" orig: %s" % ", ".join(lo)) + print(" ours: %s" % ", ".join(lu)) +print("%-9s %6s %6s delta" % ("arm", "orig", "ours")) +total = 0 +for n in lo: + a, b = so[n], su.get(n) + if a is None or b is None: + print("%-9s %6s %6s (last arm, size unknown)" % (n, a, b)) + continue + total += b - a + print("%-9s %6d %6d %+5d%s" % (n, a, b, b - a, " <<<" if abs(b - a) >= 6 else "")) +print("total %+d bytes over %d arms" % (total, len(lo) - 1)) diff --git a/tools/reimplementation-helper/batch/fix_stride.py b/tools/reimplementation-helper/batch/fix_stride.py new file mode 100644 index 0000000000..6460baa0db --- /dev/null +++ b/tools/reimplementation-helper/batch/fix_stride.py @@ -0,0 +1,64 @@ +"""Repair struct-pointer walks that kept the decompiler's element stride. + +Ghidra walks these loops with a `short*` or `int*`, so it steps the pointer by +`sizeof(struct) / 2` or `/ 4`. When the import gave the variable the struct type back, the +step has to become 1 - `ptrUnit = ptrUnit + 0x248` on a `Unit*` (0x490 bytes) advances +0x248 structs, which is 0xa6880 bytes instead of 0x490. + +A silent wrong-memory bug, and the asm shows it directly (`add esi, 0xa6880` against the +original's `add esi, 0x490`). + +usage: fix_stride.py [filter] [--apply] +""" + +import re +import sys + +import common + +SIZES = {} + + +def struct_size(type_name): + """sizeof(T) from the static_assert in the header that defines T.""" + if type_name not in SIZES: + SIZES[type_name] = None + for path in common.ROOT.joinpath("src/OpenSHC").rglob("%s.hpp" % type_name): + m = re.search(r"static_assert_cpp98_obj\(sizeof\(%s\) == (\d+)" % type_name, path.read_text(encoding="utf-8")) + if m: + SIZES[type_name] = int(m.group(1)) + break + return SIZES[type_name] + + +apply = "--apply" in sys.argv +rest = [a for a in sys.argv[1:] if a != "--apply"] +filter_ = rest[0] if rest else "" +WALK = re.compile(r"^(\s*)(\w+) = \2 \+ (0x[0-9a-f]+|\d+);$", re.M) + +fixed = 0 +for f in common.list_files(): + if filter_ not in f: + continue + text = common.read_text(f) + out, hits = text, [] + for m in WALK.finditer(text): + name, step = m.group(2), int(m.group(3), 0) + decl = re.search(r"\b([A-Z]\w+)\*\s*%s\b" % re.escape(name), text) + if not decl or step == 1: + continue + size = struct_size(decl.group(1)) + if size is None or step * size == size: + continue + # the stride is the struct size counted in 1-, 2- or 4-byte elements + if step not in (size, size // 2, size // 4) or size % step: + continue + hits.append((m.group(0), "%s%s = %s + 1;" % (m.group(1), name, name), decl.group(1), step)) + for old, new, type_name, step in hits: + out = out.replace(old, new) + print("%-55s %s* += 0x%x -> 1" % (f.split("/")[-1], type_name, step)) + if hits: + fixed += len(hits) + if apply: + common.write_text(f, out) +print("%d struct-pointer stride(s)%s" % (fixed, "" if apply else " (dry run, pass --apply)")) diff --git a/tools/reimplementation-helper/batch/mix_this_global.py b/tools/reimplementation-helper/batch/mix_this_global.py new file mode 100644 index 0000000000..138a3cc908 --- /dev/null +++ b/tools/reimplementation-helper/batch/mix_this_global.py @@ -0,0 +1,71 @@ +"""Generate `this->` / global-instance variants of one function for quick_styles.py. + +The original sources mix the two: `this->field` and `DAT_X::instance.field` are the same object at +run time but compile to different addressing, and which one each *field* used is not uniform within +a function. A blanket conversion either way therefore loses as often as it gains - 16 of 49 +functions measured worse when `this_to_global.py` converted everything - while splitting the fields +recovers the original's choice. Over Map::Units::TroopValueState: + + registerSpottedEnemyTile 38.7% -> 81.8% (the indexed array on the global, the rest on this) + initializeOrAdvanceAttackWave 40.9% -> 54.8% (the scalars on the global, the arrays on this) + scanForSiegeTentPoints 32.0% -> 37.2% (one field back on this, which stops a hoist) + +The split that wins is not predictable, which is the point of measuring all four. `arrays` are the +fields a `[` follows and `scalars` the rest; the variants convert whichever form the file uses into +the other one, so it works from either starting point. + +usage: mix_this_global.py FILE.cpp [--run] (--run then calls quick_styles.py) +""" + +import re +import subprocess +import sys + +import common + +args = [a for a in sys.argv[1:] if a != "--run"] +if not args: + sys.exit(__doc__) +path = common.repo_relative(args[0]) +text = common.read_text(path) +klass = path.split("/")[-2] +glob_prefix = "DAT_%s::instance" % klass + +this_member = re.search(r"this->(\w+)\.", text) +glob_member = re.search(r"%s\.(\w+)\." % re.escape(glob_prefix), text) +if this_member: + member, src_form = this_member.group(1), "this->" + this_member.group(1) +elif glob_member: + member, src_form = glob_member.group(1), "%s.%s" % (glob_prefix, glob_member.group(1)) +else: + sys.exit("no `this->.` or `%s..` accesses in %s" % (glob_prefix, path)) +dst_form = "%s.%s" % (glob_prefix, member) if src_form.startswith("this->") else "this->" + member +include = '#include "OpenSHC/Globals/DAT_%s.hpp"' % klass + +fields = {"arrays": set(), "scalars": set()} +for name, after in re.findall(r"%s\.(\w+)(\[?)" % re.escape(src_form), text): + fields["arrays" if after == "[" else "scalars"].add(name) +if not fields["arrays"] and not fields["scalars"]: + sys.exit("nothing to split") + + +def convert(names): + out = re.sub(r"%s\.(\w+)" % re.escape(src_form), + lambda m: "%s.%s" % (dst_form, m.group(1)) if m.group(1) in names else m.group(0), text) + if dst_form.startswith("DAT_") and include not in out: + out = out.replace('#include "OpenSHC/Globals/', include + '\n#include "OpenSHC/Globals/', 1) + return out + + +name = re.search(r"//\s*FUNCTION:[^\n]*\n\s*[\w:&*\s]+?(\w+)\s*\(", text).group(1) +out_dir = common.TMP / "variants" / name +out_dir.mkdir(parents=True, exist_ok=True) +for style, body in (("base", text), + ("all_other", convert(fields["arrays"] | fields["scalars"])), + ("arrays_other", convert(fields["arrays"])), + ("scalars_other", convert(fields["scalars"]))): + (out_dir / (style + ".cpp")).write_text(body, encoding="utf-8", newline="") +print("%s: %s -> %s, %d array field(s), %d scalar field(s)" + % (name, src_form, dst_form, len(fields["arrays"]), len(fields["scalars"]))) +if "--run" in sys.argv: + subprocess.run([sys.executable, str(common.ROOT / "tools/reimplementation-helper/batch/quick_styles.py"), name]) diff --git a/tools/reimplementation-helper/batch/move_read.py b/tools/reimplementation-helper/batch/move_read.py new file mode 100644 index 0000000000..de4aef7c12 --- /dev/null +++ b/tools/reimplementation-helper/batch/move_read.py @@ -0,0 +1,107 @@ +"""Generate "this read belongs at its point of use" variants of one function for quick_styles.py. + +The decompiler lifts a field read to the top of the block it is used in; the original reads it where +it is used, and which side of an intervening call or test the read falls on changes the register +allocation of everything around it. Measured over Map::Units::TroopValueState this was the single +strongest source choice left: 27.7% -> 49.5% on `recountTotalTroopValue` (one read moved past a +call), 70.7% -> 100% on `queueOilThrowForIdleArchers` (past two threshold tests), 49.4% -> 61.5% on +`collectArcherUnitsByLocation` (into the branch that uses it). + +For every single-line assignment whose variable is next mentioned further down, this writes a +variant with that statement moved to just before the use. Candidates whose right-hand side contains +a call are skipped: moving one of those changes evaluation order, not just allocation. + +It refuses to move a statement across a loop head, out of its enclosing block, past a `goto` or a +label, next to a use that assigns the variable, or into a block some later use sits outside of. +Each of those produced a *winning* variant that was wrong before the guard existed: a counter reset +inside its own loop, a pointer increment lifted out of its walk, a unit scan restarted per queue +entry, and a `switch` arm left reading an uninitialised local while the other arms read it too. One +of them won by 0.09 points, so the score is no protection. + +**What remains unguarded is aliasing**: moving a read later still crosses whatever sits between, and +that is only safe if nothing in between writes the field. Read `git diff` on the winner and check +the callees it now crosses before keeping it. + +usage: move_read.py FILE.cpp [--run] [--max N] +""" + +import re +import subprocess +import sys + +import common + +ASSIGN = re.compile(r"^(\s+)([A-Za-z_]\w*) = ([^;]+);$") +DECL = re.compile(r"^(\s+)(?:[A-Za-z_][\w:]*\**\s+)([A-Za-z_]\w*) = ([^;]+);$") + +args = [a for a in sys.argv[1:] if not a.startswith("--")] +if not args: + sys.exit(__doc__) +limit = 12 +if "--max" in sys.argv: + limit = int(sys.argv[sys.argv.index("--max") + 1]) +path = common.repo_relative(args[0]) +text = common.read_text(path) +lines = text.split("\n") +name = re.search(r"//\s*FUNCTION:[^\n]*\n\s*[\w:&*\s]+?(\w+)\s*\(", text).group(1) + +out_dir = common.TMP / "variants" / name +out_dir.mkdir(parents=True, exist_ok=True) +(out_dir / "base.cpp").write_text(text, encoding="utf-8", newline="") + +depth, level = [], 0 +for line in lines: + opens, closes = line.count("{"), line.count("}") + depth.append(level - closes if closes else level) + level += opens - closes + +made = 0 +for i, line in enumerate(lines): + m = ASSIGN.match(line) or DECL.match(line) + if not m: + continue + indent, var, rhs = m.group(1), m.group(2), m.group(3) + if "(" in rhs and "MACRO_CALL" in rhs: + continue + word = re.compile(r"\b%s\b" % re.escape(var)) + target = None + for j in range(i + 1, len(lines)): + if word.search(lines[j]): + target = j + break + if target is None or target <= i + 1: + continue + # a second assignment to the same variable in between means the read is not the one in use + if any(re.match(r"\s+%s = " % re.escape(var), lines[k]) for k in range(i + 1, target)): + continue + # the first mention assigning the variable means this statement is dead once moved there + if re.match(r"\s+%s = " % re.escape(var), lines[target]): + continue + # never move a statement across a loop head (it would run a different number of times) + if any(re.match(r"\s*(?:\} )?(?:do \{|while \(|for \()", lines[k]) for k in range(i + 1, target + 1)): + continue + # and never move it out of its enclosing block: only deeper or level, never shallower + if depth[target] < depth[i]: + continue + # a goto or a label in between means the statement may be re-entered or skipped + if any(re.search(r"goto|^\s*\w*LAB_\w+:", lines[k]) for k in range(i + 1, target + 1)): + continue + # every later use has to live inside the block we are moving into, or the other uses would + # read the variable before this statement runs - moving `sVar3` into one switch arm left the + # other arms reading it uninitialised + block_end = next((k for k in range(target + 1, len(lines)) if depth[k] < depth[target]), len(lines)) + if any(word.search(lines[k]) for k in range(block_end, len(lines))): + continue + moved = list(lines) + stmt = moved.pop(i) + use_indent = re.match(r"\s*", moved[target - 1]).group(0) + moved.insert(target - 1, use_indent + stmt.strip()) + style = "move_%s_%d" % (re.sub(r"\W", "", var)[:16], i + 1) + (out_dir / (style + ".cpp")).write_text("\n".join(moved), encoding="utf-8", newline="") + made += 1 + if made >= limit: + break + +print("%s: %d variant(s) in %s" % (name, made, out_dir)) +if "--run" in sys.argv and made: + subprocess.run([sys.executable, str(common.ROOT / "tools/reimplementation-helper/batch/quick_styles.py"), name]) diff --git a/tools/reimplementation-helper/batch/resolve_address.py b/tools/reimplementation-helper/batch/resolve_address.py new file mode 100644 index 0000000000..064eb577c0 --- /dev/null +++ b/tools/reimplementation-helper/batch/resolve_address.py @@ -0,0 +1,129 @@ +"""Resolve hard-coded game addresses in the sources to `DAT_X::instance.field` paths. + +Ghidra leaves absolute addresses of the original's globals in the decompiled output, as loop +bounds (`while ((int)p < 0x17802b4)`) and as bases (`*(byte*)(tile + 0x1ee2998)`). They are not +just unreadable: our DLL's resolver instances live elsewhere, so a literal address reads the wrong +memory at runtime and never matches the original's asm. + +usage: resolve_address.py [filter] report every literal in the matching sources + resolve_address.py 0x1ee2998 ... resolve single addresses +""" + +import re +import sys + +import common + +GLOBAL_RE = re.compile(r"//\s*GLOBAL:\s*STRONGHOLDCRUSADER\s+(0x[0-9A-Fa-f]+)") +RESOLVER_RE = re.compile(r"MACRO_STRUCT_RESOLVER\((\w+),\s*\w+,\s*[\w:]+\)\s*(\w+);") +DECL_RE = re.compile(r"^\s*(?:(?:const|static)\s+)?([A-Za-z_][\w:]*)\s+(\w+)((?:\[\d+\])*)\s*;" + r"\s*//\s*(0x[0-9A-Fa-f]+)\s+length:\s*(\d+)") +SIZE_RE = re.compile(r"static_assert_cpp98_obj\(sizeof\((\w+)\) == (\d+)") +SCALARS = {"byte": 1, "char": 1, "undefined1": 1, "bool": 1, "uchar": 1, + "short": 2, "ushort": 2, "undefined2": 2, "word": 2, + "int": 4, "uint": 4, "undefined4": 4, "dword": 4, "float": 4, "long": 4, "ulong": 4} + +_structs = {} + + +def struct_of(name): + """{name: (type, offset, dims)} plus its size, parsed from the header that defines it.""" + if name in _structs: + return _structs[name] + _structs[name] = None + for path in common.ROOT.joinpath("src/OpenSHC").rglob("%s.hpp" % name.split("::")[-1]): + text = path.read_text(encoding="utf-8", errors="replace") + if not re.search(r"\b(?:struct|class)\s+%s\b" % name.split("::")[-1], text): + continue + fields = [] + for line in text.splitlines(): + m = DECL_RE.match(line) + if m: + fields.append((m.group(2), m.group(1), int(m.group(4), 16), + [int(d) for d in re.findall(r"\[(\d+)\]", m.group(3))], int(m.group(5)))) + m = SIZE_RE.search(text) + if fields: + _structs[name] = (fields, int(m.group(2)) if m else None) + break + return _structs[name] + + +def path_of(type_name, offset, depth=0): + """Field path inside `type_name` for a byte offset, e.g. `.playerInfo[2].wide3`.""" + if depth > 6: + return None + info = struct_of(type_name) + if info is None: + return None + for name, ftype, foff, dims, length in info[0]: + if not foff <= offset < foff + length: + continue + rel = offset - foff + count = 1 + for d in dims: + count *= d + elem = length // count if count else length + index = "" + if dims: + flat = rel // elem + rel -= flat * elem + for d in reversed(dims): + index = "[%d]" % (flat % d) + index + flat //= d + if ftype in SCALARS or ftype.startswith("undefined"): + return ".%s%s" % (name, index) + ("+%d" % rel if rel else "") + inner = path_of(ftype, rel, depth + 1) + if inner is None: + return ".%s%s" % (name, index) + ("+%d" % rel if rel else "") + return ".%s%s%s" % (name, index, inner) + return "+%d" % offset + + +def globals_index(): + out = [] + for path in sorted(common.ROOT.glob("src/OpenSHC/Globals/*.hpp")): + text = path.read_text(encoding="utf-8", errors="replace") + g, r = GLOBAL_RE.search(text), RESOLVER_RE.search(text) + if not g or not r: + continue + base, type_name, var = int(g.group(1), 16), r.group(1), r.group(2) + info = struct_of(type_name) + size = info[1] if info and info[1] else SCALARS.get(type_name, 4) + out.append((base, base + size, var, type_name)) + return sorted(out) + + +INDEX = globals_index() + + +def resolve(address): + for base, end, var, type_name in INDEX: + if base <= address < end: + return "%s::instance%s" % (var, path_of(type_name, address - base) or "+%d" % (address - base)) + return None + + +args = [a for a in sys.argv[1:]] +if args and args[0].startswith("0x"): + for a in args: + print("%-12s %s" % (a, resolve(int(a, 16)) or "no global covers this address")) + sys.exit() + +filter_ = args[0] if args else "" +LITERAL = re.compile(r"0x1[0-9a-f]{6}\b") +total = 0 +for f in common.list_files(): + if filter_ not in f: + continue + text = common.read_text(f) + hits = {} + for n, line in enumerate(text.splitlines(), 1): + for lit in LITERAL.findall(line): + hits.setdefault(lit, []).append(n) + if not hits: + continue + print("### %s" % f.split("/")[-1]) + for lit, lines in sorted(hits.items()): + print(" %-12s %-4s %s" % (lit, len(lines), resolve(int(lit, 16)) or "UNRESOLVED")) + total += len(lines) +print("%d literal address(es)" % total) diff --git a/tools/reimplementation-helper/batch/scan_arms.py b/tools/reimplementation-helper/batch/scan_arms.py new file mode 100644 index 0000000000..f9ebfa533a --- /dev/null +++ b/tools/reimplementation-helper/batch/scan_arms.py @@ -0,0 +1,33 @@ +"""Report je/jne (and jz/jnz) pairs where the original branches the other way. + +A flipped equality branch after the same compare means the two arms of an if/else are +the other way round in the original: the arm it falls into is the one it keeps inline. +""" +import re +import sys + +import common + +FLIP = {"je": "jne", "jne": "je"} +for f, e, _ in common.entries(common.load_diff()): + if e is None or float(e["matching"]) >= 1: + continue + hits = [] + for hunk in e.get("diff") or []: + for block in hunk[1]: + orig = [r[1] for r in block.get("orig", [])] + recomp = [r[1] for r in block.get("recomp", [])] + for a in orig: + oa = a.split("\t")[0].strip().split() + if not oa or oa[0] not in FLIP: + continue + for b in recomp: + ob = b.split("\t")[0].strip().split() + if ob and ob[0] == FLIP[oa[0]]: + hits.append((oa[0], ob[0], b)) + break + if hits: + print("### %-50s %4.1f%% %d flipped branch(es)" % (e["name"].split("::")[-1], + float(e["matching"]) * 100, len(hits))) + for o, n, ctx in hits[:3]: + print(" orig %-4s ours %-4s %s" % (o, n, ctx.strip()[:90])) diff --git a/tools/reimplementation-helper/batch/scan_this_global.py b/tools/reimplementation-helper/batch/scan_this_global.py new file mode 100644 index 0000000000..c5ff274888 --- /dev/null +++ b/tools/reimplementation-helper/batch/scan_this_global.py @@ -0,0 +1,35 @@ +"""Functions where the original reaches the TroopValueState-style globals by absolute address +while our source goes through `this->`. + +An original that loads `I::instance+N` for a field our code writes as `this->field` means the +original source named the global instance there, not the implicit object. Reported per function as +the number of such lines on each side, plus how often our source mentions `this->`. +""" + +import re +import sys + +import common + +GLOBAL = re.compile(r"StructResolver::Instance<([\w:]+)") +# a field access through the this pointer: the register holding `this` plus a displacement +THIS_FIELD = re.compile(r"\[(e[a-z][a-z]) [+-] 0x[0-9a-f]+\]") + +filter_ = sys.argv[1] if len(sys.argv) > 1 else "" +rows = [] +for f, e, _ in common.entries(common.load_diff()): + if e is None or filter_ not in f: + continue + orig, recomp = common._asm_lines(e) + name = e["name"].split("::")[-1] + klass = "::".join(e["name"].split("::")[:-1]) + orig_global = sum(1 for l in orig if klass in l) + our_global = sum(1 for l in recomp if klass in l) + our_this = sum(1 for l in recomp if THIS_FIELD.search(l)) + if orig_global > our_global and our_this: + rows.append((orig_global - our_global, name, orig_global, our_global, our_this, + common.read_text(f).count("this->"))) + +for gap, name, og, ng, nt, src in sorted(rows, reverse=True): + print("%4d %-55s orig %3d ours %3d our-this-lines %3d src this-> %3d" % (gap, name, og, ng, nt, src)) +print("%d function(s)" % len(rows)) diff --git a/tools/reimplementation-helper/batch/swap_arms.py b/tools/reimplementation-helper/batch/swap_arms.py new file mode 100644 index 0000000000..0b632849c8 --- /dev/null +++ b/tools/reimplementation-helper/batch/swap_arms.py @@ -0,0 +1,72 @@ +"""Swap the two arms of an if/else chain in place, without changing behaviour. + +`if (C1) { B1 } else { B2 }` becomes `if (negC1) { B2 } else { B1 }` +`if (C1) { B1 } else if (C2) { B2 }` becomes `if (negC1) { if (C2) { B2 } } else { B1 }` + +The point is the generated block *layout*: reccmp reads the original's block order as its +source order, so putting our arms in that order is how the two streams line up. Both the +`if` header and the `else if` header may wrap over several lines. + +usage: swap_arms.py FILE IF_LINE "negated C1" (IF_LINE is 1-based) +""" +import sys +from pathlib import Path + + +def header_end(lines, start): + j = start + while not lines[j].rstrip().endswith("{"): + j += 1 + return j + + +def find_chain(lines, a): + ind = len(lines[a]) - len(lines[a].lstrip()) + body = header_end(lines, a) + 1 + depth, else_at = 1, None + for j in range(body, len(lines)): + stripped = lines[j].strip() + same_level = (len(lines[j]) - len(lines[j].lstrip())) == ind + if depth == 1 and same_level and stripped.startswith("} else"): + else_at = j + break + depth += lines[j].count("{") - lines[j].count("}") + assert else_at is not None, "no `} else` for the if at line %d" % (a + 1) + hdr = header_end(lines, else_at) + depth, end = 1, None + for j in range(hdr + 1, len(lines)): + depth += lines[j].count("{") - lines[j].count("}") + if depth == 0: + end = j + break + assert end is not None and lines[end].strip() == "}", "no close for the else at line %d" % (else_at + 1) + return ind, body, else_at, hdr, end + + +def reindent(block, extra): + pad = " " * extra + return [pad + x if x.strip() else x for x in block] + + +def main(): + path, at, neg = Path(sys.argv[1]), int(sys.argv[2]) - 1, sys.argv[3] + lines = path.read_text().split("\n") + assert lines[at].strip().startswith("if ("), repr(lines[at]) + ind, body, else_at, hdr, end = find_chain(lines, at) + pad = " " * ind + arm1 = lines[body:else_at] + arm2 = lines[hdr + 1:end] + header = [lines[else_at].strip()[len("} else "):]] + [x.strip() for x in lines[else_at + 1:hdr + 1]] + if header[0] == "{": + new = [pad + "if (" + neg + ") {"] + arm2 + [pad + "} else {"] + arm1 + [pad + "}"] + kind = "plain else" + else: + new = ([pad + "if (" + neg + ") {"] + + [pad + " " + h for h in header] + + reindent(arm2, 4) + [pad + " }", pad + "} else {"] + arm1 + [pad + "}"]) + kind = "else-if (%d header lines)" % len(header) + path.write_text("\n".join(lines[:at] + new + lines[end + 1:])) + print("line %d: arm1 %d lines, arm2 %d lines, %s" % (at + 1, len(arm1), len(arm2), kind)) + + +main() diff --git a/tools/reimplementation-helper/batch/sweep_all.py b/tools/reimplementation-helper/batch/sweep_all.py new file mode 100644 index 0000000000..1c6b593a6e --- /dev/null +++ b/tools/reimplementation-helper/batch/sweep_all.py @@ -0,0 +1,95 @@ +"""Run every diff-pattern sweep over the sub-100 functions at once. + +Each sweep looks for one way the original's instruction stream disagrees with ours in a way the +*source* controls: an inverted branch (arms the wrong way round), a near-miss constant (`x < K+1` +vs `x <= K`), swapped compare operands, a signed/unsigned mismatch, or a redundant width +conversion. See AGENTS.md for what each one means and how often it pays. +""" +import re +import subprocess + +INV = {"je": "jne", "jne": "je", "jl": "jge", "jge": "jl", "jg": "jle", "jle": "jg", + "ja": "jbe", "jbe": "ja", "jb": "jae", "jae": "jb"} +SIGN = ({"jl", "jb"}, {"jle", "jbe"}, {"jg", "ja"}, {"jge", "jae"}) +JCC = re.compile(r"^([-+])(j\w+)\s") +NUM = re.compile(r"^([-+])cmp\s+(\S+),\s*(0x[0-9a-f]+|\d+)\b") +OPS = re.compile(r"^([-+])cmp\s+(.+?),\s*([^\t]+?)\s*(?:\t.*)?$") + + +def run(*args): + return subprocess.run(["python", "tools/reimplementation-helper/batch/reccmp_report.py"] + list(args), + capture_output=True, text=True).stdout + + +names = [l.split()[2].split("::")[-1] for l in run("pct").split("\n") + if l.strip() and l.split()[0] != "100.0" and len(l.split()) > 2] +lines = run("diff", *names).split("\n") + +cur, src = None, {} +funcs = [] +for i, l in enumerate(lines): + if l.startswith("###"): + cur = l.split("::")[-1].split()[0] + funcs.append(cur) + m = re.search(r"cpp:(\d+)", l) + src[i] = m.group(1) if m else None + + +def nearest_src(i): + for j in range(i, min(i + 6, len(lines))): + if src.get(j): + return src[j] + return "?" + + +hits = {} + + +def add(kind, i, text): + hits.setdefault(funcs[i], []).append("%-9s @%-5s %s" % (kind, nearest_src(i), text)) + + +for i, l in enumerate(lines): + m = JCC.match(l) + if m and m.group(1) == "-": + for j in range(i + 1, min(i + 4, len(lines))): + m2 = JCC.match(lines[j]) + if not m2 or m2.group(1) != "+": + continue + if INV.get(m.group(2)) == m2.group(2): + add("INVERTED", j, "%s -> %s" % (m.group(2), m2.group(2))) + elif {m.group(2), m2.group(2)} in SIGN: + add("SIGNED", j, "%s / %s" % (m.group(2), m2.group(2))) + break + if re.match(r"^-movsx \w+, word ptr", l): + nxt = " ".join(lines[i + 1:i + 4]) + if "+movzx" in nxt and "movsx" in nxt: + add("WIDTH", i, l.strip()[:48]) + +pend = [] +for i, l in enumerate(lines): + m = NUM.match(l) + if m: + pend.append((i, m.group(1), int(m.group(3), 0))) +for k in range(len(pend) - 1): + (i, s1, v1), (j, s2, v2) = pend[k], pend[k + 1] + if funcs[i] == funcs[j] and s1 == "-" and s2 == "+" and v1 != v2 and abs(v1 - v2) <= 4: + add("CONST", j, "orig %s vs ours %s" % (hex(v1), hex(v2))) + +prev = None +for i, l in enumerate(lines): + if l.startswith("###"): + prev = None + continue + m = OPS.match(l) + if not m: + continue + if prev and prev[1] == "-" and m.group(1) == "+" and (prev[2] == m.group(3) or prev[3] == m.group(2)) \ + and (prev[2], prev[3]) != (m.group(2), m.group(3)): + add("OPERANDS", i, "orig %s,%s | ours %s,%s" % (prev[2][:14], prev[3][:14], m.group(2)[:14], m.group(3)[:14])) + prev = (i,) + m.groups() + +for k in sorted(hits, key=lambda x: -len(hits[x])): + print("%s (%d)" % (k, len(hits[k]))) + for t in hits[k][:6]: + print(" " + t) diff --git a/tools/reimplementation-helper/batch/this_to_global.py b/tools/reimplementation-helper/batch/this_to_global.py new file mode 100644 index 0000000000..0ad42f85a2 --- /dev/null +++ b/tools/reimplementation-helper/batch/this_to_global.py @@ -0,0 +1,42 @@ +"""Rewrite `this->field` as the global instance in the functions where the original did. + +`scan_this_global.py` lists the functions whose original asm reaches a field by absolute address +while ours goes through the `this` register. There the original source named the global instance, +so the fix is textual: `this->x` -> `DAT_::instance.x`, plus the Globals include. + +usage: this_to_global.py NAME... [--apply] (names as scan_this_global.py prints them) +""" + +import re +import sys + +import common + +apply = "--apply" in sys.argv +names = [a for a in sys.argv[1:] if a != "--apply"] +if not names: + sys.exit(__doc__) + +INCLUDE_RE = re.compile(r'^#include "OpenSHC/Globals/[^"]+"$', re.M) +changed = 0 +for f in common.list_files(): + text = common.read_text(f) + if not any(re.search(r"\b%s\b\s*\(" % n, text) for n in names) or "this->" not in text: + continue + klass = f.split("/")[-2] + glob = "DAT_%s" % klass + new = text.replace("this->", "%s::instance." % glob) + include = '#include "OpenSHC/Globals/%s.hpp"' % glob + if include not in new: + existing = INCLUDE_RE.findall(new) + if existing: + block = sorted(existing + [include]) + new = new.replace("\n".join(existing), "\n".join(block), 1) + else: + new = new.replace("\nnamespace OpenSHC {", "\n%s\n\nnamespace OpenSHC {" % include, 1) + print("%-60s %d site(s)" % (f.split("/")[-1], text.count("this->"))) + changed += 1 + if apply: + common.write_text(f, new) + common.format_file(f) +print("%d file(s)%s" % (changed, "" if apply else " (dry run, pass --apply)"))