server/loot.go (12815B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "maps" 7 "math" 8 "slices" 9 "time" 10 ) 11 12 type npcCorpse struct { 13 looter uint32 14 team uint32 15 items []ownedItem 16 credits uint32 17 expiresAt time.Time 18 lootOrder uint32 19 } 20 21 func corpsePacket(character *character) []byte { 22 monster := gamedata.MonsterTemplates[character.monsterData] 23 var body []byte 24 for _, value := range []uint32{7, 11, 0, 0} { 25 body = binary.BigEndian.AppendUint32(body, value) 26 } 27 for _, value := range character.position { 28 body = binary.BigEndian.AppendUint32(body, math.Float32bits(value)) 29 } 30 for _, value := range character.rotation { 31 body = binary.BigEndian.AppendUint32(body, math.Float32bits(value)) 32 } 33 for _, value := range []uint32{character.playfield, 0, 0} { 34 body = binary.BigEndian.AppendUint32(body, value) 35 } 36 body = append(body, 0, 0) 37 38 // Keep an unlimited use count and an animated model for the remains. 39 stats := [][2]uint32{{12, 0}, {26, 0xffffffff}, {42, monster.Mesh}, {4, uint32(character.appearance >> 4 & 7)}, 40 {59, uint32(character.appearance >> 7 & 3)}, {89, uint32(character.appearance & 3)}, 41 {223, 0}, {360, 100}, {415, 50000}, {416, character.id}, {61, character.corpse.credits}} 42 var cloth []uint32 43 if character.appearance>>4&7 < 6 { 44 head, textures := character.appearanceAssets() 45 stats[6] = [2]uint32{223, 1} 46 stats = append(stats, [2]uint32{64, head}) 47 // ClothPart order is hands, body, feet, arms, legs. Preserve the same 48 // original OnWear textures as the living actor, including both sleeves. 49 cloth = []uint32{textures[2], textures[4], textures[1], textures[0], textures[3]} 50 for _, item := range character.inventory.Items { 51 for _, section := range item.templateDefinition().Sections { 52 if section.Type != 2 || section.Subtype != 14 { 53 continue 54 } 55 for _, function := range *section.Functions { 56 if function.ID == 53039 { 57 cloth[*function.Arguments[1].Integer] = uint32(*function.Arguments[0].Integer) 58 } 59 } 60 } 61 } 62 } 63 body = binary.BigEndian.AppendUint32(body, uint32(len(stats)+1)*1009) 64 for _, stat := range stats { 65 body = binary.BigEndian.AppendUint32(body, stat[0]) 66 body = binary.BigEndian.AppendUint32(body, stat[1]) 67 } 68 name := append([]byte("Remains of "+character.name), 0) 69 body = binary.BigEndian.AppendUint32(body, uint32(len(name))) 70 body = append(body, name...) 71 72 // The creation event holds the final death frame; an empty animation list 73 // makes n3VisualDynel_t::Run replace the otherwise valid model with a cube. 74 looterType := uint32(50000) 75 if character.corpse.team != 0 && character.corpse.lootOrder == 0 { 76 looterType = 0 77 } 78 for _, value := range []uint32{2, 0, 1009, 3, 2018, 53003, 0, 4, 0, 1, 0, 0, 0xffffffff, 79 51050, character.id, monster.DeathAnimations[0], 1, 0, 50000, character.id} { 80 body = binary.BigEndian.AppendUint32(body, value) 81 } 82 // kkr.dll sub_36005440 reads indexed ClothData entries, each containing 83 // its material category, original texture identity and a boolean. 84 body = binary.BigEndian.AppendUint32(body, uint32(len(cloth)+1)*1009) 85 for index, texture := range cloth { 86 for _, value := range []uint32{uint32(index), uint32(index), texture, 1} { 87 body = binary.BigEndian.AppendUint32(body, value) 88 } 89 } 90 for _, value := range []uint32{0, character.corpse.team, 3 + character.corpse.lootOrder, looterType, character.corpse.looter, 0} { 91 body = binary.BigEndian.AppendUint32(body, value) 92 } 93 return n3Packet(0x4f474e05, 51050, character.id, body) 94 } 95 96 func lootInventoryPacket(owner uint32, corpse *character, open bool) []byte { 97 body := binary.BigEndian.AppendUint32(nil, uint32(len(corpse.corpse.items)+1)*1009) 98 for _, item := range corpse.corpse.items { 99 // Bit 0 selects the native template-proxy transfer path. 100 low, high := item.templateIDs() 101 for _, value := range []uint32{item.Slot, 0x00030000 | uint32(item.Count), item.Type, item.ID, low, high, item.Quality, 0} { 102 body = binary.BigEndian.AppendUint32(body, value) 103 } 104 } 105 for _, value := range []uint32{51050, corpse.id, 0, 0} { 106 body = binary.BigEndian.AppendUint32(body, value) 107 } 108 if open { 109 binary.BigEndian.PutUint32(body[len(body)-4:], 1) 110 } 111 return n3Packet(0x4e536976, 50000, owner, body) 112 } 113 114 func lootClosePacket(owner, corpse uint32) []byte { 115 var body []byte 116 for _, value := range []uint32{1, 102, 50000, owner} { 117 body = binary.BigEndian.AppendUint32(body, value) 118 } 119 return n3Packet(0x2049527c, 51050, corpse, body) 120 } 121 122 func (server *server) canLoot(client *worldClient, corpse *character) bool { 123 if corpse == nil || corpse.corpse == nil || corpse.health != 0 || !client.npcs[corpse.id] || 124 corpse.playfield != server.characters[client.id].playfield { 125 return false 126 } 127 if corpse.corpse.team != 0 { 128 if team := server.characters[client.id].team; team == nil || team.id != corpse.corpse.team { 129 return false 130 } 131 if corpse.corpse.lootOrder != 0 && corpse.corpse.looter != client.id { 132 return false 133 } 134 } else if corpse.corpse.looter != client.id { 135 return false 136 } 137 if !corpse.corpse.expiresAt.IsZero() && !time.Now().Before(corpse.corpse.expiresAt) { 138 return false 139 } 140 position := predictedPosition(server.characters[client.id], time.Now()) 141 x, y, z := position[0]-corpse.position[0], position[1]-corpse.position[1], position[2]-corpse.position[2] 142 // kkr.dll sub_3604D350 checks five metres for world item use. 143 return x*x+y*y+z*z <= 5*5 144 } 145 146 func (server *server) handleLootUse(client *worldClient, body []byte) error { 147 if len(body) != 48 { 148 return nil 149 } 150 player := server.characters[client.id] 151 if server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 152 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 153 binary.BigEndian.Uint32(body[16:20]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 3 || 154 binary.BigEndian.Uint32(body[28:32]) != 1 || binary.BigEndian.Uint32(body[32:36]) != 50000 || 155 binary.BigEndian.Uint32(body[36:40]) != client.id || binary.BigEndian.Uint32(body[40:44]) != 51050 { 156 return nil 157 } 158 sequence := binary.BigEndian.Uint32(body[20:24]) 159 if sequence <= client.useSequence { 160 return nil 161 } 162 client.useSequence = sequence 163 corpse := server.characters[binary.BigEndian.Uint32(body[44:48])] 164 reply := append([]byte(nil), body[16:]...) 165 binary.BigEndian.PutUint32(reply, 2) 166 if client.trade != nil || !server.canLoot(client, corpse) { 167 return client.send(n3Packet(0x52526858, 50000, client.id, reply)) 168 } 169 170 // Corpse::Use credits cash after the native success callback, including on close. 171 if corpse.corpse.credits != 0 { 172 credits := corpse.corpse.credits 173 if credits > 100000 { 174 credits = 100 175 } 176 inventory := player.inventory 177 // Mirror the native Cash setter: reject the whole addition above one billion. 178 if credits <= 1000000000-inventory.Credits { 179 inventory.Credits += credits 180 } 181 previous := corpse.corpse.credits 182 corpse.corpse.credits = 0 183 if err := server.saveInventory(player, inventory); err != nil { 184 corpse.corpse.credits = previous 185 return err 186 } 187 188 // The owner must retain the amount until its deferred Use consumes it locally. 189 body := binary.BigEndian.AppendUint32(nil, 61) 190 body = binary.BigEndian.AppendUint32(body, 0) 191 updates := make(map[*worldClient][][]byte) 192 server.queueCombat(updates, corpse, corpse, n3Packet(0x2b333d6e, 51050, corpse.id, body)) 193 delete(updates, client) 194 server.sendCombatUpdates(updates) 195 } 196 197 // Native Use toggles the same corpse when its window closes. 198 open := client.lootCorpse != corpse.id 199 if open && client.lootCorpse != 0 { 200 // Page 107 requests have no corpse identity; retire the previous window. 201 if err := client.send(lootClosePacket(client.id, client.lootCorpse)); err != nil { 202 return err 203 } 204 } 205 client.lootCorpse = 0 206 if open { 207 client.lootCorpse = corpse.id 208 } 209 binary.BigEndian.PutUint32(reply, 1) 210 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 211 return err 212 } 213 if open { 214 return client.send(lootInventoryPacket(client.id, corpse, true)) 215 } 216 // ActionIIR closes the native Chest and unregisters its container window. 217 return client.send(lootClosePacket(client.id, corpse.id)) 218 } 219 220 func (server *server) handleLootClose(client *worldClient, body []byte) error { 221 if len(body) != 36 { 222 return nil 223 } 224 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || 225 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 226 binary.BigEndian.Uint32(body[16:20]) != 142 || binary.BigEndian.Uint64(body[20:28]) != 0 || 227 binary.BigEndian.Uint32(body[28:32]) != 51050 || binary.BigEndian.Uint32(body[32:36]) != client.lootCorpse { 228 return nil 229 } 230 // The native client already closed this window after leaving Use range. 231 client.lootCorpse = 0 232 return nil 233 } 234 235 func (server *server) takeLoot(client *worldClient, body []byte) error { 236 corpse := server.characters[client.lootCorpse] 237 if client.lootCorpse == 0 || binary.BigEndian.Uint32(body[24:28]) != 50000 || 238 binary.BigEndian.Uint32(body[28:32]) != client.id || binary.BigEndian.Uint32(body[32:36]) != 111 { 239 return nil 240 } 241 if !server.canLoot(client, corpse) { 242 // Cancel the pending move before discarding an unusable native source. 243 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, binary.BigEndian.Uint32(body[20:24])})); err != nil { 244 return err 245 } 246 id := client.lootCorpse 247 client.lootCorpse = 0 248 return client.send(lootClosePacket(client.id, id)) 249 } 250 index := slices.IndexFunc(corpse.corpse.items, func(item ownedItem) bool { return item.Slot == binary.BigEndian.Uint32(body[20:24]) }) 251 if index < 0 { 252 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, binary.BigEndian.Uint32(body[20:24])})); err != nil { 253 return err 254 } 255 if err := client.send(characterActionPacket(client.id, [5]uint32{112, 107, binary.BigEndian.Uint32(body[20:24])})); err != nil { 256 return err 257 } 258 return client.send(lootInventoryPacket(client.id, corpse, false)) 259 } 260 player := server.characters[client.id] 261 item := corpse.corpse.items[index] 262 if itemStat(item.templateDefinition(), 0)&0x08000000 != 0 && player.inventory.carriesUnique(item) { 263 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, item.Slot})); err != nil { 264 return err 265 } 266 if err := client.send(lootInventoryPacket(client.id, corpse, false)); err != nil { 267 return err 268 } 269 text := "You already have this unique item..." 270 reply := binary.BigEndian.AppendUint32(nil, uint32(len(text))) 271 reply = append(reply, text...) 272 reply = append(reply, 0, 0) 273 return client.send(n3Packet(0x5f4b442a, 50000, client.id, reply)) 274 } 275 var slot uint32 276 for slot = 64; slot < 94; slot++ { 277 if !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) { 278 break 279 } 280 } 281 if slot == 94 || client.equipment != nil { 282 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, item.Slot})); err != nil { 283 return err 284 } 285 return client.send(lootInventoryPacket(client.id, corpse, false)) 286 } 287 288 // Persist the new owner before removing the corpse item or publishing its move. 289 item.Slot = slot 290 inventory := player.inventory 291 inventory.LastID = max(player.inventory.LastID, server.nextItem, item.ID) 292 inventory.Items = append(slices.Clone(player.inventory.Items), item) 293 if item.isPortableContainer() { 294 contents, err := referencedItems(server.items[item.ID].Slots, 0, 21, server.items) 295 if err != nil { 296 return err 297 } 298 if len(contents) != 0 { 299 inventory.Containers = maps.Clone(player.inventory.Containers) 300 if inventory.Containers == nil { 301 inventory.Containers = make(map[uint32][]ownedItem) 302 } 303 inventory.Containers[item.ID] = contents 304 } 305 } 306 previous := corpse.corpse.items 307 corpse.corpse.items = slices.Delete(slices.Clone(previous), index, index+1) 308 if err := server.saveInventory(player, inventory); err != nil { 309 corpse.corpse.items = previous 310 return err 311 } 312 server.nextItem = max(server.nextItem, item.ID) 313 // Other eligible open windows must lose the same icon after the commit. 314 for _, other := range server.clients { 315 if other != client && other.ready && !other.loading && other.lootCorpse == corpse.id && server.canLoot(other, corpse) { 316 // Contents updates do not remove absent rows from an already open Chest. 317 if err := other.send(characterActionPacket(other.id, [5]uint32{112, 107, binary.BigEndian.Uint32(body[20:24])})); err != nil { 318 other.disconnect(other.conn) 319 continue 320 } 321 if err := other.send(lootInventoryPacket(other.id, corpse, false)); err != nil { 322 other.disconnect(other.conn) 323 } 324 } 325 } 326 reply := append([]byte(nil), body[16:]...) 327 binary.BigEndian.PutUint32(reply[16:20], slot) 328 return client.send(n3Packet(0x47537a24, 50000, client.id, reply)) 329 }