aorecomp

Anarchy Online classic in the browser

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


server/npc_loot_range_test.go (9420B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestRangedEquipmentLootAcrossKillingActions(t *testing.T) {
     12 	for _, attack := range []string{"unarmed", "axe", "pistol", "fling", "fast", "nano"} {
     13 		t.Run(attack, func(t *testing.T) {
     14 			world, _ := newKillExperienceWorld(t, attack)
     15 			profile := *world.characters[1000000].combatProfile
     16 			profile.Loot = []gamedata.NPCLoot{{Template: 85596, Quality: [2]uint32{3, 5}, Chance: 100}}
     17 			world.characters[1000000].combatProfile = &profile
     18 			world.combatRoll = func(limit uint32) uint32 {
     19 				if limit == 3 {
     20 					return 1
     21 				}
     22 				return 0
     23 			}
     24 			identity := world.nextItem + 1
     25 
     26 			attackForExperience(t, world, attack)
     27 			world.advanceCombat(time.Now().Add(time.Hour))
     28 			world.advanceNanos(time.Now().Add(time.Hour))
     29 			cold := newTestInventoryServer(t)
     30 
     31 			if !slices.Equal(cold.characters[1000000].corpse.items, []ownedItem{{ID: identity, Type: 51022, Template: 85596, Quality: 4, Count: 1}}) || cold.nextItem != identity || cold.characters[1].experience != 100 {
     32 				t.Fatal("killing action lost or repeated its selected range quality")
     33 			}
     34 		})
     35 	}
     36 }
     37 
     38 func TestProductionLootQualityRangeBoundaries(t *testing.T) {
     39 	for _, scenario := range []struct {
     40 		level       uint16
     41 		qualityRoll uint32
     42 		empty       bool
     43 		quality     uint32
     44 		lastBounds  []uint32
     45 	}{
     46 		{1, 0, false, 1, []uint32{5, 100, 100, 100}},
     47 		{2, 0, false, 1, []uint32{5, 100, 3, 100, 3, 100, 3}},
     48 		{2, 1, false, 2, []uint32{5, 100, 3, 100, 3, 100, 3}},
     49 		{2, 2, false, 3, []uint32{5, 100, 3, 100, 3, 100, 3}},
     50 		{2, 2, true, 0, []uint32{5, 100, 100, 100}},
     51 	} {
     52 		t.Run(fmt.Sprintf("%d/%d/empty=%t", scenario.level, scenario.qualityRoll, scenario.empty), func(t *testing.T) {
     53 			world, _ := newKillExperienceWorld(t, "unarmed")
     54 			target := world.characters[1000000]
     55 			target.level, target.combatProfile = scenario.level, gamedata.NPCCombatProfiles[17655][scenario.level]
     56 			var bounds []uint32
     57 			world.combatRoll = func(limit uint32) uint32 {
     58 				bounds = append(bounds, limit)
     59 				if limit == 3 {
     60 					return scenario.qualityRoll
     61 				}
     62 				if scenario.empty && limit == 100 {
     63 					return 50
     64 				}
     65 				return 0
     66 			}
     67 			identity := world.nextItem + 1
     68 
     69 			attackForExperience(t, world, "unarmed")
     70 			world.advanceCombat(world.characters[1].nextAttack)
     71 			cold := newTestInventoryServer(t)
     72 
     73 			if len(bounds) < len(scenario.lastBounds) || !slices.Equal(bounds[len(bounds)-len(scenario.lastBounds):], scenario.lastBounds) {
     74 				t.Fatal("fixed or empty drop consumed a quality roll, or range endpoints changed", bounds)
     75 			}
     76 			for _, current := range []*server{world, cold} {
     77 				corpse := current.characters[1000000].corpse
     78 				if corpse == nil || corpse.credits != 1 || current.characters[1].experience != 100 {
     79 					t.Fatal("quality selection changed the ordinary kill outcome")
     80 				}
     81 				if scenario.empty {
     82 					if len(corpse.items) != 0 || current.nextItem != identity-1 {
     83 						t.Fatal("empty roll allocated an item")
     84 					}
     85 				} else if !slices.Equal(corpse.items, []ownedItem{{ID: identity, Type: 51005, Template: 42640, Quality: scenario.quality, Count: 1}, {ID: identity + 1, Type: 51022, Template: 70562, Quality: scenario.quality, Slot: 1, Count: 1}, {ID: identity + 2, Type: 51005, Template: 136646, Quality: scenario.quality, Slot: 2, Count: 1}}) || current.nextItem != identity+2 {
     86 					t.Fatal("quality range did not persist its selected instance", corpse.items)
     87 				}
     88 			}
     89 		})
     90 	}
     91 }
     92 
     93 func TestRangedLootRollbackAndRetry(t *testing.T) {
     94 	for _, failure := range []struct{ name, sql string }{
     95 		{"item", "CREATE TRIGGER reject_range BEFORE INSERT ON items BEGIN SELECT RAISE(FAIL,'range'); END"},
     96 		{"ignored placement", "CREATE TRIGGER reject_range BEFORE INSERT ON item_locations BEGIN SELECT RAISE(IGNORE); END"},
     97 		{"commit", "CREATE TABLE range_commit_failure (id INTEGER REFERENCES items(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_range AFTER INSERT ON items BEGIN INSERT INTO range_commit_failure VALUES(9999); END"},
     98 	} {
     99 		t.Run(failure.name, func(t *testing.T) {
    100 			world, _ := newKillExperienceWorld(t, "unarmed")
    101 			target := world.characters[1000000]
    102 			profile := *gamedata.NPCCombatProfiles[17655][2]
    103 			profile.Loot = slices.Clone(profile.Loot[:1])
    104 			target.level, target.combatProfile = 2, &profile
    105 			world.combatRoll = func(limit uint32) uint32 {
    106 				if limit == 3 {
    107 					return 2
    108 				}
    109 				return 0
    110 			}
    111 			before := world.nextItem
    112 			if _, err := world.db.Exec(failure.sql); err != nil {
    113 				t.Fatal(err)
    114 			}
    115 
    116 			attackForExperience(t, world, "unarmed")
    117 			cold := newTestInventoryServer(t)
    118 			if target.health != 1 || target.corpse != nil || world.nextItem != before || cold.characters[1000000].health != 1 || cold.characters[1000000].corpse != nil || cold.nextItem != before || cold.characters[1].experience != 0 || len(cold.items) != 0 {
    119 				t.Fatal("failed ranged reward leaked a death, quality or identity")
    120 			}
    121 			if _, err := world.db.Exec("DROP TRIGGER reject_range"); err != nil {
    122 				t.Fatal(err)
    123 			}
    124 			attackForExperience(t, world, "unarmed")
    125 			cold = newTestInventoryServer(t)
    126 
    127 			if !slices.Equal(cold.characters[1000000].corpse.items, []ownedItem{{ID: before + 2, Type: 51005, Template: 42640, Quality: 3, Count: 1}}) || cold.nextItem != before+2 || cold.characters[1].experience != 100 {
    128 				t.Fatal("retry lost the selected range quality or allocated twice")
    129 			}
    130 		})
    131 	}
    132 }
    133 
    134 func TestRangedMonsterPartFullInventoryColdPickupAndSale(t *testing.T) {
    135 	world, _ := newKillExperienceWorld(t, "unarmed")
    136 	player, client := world.characters[1], world.clients[1]
    137 	player.appearance = 0x515
    138 	if err := world.saveUser(player); err != nil {
    139 		t.Fatal(err)
    140 	}
    141 	inventory := player.inventory
    142 	for slot := uint32(64); slot < 94; slot++ {
    143 		inventory.LastID++
    144 		inventory.Items = append(inventory.Items, ownedItem{ID: inventory.LastID, Type: 51005, Template: 121567, Quality: 1, Count: 1, Slot: slot})
    145 	}
    146 	if err := world.saveInventory(player, inventory); err != nil {
    147 		t.Fatal(err)
    148 	}
    149 	profile := *world.characters[1000000].combatProfile
    150 	profile.Loot = []gamedata.NPCLoot{{Template: 42641, Quality: [2]uint32{1, 400}, Chance: 100}}
    151 	world.characters[1000000].combatProfile = &profile
    152 	world.combatRoll = func(limit uint32) uint32 {
    153 		if limit == 400 {
    154 			return 199
    155 		}
    156 		return 0
    157 	}
    158 	identity := world.nextItem + 1
    159 
    160 	// Collect cash, then reject pickup while every inventory slot is occupied.
    161 	attackForExperience(t, world, "unarmed")
    162 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
    163 		t.Fatal(err)
    164 	}
    165 	if err := world.handleInventory(client, lootTakeRequest()); err != nil {
    166 		t.Fatal(err)
    167 	}
    168 	if len(player.inventory.Items) != 30 || !slices.Equal(world.characters[1000000].corpse.items, []ownedItem{{ID: identity, Type: 51005, Template: 42641, Quality: 200, Count: 1}}) {
    169 		t.Fatal("full inventory changed the selected corpse instance")
    170 	}
    171 
    172 	// Free one slot and recover the same selected instance after a cold load.
    173 	if err := world.handleInventory(client, inventoryDeletePacket(1, 64)[20:]); err != nil {
    174 		t.Fatal(err)
    175 	}
    176 	if err := world.handleInventory(client, lootTakeRequest()); err != nil {
    177 		t.Fatal(err)
    178 	}
    179 	if err := world.handleInventory(client, lootTakeRequest()); err != nil {
    180 		t.Fatal(err)
    181 	}
    182 	world = newTestInventoryServer(t)
    183 	player = world.characters[1]
    184 	client = &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true}
    185 	world.clients[1] = client
    186 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil {
    187 		t.Fatal(err)
    188 	}
    189 	item := slices.IndexFunc(player.inventory.Items, func(item ownedItem) bool { return item.ID == identity })
    190 	if item < 0 || player.inventory.Items[item].Quality != 200 || player.inventory.Items[item].shopPrice() != 2563 || client.equipment != nil {
    191 		t.Fatal("cold pickup or rejected wear changed the interpolated part")
    192 	}
    193 
    194 	// Cancel a sale before accepting it, preserving every unrelated item.
    195 	player.position = [3]float32{60, 14.75, 53}
    196 	if err := world.savePosition(player); err != nil {
    197 		t.Fatal(err)
    198 	}
    199 	if err := world.handleShopUse(client, shopUseRequest(1)); err != nil {
    200 		t.Fatal(err)
    201 	}
    202 	add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]
    203 	if err := world.handleTrade(client, add); err != nil {
    204 		t.Fatal(err)
    205 	}
    206 	if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    207 		t.Fatal(err)
    208 	}
    209 	cancelled := newTestInventoryServer(t)
    210 	if cancelled.items[identity].Quality != 200 || cancelled.characters[1].inventory.Credits != 1 {
    211 		t.Fatal("cancelled sale changed the selected part or balance")
    212 	}
    213 	if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    214 		t.Fatal(err)
    215 	}
    216 	if err := world.handleTrade(client, add); err != nil {
    217 		t.Fatal(err)
    218 	}
    219 	for range 2 {
    220 		if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    221 			t.Fatal(err)
    222 		}
    223 	}
    224 	cold := newTestInventoryServer(t)
    225 
    226 	if cold.characters[1].inventory.Credits != 129 || len(cold.characters[1].inventory.Items) != 29 || cold.nextItem != identity || len(cold.characters[1000000].corpse.items) != 0 {
    227 		t.Fatal("interpolated sale lost other items or repeated rewards", cold.characters[1].inventory)
    228 	}
    229 	if _, exists := cold.items[identity]; exists {
    230 		t.Fatal("sold selected-quality part survived cold recovery")
    231 	}
    232 }