server/backpacks.go (20672B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "maps" 7 "slices" 8 "time" 9 ) 10 11 type backpackHandle struct { 12 id uint32 13 open bool 14 } 15 16 func (item ownedItem) isPortableContainer() bool { 17 definition := gamedata.ItemTemplates[item.Template] 18 // Owned Chest templates must support both Carry and Use; world chests do not. 19 return item.Type == 51017 && definition != nil && definition.HeaderWords[0] == 51017 && 20 itemStat(definition, 30)&9 == 9 21 } 22 23 func backpackPacket(owner, playfield uint32, item ownedItem, page uint32) []byte { 24 // ChestFullUpdate inherits SimpleItem 11 and Lockable 2, ending in Chest 3. 25 var body []byte 26 for _, value := range []uint32{11, 50000, owner, playfield, 0, 0} { 27 body = binary.BigEndian.AppendUint32(body, value) 28 } 29 body = append(body, byte(page), byte(item.Slot)) 30 body = binary.BigEndian.AppendUint32(body, 5*1009) 31 low, high := item.templateIDs() 32 for _, value := range []uint32{23, item.Template, 701, item.Quality, 702, low, 703, high, 0, 2, 0, 1009, 3} { 33 body = binary.BigEndian.AppendUint32(body, value) 34 } 35 return n3Packet(0x465a5d73, 51017, item.ID, body) 36 } 37 38 func backpackInventoryPacket(owner, bag, index uint32, items []ownedItem) []byte { 39 body := binary.BigEndian.AppendUint32(nil, uint32(len(items)+1)*1009) 40 for _, item := range items { 41 low, high := item.templateIDs() 42 for _, value := range []uint32{item.Slot, 0x00030000 | uint32(item.Count), item.Type, item.ID, low, high, item.Quality, 0} { 43 body = binary.BigEndian.AppendUint32(body, value) 44 } 45 } 46 for _, value := range []uint32{51017, bag, index, 1} { 47 body = binary.BigEndian.AppendUint32(body, value) 48 } 49 return n3Packet(0x4e536976, 50000, owner, body) 50 } 51 52 func backpackClosePacket(owner, bag uint32) []byte { 53 var body []byte 54 for _, value := range []uint32{1, 102, 50000, owner} { 55 body = binary.BigEndian.AppendUint32(body, value) 56 } 57 return n3Packet(0x2049527c, 51017, bag, body) 58 } 59 60 func (server *server) handleOwnedItemUse(client *worldClient, body []byte) error { 61 player := server.characters[client.id] 62 page := binary.BigEndian.Uint32(body[40:44]) 63 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 64 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 65 binary.BigEndian.Uint32(body[16:20]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 3 || binary.BigEndian.Uint32(body[28:32]) > 1 || 66 binary.BigEndian.Uint32(body[32:36]) != 50000 || binary.BigEndian.Uint32(body[36:40]) != client.id || 67 (page != 104 && page != 101 && page != 102 && page != 107 && page != 51017) { 68 return nil 69 } 70 sequence := binary.BigEndian.Uint32(body[20:24]) 71 if sequence <= client.useSequence { 72 return nil 73 } 74 client.useSequence = sequence 75 if client.trade != nil || client.equipment != nil { 76 reply := append([]byte(nil), body[16:]...) 77 binary.BigEndian.PutUint32(reply, 2) 78 return client.send(n3Packet(0x52526858, 50000, client.id, reply)) 79 } 80 if page == 107 { 81 // Known carried contents remain usable after their bag window closes. 82 slot := binary.BigEndian.Uint32(body[44:48]) 83 bag := client.backpacks[slot>>16].id 84 if bag == 0 || slot&65535 >= 21 || !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.ID == bag && item.isPortableContainer() }) { 85 return nil 86 } 87 for index, item := range player.inventory.Containers[bag] { 88 if item.Slot == slot&65535 { 89 if item.Template == 117610 { 90 return server.useSnakeJab(client, item, body) 91 } 92 if item.Template == 43328 { 93 return server.useEngineerShell(client, index, body) 94 } 95 if item.Template == 43552 { 96 return server.useClinic(client, index, body) 97 } 98 if stat, supported := item.tokenUpload(); supported { 99 return server.uploadToken(client, index, body, stat) 100 } 101 if flags, supported := item.mapUpload(); supported { 102 return server.uploadMap(client, index, body, flags) 103 } 104 if kit, supported := item.resourceKit(); supported { 105 return server.useResourceKit(client, index, body, kit) 106 } 107 if item.Template == 43330 || nanoCrystalProgram(item.Template) != 0 { 108 return server.learnNano(client, index, body) 109 } 110 return server.useRemoteBank(client, item, body) 111 } 112 } 113 // Complete the old command before clearing only its empty cached child row. 114 reply := append([]byte(nil), body[16:]...) 115 binary.BigEndian.PutUint32(reply, 2) 116 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 117 return err 118 } 119 return client.send(characterActionPacket(client.id, [5]uint32{112, 107, slot})) 120 } 121 if page == 104 || page == 101 { 122 index := slices.IndexFunc(player.inventory.Items, func(item ownedItem) bool { 123 return item.Slot == binary.BigEndian.Uint32(body[44:48]) && 124 (page == 104 && item.Slot >= 64 && item.Slot < 94 || 125 page == 101 && item.Slot == 6 && item.isWieldableWeapon()) 126 }) 127 if index >= 0 { 128 switch player.inventory.Items[index].Template { 129 case 117610: 130 return server.useSnakeJab(client, player.inventory.Items[index], body) 131 case 205736: 132 return server.useRemoteBank(client, player.inventory.Items[index], body) 133 case 43328: 134 return server.useEngineerShell(client, index, body) 135 case 43330, 28778: 136 return server.learnNano(client, index, body) 137 case 43552: 138 return server.useClinic(client, index, body) 139 default: 140 if stat, supported := player.inventory.Items[index].tokenUpload(); supported { 141 return server.uploadToken(client, index, body, stat) 142 } 143 if flags, supported := player.inventory.Items[index].mapUpload(); supported { 144 return server.uploadMap(client, index, body, flags) 145 } 146 if kit, supported := player.inventory.Items[index].resourceKit(); supported { 147 return server.useResourceKit(client, index, body, kit) 148 } 149 if nanoCrystalProgram(player.inventory.Items[index].Template) != 0 { 150 return server.learnNano(client, index, body) 151 } 152 } 153 } 154 } 155 // Unsupported worn use still needs a reply for its actual owned source. 156 slot := binary.BigEndian.Uint32(body[44:48]) 157 if page == 101 && (slot == 0 || slot >= 16 || !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { 158 return item.Slot == slot && item.fitsEquipmentSlot(slot) 159 })) { 160 return nil 161 } 162 if page == 104 && (slot < 64 || slot >= 94) { 163 return nil 164 } 165 if page == 102 && (slot < 16 || slot >= 32) { 166 return nil 167 } 168 item := slices.IndexFunc(player.inventory.Items, func(item ownedItem) bool { 169 return item.isPortableContainer() && ((page == 104 || page == 102) && item.Slot == slot || page == 51017 && item.ID == slot) 170 }) 171 reply := append([]byte(nil), body[16:]...) 172 if item < 0 { 173 binary.BigEndian.PutUint32(reply, 2) 174 return client.send(n3Packet(0x52526858, 50000, client.id, reply)) 175 } 176 bag := player.inventory.Items[item] 177 binary.BigEndian.PutUint32(reply, 1) 178 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil { 179 return err 180 } 181 for index, handle := range client.backpacks { 182 if handle.id == bag.ID { 183 client.backpacks[index] = backpackHandle{bag.ID, !handle.open} 184 if handle.open { 185 return client.send(backpackClosePacket(client.id, bag.ID)) 186 } 187 return client.send(backpackInventoryPacket(client.id, bag.ID, index, player.inventory.Containers[bag.ID])) 188 } 189 } 190 191 // Page 107 packs this session's container handle above its contents slot. 192 if client.backpacks == nil { 193 client.backpacks = make(map[uint32]backpackHandle) 194 } 195 index := uint32(1) 196 for client.backpacks[index].id != 0 { 197 index++ 198 } 199 client.backpacks[index] = backpackHandle{bag.ID, true} 200 parentPage := uint32(104) 201 if bag.Slot < 64 { 202 parentPage = 102 203 } 204 if err := client.send(backpackPacket(client.id, player.playfield, bag, parentPage)); err != nil { 205 return err 206 } 207 return client.send(backpackInventoryPacket(client.id, bag.ID, index, player.inventory.Containers[bag.ID])) 208 } 209 210 func (server *server) moveContainerItem(client *worldClient, body []byte) error { 211 if !client.target { 212 return nil 213 } 214 player := server.characters[client.id] 215 inventory := player.inventory 216 sourcePage, sourceSlot := binary.BigEndian.Uint32(body[16:20]), binary.BigEndian.Uint32(body[20:24]) 217 sourceBag := uint32(0) 218 items := inventory.Items 219 bank := sourcePage == 105 || binary.BigEndian.Uint32(body[24:28]) == 57005 220 // Refuse unavailable transfers and busy withdrawals without losing their source. 221 if bank && (!server.bankAvailable(client) || sourcePage == 105 && client.equipment != nil) { 222 return client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, sourceSlot})) 223 } 224 if sourcePage == 105 { 225 items = inventory.Bank 226 } else if sourcePage == 107 { 227 sourceBag = client.backpacks[sourceSlot>>16].id 228 if sourceBag == 0 || !slices.ContainsFunc(inventory.Items, func(item ownedItem) bool { return item.ID == sourceBag && item.isPortableContainer() }) { 229 return nil 230 } 231 sourceSlot &= 65535 232 items = inventory.Containers[sourceBag] 233 } else if sourcePage == 101 && sourceSlot > 0 && sourceSlot < 16 || 234 sourcePage == 102 && sourceSlot >= 16 && sourceSlot < 32 || sourcePage == 103 && sourceSlot >= 33 && sourceSlot <= 45 { 235 // Direct worn deposits are unsupported; restore their validated native source. 236 if client.equipment != nil && (sourceSlot == client.equipment.source || sourceSlot == client.equipment.destination) { 237 return nil 238 } 239 if !slices.ContainsFunc(items, func(item ownedItem) bool { return item.Slot == sourceSlot }) { 240 if sourceSlot == 8 { 241 return nil 242 } 243 targetType, targetID := binary.BigEndian.Uint32(body[24:28]), binary.BigEndian.Uint32(body[28:32]) 244 validTarget := targetType == 57005 && targetID == client.id 245 if targetType == 51017 && slices.ContainsFunc(items, func(item ownedItem) bool { return item.ID == targetID && item.isPortableContainer() }) { 246 for _, handle := range client.backpacks { 247 validTarget = validTarget || handle.id == targetID 248 } 249 } 250 if !validTarget { 251 return nil 252 } 253 return server.clearStaleWorn(client, sourcePage, sourceSlot) 254 } 255 return client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, sourceSlot})) 256 } else if sourcePage != 104 || sourceSlot < 64 || sourceSlot >= 94 { 257 return nil 258 } 259 index := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == sourceSlot }) 260 if index < 0 { 261 // Cancel a stale carried deposit before clearing only its empty source row. 262 if sourcePage == 104 && (client.equipment == nil || sourceSlot != client.equipment.source && sourceSlot != client.equipment.destination) { 263 for _, action := range []uint32{146, 112} { 264 if err := client.send(characterActionPacket(client.id, [5]uint32{action, 104, sourceSlot})); err != nil { 265 return err 266 } 267 } 268 return nil 269 } 270 // Stale container windows hide their picked source until cancellation. 271 if sourcePage == 105 && sourceSlot < bankCapacity { 272 if err := client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, sourceSlot})); err != nil { 273 return err 274 } 275 return client.send(bankInventoryPacket(player)) 276 } 277 if sourceBag != 0 && sourceSlot < 21 { 278 position := binary.BigEndian.Uint32(body[20:24]) 279 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, position})); err != nil { 280 return err 281 } 282 if client.backpacks[position>>16].open { 283 return client.send(backpackInventoryPacket(client.id, sourceBag, position>>16, items)) 284 } 285 } 286 return nil 287 } 288 item := items[index] 289 // A validated source needs correction even when its destination is refused. 290 refuse := func() error { 291 if sourcePage == 104 && !bank { 292 for _, packet := range equipmentCorrectionPackets(client.id, sourceSlot) { 293 if err := client.send(packet); err != nil { 294 return err 295 } 296 } 297 return nil 298 } 299 return client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, binary.BigEndian.Uint32(body[20:24])})) 300 } 301 if sourceBag != 0 && !client.backpacks[binary.BigEndian.Uint32(body[20:24])>>16].open { 302 return refuse() 303 } 304 if client.equipment != nil { 305 if sourceBag != 0 { 306 if sourceBag == client.equipment.sourceBag && item.ID == client.equipment.itemID { 307 return nil 308 } 309 return client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, binary.BigEndian.Uint32(body[20:24])})) 310 } 311 // Keep the admitted source pending and restore a separate carried icon. 312 if item.ID != client.equipment.itemID { 313 for _, packet := range equipmentCorrectionPackets(client.id, sourceSlot) { 314 if err := client.send(packet); err != nil { 315 return err 316 } 317 } 318 } 319 return nil 320 } 321 if bank && itemStat(item.templateDefinition(), 8) > 0 { 322 return client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, binary.BigEndian.Uint32(body[20:24])})) 323 } 324 if sourcePage == 105 && itemStat(item.templateDefinition(), 0)&0x08000000 != 0 && inventory.carriesUnique(item) { 325 if err := client.send(characterActionPacket(client.id, [5]uint32{146, sourcePage, sourceSlot})); err != nil { 326 return err 327 } 328 text := "You already have this unique item..." 329 reply := binary.BigEndian.AppendUint32(nil, uint32(len(text))) 330 reply = append(reply, text...) 331 reply = append(reply, 0, 0) 332 return client.send(n3Packet(0x5f4b442a, 50000, client.id, reply)) 333 } 334 335 // Validate the owner and placement before changing either side of the transfer. 336 targetType, targetID, slot := binary.BigEndian.Uint32(body[24:28]), binary.BigEndian.Uint32(body[28:32]), binary.BigEndian.Uint32(body[32:36]) 337 if sourceBag != 0 && targetType == 50000 && targetID == client.id && slot < 64 { 338 // Use the existing wear contract and delay; occupied-container swaps remain unsupported. 339 equipping := item 340 equipping.Slot = binary.BigEndian.Uint32(body[20:24]) 341 now := time.Now() 342 if slices.ContainsFunc(inventory.Items, func(other ownedItem) bool { return other.Slot == slot }) || 343 !player.canMoveEquipment(equipping, slot, now) || server.equipmentBlocked(player, item, now) { 344 return refuse() 345 } 346 client.equipment = &equipmentMove{itemID: item.ID, source: equipping.Slot, destination: slot, requestedSlot: slot, 347 sourceBag: sourceBag, readyAt: now.Add(item.equipDelay())} 348 return nil 349 } 350 targetBag := uint32(0) 351 targetItems := inventory.Items 352 first, end := uint32(64), uint32(94) 353 if targetType == 57005 { 354 if targetID != client.id || sourcePage == 105 { 355 return refuse() 356 } 357 targetItems, first, end, slot = inventory.Bank, 0, bankCapacity, 111 358 } else if targetType == 51017 { 359 if targetID == sourceBag || !slices.ContainsFunc(inventory.Items, func(item ownedItem) bool { return item.ID == targetID && item.isPortableContainer() }) { 360 return refuse() 361 } 362 known := false 363 for _, handle := range client.backpacks { 364 known = known || handle.id == targetID 365 } 366 if !known || item.Type == 51017 || itemStat(gamedata.ItemTemplates[item.Template], 0)&0x00200000 != 0 || itemStat(gamedata.ItemTemplates[item.Template], 8) > 0 { 367 return refuse() 368 } 369 targetBag, targetItems, first, end = targetID, inventory.Containers[targetID], 0, 21 370 if itemStat(gamedata.ItemTemplates[item.Template], 0)&0x08000000 != 0 && 371 slices.ContainsFunc(targetItems, func(other ownedItem) bool { return other.Template == item.Template }) { 372 return refuse() 373 } 374 // The native Chest receiver always chooses its first free contents slot. 375 slot = 111 376 } else if targetType != 50000 || targetID != client.id || sourceBag == 0 && sourcePage != 105 { 377 return refuse() 378 } 379 if sourcePage == 105 && targetType == 50000 { 380 // The native bank withdrawal receiver ignores the requested slot. 381 slot = 111 382 } 383 if slot == 111 { 384 for slot = first; slot < end; slot++ { 385 if !slices.ContainsFunc(targetItems, func(item ownedItem) bool { return item.Slot == slot }) { 386 break 387 } 388 } 389 } 390 if slot < first || slot >= end || slices.ContainsFunc(targetItems, func(item ownedItem) bool { return item.Slot == slot }) { 391 return refuse() 392 } 393 394 // Commit ownership, quantity and both container locations atomically. 395 inventory.Containers = maps.Clone(inventory.Containers) 396 if inventory.Containers == nil { 397 inventory.Containers = make(map[uint32][]ownedItem) 398 } 399 remaining := slices.Delete(slices.Clone(items), index, index+1) 400 if sourcePage == 105 { 401 inventory.Bank = remaining 402 } else if sourceBag == 0 { 403 inventory.Items = remaining 404 } else if len(remaining) == 0 { 405 delete(inventory.Containers, sourceBag) 406 } else { 407 inventory.Containers[sourceBag] = remaining 408 } 409 item.Slot = slot 410 targetItems = append(slices.Clone(targetItems), item) 411 if targetType == 57005 { 412 inventory.Bank = targetItems 413 } else if targetBag == 0 { 414 inventory.Items = targetItems 415 } else { 416 inventory.Containers[targetBag] = targetItems 417 } 418 if len(inventory.Containers) == 0 { 419 inventory.Containers = nil 420 } 421 422 // Transfer only this instance; the database verifies its expected source. 423 source, destination := itemPlacement{Slot: sourceSlot}, itemPlacement{Slot: slot} 424 if sourcePage == 105 { 425 source.Bank = player.id 426 } else if sourceBag != 0 { 427 source.Backpack = sourceBag 428 } else { 429 source.Player = player.id 430 } 431 if targetType == 57005 { 432 destination.Bank = player.id 433 } else if targetBag != 0 { 434 destination.Backpack = targetBag 435 } else { 436 destination.Player = player.id 437 } 438 if err := server.moveInventoryItem(player, inventory, item.ID, source, destination); err != nil { 439 return err 440 } 441 reply := append([]byte(nil), body[16:]...) 442 binary.BigEndian.PutUint32(reply[16:20], slot) 443 if err := client.send(n3Packet(0x47537a24, 50000, client.id, reply)); err != nil { 444 return err 445 } 446 if targetType == 57005 && item.isPortableContainer() { 447 for handle, bag := range client.backpacks { 448 if bag.id == item.ID && bag.open { 449 client.backpacks[handle] = backpackHandle{bag.id, false} 450 if err := client.send(backpackClosePacket(client.id, bag.id)); err != nil { 451 return err 452 } 453 } 454 } 455 } 456 return nil 457 } 458 459 func (server *server) handleBackpackAction(client *worldClient, body []byte) error { 460 position := binary.BigEndian.Uint32(body[24:28]) 461 handle := client.backpacks[position>>16] 462 player := server.characters[client.id] 463 if !client.target || !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.ID == handle.id && item.isPortableContainer() }) { 464 return nil 465 } 466 action := binary.BigEndian.Uint32(body[16:20]) 467 if !handle.open { 468 // An optimistic split needs its unavailable native window closed. 469 if action == 52 && position&65535 < 21 { 470 return client.send(backpackClosePacket(client.id, handle.id)) 471 } 472 return nil 473 } 474 items := player.inventory.Containers[handle.id] 475 index := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == position&65535 }) 476 if action == 52 && position&65535 < 21 && (client.equipment != nil || index < 0 || !items[index].isStackable()) { 477 // Contents alone cannot undo these refused optimistic splits. 478 client.backpacks[position>>16] = backpackHandle{handle.id, false} 479 return client.send(backpackClosePacket(client.id, handle.id)) 480 } 481 if client.equipment != nil { 482 if action == 52 { 483 return server.syncStackPage(client, 107, handle.id) 484 } 485 return nil 486 } 487 if action == 53 && binary.BigEndian.Uint32(body[28:32]) == 107 { 488 // A missed merge refresh can leave either side in the open bag's old view. 489 otherPosition := binary.BigEndian.Uint32(body[32:36]) 490 if position&65535 < 21 && otherPosition&65535 < 21 && otherPosition>>16 == position>>16 { 491 other := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == otherPosition&65535 }) 492 if index < 0 && other >= 0 && items[other].isStackable() || other < 0 && index >= 0 && items[index].isStackable() { 493 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 107, otherPosition})); err != nil { 494 return err 495 } 496 return server.syncStackPage(client, 107, handle.id) 497 } 498 } 499 } 500 if index < 0 { 501 if action == 52 { 502 return server.syncStackPage(client, 107, handle.id) 503 } 504 return nil 505 } 506 if action == 52 || action == 53 { 507 return server.changeStack(client, index, body) 508 } 509 if action != 112 || !items[index].acceptsDeleteTemplates(binary.BigEndian.Uint32(body[28:32]), binary.BigEndian.Uint32(body[32:36])) { 510 return nil 511 } 512 inventory := player.inventory 513 inventory.Containers = maps.Clone(inventory.Containers) 514 inventory.Containers[handle.id] = slices.Delete(slices.Clone(items), index, index+1) 515 if len(inventory.Containers[handle.id]) == 0 { 516 delete(inventory.Containers, handle.id) 517 } 518 if len(inventory.Containers) == 0 { 519 inventory.Containers = nil 520 } 521 if err := server.saveInventory(player, inventory); err != nil { 522 return err 523 } 524 return client.sendItemDeletion(body, []ownedItem{items[index]}) 525 }