aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit a3a072897ae6da3da6d98406344fb421ed5f3333
parent 1f7e91a389f3c57e0e144a7071b355b380f6f126
Author: anon <anon@example.com>
Date:   Fri, 23 Oct 2026 20:34:22 +0000

Persist native IP training with authoritative costs and caps

Diffstat:
MTODO.md | 16+++++++++++++---
Mclient/Makefile.linux | 7+++++++
Aclient/tests/ip_training.c | 245+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mdocs/character-progression.md | 5+++--
Adocs/ip-training.md | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/gamedata/ip_training.yaml | 946+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/gamedata/load.go | 15+++++++++++++++
Aserver/ip_training.go | 161+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aserver/ip_training_test.go | 436+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mserver/main.go | 4++++
Mserver/stats.go | 166++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mserver/zoning.go | 2+-
Atools/data/extract_ip_training.py | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13 files changed, 2158 insertions(+), 83 deletions(-)

diff --git a/TODO.md b/TODO.md @@ -285,15 +285,25 @@ Known-amount credit collection and native balance limits are verified in level display and the correct derived resources without a second level-up. - [ ] Award IP for that level. Check: the expected amount is granted once and the native unspent-IP value survives reconnect. -- [ ] Raise one trained skill with IP. Check: the native request spends the +- [x] Raise one trained skill with IP. Check: the native request spends the correct amount, updates effective skill and persists the base value. -- [ ] Raise one ability with IP. Check: cost and ability trickle update together +- [x] Raise one ability with IP. Check: cost and ability trickle update together without treating the ability as a trained skill. -- [ ] Enforce one skill/title cap boundary. Check: the final permitted increase +- [x] Enforce one skill/title cap boundary. Check: the final permitted increase succeeds and the next is rejected without spending IP. +- [ ] Establish original-server fractional IP rounding. Check: purchases split + across edits and ACCEPT batches, reordered stat pairs and large balances match + period server observations. Current charging sums exact tenths and rounds the + accepted batch up once; native draft float arithmetic alone cannot prove the + original server rule. +- [ ] Replace provisional new-character stats with verified breed/profession + defaults. Check: all four breeds begin with legal ability values and correct + raw skills/IP; the current all-10 abilities exceed some level-1 native caps. The recovered XP update and level-up handler, controlled WASM checks and remaining reward-rule gaps are recorded in [character progression](docs/character-progression.md). +Native training tables, packet validation, atomic saves, cap boundaries, +replay/failure tests and WASM checks are recorded in [IP training](docs/ip-training.md). ## Usable items diff --git a/client/Makefile.linux b/client/Makefile.linux @@ -171,6 +171,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/guest-heap ./build/linux/native-input ./build/linux/weapon-range + ./build/linux/ip-training ./build/linux/guest-crt ./build/linux/getenv ./build/linux/linux-platform @@ -653,3 +654,9 @@ build/linux/weapon-range: tests/weapon_range.c game/kkr.dll/lifted-0050.c runtim $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ test: build/linux/weapon-range + +build/linux/ip-training: tests/ip_training.c game/kkr.dll/lifted-0034.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + @mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ + +test: build/linux/ip-training diff --git a/client/tests/ip_training.c b/client/tests/ip_training.c @@ -0,0 +1,245 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_360751E0(void); +void sub_360755C0(void); + +static int32_t profession, level, title, factor, category; +static int32_t title_cap, previous_cap, per_level, title_start; +static int32_t breed_base, breed_cap, tested_stat; +static double trickle; + +static void actor_stat(void) +{ + assert(g_ecx == 0x500002e4 && MEM32(g_esp + 8) == 2); + switch (MEM32(g_esp + 4)) { + case 4: g_eax = 1; break; + case 37: g_eax = title; break; + case 54: g_eax = level; break; + case 60: g_eax = profession; break; + default: abort(); + } + g_esp += 12; +} + +static void ability_factor(void) +{ + assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); + g_eax = factor; + g_esp += 4; +} + +static void skill_factor(void) +{ + assert(MEM32(g_esp + 4) == (uint32_t)profession); + assert(MEM32(g_esp + 8) == (uint32_t)tested_stat); + g_eax = factor; + g_esp += 4; +} + +static void category_cap(void) +{ + assert(MEM32(g_esp + 4) == (uint32_t)category); + int32_t requested_title = MEM32(g_esp + 8); + assert(requested_title == title || requested_title == title - 1); + g_eax = requested_title == title ? title_cap : previous_cap; + g_esp += 4; +} + +static void category_growth(void) +{ + assert(MEM32(g_esp + 4) == (uint32_t)category); + g_eax = per_level; + g_esp += 4; +} + +static void profession_title(void) +{ + assert(MEM32(g_esp + 4) == (uint32_t)profession); + assert(MEM32(g_esp + 8) == (uint32_t)title); + MEM32(0x5000040c) = title_start; + g_eax = 0x50000400; + g_esp += 4; +} + +static void ability_trickle(void) +{ + assert(g_ecx == 0x50000000); + assert(MEM32(g_esp + 4) == (uint32_t)tested_stat && MEM32(g_esp + 8) == 0); + fp_push_impl(g_st, &g_fp_top, trickle); + g_esp += 12; +} + +static void breed_start(void) +{ + assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); + g_eax = breed_base; + g_esp += 4; +} + +static void breed_maximum(void) +{ + assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); + g_eax = breed_cap; + g_esp += 4; +} + +static void integer(void) +{ + g_eax = (uint32_t)(int32_t)g_st[0]; + fp_pop_impl(g_st, &g_fp_top); + g_esp += 4; +} + +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x50000300: return actor_stat; + case 0x360dd900: return ability_factor; + case 0x360e95d0: return skill_factor; + case 0x360ee030: return category_cap; + case 0x360ee060: return category_growth; + case 0x360e6050: return profession_title; + case 0x36073fa0: return ability_trickle; + case 0x360ee870: return breed_start; + case 0x360ee8c0: return breed_maximum; + case 0x3615fd8c: return integer; + default: fprintf(stderr, "unexpected IP training call: %08x\n", address); abort(); + } +} +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } + +static void setup(void) +{ + g_esp = 0x5000ff00; + g_ecx = 0x50000000; + g_fp_top = 0; + profession = 6; + level = 3; + title = 1; + factor = 10; + category = 1; + title_cap = 100; + previous_cap = 40; + per_level = 5; + title_start = 15; + trickle = 100; + breed_base = 6; + breed_cap = 512; +} + +static void point_costs(void) +{ + const struct { int32_t stat, raw, factor, profession; double expected; } cases[] = { + {100, 5, 10, 6, 5.000000074505806}, + {100, 5, 15, 6, 7.500000111758709}, + {100, 5, 16, 6, 8.00000011920929}, + {16, 5, 2, 6, 10}, {21, 5, 2, 6, 10}, + {100, 0, 10, 6, 1.0000000149011612}, + {100, -3, 10, 6, 1.0000000149011612}, + {100, 5, 10, -1, 1}, + {15, 5, 10, 6, 2.5}, {22, 5, 10, 6, 2.5}, + {99, 5, 10, 6, 2.5}, {168, 5, 10, 6, 5.000000074505806}, + {169, 5, 10, 6, 2.5} + }; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + setup(); + tested_stat = cases[i].stat; + factor = cases[i].factor; + profession = cases[i].profession; + MEM32(g_esp + 4) = tested_stat; + MEM32(g_esp + 8) = cases[i].raw; + + sub_360751E0(); + + assert(g_fp_top == 1 && g_st[0] == cases[i].expected); + assert(g_esp == 0x5000ff0c); + } +} + +static void skill_caps(void) +{ + const struct { + int32_t factor, category, title, level, per_level, title_cap; + double trickle; + int32_t expected; + } cases[] = { + {10, 1, 1, 3, 5, 100, 100, 20}, + {14, 1, 1, 3, 5, 100, 100, 20}, + {15, 2, 1, 3, 5, 100, 100, 20}, + {16, 2, 1, 3, 5, 100, 100, 20}, + {24, 2, 1, 3, 5, 100, 100, 20}, + {25, 3, 1, 3, 5, 100, 100, 20}, + {10, 1, 1, 3, 0, 100, 100, 23}, + {10, 1, 1, 3, 11, 100, 100, 23}, + {10, 1, 1, 3, 5, 100, 1.99, 15}, + {10, 1, 1, 3, 5, 100, 0, 20}, + {10, 1, 2, 20, 5, 100, 100, 70}, + {10, 1, 2, 20, 5, 60, 100, 60}, + {10, 1, 2, 20, 10, 100, 100, 76}, + {10, 1, 2, 20, 5, 100, 3.125, 25} + }; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + setup(); + tested_stat = 100; + factor = cases[i].factor; + category = cases[i].category; + title = cases[i].title; + level = cases[i].level; + per_level = cases[i].per_level; + title_cap = cases[i].title_cap; + trickle = cases[i].trickle; + MEM32(g_esp + 4) = tested_stat; + + sub_360755C0(); + + assert((int32_t)g_eax == cases[i].expected); + assert(g_esp == 0x5000ff08 && g_fp_top == 0); + } +} + +static void ability_caps(void) +{ + const struct { int32_t stat, level, base, maximum, expected; } cases[] = { + {16, 1, 6, 512, 9}, {21, 3, 6, 512, 15}, + {16, 200, 6, 480, 480}, {21, 200, 6, 512, 512} + }; + for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + setup(); + tested_stat = cases[i].stat; + level = cases[i].level; + breed_base = cases[i].base; + breed_cap = cases[i].maximum; + MEM32(g_esp + 4) = tested_stat; + + sub_360755C0(); + + assert((int32_t)g_eax == cases[i].expected); + assert(g_esp == 0x5000ff08 && g_fp_top == 0); + } +} + +int main(void) +{ + assert(guest_map("IP training fixture", 0x50000000, 0x10000)); + assert(guest_map("native IP constants", 0x36170000, 0x10000)); + MEM32(0x50000014) = 0x50000200; + MEM32(0x500002e4) = 0x50000100; + MEM32(0x50000104) = 0x50000300; + + // Original kkr.dll constant bytes, rebased from 1017xxxx to 3617xxxx. + MEM32(0x36170380) = 0x3f800000; + MEM32(0x36170464) = 0x3f000000; + MEM32(0x361709dc) = 0x447a0000; + MEM32(0x36170ba8) = 0x41000000; + MEM32(0x361710c4) = 0x40a00000; + MEM32(0x361714f0) = 0; + MEM32(0x361714f4) = 0x3fe00000; + MEM32(0x361714f8) = 0x3dcccccd; + + point_costs(); + skill_caps(); + ability_caps(); + puts("native IP training: point costs, category boundaries and skill/ability caps passed"); +} diff --git a/docs/character-progression.md b/docs/character-progression.md @@ -1,7 +1,8 @@ # Character progression: 14.9.1 protocol evidence -Kill XP, level advancement and IP spending remain unimplemented. This report -recovers the native presentation contract; the replay values below are test +Kill XP and level advancement remain unimplemented. Native +[IP spending](ip-training.md) now persists validated skill/ability purchases. +This report recovers the native presentation contract; the replay values below are test inputs, not historical creature rewards. ## Absolute XP updates diff --git a/docs/ip-training.md b/docs/ip-training.md @@ -0,0 +1,153 @@ +# IP training in 14.9.1 + +The native Skills window now commits purchased abilities and skills to SQLite. +An accepted batch updates its raw values and IP balance in one transaction. +The server validates ownership, session state, costs and caps; the client cannot +choose its own balance. Repeated ACCEPT packets do not purchase the same points +again. Resetting already committed points remains a separate, unimplemented action. + +## Original resources and calculations + +The original `kkr.dll` has SHA-256 +`a37bc1c15053fdcc5572772309f7074c4f83baffe2faa164df40b059b7c480c0`. +The following functions and resources establish the client calculations: + +| Resource | Reader | Consumed data | +| --- | --- | --- | +| 1000027, breeds 1-4 | `FUN_100ef270` | Six ability baselines and six absolute maxima | +| 1000210, breeds 1-4 | `FUN_100dd9a0` | Ability price factors, in STR/INT/AGI/SEN/STA/PSY order | +| 1000206, professions 1-12 | `FUN_100e9670` | Skill price factors in tenths, in the reader's explicit 77-stat order | +| 1000209, categories 1-5 | `FUN_100ee0e0` | Six title caps and growth per level | +| 1000200, professions 1-12 | `FUN_100e60f0` | Title thresholds 1, 15, 50, 100, 150, 190 | +| 1000204, skills 100-168 | `FUN_100ec8e0` getter | Six ability weights | + +`tools/data/extract_ip_training.py` writes the consumed data to +`server/gamedata/ip_training.yaml` and retains source bytes, per-record hashes +and the resource-index hash in `local/ip-training-research/resources.json`. +It excludes AC fields 90-97 and the additional profession/breed records outside +the supported original player profiles. Original Run Speed weights sum to 110; +the extractor preserves them. It also preserves the original Solitus Strength +maximum of 472 rather than substituting a modern table's 480. + +`FUN_100751e0` prices the next purchased point from the CURRENT raw value, +clamped to at least 1. Abilities multiply that value by the breed factor; +skills multiply by the profession's integer factor and the native float 0.1. +Neither ability trickle nor direct skill equipment bonuses enter the price. +The exported `N3Msg_GetSkillCost` explicitly obtains raw stat mode 2 first. + +`FUN_100755c0` limits abilities to `breed baseline + 3 * level`, bounded by +the breed maximum. Skill category is the truncated result of +`factor * float32(0.1) + 0.5`. Its title cap and growth table are separate from +the skill's displayed color. In title 1 the growth limit is `5 + level * growth`. +Later titles use the previous title's cap plus growth for +`level + 1 - current title start`; a separate six-per-level bound also applies. + +Skills also have a positive ability-dependent bound: truncate eight times the +UNTRUNCATED native trickle calculation. `FUN_10073fa0(stat, 0)` includes worn +ability modifiers through `FUN_10074a30`. The 0.01 single-precision constant +matters: six abilities of 10 can yield a raw cap of 19 rather than 20. +The server checks the final proposed abilities when validating a multi-stat +batch. Existing administrator-set values above a cap may remain unchanged; +they cannot be increased beyond that cap through training. + +The shipped help files `Improving skills and abilities.txt` and `IPReset.txt` +independently describe breed/profession costs, local draft changes, ACCEPT, +CANCEL and the distinction between purchased values and ability additions. +The February 7, 2003 firsthand post +[Truth about Skill Color](https://forums-archive.anarchy-online.com/showthread.php?98121-Truth-about-Skill-Color-MUST-READ-Newbs-amp-Veterans=) +supports current-raw-value pricing. Some of its category boundaries and title-1 +caps differ from the executable and shipped tables; the implementation follows +the latter. The post was retrieved through its indexed search body; a direct +HTML fetch returned 404. + +## Native request and synchronization + +`SkillIIR_t` has key `0x3e205660`. Constructor `FUN_10083ad0`, serializer +`FUN_10083d60` and shared map writer `FUN_10008c00` establish its body: + +1. Signed 32-bit count, network byte order. +2. That many `(stat ID, absolute raw value)` signed 32-bit pairs. + +The native ACCEPT path sends all 75 entries, stats 16-21 and 100-168, including +unchanged entries. It does not send stat 53, an IP cost, status, or request ID. +`FUN_10074380` snapshots the draft; `FUN_10074ec0` changes local values and +float IP; `FUN_10074130` sends ACCEPT. `FUN_100741e0` restores CANCEL locally +without a SkillIIR request. + +Receiver `FUN_10083d80` assigns authoritative raw values and refreshes the local +Skills view. The server returns this snapshot and an absolute StatIIR update +for IP 53 on acceptance or rejection. It shares accepted raw stats and changed +HP/attack foundations with observers; IP and nano remain private. No historical +failure-status opcode has been established. Malformed packet framing ends the +connection; well-framed invalid purchases receive the unchanged authoritative +snapshot. Database failures publish no successful update. + +## Explicit limits of the reconstruction + +The server totals exact tenths of IP and rounds the accepted batch up once to +its integer IP charge. That policy agrees with the exercised native purchases +and prevents large balances or pair ordering from creating free points. The +original client's draft uses floats and can depend on editing order or grouping +at rounding boundaries. The wire omits both the draft cost and edit history; +the ORIGINAL SERVER's fractional/batch rounding remains unverified. The client +receives the authoritative result rather than controlling the charge. + +Increasing maximum health/nano preserves current resources; it grants no free +heal. The historical training-time refill policy remains unverified. Training +does not reschedule a weapon cycle already in progress. New-character ability +and skill defaults are still provisional: the current all-10 ability default +already exceeds some level-1 breed caps. Training never lowers those values. +XP rewards, level advancement, IP grants and IP Reset remain open. + +## Verification + +`client/tests/ip_training.c` executes the unchanged lifted implementations +`sub_360751E0` and `sub_360755C0` from +`client/game/kkr.dll/lifted-0034.c`. Its 31 cases cover raw point pricing, +fractional factors, category boundaries, title growth, ability-derived bounds, +truncation and breed limits. It is included in the Linux native test target. + +`server/ip_training_test.go` covers all twelve profession Pistol prices, four +breed prices and caps, level/title/ability boundary pairs, proposed abilities, +worn versus stored modifiers, atomic save failure, insufficient IP, reductions, +negative/overflow values, unknown/duplicate/missing fields, malformed snapshots, +wrong owners, dead/inactive/replaced sessions, concurrent duplicate ACCEPT, +large balances, fractional charges, restart persistence, observer/private +updates, world dispatch and movement anchoring. Run Speed prediction now reads +the trained raw value rather than an assumed 100. + +The maintained server suite passed (`go test ./server/...`, server 272.459 s), +as did the training race suite (118.343 s). A final focused run including +integer/fractional charging and low-health movement passed in 34.724 s. +The original-code contract also passed when compiled to WASM and run with Node. + +## Native Skills window checks + +One isolated browser visitor used the unchanged published WASM client against +the single HTTPS preview. The profile was set to level 3, Soldier, with 1500 IP; +these are controlled starting conditions, not an XP or IP-grant test. + +| Native action | Verified result | +| --- | --- | +| Draft Strength 10 to 11 | Native draft shows 1480 IP; durable profile remains Strength 10/IP 1500 | +| CANCEL and reopen | Strength 10/IP 1500; no SkillIIR ACCEPT sent | +| ACCEPT Strength 10 to 12 | Raw Strength 12/IP 1458, immediately durable | +| ACCEPT raw Pistol 5 to 8 | Displayed Pistol 10/IP 1440; durable raw Pistol 8 | +| Raise Strength to 15, attempt 16 | Native level-cap message; ACCEPT saves 15/IP 1362 | +| Raise Agility and Sense 10 to 15 | Raw Pistol stays 8; displayed Pistol becomes 11/IP 1122 | +| Restart server, reconnect same visitor | Strength/Agility/Sense 15, raw Pistol 8 and IP 1122 survive | + +Four actual ACCEPT captures each contain precisely the expected 75 raw entries +in ascending stat order and no IP field. Seven memory/profile captures check +local draft versus authoritative state; `tmp_verify.py` validates these values. +Native window screenshots verify the effective display and cap feedback. +The final build additionally exercises a controlled Engineer profile with 8 IP: +Nano Resist's next raw point has a 7.5 IP price; the native integer display shows +8 spent. ACCEPT saves raw 6 and IP 0, which also survive another reconnect. +This confirms the exercised single fractional purchase, not the unresolved +original-server rounding policy across arbitrary edit/batch histories. + +REA evidence bundles, exact extracted resource bytes, the checking script, +packet captures and screenshots are retained in `local/ip-training-research/`. +Test browser contexts are closed afterward; the one requested LAN preview stays +available. No production client adapter changes were needed. diff --git a/server/gamedata/ip_training.yaml b/server/gamedata/ip_training.yaml @@ -0,0 +1,946 @@ +# Generated from original 14.9.1 resources by extract_ip_training.py. +title_levels: [1, 15, 50, 100, 150, 190] +breeds: + 1: + base: [6, 6, 6, 6, 6, 6] + maximum: [472, 480, 480, 480, 480, 480] + cost: [2, 2, 2, 2, 2, 2] + 2: + base: [3, 15, 6, 6, 10, 3] + maximum: [464, 544, 480, 464, 512, 448] + cost: [2, 1, 3, 2, 1, 2] + 3: + base: [3, 3, 3, 15, 6, 10] + maximum: [464, 464, 448, 512, 480, 512] + cost: [3, 3, 2, 1, 2, 1] + 4: + base: [15, 6, 10, 3, 3, 3] + maximum: [512, 480, 512, 400, 400, 400] + cost: [1, 2, 1, 3, 3, 3] +professions: + 1: + 100: 20 + 101: 20 + 102: 25 + 103: 20 + 104: 22 + 105: 25 + 106: 25 + 107: 25 + 108: 16 + 109: 16 + 110: 10 + 111: 24 + 112: 10 + 113: 20 + 114: 15 + 115: 15 + 116: 10 + 117: 16 + 118: 24 + 119: 10 + 120: 24 + 121: 20 + 122: 24 + 123: 20 + 124: 20 + 125: 20 + 126: 24 + 127: 20 + 128: 24 + 129: 20 + 130: 25 + 131: 32 + 132: 20 + 133: 10 + 134: 20 + 135: 24 + 136: 24 + 137: 15 + 138: 15 + 139: 16 + 140: 16 + 141: 10 + 142: 20 + 143: 24 + 144: 40 + 145: 25 + 146: 30 + 147: 24 + 148: 15 + 149: 40 + 150: 10 + 151: 18 + 152: 11 + 153: 18 + 154: 15 + 155: 20 + 156: 20 + 157: 24 + 158: 15 + 159: 20 + 160: 20 + 161: 20 + 162: 15 + 163: 24 + 164: 20 + 165: 20 + 166: 10 + 167: 15 + 168: 22 + 2: + 100: 10 + 101: 25 + 102: 25 + 103: 20 + 104: 30 + 105: 20 + 106: 20 + 107: 20 + 108: 10 + 109: 24 + 110: 40 + 111: 10 + 112: 35 + 113: 40 + 114: 30 + 115: 40 + 116: 40 + 117: 25 + 118: 20 + 119: 24 + 120: 10 + 121: 10 + 122: 16 + 123: 16 + 124: 20 + 125: 24 + 126: 32 + 127: 20 + 128: 16 + 129: 20 + 130: 24 + 131: 16 + 132: 16 + 133: 40 + 134: 40 + 135: 25 + 136: 16 + 137: 16 + 138: 14 + 139: 30 + 140: 20 + 141: 10 + 142: 12 + 143: 10 + 144: 12 + 145: 15 + 146: 30 + 147: 20 + 148: 40 + 149: 25 + 150: 32 + 151: 30 + 152: 15 + 153: 10 + 154: 10 + 155: 10 + 156: 10 + 157: 32 + 158: 24 + 159: 24 + 160: 24 + 161: 20 + 162: 16 + 163: 24 + 164: 15 + 165: 20 + 166: 25 + 167: 40 + 168: 16 + 3: + 100: 28 + 101: 40 + 102: 24 + 103: 32 + 104: 40 + 105: 32 + 106: 32 + 107: 32 + 108: 32 + 109: 20 + 110: 20 + 111: 40 + 112: 15 + 113: 40 + 114: 32 + 115: 32 + 116: 30 + 117: 16 + 118: 32 + 119: 32 + 120: 30 + 121: 35 + 122: 24 + 123: 20 + 124: 16 + 125: 10 + 126: 10 + 127: 10 + 128: 24 + 129: 24 + 130: 10 + 131: 10 + 132: 18 + 133: 30 + 134: 40 + 135: 16 + 136: 24 + 137: 20 + 138: 20 + 139: 16 + 140: 16 + 141: 10 + 142: 24 + 143: 32 + 144: 40 + 145: 40 + 146: 40 + 147: 40 + 148: 30 + 149: 16 + 150: 32 + 151: 40 + 152: 24 + 153: 22 + 154: 25 + 155: 40 + 156: 20 + 157: 10 + 158: 10 + 159: 15 + 160: 12 + 161: 13 + 162: 24 + 163: 12 + 164: 32 + 165: 16 + 166: 16 + 167: 30 + 168: 15 + 4: + 100: 28 + 101: 25 + 102: 25 + 103: 20 + 104: 32 + 105: 25 + 106: 25 + 107: 32 + 108: 25 + 109: 22 + 110: 25 + 111: 24 + 112: 16 + 113: 20 + 114: 10 + 115: 18 + 116: 28 + 117: 10 + 118: 25 + 119: 16 + 120: 24 + 121: 20 + 122: 24 + 123: 12 + 124: 12 + 125: 15 + 126: 15 + 127: 24 + 128: 32 + 129: 24 + 130: 25 + 131: 32 + 132: 16 + 133: 25 + 134: 20 + 135: 10 + 136: 10 + 137: 20 + 138: 20 + 139: 10 + 140: 20 + 141: 10 + 142: 18 + 143: 24 + 144: 40 + 145: 24 + 146: 39 + 147: 25 + 148: 15 + 149: 24 + 150: 16 + 151: 25 + 152: 18 + 153: 10 + 154: 10 + 155: 16 + 156: 10 + 157: 15 + 158: 13 + 159: 15 + 160: 20 + 161: 10 + 162: 15 + 163: 20 + 164: 15 + 165: 10 + 166: 10 + 167: 22 + 168: 16 + 5: + 100: 16 + 101: 25 + 102: 16 + 103: 20 + 104: 32 + 105: 25 + 106: 25 + 107: 25 + 108: 12 + 109: 16 + 110: 30 + 111: 20 + 112: 18 + 113: 13 + 114: 25 + 115: 32 + 116: 30 + 117: 24 + 118: 16 + 119: 16 + 120: 16 + 121: 20 + 122: 16 + 123: 20 + 124: 20 + 125: 15 + 126: 20 + 127: 12 + 128: 16 + 129: 16 + 130: 14 + 131: 24 + 132: 12 + 133: 25 + 134: 18 + 135: 20 + 136: 10 + 137: 30 + 138: 16 + 139: 24 + 140: 16 + 141: 10 + 142: 28 + 143: 30 + 144: 16 + 145: 16 + 146: 10 + 147: 25 + 148: 32 + 149: 16 + 150: 32 + 151: 11 + 152: 24 + 153: 16 + 154: 21 + 155: 24 + 156: 16 + 157: 20 + 158: 40 + 159: 16 + 160: 24 + 161: 16 + 162: 10 + 163: 15 + 164: 10 + 165: 15 + 166: 24 + 167: 40 + 168: 16 + 6: + 100: 28 + 101: 14 + 102: 15 + 103: 10 + 104: 15 + 105: 15 + 106: 15 + 107: 15 + 108: 16 + 109: 16 + 110: 30 + 111: 18 + 112: 10 + 113: 17 + 114: 25 + 115: 24 + 116: 16 + 117: 10 + 118: 18 + 119: 20 + 120: 16 + 121: 16 + 122: 16 + 123: 12 + 124: 10 + 125: 12 + 126: 16 + 127: 18 + 128: 15 + 129: 18 + 130: 18 + 131: 18 + 132: 16 + 133: 24 + 134: 15 + 135: 16 + 136: 16 + 137: 10 + 138: 10 + 139: 10 + 140: 10 + 141: 10 + 142: 24 + 143: 32 + 144: 40 + 145: 15 + 146: 15 + 147: 20 + 148: 18 + 149: 20 + 150: 10 + 151: 22 + 152: 12 + 153: 16 + 154: 16 + 155: 18 + 156: 10 + 157: 16 + 158: 16 + 159: 24 + 160: 40 + 161: 16 + 162: 16 + 163: 16 + 164: 17 + 165: 20 + 166: 10 + 167: 24 + 168: 24 + 7: + 100: 20 + 101: 32 + 102: 18 + 103: 32 + 104: 25 + 105: 25 + 106: 25 + 107: 25 + 108: 24 + 109: 24 + 110: 40 + 111: 40 + 112: 20 + 113: 28 + 114: 24 + 115: 15 + 116: 40 + 117: 14 + 118: 32 + 119: 25 + 120: 32 + 121: 40 + 122: 18 + 123: 16 + 124: 16 + 125: 12 + 126: 10 + 127: 16 + 128: 18 + 129: 15 + 130: 15 + 131: 15 + 132: 12 + 133: 30 + 134: 25 + 135: 24 + 136: 14 + 137: 14 + 138: 15 + 139: 14 + 140: 13 + 141: 10 + 142: 20 + 143: 32 + 144: 40 + 145: 25 + 146: 40 + 147: 30 + 148: 35 + 149: 15 + 150: 25 + 151: 25 + 152: 20 + 153: 19 + 154: 19 + 155: 19 + 156: 19 + 157: 12 + 158: 10 + 159: 10 + 160: 14 + 161: 15 + 162: 10 + 163: 13 + 164: 18 + 165: 18 + 166: 14 + 167: 35 + 168: 16 + 8: + 100: 28 + 101: 40 + 102: 32 + 103: 40 + 104: 40 + 105: 40 + 106: 40 + 107: 40 + 108: 32 + 109: 40 + 110: 40 + 111: 40 + 112: 16 + 113: 40 + 114: 32 + 115: 32 + 116: 40 + 117: 24 + 118: 40 + 119: 32 + 120: 20 + 121: 40 + 122: 10 + 123: 20 + 124: 20 + 125: 18 + 126: 24 + 127: 16 + 128: 10 + 129: 10 + 130: 16 + 131: 16 + 132: 14 + 133: 40 + 134: 40 + 135: 24 + 136: 16 + 137: 20 + 138: 20 + 139: 20 + 140: 20 + 141: 10 + 142: 32 + 143: 32 + 144: 30 + 145: 32 + 146: 24 + 147: 40 + 148: 40 + 149: 10 + 150: 40 + 151: 40 + 152: 24 + 153: 24 + 154: 24 + 155: 24 + 156: 24 + 157: 24 + 158: 25 + 159: 24 + 160: 16 + 161: 10 + 162: 10 + 163: 24 + 164: 24 + 165: 20 + 166: 24 + 167: 40 + 168: 16 + 9: + 100: 16 + 101: 10 + 102: 10 + 103: 10 + 104: 18 + 105: 10 + 106: 10 + 107: 14 + 108: 16 + 109: 25 + 110: 25 + 111: 40 + 112: 30 + 113: 40 + 114: 25 + 115: 25 + 116: 35 + 117: 16 + 118: 10 + 119: 30 + 120: 16 + 121: 20 + 122: 25 + 123: 16 + 124: 20 + 125: 20 + 126: 18 + 127: 25 + 128: 25 + 129: 25 + 130: 25 + 131: 25 + 132: 20 + 133: 40 + 134: 40 + 135: 24 + 136: 24 + 137: 15 + 138: 20 + 139: 16 + 140: 16 + 141: 10 + 142: 10 + 143: 12 + 144: 40 + 145: 14 + 146: 20 + 147: 15 + 148: 30 + 149: 24 + 150: 35 + 151: 35 + 152: 10 + 153: 20 + 154: 20 + 155: 15 + 156: 24 + 157: 32 + 158: 15 + 159: 16 + 160: 24 + 161: 16 + 162: 10 + 163: 10 + 164: 20 + 165: 20 + 166: 10 + 167: 30 + 168: 22 + 10: + 100: 20 + 101: 32 + 102: 24 + 103: 24 + 104: 32 + 105: 32 + 106: 24 + 107: 32 + 108: 32 + 109: 32 + 110: 40 + 111: 40 + 112: 16 + 113: 40 + 114: 32 + 115: 24 + 116: 40 + 117: 24 + 118: 32 + 119: 32 + 120: 20 + 121: 40 + 122: 16 + 123: 10 + 124: 10 + 125: 20 + 126: 16 + 127: 10 + 128: 10 + 129: 16 + 130: 16 + 131: 16 + 132: 10 + 133: 40 + 134: 40 + 135: 24 + 136: 24 + 137: 20 + 138: 20 + 139: 24 + 140: 16 + 141: 10 + 142: 28 + 143: 32 + 144: 40 + 145: 24 + 146: 32 + 147: 24 + 148: 30 + 149: 10 + 150: 24 + 151: 32 + 152: 20 + 153: 24 + 154: 24 + 155: 32 + 156: 24 + 157: 16 + 158: 15 + 159: 10 + 160: 16 + 161: 10 + 162: 23 + 163: 20 + 164: 24 + 165: 20 + 166: 16 + 167: 40 + 168: 12 + 11: + 100: 28 + 101: 40 + 102: 40 + 103: 40 + 104: 32 + 105: 25 + 106: 40 + 107: 32 + 108: 24 + 109: 24 + 110: 40 + 111: 40 + 112: 16 + 113: 40 + 114: 32 + 115: 30 + 116: 45 + 117: 24 + 118: 35 + 119: 30 + 120: 32 + 121: 40 + 122: 10 + 123: 20 + 124: 20 + 125: 20 + 126: 16 + 127: 10 + 128: 10 + 129: 10 + 130: 10 + 131: 10 + 132: 10 + 133: 40 + 134: 40 + 135: 24 + 136: 24 + 137: 20 + 138: 20 + 139: 24 + 140: 16 + 141: 10 + 142: 28 + 143: 24 + 144: 25 + 145: 32 + 146: 32 + 147: 32 + 148: 40 + 149: 10 + 150: 40 + 151: 32 + 152: 24 + 153: 24 + 154: 24 + 155: 32 + 156: 24 + 157: 16 + 158: 32 + 159: 24 + 160: 10 + 161: 10 + 162: 24 + 163: 20 + 164: 25 + 165: 25 + 166: 24 + 167: 50 + 168: 10 + 12: + 100: 28 + 101: 40 + 102: 26 + 103: 40 + 104: 40 + 105: 25 + 106: 32 + 107: 40 + 108: 32 + 109: 40 + 110: 40 + 111: 25 + 112: 24 + 113: 40 + 114: 32 + 115: 40 + 116: 40 + 117: 25 + 118: 30 + 119: 40 + 120: 32 + 121: 25 + 122: 16 + 123: 20 + 124: 20 + 125: 20 + 126: 20 + 127: 10 + 128: 10 + 129: 16 + 130: 10 + 131: 10 + 132: 10 + 133: 40 + 134: 25 + 135: 24 + 136: 24 + 137: 20 + 138: 20 + 139: 25 + 140: 20 + 141: 10 + 142: 28 + 143: 32 + 144: 25 + 145: 40 + 146: 40 + 147: 40 + 148: 40 + 149: 10 + 150: 40 + 151: 40 + 152: 24 + 153: 24 + 154: 16 + 155: 16 + 156: 24 + 157: 24 + 158: 25 + 159: 20 + 160: 10 + 161: 10 + 162: 16 + 163: 20 + 164: 25 + 165: 24 + 166: 25 + 167: 40 + 168: 16 +limits: + 1: + titles: [60, 200, 360, 490, 550, 600] + per_level: 5 + 2: + titles: [55, 180, 330, 450, 500, 540] + per_level: 4 + 3: + titles: [50, 160, 300, 410, 450, 480] + per_level: 4 + 4: + titles: [45, 140, 270, 370, 400, 420] + per_level: 3 + 5: + titles: [40, 120, 240, 330, 350, 360] + per_level: 3 +weights: + 100: [20, 50, 0, 0, 0, 30] + 101: [30, 60, 10, 0, 0, 0] + 102: [50, 10, 40, 0, 0, 0] + 103: [30, 40, 30, 0, 0, 0] + 104: [0, 0, 50, 50, 0, 0] + 105: [60, 0, 40, 0, 0, 0] + 106: [20, 50, 30, 0, 0, 0] + 107: [50, 0, 50, 0, 0, 0] + 108: [20, 60, 0, 0, 20, 0] + 109: [0, 40, 0, 20, 40, 0] + 110: [40, 60, 0, 0, 0, 0] + 111: [20, 40, 0, 0, 40, 0] + 112: [0, 60, 0, 0, 40, 0] + 113: [0, 60, 0, 0, 40, 0] + 114: [30, 30, 30, 0, 10, 0] + 115: [40, 60, 0, 0, 0, 0] + 116: [10, 30, 40, 0, 20, 0] + 117: [0, 20, 0, 20, 60, 0] + 118: [0, 10, 0, 10, 60, 20] + 119: [0, 10, 0, 10, 60, 20] + 120: [0, 10, 0, 10, 60, 20] + 121: [10, 50, 0, 0, 40, 0] + 122: [20, 0, 0, 80, 0, 0] + 123: [0, 30, 0, 30, 40, 0] + 124: [0, 30, 0, 50, 20, 0] + 125: [0, 50, 0, 50, 0, 0] + 126: [0, 30, 20, 50, 0, 0] + 127: [0, 0, 0, 80, 0, 20] + 128: [0, 0, 0, 80, 0, 20] + 129: [0, 0, 0, 80, 20, 0] + 130: [0, 0, 20, 80, 0, 0] + 131: [0, 20, 0, 80, 0, 0] + 132: [0, 0, 10, 10, 10, 70] + 133: [0, 0, 0, 20, 40, 40] + 134: [0, 60, 0, 40, 0, 0] + 135: [0, 20, 0, 20, 60, 0] + 136: [0, 0, 0, 30, 70, 0] + 137: [20, 50, 30, 0, 0, 0] + 138: [20, 20, 60, 0, 0, 0] + 139: [0, 20, 0, 20, 60, 0] + 140: [0, 0, 0, 40, 50, 10] + 141: [0, 0, 0, 70, 20, 10] + 142: [60, 0, 40, 0, 0, 0] + 143: [0, 50, 0, 0, 50, 0] + 144: [0, 0, 0, 0, 80, 20] + 145: [50, 20, 0, 0, 30, 0] + 146: [0, 50, 0, 30, 20, 0] + 147: [0, 60, 0, 0, 40, 0] + 148: [30, 50, 20, 0, 0, 0] + 149: [0, 40, 0, 0, 60, 0] + 150: [0, 100, 0, 0, 0, 0] + 151: [0, 0, 0, 0, 100, 0] + 152: [0, 0, 100, 0, 0, 0] + 153: [0, 50, 0, 20, 30, 0] + 154: [0, 50, 0, 20, 30, 0] + 155: [0, 50, 0, 20, 30, 0] + 156: [20, 40, 40, 0, 10, 0] + 157: [0, 0, 0, 50, 0, 50] + 158: [50, 0, 0, 50, 0, 0] + 159: [0, 20, 0, 80, 0, 0] + 160: [0, 0, 0, 100, 0, 0] + 161: [0, 0, 0, 100, 0, 0] + 162: [0, 0, 0, 50, 50, 0] + 163: [0, 0, 50, 50, 0, 0] + 164: [0, 30, 0, 0, 70, 0] + 165: [0, 40, 0, 0, 30, 30] + 166: [0, 20, 0, 20, 60, 0] + 167: [60, 0, 40, 0, 0, 0] + 168: [0, 0, 0, 20, 0, 80] diff --git a/server/gamedata/load.go b/server/gamedata/load.go @@ -32,6 +32,20 @@ var ( Health [6]uint32 `yaml:"health"` Nano [6]uint32 `yaml:"nano"` } + IPTraining struct { + TitleLevels [6]uint32 `yaml:"title_levels"` + Breeds map[uint16]struct { + Base [6]uint32 `yaml:"base"` + Maximum [6]uint32 `yaml:"maximum"` + Cost [6]uint32 `yaml:"cost"` + } `yaml:"breeds"` + Professions map[uint16]map[uint32]uint32 `yaml:"professions"` + Limits map[uint32]struct { + Titles [6]uint32 `yaml:"titles"` + PerLevel uint32 `yaml:"per_level"` + } `yaml:"limits"` + Weights map[uint32][6]uint32 `yaml:"weights"` + } ) func init() { @@ -52,6 +66,7 @@ func init() { loadYAML("terrain_evidence.yaml", &TerrainEvidence) loadYAML("playfields.yaml", &Playfields) loadYAML("profession_resources.yaml", &ProfessionTitles) + loadYAML("ip_training.yaml", &IPTraining) zones, err := fs.Glob(files, "zones/*.yaml") if err != nil { diff --git a/server/ip_training.go b/server/ip_training.go @@ -0,0 +1,161 @@ +package main + +import ( + "ao/server/gamedata" + "encoding/binary" + "fmt" + "time" +) + +func (character *character) trainingMaximum(stat uint32) uint32 { + // kkr.dll FUN_100755c0: abilities have breed and level limits. + if stat >= 16 && stat <= 21 { + breed := gamedata.IPTraining.Breeds[character.appearance>>4&7] + return min(breed.Maximum[stat-16], breed.Base[stat-16]+3*uint32(character.level)) + } + // Skill cost selects the native cap category, independently of displayed color. + cost := gamedata.IPTraining.Professions[max(1, character.profession)][stat] + category := uint32(float64(cost)*float64(float32(0.1)) + 0.5) + limits := gamedata.IPTraining.Limits[category] + title := character.titleLevel() + maximum := limits.Titles[title-1] + if title == 1 { + maximum = min(maximum, 5+uint32(character.level)*limits.PerLevel) + } else { + levels := uint32(character.level) + 1 - gamedata.IPTraining.TitleLevels[title-1] + maximum = min(maximum, limits.Titles[title-2]+levels*limits.PerLevel) + maximum = min(maximum, limits.Titles[title-2]+levels*6) + } + + // FUN_10073fa0(stat, 0) includes equipment ability modifiers in this bound. + var weighted int64 + for index, weight := range gamedata.IPTraining.Weights[stat] { + weighted += int64(int32(character.effectiveAbility(16+uint32(index)))) * int64(weight) + } + abilityMaximum := int64(float64(weighted) * float64(float32(0.01)) * 0.25 * 8) + if abilityMaximum > 0 { + maximum = min(maximum, uint32(abilityMaximum)) + } + return maximum +} + +func (server *server) handleSkillTraining(client *worldClient, body []byte) error { + // SkillIIR contains the complete 75-stat raw snapshot, not deltas or an IP balance. + if len(body) != 620 || binary.BigEndian.Uint32(body[16:20]) != 75 { + return fmt.Errorf("invalid skill training snapshot length") + } + server.updateMutex.Lock() + defer server.updateMutex.Unlock() + server.mutex.Lock() + player := server.characters[client.id] + if server.clients[client.id] != client || !client.ready || client.loading { + server.mutex.Unlock() + return nil + } + updated := *player + updated.stats = player.baseStats() + fields := updated.stats.trainingStats() + requested := make(map[uint32]uint32) + accepted := player.health != 0 && binary.BigEndian.Uint32(body[4:8]) == 50000 && + binary.BigEndian.Uint32(body[8:12]) == client.id && + gamedata.IPTraining.Breeds[player.appearance>>4&7].Cost[0] != 0 + for offset := 20; offset < len(body); offset += 8 { + stat, value := binary.BigEndian.Uint32(body[offset:]), binary.BigEndian.Uint32(body[offset+4:]) + if _, exists := requested[stat]; exists { + accepted = false + } + requested[stat] = value + } + + var changes []struct{ stat, from, to uint32 } + for _, field := range fields { + value, exists := requested[field.stat] + if !exists || value < uint32(*field.value) || value > 65535 { + accepted = false + continue + } + if value > uint32(*field.value) { + changes = append(changes, struct{ stat, from, to uint32 }{field.stat, uint32(*field.value), value}) + } + *field.value = uint16(value) + } + for _, change := range changes { + if change.to > updated.trainingMaximum(change.stat) { + accepted = false + } + } + + // Price raw points in tenths of an IP, so packet order and large balances + // cannot change the price. Round the batch up to the stored integer balance. + var costTenths uint64 + if accepted { + for _, change := range changes { + factor := gamedata.IPTraining.Professions[max(1, player.profession)][change.stat] + if change.stat <= 21 { + factor = 10 * gamedata.IPTraining.Breeds[player.appearance>>4&7].Cost[change.stat-16] + } + for raw := change.from; raw < change.to; raw++ { + costTenths += uint64(factor) * uint64(max(1, raw)) + } + } + if (costTenths+9)/10 > uint64(updated.stats.ImprovementPoints) { + accepted = false + } + } + updates := make(map[*worldClient][][]byte) + if accepted { + // Persist IP and all raw stats together before exposing any live change. + updated.stats.ImprovementPoints -= uint32((costTenths + 9) / 10) + now := time.Now() + position := predictedPosition_locked(player, client, now) + if err := server.saveUser_locked(&updated); err != nil { + server.mutex.Unlock() + return err + } + baseHealth := player.maximumHealth() - uint32(player.inventory.equipmentBonus(1)) + baseNano := player.maximumNano() - uint32(player.inventory.equipmentBonus(221)) + attackRating := player.attackRating() + movementChanged := player.maximumHealth() != updated.maximumHealth() || + player.baseStats().Speed.RunSpeed != updated.stats.Speed.RunSpeed + *player = updated + if movementChanged { + player.position, client.movementStarted = position, now + } + for _, stat := range [][3]uint32{ + {1, baseHealth, player.maximumHealth() - uint32(player.inventory.equipmentBonus(1))}, + {221, baseNano, player.maximumNano() - uint32(player.inventory.equipmentBonus(221))}, + {22, attackRating, player.attackRating()}, + } { + if stat[1] != stat[2] { + body := binary.BigEndian.AppendUint32(nil, stat[0]) + body = binary.BigEndian.AppendUint32(body, stat[2]) + packet := n3Packet(0x2b333d6e, 50000, player.id, body) + if stat[0] == 221 { + updates[client] = append(updates[client], packet) + } else { + server.queueCombat_locked(updates, player, player, packet) + } + } + } + } + + // Both acceptance and rejection replace local tentative values with server state. + stats := player.baseStats() + payload := binary.BigEndian.AppendUint32(nil, 75) + for _, field := range stats.trainingStats() { + payload = binary.BigEndian.AppendUint32(payload, field.stat) + payload = binary.BigEndian.AppendUint32(payload, uint32(*field.value)) + } + packet := n3Packet(0x3e205660, 50000, player.id, payload) + if accepted && len(changes) != 0 { + server.queueCombat_locked(updates, player, player, packet) + } else { + updates[client] = append(updates[client], packet) + } + ip := binary.BigEndian.AppendUint32(nil, 53) + ip = binary.BigEndian.AppendUint32(ip, stats.ImprovementPoints) + updates[client] = append(updates[client], n3Packet(0x2b333d6e, 50000, player.id, ip)) + server.mutex.Unlock() + server.sendCombatUpdates(updates) + return nil +} diff --git a/server/ip_training_test.go b/server/ip_training_test.go @@ -0,0 +1,436 @@ +package main + +import ( + "bytes" + "encoding/binary" + "fmt" + "slices" + "sync" + "testing" + "time" +) + +func skillTrainingRequest(stats characterStats) []byte { + body := binary.BigEndian.AppendUint32(nil, 75) + for _, value := range stats.nativeStats()[1:] { + body = binary.BigEndian.AppendUint32(body, value[0]) + body = binary.BigEndian.AppendUint32(body, value[1]) + } + return n3Packet(0x3e205660, 50000, 1, body) +} + +func newTrainingWorld(t *testing.T) (*server, *combatTestConn) { + t.Helper() + world, conn := newRangedWorld(t) + player := world.characters[1] + player.level = 3 + player.health, player.nano = player.maximumHealth(), player.maximumNano() + if err := world.saveUser_locked(player); err != nil { + t.Fatal(err) + } + return world, conn +} + +func TestSkillTrainingPricesOriginalTwelveProfessionCosts(t *testing.T) { + for _, scenario := range []struct { + profession uint16 + remaining uint32 + }{ + {1, 1482}, {2, 1437}, {3, 1473}, {4, 1471}, {5, 1467}, {6, 1482}, + {7, 1464}, {8, 1471}, {9, 1446}, {10, 1471}, {11, 1471}, {12, 1456}, + } { + t.Run(fmt.Sprint(scenario.profession), func(t *testing.T) { + world, conn := newTrainingWorld(t) + player := world.characters[1] + player.profession = scenario.profession + stats := player.baseStats() + stats.Ranged.Pistol = 8 + stats.ImprovementPoints = 999999 // Never supplied by the native request. + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + restored := newTestInventoryServer(t).characters[1] + + if err != nil || player.stats.Ranged.Pistol != 8 || player.stats.ImprovementPoints != scenario.remaining || + restored.stats != player.stats || player.inventory.Items[0].Ammo != 3 || player.inventory.Items[1].Count != 100 { + t.Fatalf("training changed price, persistence or inventory: %+v, %v", player.stats, err) + } + if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[2][29:33]) != 53 || + binary.BigEndian.Uint32(conn.packets[2][33:37]) != scenario.remaining { + t.Fatalf("missing authoritative IP correction: %x", conn.packets) + } + }) + } +} + +func TestSkillTrainingAppliesAbilityDependenciesTogetherWithoutFreeHealing(t *testing.T) { + world, conn := newTrainingWorld(t) + player := world.characters[1] + stats := player.baseStats() + stats.Abilities.Agility, stats.Abilities.Sense = 15, 15 + stats.Abilities.Stamina, stats.Abilities.Psychic = 12, 12 + stats.Body.BodyDevelopment, stats.Body.NanoPool = 7, 7 + stats.Ranged.Pistol = 8 + health, nano := player.health, player.nano + observer := &combatTestConn{} + world.characters[2] = &character{id: 2, playfield: 950, health: 100} + world.clients[2] = &worldClient{id: 2, conn: observer, target: true, ready: true} + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + restored := newTestInventoryServer(t).characters[1] + + if err != nil || player.stats.Abilities != stats.Abilities || player.stats.Body != stats.Body || + player.stats.Ranged.Pistol != 8 || player.effectiveSkill(112, 8, [6]uint32{0, 60, 0, 0, 40, 0}) != 11 || + player.health != health || player.nano != nano || player.maximumHealth() != 55 || player.maximumNano() != 52 || + restored.stats != player.stats || restored.health != health || restored.nano != nano { + t.Fatalf("ability dependency, resource or durable batch mismatch: %+v, %v", player, err) + } + var keys []uint32 + for _, packet := range observer.packets { + keys = append(keys, binary.BigEndian.Uint32(packet[16:20])) + } + if !slices.Equal(keys, []uint32{0x2b333d6e, 0x2b333d6e, 0x3e205660}) || + binary.BigEndian.Uint32(observer.packets[0][29:33]) != 1 || + binary.BigEndian.Uint32(observer.packets[1][29:33]) != 22 || len(conn.packets) != 5 { + t.Fatalf("observer received wrong shared/private stat updates: %x", keys) + } +} + +func TestSkillTrainingEnforcesSkillLevelTitleAndAbilityBoundaries(t *testing.T) { + for _, scenario := range []struct { + name string + level uint16 + ability uint16 + from, to uint16 + remaining uint32 + }{ + {"level1", 1, 600, 9, 10, 1491}, + {"title1", 14, 600, 59, 60, 1441}, + {"title2start", 15, 600, 64, 65, 1436}, + {"title2end", 49, 600, 199, 200, 1301}, + {"title6", 200, 600, 599, 600, 901}, + {"ability", 10, 10, 18, 19, 1482}, + } { + t.Run(scenario.name, func(t *testing.T) { + world, conn := newTrainingWorld(t) + player := world.characters[1] + player.level = scenario.level + player.stats.Abilities = characterAbilitiesStats{scenario.ability, scenario.ability, scenario.ability, scenario.ability, scenario.ability, scenario.ability} + player.stats.Ranged.Pistol = scenario.from + stats := player.baseStats() + stats.Ranged.Pistol = scenario.to + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + stats.Ranged.Pistol++ + if err == nil { + err = world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + } + restored := newTestInventoryServer(t).characters[1] + + if err != nil || player.stats.Ranged.Pistol != scenario.to || player.stats.ImprovementPoints != scenario.remaining || restored.stats != player.stats { + t.Fatalf("cap boundary changed skill or charged rejected point: %+v, %v", player.stats, err) + } + if binary.BigEndian.Uint32(conn.packets[len(conn.packets)-1][33:37]) != scenario.remaining { + t.Fatal("rejected point did not restore the IP display") + } + }) + } +} + +func TestSkillTrainingEnforcesBreedAbilityCostsAndCaps(t *testing.T) { + for _, scenario := range []struct { + breed uint16 + level uint16 + from, to uint16 + remaining uint32 + }{ + {1, 3, 14, 15, 1472}, {2, 3, 11, 12, 1478}, + {3, 3, 11, 12, 1467}, {4, 3, 23, 24, 1477}, + {1, 200, 471, 472, 558}, {2, 200, 463, 464, 574}, + {3, 200, 463, 464, 111}, {4, 200, 511, 512, 989}, + } { + t.Run(fmt.Sprintf("breed%d/level%d", scenario.breed, scenario.level), func(t *testing.T) { + world, _ := newTrainingWorld(t) + player := world.characters[1] + player.appearance, player.level = scenario.breed<<4, scenario.level + player.stats.Abilities.Strength = scenario.from + stats := player.baseStats() + stats.Abilities.Strength = scenario.to + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + stats.Abilities.Strength++ + if err == nil { + err = world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + } + + if err != nil || player.stats.Abilities.Strength != scenario.to || player.stats.ImprovementPoints != scenario.remaining { + t.Fatalf("breed ability price/cap mismatch: %+v, %v", player.stats, err) + } + }) + } +} + +func TestSkillTrainingUsesProposedAbilitiesAndWornAbilityBonusesForCaps(t *testing.T) { + for _, scenario := range []string{"proposed abilities", "worn backpack", "stored backpack"} { + t.Run(scenario, func(t *testing.T) { + world, _ := newTrainingWorld(t) + player := world.characters[1] + player.level = 10 + player.stats.Ranged.Pistol = 19 + stats := player.baseStats() + stats.Ranged.Pistol = 21 + if scenario == "proposed abilities" { + stats.Abilities.Agility, stats.Abilities.Sense = 15, 15 + } else { + slot := uint32(19) + if scenario == "stored backpack" { + slot = 65 + } + player.inventory.Items = append(player.inventory.Items, ownedItem{ID: 3, Type: 51017, Template: 152039, Quality: 1, Count: 1, Slot: slot}) + } + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + + expected := uint16(21) + if scenario == "stored backpack" { + expected = 19 + } + if err != nil || player.stats.Ranged.Pistol != expected { + t.Fatalf("dependency cap used wrong ability state: %+v, %v", player.stats, err) + } + }) + } +} + +func TestSkillTrainingRejectsWholeBatchAndRestoresTentativeStats(t *testing.T) { + for _, scenario := range []string{"insufficient IP", "decrease", "over cap", "ability cap", "duplicate", "unknown", "negative", "overflow", "wrong owner", "wrong type", "dead", "unsupported breed"} { + t.Run(scenario, func(t *testing.T) { + world, conn := newTrainingWorld(t) + player := world.characters[1] + stats := player.baseStats() + stats.Ranged.Pistol = 6 + switch scenario { + case "insufficient IP": + player.stats.ImprovementPoints = 4 + case "decrease": + stats.Body.MartialArts = 4 + case "over cap": + stats.Body.MartialArts = 1000 + case "ability cap": + stats.Abilities.Strength = 16 + case "dead": + player.health = 0 + case "unsupported breed": + player.appearance = 6 << 4 + } + request := skillTrainingRequest(stats)[20:] + switch scenario { + case "duplicate": + binary.BigEndian.PutUint32(request[28:32], 16) + case "unknown": + binary.BigEndian.PutUint32(request[20:24], 53) + case "negative": + binary.BigEndian.PutUint32(request[24:28], 0xffffffff) + case "overflow": + binary.BigEndian.PutUint32(request[24:28], 65536) + case "wrong owner": + binary.BigEndian.PutUint32(request[8:12], 2) + case "wrong type": + binary.BigEndian.PutUint32(request[4:8], 51018) + } + if err := world.saveUser_locked(player); err != nil { + t.Fatal(err) + } + before := player.stats + + err := world.handleSkillTraining(world.clients[1], request) + restored := newTestInventoryServer(t).characters[1] + + if err != nil || player.stats != before || restored.stats != before || len(conn.packets) != 2 || + binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3e205660 || + binary.BigEndian.Uint32(conn.packets[1][33:37]) != before.ImprovementPoints { + t.Fatalf("rejected batch changed state or lost correction: %+v, %v", player.stats, err) + } + }) + } +} + +func TestSkillTrainingRejectsMalformedSnapshotsAndInactiveSessions(t *testing.T) { + for _, scenario := range []string{"short", "long", "count", "unready", "loading", "replaced"} { + t.Run(scenario, func(t *testing.T) { + world, conn := newTrainingWorld(t) + player, client := world.characters[1], world.clients[1] + before := player.stats + stats := before + stats.Ranged.Pistol = 6 + request := skillTrainingRequest(stats)[20:] + malformed := true + switch scenario { + case "short": + request = request[:len(request)-1] + case "long": + request = append(request, 0) + case "count": + binary.BigEndian.PutUint32(request[16:20], 74) + case "unready": + client.ready, malformed = false, false + case "loading": + client.loading, malformed = true, false + case "replaced": + world.clients[1], malformed = &worldClient{id: 1}, false + } + + err := world.handleSkillTraining(client, request) + + if (err != nil) != malformed || player.stats != before || len(conn.packets) != 0 { + t.Fatalf("invalid snapshot/session changed state: %v", err) + } + }) + } +} + +func TestSkillTrainingRepeatedConcurrentAcceptChargesOnce(t *testing.T) { + world, _ := newTrainingWorld(t) + player := world.characters[1] + player.stats.ImprovementPoints = 5 + stats := player.baseStats() + stats.Ranged.Pistol = 6 + request := skillTrainingRequest(stats)[20:] + var calls sync.WaitGroup + for range 8 { + calls.Go(func() { + if err := world.handleSkillTraining(world.clients[1], request); err != nil { + t.Error(err) + } + }) + } + + calls.Wait() + restored := newTestInventoryServer(t).characters[1] + + if player.stats.Ranged.Pistol != 6 || player.stats.ImprovementPoints != 0 || restored.stats != player.stats { + t.Fatalf("repeated accept charged twice or lost exact balance: %+v", player.stats) + } +} + +func TestSkillTrainingSaveFailurePublishesNothing(t *testing.T) { + world, conn := newTrainingWorld(t) + player := world.characters[1] + before := player.stats + stats := before + stats.Abilities.Agility, stats.Ranged.Pistol = 12, 8 + if _, err := world.db.Exec("CREATE TRIGGER fail_training BEFORE UPDATE OF improvement_points ON characters BEGIN SELECT RAISE(FAIL, 'test training failure'); END"); err != nil { + t.Fatal(err) + } + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + restored := newTestInventoryServer(t).characters[1] + + if err == nil || player.stats != before || restored.stats != before || len(conn.packets) != 0 { + t.Fatal("failed training transaction changed stats, IP or client state") + } +} + +func TestSkillTrainingPricesLargeIPBalancesWithoutFloatRounding(t *testing.T) { + world, _ := newTrainingWorld(t) + player := world.characters[1] + player.stats.ImprovementPoints = 16777217 + stats := player.stats + stats.Ranged.Pistol = 6 + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + + if err != nil || player.stats.ImprovementPoints != 16777212 || player.stats.Ranged.Pistol != 6 { + t.Fatalf("large balance changed the point price: %+v, %v", player.stats, err) + } +} + +func TestSkillTrainingRoundsFractionalCostOncePerAcceptedBatch(t *testing.T) { + for _, scenario := range []struct { + ip uint32 + both, accepted bool + }{ + {7, false, false}, {8, false, true}, {15, true, true}, + } { + t.Run(fmt.Sprintf("ip%d/both%t", scenario.ip, scenario.both), func(t *testing.T) { + world, _ := newTrainingWorld(t) + player := world.characters[1] + player.profession, player.stats.ImprovementPoints = 3, scenario.ip + stats := player.stats + stats.Ranged.Pistol = 6 + if scenario.both { + stats.Speed.NanoResist = 6 + } + + err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:]) + + if err != nil { + t.Fatal(err) + } + if scenario.accepted { + if player.stats.Ranged.Pistol != 6 || player.stats.ImprovementPoints != 0 { + t.Fatalf("wrong fractional batch charge: %+v", player.stats) + } + } else if player.stats.Ranged.Pistol != 5 || player.stats.ImprovementPoints != 7 { + t.Fatalf("fractional price admitted insufficient balance: %+v", player.stats) + } + }) + } +} + +func TestSkillTrainingDispatchesNativeSnapshotThroughWorldConnection(t *testing.T) { + world, _ := newTrainingWorld(t) + world.clients[1].conn = &combatInterestConn{} + stats := world.characters[1].stats + stats.Ranged.Pistol = 6 + cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}) + input := append(cookie, n3Packet(0x570c2039, 50000, 1, nil)...) + input = append(input, skillTrainingRequest(stats)...) + conn := &combatInterestConn{input: bytes.NewReader(input)} + + err := world.serveWorldClient(&worldClient{conn: conn, loading: true}) + + if err != nil || world.characters[1].stats.Ranged.Pistol != 6 || world.characters[1].stats.ImprovementPoints != 1495 { + t.Fatalf("native training dispatcher failed: %+v, %v", world.characters[1].stats, err) + } +} + +func TestSkillTrainingAnchorsHeldMovementBeforeRunSpeedChange(t *testing.T) { + world, _ := newTrainingWorld(t) + player, client := world.characters[1], world.clients[1] + player.level, player.running = 100, true + player.stats.Abilities = characterAbilitiesStats{100, 100, 100, 100, 100, 100} + player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1} + player.health = player.maximumHealth() + client.forward, client.movementStarted = 1, time.Now().Add(-time.Second) + stats := player.stats + stats.Speed.RunSpeed = 101 + + err := world.handleSkillTraining(client, skillTrainingRequest(stats)[20:]) + position := predictedPosition_locked(player, client, client.movementStarted.Add(time.Second)) + + if err != nil || player.stats.Speed.RunSpeed != 101 || player.position[2] < 4.4 || player.position[2] > 4.8 || + position[2]-player.position[2] < 4.504 || position[2]-player.position[2] > 4.506 { + t.Fatalf("training retroactively applied new run speed: %v -> %v, %v", player.position, position, err) + } +} + +func TestSkillTrainingAnchorsLowHealthMovementBeforeMaximumHealthChanges(t *testing.T) { + world, _ := newTrainingWorld(t) + player, client := world.characters[1], world.clients[1] + player.level, player.running = 100, true + player.stats.Abilities = characterAbilitiesStats{100, 100, 100, 100, 100, 100} + player.position, player.rotation = [3]float32{}, [4]float32{0, 0, 0, 1} + player.health = 100 + client.forward, client.movementStarted = 1, time.Now().Add(-time.Second) + stats := player.stats + stats.Body.BodyDevelopment = 6 + + err := world.handleSkillTraining(client, skillTrainingRequest(stats)[20:]) + position := predictedPosition_locked(player, client, client.movementStarted.Add(time.Second)) + + if err != nil || player.stats.Body.BodyDevelopment != 6 || player.maximumHealth() != 1000 || + player.position[2] < 3.506 || player.position[2] > 3.7 || + position[2]-player.position[2] < 3.499 || position[2]-player.position[2] > 3.501 { + t.Fatalf("training changed the previous low-health movement segment: %v -> %v, %v", player.position, position, err) + } +} diff --git a/server/main.go b/server/main.go @@ -439,6 +439,10 @@ func (server *server) serveWorldClient(client *worldClient) error { decodeInventory1491(body) } switch binary.BigEndian.Uint32(body[0:4]) { + case 0x3e205660: + if err := server.handleSkillTraining(client, body); err != nil { + return err + } case 0x2252445f: if err := server.handleTargetSelection(client, body); err != nil { return err diff --git a/server/stats.go b/server/stats.go @@ -158,83 +158,95 @@ func (character *character) effectiveSkill(stat uint32, base uint16, weights [6] return uint32(int32(base) + int32(float64(weighted)*float64(float32(0.01))*0.25) + character.inventory.equipmentBonus(stat)) } +type trainedStat struct { + stat uint32 + value *uint16 +} + +func (stats *characterStats) trainingStats() []trainedStat { + return []trainedStat{ + {16, &stats.Abilities.Strength}, + {17, &stats.Abilities.Agility}, + {18, &stats.Abilities.Stamina}, + {19, &stats.Abilities.Intelligence}, + {20, &stats.Abilities.Sense}, + {21, &stats.Abilities.Psychic}, + {100, &stats.Body.MartialArts}, + {142, &stats.Body.Brawling}, + {143, &stats.Body.Riposte}, + {144, &stats.Body.Dimach}, + {137, &stats.Body.Adventuring}, + {138, &stats.Body.Swimming}, + {152, &stats.Body.BodyDevelopment}, + {132, &stats.Body.NanoPool}, + {102, &stats.Melee.OneHandBlunt}, + {103, &stats.Melee.OneHandEdged}, + {106, &stats.Melee.Piercing}, + {107, &stats.Melee.TwoHandBlunt}, + {105, &stats.Melee.TwoHandEdged}, + {104, &stats.Melee.MeleeEnergy}, + {145, &stats.Melee.Parry}, + {146, &stats.Melee.SneakAttack}, + {101, &stats.Melee.MultiMelee}, + {147, &stats.Melee.FastAttack}, + {108, &stats.MiscWeapons.SharpObjects}, + {109, &stats.MiscWeapons.Grenade}, + {110, &stats.MiscWeapons.HeavyWeapons}, + {111, &stats.Ranged.Bow}, + {112, &stats.Ranged.Pistol}, + {116, &stats.Ranged.AssaultRifle}, + {114, &stats.Ranged.SubmachineGun}, + {115, &stats.Ranged.Shotgun}, + {113, &stats.Ranged.Rifle}, + {133, &stats.Ranged.RangedEnergy}, + {150, &stats.Ranged.FlingShot}, + {151, &stats.Ranged.AimedShot}, + {148, &stats.Ranged.Burst}, + {167, &stats.Ranged.FullAuto}, + {121, &stats.Ranged.BowSpecialAttack}, + {134, &stats.Ranged.MultiRanged}, + {118, &stats.Speed.MeleeInitiative}, + {119, &stats.Speed.RangedInitiative}, + {120, &stats.Speed.PhysicalInitiative}, + {149, &stats.Speed.NanoInitiative}, + {154, &stats.Speed.DodgeRanged}, + {155, &stats.Speed.EvadeClose}, + {153, &stats.Speed.DuckExplosions}, + {168, &stats.Speed.NanoResist}, + {156, &stats.Speed.RunSpeed}, + {125, &stats.TradeRepair.MechanicalEngineering}, + {126, &stats.TradeRepair.ElectricalEngineering}, + {157, &stats.TradeRepair.QuantumFieldTechnology}, + {158, &stats.TradeRepair.WeaponSmithing}, + {159, &stats.TradeRepair.PharmaceuticalTechnology}, + {160, &stats.TradeRepair.NanoProgramming}, + {161, &stats.TradeRepair.ComputerLiteracy}, + {162, &stats.TradeRepair.Psychology}, + {163, &stats.TradeRepair.Chemistry}, + {141, &stats.TradeRepair.Tutoring}, + {127, &stats.NanoAiding.MatterMetamorphosis}, + {128, &stats.NanoAiding.BiologicalMetamorphosis}, + {129, &stats.NanoAiding.PsychologicalModification}, + {130, &stats.NanoAiding.MatterCreation}, + {131, &stats.NanoAiding.TimeAndSpace}, + {122, &stats.NanoAiding.SensoryImprovement}, + {123, &stats.NanoAiding.FirstAid}, + {124, &stats.NanoAiding.Treatment}, + {164, &stats.Spying.Concealment}, + {165, &stats.Spying.BreakingAndEntering}, + {135, &stats.Spying.TrapDisarm}, + {136, &stats.Spying.Perception}, + {139, &stats.Navigation.VehicleAir}, + {166, &stats.Navigation.VehicleGround}, + {117, &stats.Navigation.VehicleWater}, + {140, &stats.Navigation.MapNavigation}, + } +} + func (stats characterStats) nativeStats() [][2]uint32 { - return [][2]uint32{ - {53, stats.ImprovementPoints}, - {16, uint32(stats.Abilities.Strength)}, - {17, uint32(stats.Abilities.Agility)}, - {18, uint32(stats.Abilities.Stamina)}, - {19, uint32(stats.Abilities.Intelligence)}, - {20, uint32(stats.Abilities.Sense)}, - {21, uint32(stats.Abilities.Psychic)}, - {100, uint32(stats.Body.MartialArts)}, - {142, uint32(stats.Body.Brawling)}, - {143, uint32(stats.Body.Riposte)}, - {144, uint32(stats.Body.Dimach)}, - {137, uint32(stats.Body.Adventuring)}, - {138, uint32(stats.Body.Swimming)}, - {152, uint32(stats.Body.BodyDevelopment)}, - {132, uint32(stats.Body.NanoPool)}, - {102, uint32(stats.Melee.OneHandBlunt)}, - {103, uint32(stats.Melee.OneHandEdged)}, - {106, uint32(stats.Melee.Piercing)}, - {107, uint32(stats.Melee.TwoHandBlunt)}, - {105, uint32(stats.Melee.TwoHandEdged)}, - {104, uint32(stats.Melee.MeleeEnergy)}, - {145, uint32(stats.Melee.Parry)}, - {146, uint32(stats.Melee.SneakAttack)}, - {101, uint32(stats.Melee.MultiMelee)}, - {147, uint32(stats.Melee.FastAttack)}, - {108, uint32(stats.MiscWeapons.SharpObjects)}, - {109, uint32(stats.MiscWeapons.Grenade)}, - {110, uint32(stats.MiscWeapons.HeavyWeapons)}, - {111, uint32(stats.Ranged.Bow)}, - {112, uint32(stats.Ranged.Pistol)}, - {116, uint32(stats.Ranged.AssaultRifle)}, - {114, uint32(stats.Ranged.SubmachineGun)}, - {115, uint32(stats.Ranged.Shotgun)}, - {113, uint32(stats.Ranged.Rifle)}, - {133, uint32(stats.Ranged.RangedEnergy)}, - {150, uint32(stats.Ranged.FlingShot)}, - {151, uint32(stats.Ranged.AimedShot)}, - {148, uint32(stats.Ranged.Burst)}, - {167, uint32(stats.Ranged.FullAuto)}, - {121, uint32(stats.Ranged.BowSpecialAttack)}, - {134, uint32(stats.Ranged.MultiRanged)}, - {118, uint32(stats.Speed.MeleeInitiative)}, - {119, uint32(stats.Speed.RangedInitiative)}, - {120, uint32(stats.Speed.PhysicalInitiative)}, - {149, uint32(stats.Speed.NanoInitiative)}, - {154, uint32(stats.Speed.DodgeRanged)}, - {155, uint32(stats.Speed.EvadeClose)}, - {153, uint32(stats.Speed.DuckExplosions)}, - {168, uint32(stats.Speed.NanoResist)}, - {156, uint32(stats.Speed.RunSpeed)}, - {125, uint32(stats.TradeRepair.MechanicalEngineering)}, - {126, uint32(stats.TradeRepair.ElectricalEngineering)}, - {157, uint32(stats.TradeRepair.QuantumFieldTechnology)}, - {158, uint32(stats.TradeRepair.WeaponSmithing)}, - {159, uint32(stats.TradeRepair.PharmaceuticalTechnology)}, - {160, uint32(stats.TradeRepair.NanoProgramming)}, - {161, uint32(stats.TradeRepair.ComputerLiteracy)}, - {162, uint32(stats.TradeRepair.Psychology)}, - {163, uint32(stats.TradeRepair.Chemistry)}, - {141, uint32(stats.TradeRepair.Tutoring)}, - {127, uint32(stats.NanoAiding.MatterMetamorphosis)}, - {128, uint32(stats.NanoAiding.BiologicalMetamorphosis)}, - {129, uint32(stats.NanoAiding.PsychologicalModification)}, - {130, uint32(stats.NanoAiding.MatterCreation)}, - {131, uint32(stats.NanoAiding.TimeAndSpace)}, - {122, uint32(stats.NanoAiding.SensoryImprovement)}, - {123, uint32(stats.NanoAiding.FirstAid)}, - {124, uint32(stats.NanoAiding.Treatment)}, - {164, uint32(stats.Spying.Concealment)}, - {165, uint32(stats.Spying.BreakingAndEntering)}, - {135, uint32(stats.Spying.TrapDisarm)}, - {136, uint32(stats.Spying.Perception)}, - {139, uint32(stats.Navigation.VehicleAir)}, - {166, uint32(stats.Navigation.VehicleGround)}, - {117, uint32(stats.Navigation.VehicleWater)}, - {140, uint32(stats.Navigation.MapNavigation)}, + values := [][2]uint32{{53, stats.ImprovementPoints}} + for _, field := range stats.trainingStats() { + values = append(values, [2]uint32{field.stat, uint32(*field.value)}) } + return values } diff --git a/server/zoning.go b/server/zoning.go @@ -236,7 +236,7 @@ func predictedPosition_locked(character *character, client *worldClient, now tim speed := float32(1.5) if character.running || character.flying() { // Native movement reads RunSpeed without ability trickle-down. - runSpeed := float32(100 + character.inventory.equipmentBonus(156)) + runSpeed := float32(character.baseStats().Speed.RunSpeed) + float32(character.inventory.equipmentBonus(156)) if float32(character.health) < float32(character.maximumHealth())*float32(0.15) { runSpeed = (runSpeed+500)*(float32(character.health)/(float32(character.maximumHealth())*float32(0.15))) - 500 } diff --git a/tools/data/extract_ip_training.py b/tools/data/extract_ip_training.py @@ -0,0 +1,85 @@ +import hashlib +import json +import pathlib +import struct + +import ao_database + + +def main(): + client = pathlib.Path('anarchy-vanilla-14.9.1') + assert (client / 'version.id').read_text().strip() == '14.9.1' + database = ao_database.Database(client) + records = [] + + # kkr.dll FUN_100e9670 reads resource 1000206 in this stat order. + order = [152, 137, 154, 153, 155, 156, 138, 142, 144, 100, 143, 93, 95, + 139, 166, 117, 140, 92, 97, 168, 123, 124, 118, 119, 149, 120, + 151, 167, 111, 148, 133, 134, 150, 114, 112, 116, 115, 113, + 121, 107, 106, 102, 104, 147, 105, 145, 146, 103, 101, 91, + 128, 132, 130, 127, 129, 122, 131, 90, 126, 125, 96, 94, + 165, 164, 135, 136, 163, 161, 159, 160, 162, 157, 158, 141, + 110, 109, 108] + assert len(order) == len(set(order)) == 77 + + def read(kind, identity, count): + raw = database.read(kind, identity) + words = struct.unpack('<' + 'i' * count, raw) + records.append(dict(kind=kind, identity=identity, payload_hex=raw.hex(), + payload_sha256=hashlib.sha256(raw).hexdigest())) + return words + + # All supported professions share these six title thresholds. + starts = None + for profession in range(1, 13): + levels = list(read(1000200, profession, 24)[::4]) + assert starts is None or starts == levels + starts = levels + lines = ['# Generated from original 14.9.1 resources by extract_ip_training.py.', + f'title_levels: {json.dumps(starts)}', 'breeds:'] + + # FUN_100ef270: normal ability order; FUN_100dd9a0: STR, INT, AGI, SEN, STA, PSY. + for breed in range(1, 5): + bounds = read(1000027, breed, 13) + assert bounds[0] == breed + costs = read(1000210, breed, 6) + costs = [costs[index] for index in [0, 2, 4, 1, 3, 5]] + lines.extend([f' {breed}:', f' base: {json.dumps(bounds[1:7])}', + f' maximum: {json.dumps(bounds[7:])}', f' cost: {json.dumps(costs)}']) + + # Retain only the 69 trainable skills, excluding the eight armor classes. + lines.append('professions:') + for profession in range(1, 13): + costs = dict(zip(order, read(1000206, profession, 77))) + lines.append(f' {profession}:') + for stat in range(100, 169): + assert 10 <= costs[stat] <= 50 + lines.append(f' {stat}: {costs[stat]}') + + # FUN_100ee0e0 reads six title caps, followed by growth per level. + lines.append('limits:') + for category in range(1, 6): + limits = read(1000209, category, 7) + lines.extend([f' {category}:', f' titles: {json.dumps(limits[:6])}', + f' per_level: {limits[6]}']) + lines.append('weights:') + for stat in range(100, 169): + weights = read(1000204, stat, 6) + assert all(0 <= weight <= 100 for weight in weights) + assert sum(weights) == (110 if stat == 156 else 100) + lines.append(f' {stat}: {json.dumps(weights)}') + database.close() + + # Keep source bytes and their hashes with the local research evidence. + output = pathlib.Path('server/gamedata/ip_training.yaml') + output.write_text('\n'.join(lines) + '\n') + report = pathlib.Path('local/ip-training-research/resources.json') + report.parent.mkdir(parents=True, exist_ok=True) + report.write_text(json.dumps(dict(index_sha256=hashlib.sha256(database.index).hexdigest(), + records=records), indent=2) + '\n') + print(output) + print(report) + + +if __name__ == '__main__': + main()