aorecomp

Anarchy Online classic in the browser

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


server/tiny_stalker_test.go (9645B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestTinyStalkerPopulationProfilesRecoverNativeWeapons(t *testing.T) {
     13 	data := testNPCData()
     14 	data.Playfields[0].Districts[0].Groups[0] = *gamedata.NPCGroups["950/district/4/group/3/authored_point"]
     15 	useNPCData(t, data)
     16 	world := newTestServer(t)
     17 	if err := world.flushWorld(); err != nil {
     18 		t.Fatal(err)
     19 	}
     20 
     21 	cold := newTestInventoryServer(t)
     22 
     23 	for _, current := range []*server{world, cold} {
     24 		for index, expected := range []struct {
     25 			level  uint16
     26 			health uint32
     27 			rating int32
     28 			low    uint32
     29 			high   uint32
     30 		}{{2, 20, 5, 4, 10}, {3, 30, 10, 4, 11}, {4, 40, 15, 4, 12}, {2, 20, 5, 4, 10}} {
     31 			npc := current.characters[1000000+uint32(index)]
     32 			if npc.name != "Tiny Stalker" || npc.monsterData != 17708 || npc.level != expected.level || npc.health != expected.health ||
     33 				npc.combatProfile == nil || !slices.Equal(npc.combatProfile.TemplateCodes, []string{"SR01"}) || npc.attackRating() != expected.rating ||
     34 				itemStat(npc.attackWeapon(), 286) != expected.low || itemStat(npc.attackWeapon(), 285) != expected.high {
     35 				t.Fatal("authored Stalker population lost its bounded profile or weapon quality", index, npc)
     36 			}
     37 			packet := targetPacket(characterPacket(npc))
     38 			end := int(binary.BigEndian.Uint16(packet[6:8]))
     39 			if binary.BigEndian.Uint32(packet[end-16:end-12]) != 121113 || binary.BigEndian.Uint32(packet[end-12:end-8]) != 121114 ||
     40 				binary.BigEndian.Uint32(packet[end-4:end]) != uint32(expected.level) {
     41 				t.Fatal("native spawn received a placeholder or wrong-quality weapon")
     42 			}
     43 		}
     44 	}
     45 }
     46 
     47 func TestTinyStalkerProfileRejectsUnreviewedBindings(t *testing.T) {
     48 	for _, change := range []string{"level 1", "level 5", "other code", "placeholder", "other monster"} {
     49 		t.Run(change, func(t *testing.T) {
     50 			data := testNPCData()
     51 			group := &data.Playfields[0].Districts[0].Groups[0]
     52 			group.TemplateCode, group.MonsterData, group.Levels, group.AppearanceStatus = "SR01", 17708, [2]uint16{2, 2}, "inferred_species"
     53 			switch change {
     54 			case "level 1":
     55 				group.Levels = [2]uint16{1, 1}
     56 			case "level 5":
     57 				group.Levels = [2]uint16{5, 5}
     58 			case "other code":
     59 				group.TemplateCode = "SR02"
     60 			case "placeholder":
     61 				group.AppearanceStatus = "placeholder_creature"
     62 			case "other monster":
     63 				group.MonsterData = 17655
     64 			}
     65 			useNPCData(t, data)
     66 			world := newTestServer(t)
     67 			if err := world.flushWorld(); err != nil {
     68 				t.Fatal(err)
     69 			}
     70 
     71 			cold := newTestInventoryServer(t)
     72 
     73 			for _, current := range []*server{world, cold} {
     74 				npc := current.characters[1000000]
     75 				if change == "other monster" {
     76 					if npc.combatProfile != gamedata.NPCCombatProfiles[17655][2] {
     77 						t.Fatal("Stalker profile replaced independently admitted Leet combat")
     78 					}
     79 				} else if npc.combatProfile != nil {
     80 					t.Fatal("unreviewed Stalker population inherited a profile", change)
     81 				}
     82 			}
     83 		})
     84 	}
     85 }
     86 
     87 func TestTinyStalkerCombatUsesStompPhases(t *testing.T) {
     88 	data := testNPCData()
     89 	group := &data.Playfields[0].Districts[0].Groups[0]
     90 	group.TemplateCode, group.MonsterData, group.Levels, group.AppearanceStatus = "SR01", 17708, [2]uint16{2, 2}, "inferred_species"
     91 	useNPCData(t, data)
     92 	world := newTestServer(t)
     93 	player := world.newCharacter()
     94 	player.position = group.Positions[0]
     95 	output := &combatTestConn{}
     96 	world.clients[player.id] = &worldClient{id: player.id, conn: output, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     97 	world.combatRoll = func(limit uint32) uint32 {
     98 		if limit == 100 {
     99 			return 50
    100 		}
    101 		return 0
    102 	}
    103 
    104 	if err := world.handleCombat(world.clients[player.id], attackPacket(player.id, 1000000)[20:]); err != nil {
    105 		t.Fatal(err)
    106 	}
    107 	start := player.nextAttack
    108 	world.advanceCombat(start)
    109 	world.advanceCombat(start.Add(2350*time.Millisecond - time.Nanosecond))
    110 	if player.health != 34 {
    111 		t.Fatal("Stomp landed before its original attack phase ended")
    112 	}
    113 	world.advanceCombat(start.Add(2350 * time.Millisecond))
    114 
    115 	npc := world.characters[1000000]
    116 	if player.health != 30 || npc.nextAttack != start.Add(7050*time.Millisecond) {
    117 		t.Fatal("Stomp did not use its catalog damage and attack/recharge phases", player.health, npc.nextAttack.Sub(start))
    118 	}
    119 }
    120 
    121 func TestTinyStalkerDeathsRetainRewardsAcrossActions(t *testing.T) {
    122 	for _, attack := range []string{"unarmed", "axe", "pistol", "fling", "fast", "nano"} {
    123 		for _, expected := range []struct {
    124 			level uint16
    125 			xp    uint32
    126 			ql    uint32
    127 		}{{2, 100, 3}, {3, 125, 4}, {4, 150, 5}} {
    128 			t.Run(fmt.Sprintf("%s/%d", attack, expected.level), func(t *testing.T) {
    129 				world, _ := newKillExperienceWorld(t, attack)
    130 				target := world.characters[1000000]
    131 				target.monsterData, target.level, target.npcDefinitionID = 17708, expected.level, "950/district/4/group/3/authored_point"
    132 				target.combatProfile = gamedata.NPCCombatProfiles[17708][expected.level]
    133 				world.combatRoll = func(limit uint32) uint32 {
    134 					if limit == 3 {
    135 						return 2
    136 					}
    137 					return 0
    138 				}
    139 				identity := world.nextItem + 1
    140 				if err := world.flushWorld(); err != nil {
    141 					t.Fatal(err)
    142 				}
    143 
    144 				attackForExperience(t, world, attack)
    145 				cold := newTestInventoryServer(t)
    146 
    147 				for _, current := range []*server{world, cold} {
    148 					npc := current.characters[1000000]
    149 					if npc.health != 0 || npc.corpse == nil || npc.corpse.credits != 1 || current.characters[1].experience != expected.xp ||
    150 						!slices.Equal(npc.corpse.items, []ownedItem{{ID: identity, Type: 51005, Template: 42640, Quality: expected.ql, Count: 1}}) ||
    151 						current.nextItem != identity || npc.combatProfile != gamedata.NPCCombatProfiles[17708][expected.level] {
    152 						t.Fatal("Stalker death lost or repeated its shared transactional rewards", npc.corpse)
    153 					}
    154 				}
    155 			})
    156 		}
    157 	}
    158 }
    159 
    160 func TestTinyStalkerFailedDeathRetriesPickupWithoutDuplicatingRewards(t *testing.T) {
    161 	for _, failure := range []struct{ name, sql string }{
    162 		{"item", "CREATE TRIGGER fail_stalker BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'stalker'); END"},
    163 		{"ignored placement", "CREATE TRIGGER fail_stalker BEFORE INSERT ON item_locations BEGIN SELECT RAISE(IGNORE); END"},
    164 		{"commit", "CREATE TABLE stalker_commit_failure (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_stalker AFTER INSERT ON items BEGIN INSERT INTO stalker_commit_failure VALUES(9999); END"},
    165 	} {
    166 		t.Run(failure.name, func(t *testing.T) {
    167 			world, output := newKillExperienceWorld(t, "unarmed")
    168 			target := world.characters[1000000]
    169 			target.monsterData, target.level, target.npcDefinitionID = 17708, 3, "950/district/4/group/3/authored_point"
    170 			target.combatProfile = gamedata.NPCCombatProfiles[17708][3]
    171 			world.combatRoll = func(uint32) uint32 { return 0 }
    172 			identity := world.nextItem + 1
    173 			if err := world.flushWorld(); err != nil {
    174 				t.Fatal(err)
    175 			}
    176 			if _, err := world.db.Exec(failure.sql); err != nil {
    177 				t.Fatal(err)
    178 			}
    179 
    180 			attackForExperience(t, world, "unarmed")
    181 			cold := newTestInventoryServer(t)
    182 
    183 			for _, current := range []*server{world, cold} {
    184 				if current.characters[1000000].health != 1 || current.characters[1000000].corpse != nil ||
    185 					current.characters[1].experience != 0 || current.nextItem != identity-1 {
    186 					t.Fatal("failed Stalker death leaked resources or an item identity")
    187 				}
    188 			}
    189 			for _, packet := range output.packets {
    190 				switch binary.BigEndian.Uint32(packet[16:20]) {
    191 				case 0x46002f16, 0x754f1115, 0x4727213e, 0x4f474e05, 0x7f405a16:
    192 					t.Fatal("failed Stalker death published a successful outcome")
    193 				}
    194 			}
    195 
    196 			// The failed reservation remains unused. Retry and pick up a fresh identity.
    197 			if _, err := world.db.Exec("DROP TRIGGER fail_stalker"); err != nil {
    198 				t.Fatal(err)
    199 			}
    200 			attackForExperience(t, world, "unarmed")
    201 			if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil {
    202 				t.Fatal(err)
    203 			}
    204 			if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
    205 				t.Fatal(err)
    206 			}
    207 			if err := world.handleLootUse(world.clients[1], lootUseRequest(2)); err != nil {
    208 				t.Fatal(err)
    209 			}
    210 			cold = newTestInventoryServer(t)
    211 			for _, current := range []*server{world, cold} {
    212 				player, corpse := current.characters[1], current.characters[1000000].corpse
    213 				if player.experience != 125 || player.inventory.Credits != 1 || current.nextItem != identity+1 ||
    214 					!slices.Equal(player.inventory.Items, []ownedItem{{ID: identity + 1, Type: 51005, Template: 42640, Quality: 2, Slot: 64, Count: 1}}) ||
    215 					corpse.credits != 0 || len(corpse.items) != 0 {
    216 					t.Fatal("retry or repeated pickup lost or repeated Stalker rewards")
    217 				}
    218 			}
    219 		})
    220 	}
    221 }
    222 
    223 func TestTinyStalkerEmptyRollRetainsCashWithoutAllocatingAnItem(t *testing.T) {
    224 	world, _ := newKillExperienceWorld(t, "unarmed")
    225 	target := world.characters[1000000]
    226 	target.monsterData, target.level, target.npcDefinitionID = 17708, 2, "950/district/4/group/3/authored_point"
    227 	target.combatProfile = gamedata.NPCCombatProfiles[17708][2]
    228 	world.combatRoll = func(limit uint32) uint32 {
    229 		if limit == 5 {
    230 			return 4
    231 		}
    232 		if limit == 100 {
    233 			return 50
    234 		}
    235 		return 0
    236 	}
    237 	before := world.nextItem
    238 	if err := world.flushWorld(); err != nil {
    239 		t.Fatal(err)
    240 	}
    241 
    242 	attackForExperience(t, world, "unarmed")
    243 	cold := newTestInventoryServer(t)
    244 
    245 	for _, current := range []*server{world, cold} {
    246 		corpse := current.characters[1000000].corpse
    247 		if corpse == nil || len(corpse.items) != 0 || corpse.credits != 5 || current.nextItem != before || current.characters[1].experience != 100 {
    248 			t.Fatal("empty membership roll changed cash, XP or allocated an identity", corpse)
    249 		}
    250 	}
    251 }