server/loot_economy_test.go (5014B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "slices" 7 "testing" 8 ) 9 10 func TestStarterValuableSalvageChanceBoundary(t *testing.T) { 11 for _, scenario := range []struct { 12 roll uint32 13 pearl bool 14 }{{34, true}, {35, false}} { 15 world, _ := newKillExperienceWorld(t, "unarmed") 16 world.characters[1000000].combatProfile = gamedata.NPCCombatProfiles[17655][1] 17 world.combatRoll = func(limit uint32) uint32 { 18 if limit == 100 { 19 return scenario.roll 20 } 21 return 0 22 } 23 24 attackForExperience(t, world, "unarmed") 25 cold := newTestInventoryServer(t) 26 27 if slices.ContainsFunc(cold.characters[1000000].corpse.items, func(item ownedItem) bool { return item.Template == 136646 }) != scenario.pearl { 28 t.Fatal("valuable salvage probability changed", scenario) 29 } 30 } 31 } 32 33 func TestStarterLootSaleFundsOriginalPricePurchase(t *testing.T) { 34 world, _ := newKillExperienceWorld(t, "unarmed") 35 player, client := world.characters[1], world.clients[1] 36 world.characters[1000000].combatProfile = gamedata.NPCCombatProfiles[17655][1] 37 world.combatRoll = func(limit uint32) uint32 { 38 if limit == 100 { 39 return 34 40 } 41 return 0 42 } 43 player.stats.Abilities.Intelligence = 5 44 player.stats.TradeRepair.ComputerLiteracy = 0 45 46 // Earn the pearl and corpse cash through combat and normal pickup. 47 attackForExperience(t, world, "unarmed") 48 if err := world.handleLootUse(client, lootUseRequest(1)); err != nil { 49 t.Fatal(err) 50 } 51 request := lootTakeRequest() 52 binary.BigEndian.PutUint32(request[20:24], 1) 53 if err := world.handleInventory(client, request); err != nil { 54 t.Fatal(err) 55 } 56 if len(player.inventory.Items) != 1 || player.inventory.Items[0].Template != 136646 { 57 t.Fatal("valuable loot was not collected") 58 } 59 if err := world.handleLootClose(client, characterActionPacket(1, [5]uint32{142, 0, 0, 51050, 1000000})[20:]); err != nil { 60 t.Fatal(err) 61 } 62 63 // Spend earned proceeds at the existing tool machine, without a cash grant. 64 player.playfield, player.position, player.appearance = 1183, [3]float32{188, 5.75, 135.5}, 0x515 65 if err := world.saveUser(player); err != nil { 66 t.Fatal(err) 67 } 68 for _, selection := range []struct{ owner, row [2]uint32 }{ 69 {[2]uint32{50000, 1}, [2]uint32{104, 64}}, 70 {[2]uint32{51035, 3221488799}, [2]uint32{111, 0}}, 71 } { 72 request = shopUseRequest(1) 73 binary.BigEndian.PutUint32(request[20:24], client.useSequence+1) 74 binary.BigEndian.PutUint32(request[44:48], 3221488799) 75 if err := world.handleShopUse(client, request); err != nil { 76 t.Fatal(err) 77 } 78 if client.shop == nil { 79 t.Fatal("existing vendor did not open") 80 } 81 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, selection.owner, selection.row)[20:]); err != nil { 82 t.Fatal(err) 83 } 84 if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil { 85 t.Fatal(err) 86 } 87 } 88 cold := newTestInventoryServer(t) 89 if cold.characters[1].inventory.Credits != 11 || len(cold.characters[1].inventory.Items) != 1 || cold.characters[1].inventory.Items[0].Template != 95577 { 90 t.Fatal("loot sale did not fund the original 230-credit lock pick", cold.characters[1].inventory) 91 } 92 } 93 94 func TestStarterLootIncomeCoversSuppliesAndUpgrade(t *testing.T) { 95 for monster, profiles := range gamedata.NPCCombatProfiles { 96 for level, profile := range profiles { 97 if len(profile.Loot) == 0 || !slices.Contains([]uint32{42640, 42620, 82102, 82912}, profile.Loot[0].Template) { 98 continue 99 } 100 if !slices.ContainsFunc(profile.Loot, func(drop gamedata.NPCLoot) bool { 101 return drop.Template == 136646 && drop.Chance == 35 && drop.Quality == profile.Loot[0].Quality 102 }) { 103 t.Fatal("ordinary starter profile lacks level-matched valuables", monster, level) 104 } 105 } 106 } 107 108 // Clan/Neutral pays only 4% of item value, so use that conservative shop. 109 profile := gamedata.NPCCombatProfiles[17655][1] 110 var expected float64 111 for _, drop := range profile.Loot { 112 definition := gamedata.ItemTemplates[drop.Template] 113 item := ownedItem{Type: definition.HeaderWords[0], Template: drop.Template, Quality: 1, Count: 1} 114 expected += float64(shopSalePrice(item, 4, 0)) * float64(drop.Chance) / 100 115 } 116 var basket uint64 117 for _, id := range []uint32{25812, 85366, 25820, 21609, 70562} { 118 item := ownedItem{Type: gamedata.ItemTemplates[id].HeaderWords[0], Template: id, Quality: 1, Count: 1} 119 basket += shopBuyPrice(item, 105, 0) 120 } 121 if basket != 623 || expected != 58.5 || expected*12 < float64(basket) { 122 t.Fatal("ordinary sale income no longer funds the unchanged starter basket", basket, expected) 123 } 124 // Boots and two sleeves complete the previously assessed 12-melee-AC set. 125 for _, id := range []uint32{70558, 70561, 70561} { 126 item := ownedItem{Type: gamedata.ItemTemplates[id].HeaderWords[0], Template: id, Quality: 1, Count: 1} 127 basket += shopBuyPrice(item, 105, 0) 128 } 129 if basket != 1034 || expected*18 < float64(basket) { 130 t.Fatal("loot cannot fund the full starter armor-and-supplies basket", basket, expected) 131 } 132 }