aorecomp

Anarchy Online classic in the browser

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


server/regeneration_delta_test.go (10054B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"reflect"
      7 	"slices"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestRegenerationUsesSignedDeltasAndNativeFullResourceGates(t *testing.T) {
     13 	for _, scenario := range []struct {
     14 		name                 string
     15 		health, nano         uint32
     16 		wantHealth, wantNano uint32
     17 	}{
     18 		{"injured", 20, 10, 14, 4},
     19 		{"full health skips negative delta", 37, 35, 37, 29},
     20 		{"nano drains to zero", 20, 1, 14, 0},
     21 	} {
     22 		t.Run(scenario.name, func(t *testing.T) {
     23 			// All authored delta implants are positive; this fixture checks signed arithmetic.
     24 			original := gamedata.ItemTemplates[191531]
     25 			definition := *original
     26 			definition.Sections = slices.Clone(original.Sections)
     27 			functions := slices.Clone(*definition.Sections[0].Functions)
     28 			for index := range functions {
     29 				functions[index].Arguments = slices.Clone(functions[index].Arguments)
     30 				functions[index].Arguments[1].Integer = new(int32(-9))
     31 			}
     32 			definition.Sections[0].Functions = &functions
     33 			gamedata.ItemTemplates[191531] = &definition
     34 			t.Cleanup(func() { gamedata.ItemTemplates[191531] = original })
     35 			world, _, start := newRegenerationWorld(t)
     36 			player := world.characters[1]
     37 			player.inventory.Items = []ownedItem{{Type: 51022, Template: 191531, Quality: 1, Slot: 45}}
     38 			player.health, player.nano = scenario.health, scenario.nano
     39 			world.clients[1].regenerationSeconds = 810
     40 
     41 			world.advanceRegeneration(start.Add(2 * time.Second))
     42 
     43 			if player.health != scenario.wantHealth || player.nano != scenario.wantNano {
     44 				t.Fatal("signed recovery or the full-resource gate diverged", player.health, player.nano)
     45 			}
     46 		})
     47 	}
     48 }
     49 
     50 func TestRegenerationAddsAuthoredEquipmentAndNanoDeltas(t *testing.T) {
     51 	for _, source := range []struct {
     52 		name       string
     53 		items      []ownedItem
     54 		nanos      []activeNano
     55 		heal, nano uint32
     56 	}{
     57 		{name: "breed only"},
     58 		{name: "heal implant", items: []ownedItem{{Type: 51022, Template: 178548, Quality: 1, Slot: 38}}, heal: 5},
     59 		{name: "nano implant", items: []ownedItem{{Type: 51022, Template: 166365, Quality: 1, Slot: 39}}, nano: 5},
     60 		{name: "both deltas", items: []ownedItem{{Type: 51022, Template: 191531, Quality: 1, Slot: 45}}, heal: 3, nano: 2},
     61 		{name: "stacked implants", items: []ownedItem{
     62 			{Type: 51022, Template: 178548, Quality: 1, Slot: 38},
     63 			{Type: 51022, Template: 166365, Quality: 1, Slot: 39},
     64 			{Type: 51022, Template: 191531, Quality: 1, Slot: 45},
     65 		}, heal: 8, nano: 7},
     66 		// These original programs are active-effect fixtures, not newly admitted casts.
     67 		{name: "implants and nanos", items: []ownedItem{
     68 			{Type: 51022, Template: 178548, Quality: 1, Slot: 38},
     69 			{Type: 51022, Template: 166365, Quality: 1, Slot: 39},
     70 			{Type: 51022, Template: 191531, Quality: 1, Slot: 45},
     71 		}, nanos: []activeNano{{program: 27144}, {program: 27118}}, heal: 11, nano: 9},
     72 		{name: "carried implants", items: []ownedItem{
     73 			{Type: 51022, Template: 178548, Quality: 1, Slot: 64},
     74 			{Type: 51022, Template: 166365, Quality: 1, Slot: 65},
     75 		}},
     76 	} {
     77 		for _, posture := range []struct {
     78 			name                  string
     79 			sitting, fighting     bool
     80 			counter, health, nano uint32
     81 			interval              uint32
     82 		}{
     83 			{"standing", false, false, 812, 25, 5, 29},
     84 			{"sitting", true, false, 28, 31, 11, 29},
     85 			{"attacking", false, true, 3422, 25, 5, 59},
     86 		} {
     87 			t.Run(source.name+"/"+posture.name, func(t *testing.T) {
     88 				world, conn, start := newRegenerationWorld(t)
     89 				player := world.characters[1]
     90 				player.level, player.stats.Body.BodyDevelopment = 50, 198
     91 				player.inventory.Items, player.activeNanos = source.items, source.nanos
     92 				player.sitting = posture.sitting
     93 				if posture.fighting {
     94 					player.fighting = 1000000
     95 				}
     96 				world.clients[1].regenerationSeconds = posture.counter - 2
     97 
     98 				world.advanceRegeneration(start.Add(2 * time.Second))
     99 				healthInterval, nanoInterval := player.regenerationIntervals()
    100 
    101 				if player.health != posture.health+source.heal || player.nano != posture.nano+source.nano ||
    102 					healthInterval != posture.interval || nanoInterval != posture.interval {
    103 					t.Fatal("deltas changed timing or were divided by posture", player.health, player.nano, healthInterval, nanoInterval)
    104 				}
    105 				last := conn.packets[len(conn.packets)-1]
    106 				if binary.BigEndian.Uint32(last[29:33]) != 214 || binary.BigEndian.Uint32(last[33:37]) != player.nano {
    107 					t.Fatal("recovery did not publish authoritative nano", last)
    108 				}
    109 			})
    110 		}
    111 	}
    112 }
    113 
    114 func TestRegenerationDeltasClampWithoutRevivingOrChangingSkippedActors(t *testing.T) {
    115 	for _, scenario := range []string{"below cap", "at cap", "dead", "loading", "offline", "not ready"} {
    116 		t.Run(scenario, func(t *testing.T) {
    117 			world, _, start := newRegenerationWorld(t)
    118 			player := world.characters[1]
    119 			player.inventory.Items = []ownedItem{{Type: 51022, Template: 191531, Quality: 1, Slot: 45}}
    120 			player.health, player.nano = 36, 34
    121 			world.clients[1].regenerationSeconds = 810
    122 			switch scenario {
    123 			case "at cap":
    124 				player.health, player.nano = 37, 35
    125 			case "dead":
    126 				player.health = 0
    127 			case "loading":
    128 				world.clients[1].loading = true
    129 			case "offline":
    130 				delete(world.clients, 1)
    131 			case "not ready":
    132 				world.clients[1].ready = false
    133 			}
    134 			health, nano := player.health, player.nano
    135 
    136 			world.advanceRegeneration(start.Add(2 * time.Second))
    137 
    138 			if scenario == "below cap" || scenario == "at cap" {
    139 				if player.health != 37 || player.nano != 35 {
    140 					t.Fatal("delta overflowed the resource cap", player.health, player.nano)
    141 				}
    142 			} else if player.health != health || player.nano != nano {
    143 				t.Fatal("delta bypassed the actor recovery gate", player.health, player.nano)
    144 			}
    145 		})
    146 	}
    147 }
    148 
    149 func TestRegenerationImplantMovesPersistAndTicksWaitForCheckpoint(t *testing.T) {
    150 	world, _ := newCatalogImplantWorld(t, 178548)
    151 	player, client := world.characters[1], world.clients[1]
    152 	player.level = 50
    153 	player.stats.Abilities.Strength, player.stats.Abilities.Agility, player.stats.Abilities.Sense = 16, 16, 16
    154 	player.appearance, player.health, player.nano = 1<<4, 20, 0
    155 	if err := world.saveUser(player); err != nil {
    156 		t.Fatal(err)
    157 	}
    158 	for _, template := range []uint32{166365, 191531} {
    159 		if granted, err := world.grantItem(player, template); err != nil || !granted {
    160 			t.Fatal(granted, err)
    161 		}
    162 	}
    163 	before := slices.Clone(player.inventory.Items)
    164 
    165 	for _, move := range [][2]uint32{{66, 38}, {67, 39}, {68, 45}} {
    166 		if err := world.handleInventory(client, inventoryMovePacket(1, move[0], move[1])[20:]); err != nil || client.equipment == nil {
    167 			t.Fatal("valid regeneration implant did not start installation", move, err)
    168 		}
    169 		world.advanceEquipment(client.equipment.readyAt)
    170 	}
    171 	restarted := newTestInventoryServer(t).characters[1]
    172 	if restarted.equipmentBonus(343) != 8 || restarted.equipmentBonus(364) != 7 || len(restarted.inventory.Items) != len(before) {
    173 		t.Fatal("cold load lost installed deltas or items", restarted.inventory.Items)
    174 	}
    175 	auditCheckpointWrites(t, world)
    176 	start := time.Unix(100, 0)
    177 	client.regenerationAt, client.regenerationSeconds = start, 810
    178 	world.advanceRegeneration(start.Add(2 * time.Second))
    179 
    180 	if player.health != 31 || player.nano != 10 || checkpointWrites(t, world)["characters/UPDATE"] != 0 {
    181 		t.Fatal("recovery lost deltas or wrote its low-significance tick to SQLite", player.health, player.nano, checkpointWrites(t, world))
    182 	}
    183 	if cold := newTestInventoryServer(t).characters[1]; cold.health != 20 || cold.nano != 0 {
    184 		t.Fatal("recovery persisted before checkpoint", cold.health, cold.nano)
    185 	}
    186 
    187 	// Remove equipment through the same public move path without forcing a vitals checkpoint.
    188 	if err := world.handleInventory(client, itemMovePacket(1, 45, 111)[20:]); err != nil || client.equipment == nil {
    189 		t.Fatal("regeneration implant did not start removal", err)
    190 	}
    191 	world.advanceEquipment(client.equipment.readyAt)
    192 	restarted = newTestInventoryServer(t).characters[1]
    193 	if restarted.health != 20 || restarted.nano != 0 || restarted.equipmentBonus(343) != 5 || restarted.equipmentBonus(364) != 5 ||
    194 		len(restarted.inventory.Items) != len(before) || restarted.inventory.Items[4].ID != before[4].ID || restarted.inventory.Items[4].Slot != 66 {
    195 		t.Fatal("removal lost vitals, identity, placement or remaining deltas", restarted.inventory.Items)
    196 	}
    197 	client.regenerationSeconds = 1622
    198 	world.advanceRegeneration(start.Add(4 * time.Second))
    199 	if player.health != 39 || player.nano != 18 || client.regenerationSeconds != 1624 {
    200 		t.Fatal("removing a delta did not change the next gain exactly once", player.health, player.nano)
    201 	}
    202 	if err := world.flushWorld(); err != nil {
    203 		t.Fatal(err)
    204 	}
    205 	if cold := newTestInventoryServer(t).characters[1]; cold.health != 39 || cold.nano != 18 {
    206 		t.Fatal("world flush lost accumulated recovery", cold.health, cold.nano)
    207 	}
    208 }
    209 
    210 func TestRegenerationDeltaInstallFailurePreservesItemsAndRecovery(t *testing.T) {
    211 	for _, scenario := range []string{"ability equality", "title equality", "failed save"} {
    212 		t.Run(scenario, func(t *testing.T) {
    213 			world, conn := newCatalogImplantWorld(t, 191531)
    214 			player, client := world.characters[1], world.clients[1]
    215 			player.level, player.stats.Abilities.Strength = 50, 16
    216 			if scenario == "ability equality" {
    217 				player.stats.Abilities.Strength = 15
    218 			} else if scenario == "title equality" {
    219 				player.level = 49
    220 			}
    221 			before := slices.Clone(player.inventory.Items)
    222 			stored, _ := readTestItemStore(world)
    223 
    224 			err := world.handleInventory(client, inventoryMovePacket(1, 66, 45)[20:])
    225 			if scenario == "failed save" {
    226 				if client.equipment == nil {
    227 					t.Fatal("valid delta installation did not start")
    228 				}
    229 				failDatabaseWrites(t, world)
    230 				world.advanceEquipment(client.equipment.readyAt)
    231 			}
    232 			after, _ := readTestItemStore(world)
    233 
    234 			if err != nil || client.equipment != nil || !reflect.DeepEqual(player.inventory.Items, before) || !slices.Equal(stored, after) ||
    235 				player.equipmentBonus(343) != 0 || player.equipmentBonus(364) != 0 {
    236 				t.Fatal("rejected delta install changed ownership or recovery", err, conn.packets)
    237 			}
    238 		})
    239 	}
    240 }