aorecomp

Anarchy Online classic in the browser

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


server/inventory.go (22045B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"maps"
      8 	"slices"
      9 	"time"
     10 )
     11 
     12 type ownedItem struct {
     13 	ID       uint32
     14 	Type     uint32
     15 	Template uint32
     16 	Quality  uint32
     17 	Slot     uint32
     18 	Count    uint16
     19 	Ammo     uint32
     20 }
     21 
     22 type reclaimItem struct {
     23 	Item        ownedItem
     24 	AvailableAt time.Time
     25 }
     26 
     27 type playerInventory struct {
     28 	LastID       uint32
     29 	Credits      uint32
     30 	Items        []ownedItem
     31 	Bank         []ownedItem
     32 	Reclaim      []reclaimItem
     33 	Containers   map[uint32][]ownedItem
     34 	LearnedNanos []uint32
     35 	// Native stats 470..472: display options followed by the two area bit banks.
     36 	MapFlags   [3]uint32
     37 	ClanTokens uint32
     38 	OmniTokens uint32
     39 }
     40 
     41 func (inventory playerInventory) validate() error {
     42 	if inventory.ClanTokens > 2147483647 || inventory.OmniTokens > 2147483647 {
     43 		return fmt.Errorf("token count exceeds the native signed stat range")
     44 	}
     45 	if inventory.Credits > 1000000000 {
     46 		return fmt.Errorf("credit balance exceeds the native limit")
     47 	}
     48 	learned := make(map[uint32]bool)
     49 	for _, id := range inventory.LearnedNanos {
     50 		if gamedata.NanoTemplates[id] == nil || learned[id] {
     51 			return fmt.Errorf("invalid or duplicate learned nano program")
     52 		}
     53 		learned[id] = true
     54 	}
     55 	ids := make(map[uint32]bool)
     56 	slots := make(map[uint32]bool)
     57 	bags := make(map[uint32]bool)
     58 	for _, item := range inventory.Items {
     59 		if item.ID == 0 || item.ID > inventory.LastID || ids[item.ID] || item.Count == 0 || !item.validAmmo() ||
     60 			(item.Count != 1 && !item.isStackable()) ||
     61 			(item.Slot < 64 && !item.fitsEquipmentSlot(item.Slot)) ||
     62 			item.Slot >= 94 || slots[item.Slot] {
     63 			return fmt.Errorf("invalid inventory item identity or slot")
     64 		}
     65 		ids[item.ID], slots[item.Slot] = true, true
     66 		if item.isPortableContainer() {
     67 			bags[item.ID] = true
     68 		}
     69 	}
     70 	deckSlots := inventory.equipmentBonus(45)
     71 	for slot := uint32(9); slot <= 14; slot++ {
     72 		if slots[slot] && int32(slot-9) >= deckSlots {
     73 			return fmt.Errorf("equipment in a disabled deck slot")
     74 		}
     75 	}
     76 	bankSlots := make(map[uint32]bool)
     77 	for _, item := range inventory.Bank {
     78 		if item.ID == 0 || item.ID > inventory.LastID || ids[item.ID] || item.Count == 0 || !item.validAmmo() ||
     79 			(item.Count != 1 && !item.isStackable()) || item.Slot >= bankCapacity || bankSlots[item.Slot] {
     80 			return fmt.Errorf("invalid bank item identity or slot")
     81 		}
     82 		ids[item.ID], bankSlots[item.Slot] = true, true
     83 		if item.isPortableContainer() {
     84 			bags[item.ID] = true
     85 		}
     86 	}
     87 	for _, entry := range inventory.Reclaim {
     88 		if entry.Item.ID == 0 || entry.Item.ID > inventory.LastID || ids[entry.Item.ID] || entry.Item.Count == 0 || !entry.Item.validAmmo() ||
     89 			(entry.Item.Count != 1 && !entry.Item.isStackable()) || entry.Item.Slot < 64 || entry.Item.Slot >= 94 {
     90 			return fmt.Errorf("invalid reclaim item identity or slot")
     91 		}
     92 		ids[entry.Item.ID] = true
     93 		if entry.Item.isPortableContainer() {
     94 			bags[entry.Item.ID] = true
     95 		}
     96 	}
     97 	for bag, items := range inventory.Containers {
     98 		if !bags[bag] {
     99 			return fmt.Errorf("contents without an owned backpack")
    100 		}
    101 		var occupied [21]bool
    102 		for _, item := range items {
    103 			if item.ID == 0 || item.ID > inventory.LastID || ids[item.ID] || item.Type == 51017 ||
    104 				item.Count == 0 || !item.validAmmo() || (item.Count != 1 && !item.isStackable()) || item.Slot >= 21 || occupied[item.Slot] {
    105 				return fmt.Errorf("invalid backpack item identity or slot")
    106 			}
    107 			ids[item.ID], occupied[item.Slot] = true, true
    108 		}
    109 	}
    110 	return nil
    111 }
    112 
    113 func (server *server) saveInventory(character *character, inventory playerInventory) error {
    114 	if err := inventory.validate(); err != nil {
    115 		return err
    116 	}
    117 	store, err := server.itemStore(character, inventory)
    118 	if err != nil {
    119 		return err
    120 	}
    121 	healthMaximum, nanoMaximum := character.maximumHealth(), character.maximumNano()
    122 	if err := server.writeItemStore(store); err != nil {
    123 		return err
    124 	}
    125 	previous := *character
    126 	character.inventory = inventory
    127 	if character.body.mesh != 0 {
    128 		previous.collisionBody()
    129 		character.body = previous.body
    130 	}
    131 	// Apply only committed wear transitions, including their ordered pose changes.
    132 	for _, item := range previous.inventory.Items {
    133 		if !slices.ContainsFunc(inventory.Items, func(other ownedItem) bool { return other.ID == item.ID && other.Slot == item.Slot }) {
    134 			character.wearEffects(item, true)
    135 		}
    136 	}
    137 	for _, item := range inventory.Items {
    138 		if !slices.ContainsFunc(previous.inventory.Items, func(other ownedItem) bool { return other.ID == item.ID && other.Slot == item.Slot }) {
    139 			character.wearEffects(item, false)
    140 		}
    141 	}
    142 	if character.maximumHealth() < healthMaximum {
    143 		character.health = min(character.health, character.maximumHealth())
    144 	}
    145 	if character.maximumNano() < nanoMaximum {
    146 		character.nano = min(character.nano, character.maximumNano())
    147 	}
    148 	return nil
    149 }
    150 
    151 func (server *server) saveItemEffect(player, target *character, inventory playerInventory, vitals playerVitals, lock skillLockUpdate) error {
    152 	if err := inventory.validate(); err != nil {
    153 		return err
    154 	}
    155 	store, err := server.itemStore(player, inventory)
    156 	if err != nil {
    157 		return err
    158 	}
    159 	store.vitalUpdate = &playerVitalUpdate{target.id, vitals}
    160 	store.skillLock = &lock
    161 	if err := server.writeItemStore(store); err != nil {
    162 		return err
    163 	}
    164 
    165 	// Publish both participants only after the debit, lock and resource gain commit.
    166 	player.inventory = inventory
    167 	target.applyVitals(vitals)
    168 	target.savedVitals = vitals
    169 	server.characters[lock.Player].setSkillLock(lock.Skill, lock.Until)
    170 	return nil
    171 }
    172 
    173 func (server *server) grantItem(character *character, template uint32) (bool, error) {
    174 	// Start with exact catalog qualities; arbitrary QL interpolation is separate.
    175 	definition := gamedata.ItemTemplates[template]
    176 	if definition == nil || definition.Quality == nil {
    177 		return false, fmt.Errorf("unknown item template %d", template)
    178 	}
    179 	var slot uint32
    180 	for slot = 64; slot < 94; slot++ {
    181 		if !slices.ContainsFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) {
    182 			break
    183 		}
    184 	}
    185 	if slot == 94 {
    186 		return false, nil
    187 	}
    188 	id, err := server.reserveItems(1)
    189 	if err != nil {
    190 		return false, err
    191 	}
    192 
    193 	// Publish the new identity only after the complete inventory reaches disk.
    194 	inventory := character.inventory
    195 	inventory.LastID = id
    196 	inventory.Items = slices.Clone(character.inventory.Items)
    197 	item := ownedItem{ID: inventory.LastID, Type: definition.HeaderWords[0], Template: template, Quality: *definition.Quality, Slot: slot, Count: 1}
    198 	if item.isStackable() {
    199 		item.Count = uint16(itemStat(definition, 212))
    200 	}
    201 	inventory.Items = append(inventory.Items, item)
    202 	if err := server.saveInventory(character, inventory); err != nil {
    203 		return false, err
    204 	}
    205 	server.nextItem = max(server.nextItem, inventory.LastID)
    206 	return true, nil
    207 }
    208 
    209 func (server *server) handleInventory(client *worldClient, body []byte) error {
    210 	key := binary.BigEndian.Uint32(body)
    211 	if len(body) != 36 {
    212 		return fmt.Errorf("invalid inventory message length")
    213 	}
    214 	if key == 0x47537a24 && binary.BigEndian.Uint32(body[16:20]) == 106 {
    215 		return server.takeReclaim(client, body)
    216 	}
    217 	character := server.characters[client.id]
    218 	if server.clients[client.id] != client || !client.ready || client.loading || character.health == 0 ||
    219 		binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id {
    220 		return nil
    221 	}
    222 	if key == 0x47537a24 && binary.BigEndian.Uint32(body[16:20]) == 51005 {
    223 		return server.takeWorldItem(client, body)
    224 	}
    225 	if client.trade != nil {
    226 		if err := server.abortPlayerTrade(client); err != nil {
    227 			return err
    228 		}
    229 		if key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 52 {
    230 			position := binary.BigEndian.Uint32(body[24:28])
    231 			if binary.BigEndian.Uint32(body[20:24]) == 107 && position&65535 < 21 {
    232 				handle := client.backpacks[position>>16]
    233 				if handle.id != 0 {
    234 					// Ordinary stack replay cannot remove an optimistic bag clone.
    235 					client.backpacks[position>>16] = backpackHandle{handle.id, false}
    236 					return client.send(backpackClosePacket(client.id, handle.id))
    237 				}
    238 			}
    239 			return server.syncStacks(client)
    240 		}
    241 		if key == 0x47537a24 || key == 0x3a243f41 {
    242 			return client.send(characterActionPacket(client.id, [5]uint32{146, binary.BigEndian.Uint32(body[16:20]), binary.BigEndian.Uint32(body[20:24])}))
    243 		}
    244 		return nil
    245 	}
    246 	if client.shop != nil {
    247 		if err := server.abortShop(client); err != nil {
    248 			return err
    249 		}
    250 	}
    251 	if key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 52 && binary.BigEndian.Uint32(body[20:24]) == 104 {
    252 		source := binary.BigEndian.Uint32(body[24:28])
    253 		index := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == source })
    254 		if source >= 64 && source < 94 && index >= 0 && !character.inventory.Items[index].isStackable() {
    255 			// A stale template split can name a slot now occupied by an individual item.
    256 			return server.syncStackSource(client, source)
    257 		}
    258 	}
    259 	if key == 0x3a243f41 {
    260 		// Ground storage is unsupported; restore only a validated owned source.
    261 		page, slot := binary.BigEndian.Uint32(body[16:20]), binary.BigEndian.Uint32(body[20:24])
    262 		var packets [][]byte
    263 		if page == 104 {
    264 			index := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == slot })
    265 			if slot < 64 || slot >= 94 || client.equipment != nil &&
    266 				(slot == client.equipment.source || slot == client.equipment.destination ||
    267 					index >= 0 && character.inventory.Items[index].ID == client.equipment.itemID) {
    268 				return nil
    269 			}
    270 			if index < 0 {
    271 				// Cancel the stale source before clearing its now-empty carried row.
    272 				packets = [][]byte{characterActionPacket(client.id, [5]uint32{146, 104, slot}),
    273 					characterActionPacket(client.id, [5]uint32{112, 104, slot})}
    274 			} else {
    275 				packets = equipmentCorrectionPackets(client.id, slot)
    276 			}
    277 		} else if page == 107 {
    278 			bag := client.backpacks[slot>>16].id
    279 			if bag == 0 || slot&65535 >= 21 ||
    280 				client.equipment != nil && client.equipment.sourceBag == bag && client.equipment.source == slot ||
    281 				!slices.ContainsFunc(character.inventory.Items, func(item ownedItem) bool { return item.ID == bag && item.isPortableContainer() }) {
    282 				return nil
    283 			}
    284 			packets = [][]byte{characterActionPacket(client.id, [5]uint32{146, page, slot})}
    285 			if client.backpacks[slot>>16].open &&
    286 				!slices.ContainsFunc(character.inventory.Containers[bag], func(item ownedItem) bool { return item.Slot == slot&65535 }) {
    287 				packets = append(packets, backpackInventoryPacket(client.id, bag, slot>>16, character.inventory.Containers[bag]))
    288 			}
    289 		} else {
    290 			return nil
    291 		}
    292 		for _, packet := range packets {
    293 			if err := client.send(packet); err != nil {
    294 				return err
    295 			}
    296 		}
    297 		text := "You are not able to drop this item."
    298 		feedback := binary.BigEndian.AppendUint32(nil, uint32(len(text)))
    299 		feedback = append(feedback, text...)
    300 		feedback = append(feedback, 0, 0)
    301 		return client.send(n3Packet(0x5f4b442a, 50000, client.id, feedback))
    302 	}
    303 	if key == 0x5e477770 && binary.BigEndian.Uint32(body[20:24]) == 107 && binary.BigEndian.Uint32(body[24:28])>>16 != 0 {
    304 		return server.handleBackpackAction(client, body)
    305 	}
    306 	if key == 0x5e477770 && binary.BigEndian.Uint32(body[20:24]) == 105 {
    307 		return server.handleBankAction(client, body)
    308 	}
    309 	if key == 0x47537a24 && (binary.BigEndian.Uint32(body[16:20]) == 105 || binary.BigEndian.Uint32(body[24:28]) == 57005) {
    310 		return server.moveContainerItem(client, body)
    311 	}
    312 	if key == 0x47537a24 && (binary.BigEndian.Uint32(body[24:28]) == 51017 ||
    313 		binary.BigEndian.Uint32(body[16:20]) == 107 && binary.BigEndian.Uint32(body[20:24])>>16 != 0) {
    314 		return server.moveContainerItem(client, body)
    315 	}
    316 	if client.equipment != nil {
    317 		// A separate corpse pickup still needs a reply while equipment is pending.
    318 		if key == 0x47537a24 && binary.BigEndian.Uint32(body[16:20]) == 107 {
    319 			return server.takeLoot(client, body)
    320 		}
    321 		if key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 52 {
    322 			return server.syncStacks(client)
    323 		}
    324 		// Join waits for a reply with its removed stack's source hidden.
    325 		if key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 53 &&
    326 			binary.BigEndian.Uint32(body[20:24]) == 104 && binary.BigEndian.Uint32(body[28:32]) == 104 {
    327 			source := binary.BigEndian.Uint32(body[32:36])
    328 			index := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == source })
    329 			if source >= 64 && source < 94 && index >= 0 && character.inventory.Items[index].isStackable() &&
    330 				character.inventory.Items[index].ID != client.equipment.itemID {
    331 				// Keep both pending equipment slots out of transient GUI moves.
    332 				other := uint32(64)
    333 				for other == source || other == client.equipment.source || other == client.equipment.destination {
    334 					other++
    335 				}
    336 				for _, packet := range [][]byte{itemMovePacket(client.id, source, other), itemMovePacket(client.id, other, source)} {
    337 					if err := client.send(packet); err != nil {
    338 						return err
    339 					}
    340 				}
    341 			}
    342 			return nil
    343 		}
    344 		// Validate a separate owned-item move before restoring its native source.
    345 		if key != 0x47537a24 || binary.BigEndian.Uint32(body[24:28]) != 50000 ||
    346 			binary.BigEndian.Uint32(body[28:32]) != client.id ||
    347 			binary.BigEndian.Uint32(body[16:20]) < 101 || binary.BigEndian.Uint32(body[16:20]) > 104 {
    348 			return nil
    349 		}
    350 	}
    351 
    352 	// Page identities must agree with this owner's actual logical slot.
    353 	sourceType, sourceSlot := binary.BigEndian.Uint32(body[16:20]), binary.BigEndian.Uint32(body[20:24])
    354 	if key == 0x5e477770 {
    355 		sourceType, sourceSlot = binary.BigEndian.Uint32(body[20:24]), binary.BigEndian.Uint32(body[24:28])
    356 	}
    357 	if key == 0x47537a24 && sourceType == 107 {
    358 		return server.takeLoot(client, body)
    359 	}
    360 	index := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == sourceSlot })
    361 	if key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 52 && sourceType == 104 &&
    362 		sourceSlot >= 64 && sourceSlot < 94 && index < 0 {
    363 		return server.syncStacks(client)
    364 	}
    365 	if index < 0 && key == 0x5e477770 && binary.BigEndian.Uint32(body[16:20]) == 53 &&
    366 		sourceType == 104 && sourceSlot >= 64 && sourceSlot < 94 && binary.BigEndian.Uint32(body[28:32]) == 104 {
    367 		// A stale join target must not leave its still-owned removed source hidden.
    368 		removedSlot := binary.BigEndian.Uint32(body[32:36])
    369 		removed := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == removedSlot })
    370 		if removedSlot >= 64 && removedSlot < 94 && removed >= 0 && character.inventory.Items[removed].isStackable() {
    371 			for _, packet := range equipmentCorrectionPackets(client.id, removedSlot) {
    372 				if err := client.send(packet); err != nil {
    373 					return err
    374 				}
    375 			}
    376 			return server.syncStacks(client)
    377 		}
    378 	}
    379 	destination := binary.BigEndian.Uint32(body[32:36])
    380 	if index < 0 && (client.equipment == nil || sourceSlot != client.equipment.source && sourceSlot != client.equipment.destination) &&
    381 		key == 0x47537a24 && sourceType == 104 && sourceSlot >= 64 && sourceSlot < 94 &&
    382 		binary.BigEndian.Uint32(body[24:28]) == 50000 && binary.BigEndian.Uint32(body[28:32]) == client.id &&
    383 		(destination > 0 && destination < 46 && destination != 32 || destination >= 64 && destination < 94) {
    384 		// Cancel the stale drag before clearing only its now-empty carried row.
    385 		if err := client.send(characterActionPacket(client.id, [5]uint32{146, 104, sourceSlot})); err != nil {
    386 			return err
    387 		}
    388 		return client.send(characterActionPacket(client.id, [5]uint32{112, 104, sourceSlot}))
    389 	}
    390 	if index < 0 && (client.equipment == nil || sourceSlot != client.equipment.source && sourceSlot != client.equipment.destination) &&
    391 		key == 0x47537a24 && sourceType == 101 && sourceSlot == 6 &&
    392 		binary.BigEndian.Uint32(body[24:28]) == 50000 && binary.BigEndian.Uint32(body[28:32]) == client.id &&
    393 		(destination == 111 || destination >= 64 && destination < 94) {
    394 		// Clear an empty hand without destroying its detached physical weapon.
    395 		return server.clearStaleWorn(client, sourceType, sourceSlot)
    396 	}
    397 	if index < 0 && (client.equipment == nil || sourceSlot != client.equipment.source && sourceSlot != client.equipment.destination) &&
    398 		key == 0x47537a24 &&
    399 		(sourceType == 101 && sourceSlot > 0 && sourceSlot < 16 && sourceSlot != 6 && sourceSlot != 8 ||
    400 			sourceType == 102 && sourceSlot >= 16 && sourceSlot < 32 || sourceType == 103 && sourceSlot >= 33 && sourceSlot <= 45) &&
    401 		binary.BigEndian.Uint32(body[24:28]) == 50000 && binary.BigEndian.Uint32(body[28:32]) == client.id &&
    402 		(destination == 111 || destination >= 64 && destination < 94) {
    403 		// Clear the stale worn row without deleting its detached physical instance.
    404 		return server.clearStaleWorn(client, sourceType, sourceSlot)
    405 	}
    406 	if index < 0 || (sourceSlot >= 64 && sourceType != 104) ||
    407 		(sourceSlot > 0 && sourceSlot < 16 && sourceType != 101) ||
    408 		(sourceSlot >= 33 && sourceSlot <= 45 && sourceType != 103) ||
    409 		(sourceSlot >= 16 && sourceSlot < 32 && sourceType != 102) {
    410 		return nil
    411 	}
    412 	if client.equipment != nil {
    413 		// A duplicate of the admitted move must retain its pending native source.
    414 		if character.inventory.Items[index].ID != client.equipment.itemID {
    415 			for _, packet := range equipmentCorrectionPackets(client.id, sourceSlot) {
    416 				if err := client.send(packet); err != nil {
    417 					return err
    418 				}
    419 			}
    420 		}
    421 		return nil
    422 	}
    423 	if key == 0x47537a24 && (sourceSlot < 64 || destination < 64) {
    424 		if binary.BigEndian.Uint32(body[24:28]) != 50000 || binary.BigEndian.Uint32(body[28:32]) != client.id {
    425 			return nil
    426 		}
    427 		return server.beginEquipment(client, index, destination)
    428 	}
    429 	if sourceSlot < 64 {
    430 		return nil
    431 	}
    432 	if key == 0x5e477770 && (binary.BigEndian.Uint32(body[16:20]) == 52 || binary.BigEndian.Uint32(body[16:20]) == 53) {
    433 		return server.changeStack(client, index, body)
    434 	}
    435 	inventory := character.inventory
    436 	inventory.Items = slices.Clone(character.inventory.Items)
    437 	if key == 0x47537a24 {
    438 		if binary.BigEndian.Uint32(body[24:28]) != 50000 || binary.BigEndian.Uint32(body[28:32]) != client.id ||
    439 			destination < 64 || destination >= 94 ||
    440 			slices.ContainsFunc(inventory.Items, func(item ownedItem) bool { return item.Slot == destination }) {
    441 			return nil
    442 		}
    443 		inventory.Items[index].Slot = destination
    444 		// Reordering carried items has no wear effects or other world changes.
    445 		item := inventory.Items[index]
    446 		if err := server.moveInventoryItem(character, inventory, item.ID,
    447 			itemPlacement{Player: character.id, Slot: sourceSlot}, itemPlacement{Player: character.id, Slot: destination}); err != nil {
    448 			return err
    449 		}
    450 		return client.send(n3Packet(key, 50000, client.id, body[16:]))
    451 	} else {
    452 		if binary.BigEndian.Uint32(body[16:20]) != 112 ||
    453 			!inventory.Items[index].acceptsDeleteTemplates(binary.BigEndian.Uint32(body[28:32]), binary.BigEndian.Uint32(body[32:36])) {
    454 			return nil
    455 		}
    456 		inventory.Items = slices.Delete(inventory.Items, index, index+1)
    457 		if character.inventory.Items[index].isPortableContainer() {
    458 			inventory.Containers = maps.Clone(inventory.Containers)
    459 			delete(inventory.Containers, character.inventory.Items[index].ID)
    460 			if len(inventory.Containers) == 0 {
    461 				inventory.Containers = nil
    462 			}
    463 		}
    464 	}
    465 	var deleted []ownedItem
    466 	if key == 0x5e477770 {
    467 		item := character.inventory.Items[index]
    468 		deleted = append([]ownedItem{item}, character.inventory.Containers[item.ID]...)
    469 	}
    470 	if err := server.saveInventory(character, inventory); err != nil {
    471 		return err
    472 	}
    473 	if key == 0x5e477770 {
    474 		for handle, bag := range client.backpacks {
    475 			if !slices.ContainsFunc(inventory.Items, func(item ownedItem) bool { return item.ID == bag.id }) {
    476 				delete(client.backpacks, handle)
    477 				if bag.open {
    478 					if err := client.send(backpackClosePacket(client.id, bag.id)); err != nil {
    479 						return err
    480 					}
    481 				}
    482 			}
    483 		}
    484 		return client.sendItemDeletion(body, deleted)
    485 	}
    486 	return client.send(n3Packet(key, 50000, client.id, body[16:]))
    487 }
    488 
    489 func (item ownedItem) acceptsDeleteTemplates(low, high uint32) bool {
    490 	// Native live-item deletion can omit the auxiliary template pair entirely.
    491 	if low == 0 && high == 0 {
    492 		return true
    493 	}
    494 	first, second := item.templateIDs()
    495 	return low == first && high == second
    496 }
    497 
    498 func (client *worldClient) sendItemDeletion(body []byte, deleted []ownedItem) error {
    499 	if err := client.send(n3Packet(0x5e477770, 50000, client.id, body[16:])); err != nil {
    500 		return err
    501 	}
    502 	// Native row deletion does not destroy detached weapon dynels.
    503 	for _, item := range deleted {
    504 		if item.isWieldableWeapon() {
    505 			if err := client.send(n3Packet(0x36510078, item.Type, item.ID, nil)); err != nil {
    506 				return err
    507 			}
    508 		}
    509 	}
    510 	return nil
    511 }
    512 
    513 func (server *server) clearStaleWorn(client *worldClient, page, slot uint32) error {
    514 	if err := client.send(characterActionPacket(client.id, [5]uint32{146, page, slot})); err != nil {
    515 		return err
    516 	}
    517 	removal := client.wornRemoval[slot]
    518 	if slot == 6 {
    519 		removal = weaponUnequipPacket(client.id)
    520 	}
    521 	if removal != nil {
    522 		if err := client.send(removal); err != nil {
    523 			return err
    524 		}
    525 		client.wornRemoval[slot] = nil
    526 	}
    527 	if err := client.send(characterActionPacket(client.id, [5]uint32{112, page, slot})); err != nil {
    528 		return err
    529 	}
    530 	if removal == nil {
    531 		return nil
    532 	}
    533 	return server.sendEquipmentResources(client)
    534 }
    535 
    536 func (server *server) sendEquipmentResources(client *worldClient) error {
    537 	character := server.characters[client.id]
    538 	// Inverse effects do not refresh the owner's cached derived foundations.
    539 	for _, stat := range [][2]uint32{
    540 		{1, character.maximumHealth() - uint32(character.equipmentBonus(1)+character.nanoBonus(1))},
    541 		{221, character.maximumNano() - uint32(character.inventory.equipmentBonus(221))},
    542 		{22, uint32(character.attackRating())},
    543 	} {
    544 		body := binary.BigEndian.AppendUint32(nil, stat[0])
    545 		body = binary.BigEndian.AppendUint32(body, stat[1])
    546 		if err := client.send(n3Packet(0x2b333d6e, 50000, client.id, body)); err != nil {
    547 			return err
    548 		}
    549 	}
    550 	for _, packet := range [][]byte{healthSnapshotPacket(character), nanoPointsPacket(character)} {
    551 		if err := client.send(packet); err != nil {
    552 			return err
    553 		}
    554 	}
    555 	return nil
    556 }