server/equipment.go (17535B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "log" 7 "maps" 8 "slices" 9 "time" 10 ) 11 12 type equipmentMove struct { 13 itemID uint32 14 source uint32 15 destination uint32 16 requestedSlot uint32 17 replacedID uint32 18 sourceBag uint32 19 readyAt time.Time 20 } 21 22 func (item ownedItem) fitsEquipmentSlot(slot uint32) bool { 23 // Carried slots cannot provide worn bonuses; skip catalog classification. 24 if slot >= 64 { 25 return false 26 } 27 if item.isWearableContainer() { 28 return slot >= 16 && slot < 32 && itemStat(gamedata.ItemTemplates[item.Template], 298)&(1<<(slot-16)) != 0 29 } 30 if item.isKodaik() || item.isYalmaha() { 31 return slot == 1 32 } 33 if item.isWieldableWeapon() { 34 return slot == 6 35 } 36 if item.isStatEquipment() { 37 if itemStat(gamedata.ItemTemplates[item.Template], 76) == 1 { 38 return slot > 0 && slot < 16 && slot != 6 && slot != 8 && itemStat(gamedata.ItemTemplates[item.Template], 298)&(1<<slot) != 0 39 } 40 return slot >= 16 && slot < 32 && itemStat(gamedata.ItemTemplates[item.Template], 298)&(1<<(slot-16)) != 0 41 } 42 if item.isImplant() { 43 return slot >= 33 && slot <= 45 && itemStat(gamedata.ItemTemplates[item.Template], 298)&(1<<(slot-32)) != 0 44 } 45 return false 46 } 47 48 func (character *character) canEquip(item ownedItem) bool { 49 if item.isWearableContainer() { 50 return character.canWearContainer(item) 51 } 52 if item.isKodaik() { 53 stats := character.baseStats() 54 return gamedata.Playfields[character.playfield].Outdoor && !character.driving() && 55 character.effectiveSkill(166, stats.Navigation.VehicleGround, [6]uint32{0, 20, 0, 20, 60, 0}) > 80 56 } 57 if item.isYalmaha() { 58 stats := character.baseStats() 59 return gamedata.Playfields[character.playfield].Outdoor && !character.driving() && 60 character.effectiveSkill(139, stats.Navigation.VehicleAir, [6]uint32{0, 20, 0, 20, 60, 0}) > 80 61 } 62 if item.isWieldableWeapon() { 63 return item.requirementsMet(8, character.meetsStatRequirement) 64 } 65 if item.isStatEquipment() && itemStat(gamedata.ItemTemplates[item.Template], 76) == 1 { 66 return item.requirementsMet(8, character.meetsStatRequirement) 67 } 68 if item.isStatEquipment() || item.isImplant() { 69 return item.requirementsMet(6, character.meetsStatRequirement) 70 } 71 return false 72 } 73 74 func (inventory playerInventory) equipmentBonus(stat uint32) int32 { 75 // Authored bonuses remain available for requirements and remote OnWear effects. 76 var bonus int32 77 for _, item := range inventory.Items { 78 bonus += item.equipmentBonus(stat, 0) 79 } 80 return bonus 81 } 82 83 func (item ownedItem) equipmentBonus(stat, tier uint32) int32 { 84 if !item.fitsEquipmentSlot(item.Slot) { 85 return 0 86 } 87 low, high := item.templateIDs() 88 first, second := gamedata.ItemTemplates[low], gamedata.ItemTemplates[high] 89 var bonus int32 90 for sectionIndex, section := range first.Sections { 91 if section.Type != 2 || section.Subtype != 14 { 92 continue 93 } 94 for index, function := range *section.Functions { 95 if (function.ID == 53012 || function.ID == 53045) && uint32(*function.Arguments[0].Integer) == stat { 96 value := interpolateItemValue(*function.Arguments[1].Integer, *(*second.Sections[sectionIndex].Functions)[index].Arguments[1].Integer, *first.Quality, *second.Quality, item.Quality) 97 // FUN_10063130/10062f60: subtract each signed, truncated penalty. 98 bonus += value - value*int32(tier)/4 99 } 100 } 101 } 102 return bonus 103 } 104 105 func (character *character) maximumNCU() uint32 { 106 return uint32(8 + character.inventory.equipmentBonus(181)) 107 } 108 109 func (character *character) canMoveEquipment(item ownedItem, destination uint32, now time.Time) bool { 110 if destination < 64 && (!item.fitsEquipmentSlot(destination) || !character.canEquip(item)) { 111 return false 112 } 113 if item.isImplant() && (!character.canChangeImplants(now) || 114 item.Slot < 64 && !item.requirementsMet(7, character.meetsStatRequirement)) { 115 return false 116 } 117 118 // The native belt validator requires every deck position to be empty on removal. 119 if item.Slot == 7 && slices.ContainsFunc(character.inventory.Items, func(other ownedItem) bool { return other.Slot >= 9 && other.Slot <= 14 }) { 120 return false 121 } 122 return destination < 9 || destination > 14 || int32(destination-9) < character.inventory.equipmentBonus(45) 123 } 124 125 func (character *character) attackWeapon() *gamedata.Item { 126 if character.npc && character.combatProfile != nil { 127 first, second := gamedata.ItemTemplates[character.combatProfile.Weapon[0]], gamedata.ItemTemplates[character.combatProfile.Weapon[1]] 128 // client/game/kkr.dll/lifted-0035.c::sub_3607B9A0 constructs at actor level. 129 return interpolateWeapon(first, second, uint32(character.level)) 130 } 131 for _, item := range character.inventory.Items { 132 if item.Slot == 6 && item.isWieldableWeapon() { 133 low, high := item.templateIDs() 134 return interpolateWeapon(gamedata.ItemTemplates[low], gamedata.ItemTemplates[high], item.Quality) 135 } 136 } 137 return nil 138 } 139 140 func itemStat(item *gamedata.Item, stat uint32) uint32 { 141 for _, value := range item.Stats { 142 if value[0] == stat { 143 return value[1] 144 } 145 } 146 return 0 147 } 148 149 func itemMovePacket(owner, source, destination uint32) []byte { 150 page := uint32(104) 151 if source>>16 != 0 { 152 page = 107 153 } else if source > 0 && source < 16 { 154 page = 101 155 } else if source >= 16 && source < 32 { 156 page = 102 157 } else if source >= 33 && source <= 45 { 158 page = 103 159 } 160 var body []byte 161 for _, value := range []uint32{page, source, 50000, owner, destination} { 162 body = binary.BigEndian.AppendUint32(body, value) 163 } 164 return n3Packet(0x47537a24, 50000, owner, body) 165 } 166 167 func equipmentCorrectionPackets(owner, source uint32) [][]byte { 168 if source>>16 != 0 { 169 return [][]byte{characterActionPacket(owner, [5]uint32{146, 107, source})} 170 } 171 if source < 64 { 172 page := uint32(101) 173 if source >= 16 { 174 page = 102 175 } 176 if source >= 33 { 177 page = 103 178 } 179 return [][]byte{characterActionPacket(owner, [5]uint32{146, page, source})} 180 } 181 // kkr.dll FUN_100614e0 skips GUI notifications for same-slot replies. 182 // Swap ordinary entries twice to restore their icons and final placement, 183 // including when the inventory is full, without replaying worn effects. 184 other := uint32(64) 185 if source == other { 186 other = 65 187 } 188 return [][]byte{itemMovePacket(owner, source, other), itemMovePacket(owner, other, source)} 189 } 190 191 func (server *server) equipmentBlocked(character *character, item ownedItem, now time.Time) bool { 192 // Incoming hits do not interrupt an idle player's weapon move. 193 // Own combat swaps and other equipment's combat restrictions remain separate. 194 if now.Before(character.rechargeUntil) { 195 return true 196 } 197 for _, attacker := range server.fighting { 198 if attacker.id == character.id || attacker.fighting == character.id && !item.isWieldableWeapon() { 199 return true 200 } 201 } 202 return false 203 } 204 205 func (item ownedItem) equipDelay() time.Duration { 206 // Beholder FUN_1008e840 defaults EquipDelay to 10 centiseconds. 207 for _, stat := range item.templateDefinition().Stats { 208 if stat[0] == 211 { 209 return time.Duration(item.templateStat(211)) * 10 * time.Millisecond 210 } 211 } 212 return 100 * time.Millisecond 213 } 214 215 func (server *server) beginEquipment(client *worldClient, index int, destination uint32) error { 216 character := server.characters[client.id] 217 item := character.inventory.Items[index] 218 requestedSlot := destination 219 if destination == 111 && item.Slot < 64 { 220 for destination = 64; destination < 94; destination++ { 221 if !slices.ContainsFunc(character.inventory.Items, func(other ownedItem) bool { return other.Slot == destination }) { 222 break 223 } 224 } 225 } 226 replaced := slices.IndexFunc(character.inventory.Items, func(other ownedItem) bool { return other.Slot == destination }) 227 if destination == item.Slot || destination >= 94 || !character.canMoveEquipment(item, destination, time.Now()) || 228 replaced >= 0 && (item.Slot < 64 || destination >= 64 || !character.canMoveEquipment(character.inventory.Items[replaced], item.Slot, time.Now())) || 229 server.equipmentBlocked(character, item, time.Now()) { 230 for _, packet := range equipmentCorrectionPackets(client.id, item.Slot) { 231 if err := client.send(packet); err != nil { 232 return err 233 } 234 } 235 return nil 236 } 237 238 // client/game/kkr.dll/lifted-0029.c::sub_360628B0 includes both EquipDelay values. 239 delay, replacedID := item.equipDelay(), uint32(0) 240 if replaced >= 0 { 241 delay += character.inventory.Items[replaced].equipDelay() 242 replacedID = character.inventory.Items[replaced].ID 243 } 244 client.equipment = &equipmentMove{itemID: item.ID, source: item.Slot, destination: destination, 245 requestedSlot: requestedSlot, replacedID: replacedID, readyAt: time.Now().Add(delay)} 246 return nil 247 } 248 249 func (server *server) advanceEquipment(now time.Time) { 250 updates := make(map[*worldClient][][]byte) 251 for _, client := range server.clients { 252 move := client.equipment 253 if move == nil || now.Before(move.readyAt) { 254 continue 255 } 256 client.equipment = nil 257 character := server.characters[client.id] 258 if !client.ready || client.loading || character.health == 0 { 259 continue 260 } 261 // Resolve the admitted physical source in its current owned container. 262 sourceItems, sourceSlot := character.inventory.Items, move.source 263 if move.sourceBag != 0 { 264 if client.backpacks[move.source>>16].id != move.sourceBag || !slices.ContainsFunc(character.inventory.Items, func(item ownedItem) bool { 265 return item.ID == move.sourceBag && item.isPortableContainer() 266 }) { 267 updates[client] = append(updates[client], equipmentCorrectionPackets(client.id, move.source)...) 268 continue 269 } 270 sourceItems, sourceSlot = character.inventory.Containers[move.sourceBag], move.source&65535 271 } 272 index := slices.IndexFunc(sourceItems, func(item ownedItem) bool { return item.ID == move.itemID && item.Slot == sourceSlot }) 273 if index < 0 { 274 if move.sourceBag != 0 { 275 updates[client] = append(updates[client], equipmentCorrectionPackets(client.id, move.source)...) 276 } 277 continue 278 } 279 item := sourceItems[index] 280 // A packed carried source must not be interpreted as an inner worn slot. 281 item.Slot = move.source 282 replaced := slices.IndexFunc(character.inventory.Items, func(item ownedItem) bool { return item.Slot == move.destination }) 283 replacedID := uint32(0) 284 if replaced >= 0 { 285 replacedID = character.inventory.Items[replaced].ID 286 } 287 if server.equipmentBlocked(character, item, now) || replacedID != move.replacedID || 288 !character.canMoveEquipment(item, move.destination, now) || 289 replaced >= 0 && !character.canMoveEquipment(character.inventory.Items[replaced], move.source, now) { 290 updates[client] = append(updates[client], equipmentCorrectionPackets(client.id, move.source)...) 291 continue 292 } 293 294 // Save the entire move before publishing inventory or appearance changes. 295 inventory := character.inventory 296 inventory.Items = slices.Clone(character.inventory.Items) 297 if move.sourceBag != 0 { 298 inventory.Containers = maps.Clone(inventory.Containers) 299 remaining := slices.Delete(slices.Clone(sourceItems), index, index+1) 300 if len(remaining) == 0 { 301 delete(inventory.Containers, move.sourceBag) 302 } else { 303 inventory.Containers[move.sourceBag] = remaining 304 } 305 if len(inventory.Containers) == 0 { 306 inventory.Containers = nil 307 } 308 index = len(inventory.Items) 309 inventory.Items = append(inventory.Items, item) 310 } 311 removedItems := []ownedItem{} 312 if move.source < 64 { 313 removedItems = append(removedItems, item) 314 } 315 if replaced >= 0 { 316 removedItems = append(removedItems, inventory.Items[replaced]) 317 inventory.Items[replaced].Slot = move.source 318 } 319 inventory.Items[index].Slot = move.destination 320 movementChanged := move.source == 1 || move.destination == 1 || inventory.equipmentBonus(156) != character.inventory.equipmentBonus(156) 321 motion := *character 322 if movementChanged { 323 motion = character.predictedMovement(now) 324 } 325 health, nano := character.health, character.nano 326 baseHealth := character.maximumHealth() - uint32(character.directHealthBonus()) 327 baseNano := character.maximumNano() - uint32(character.inventory.equipmentBonus(221)) 328 attackRating := character.attackRating() 329 if err := server.saveInventory(&motion, inventory); err != nil { 330 log.Printf("Equipment: %v", err) 331 updates[client] = append(updates[client], equipmentCorrectionPackets(client.id, move.source)...) 332 continue 333 } 334 // Commit the old-speed prediction and ordered wear effects together. 335 *character = motion 336 for _, removed := range removedItems { 337 if removed.Slot > 0 && removed.Slot <= 45 { 338 client.wornRemoval[removed.Slot] = nil 339 } 340 if !removed.isWieldableWeapon() { 341 server.queueCombat(updates, character, character, equipmentActionPacket(character.id, removed, 7)) 342 } 343 if removed.Slot == 6 { 344 server.queueCombat(updates, character, character, weaponUnequipPacket(client.id)) 345 // The owner's live inventory row still needs its detached weapon dynel. 346 for id, observer := range server.clients { 347 if id != client.id && observer.sees(character) && observer.ready && !observer.loading && server.characters[id].playfield == character.playfield { 348 updates[observer] = append(updates[observer], n3Packet(0x36510078, removed.Type, removed.ID, nil)) 349 } 350 } 351 } 352 } 353 if move.destination == 6 { 354 // FullUpdate100b38b0 only creates absent dynels. Retire after the UI request. 355 updates[client] = append(updates[client], n3Packet(0x36510078, item.Type, move.itemID, nil)) 356 } 357 updates[client] = append(updates[client], itemMovePacket(client.id, move.source, move.requestedSlot)) 358 if move.destination < 64 && !item.isWieldableWeapon() { 359 server.queueCombat(updates, character, character, equipmentActionPacket(character.id, character.inventory.Items[index], 6)) 360 if move.destination > 0 && move.destination <= 45 { 361 client.wornRemoval[move.destination] = equipmentActionPacket(character.id, character.inventory.Items[index], 7) 362 } 363 } 364 if move.source == 6 || move.destination == 6 { 365 server.queueCombat(updates, character, character, equippedWeaponPackets(character)...) 366 } 367 // Ability modifiers change derived foundations that the native client caches. 368 for _, stat := range [][3]uint32{ 369 {1, baseHealth, character.maximumHealth() - uint32(character.directHealthBonus())}, 370 {221, baseNano, character.maximumNano() - uint32(character.inventory.equipmentBonus(221))}, 371 {22, uint32(attackRating), uint32(character.attackRating())}, 372 } { 373 if stat[1] != stat[2] { 374 if stat[0] == 1 || stat[0] == 221 { 375 server.queueResourceMaximum(updates, character, stat[0], stat[2]) 376 continue 377 } 378 body := binary.BigEndian.AppendUint32(nil, stat[0]) 379 body = binary.BigEndian.AppendUint32(body, stat[2]) 380 packet := n3Packet(0x2b333d6e, 50000, character.id, body) 381 server.queueCombat(updates, character, character, packet) 382 } 383 } 384 if character.health != health { 385 server.queueQuietHealth(updates, character) 386 } 387 if character.nano != nano { 388 updates[client] = append(updates[client], nanoPointsPacket(character)) 389 } 390 } 391 server.sendCombatUpdates(updates) 392 } 393 394 func weaponUnequipPacket(owner uint32) []byte { 395 var body []byte 396 for _, value := range []uint32{97, 0, 0, 0, 6} { 397 body = binary.BigEndian.AppendUint32(body, value) 398 } 399 return n3Packet(0x5e477770, 50000, owner, body) 400 } 401 402 func equippedWeaponPackets(character *character) [][]byte { 403 for _, item := range character.inventory.Items { 404 if item.Slot != 6 || !item.isWieldableWeapon() { 405 continue 406 } 407 return [][]byte{weaponPacket(character.id, character.playfield, item, 1)} 408 } 409 return nil 410 } 411 412 func weaponPacket(owner, playfield uint32, item ownedItem, page uint32) []byte { 413 // kkr.dll 100b2440: SimpleItem 11, parent, playfield and page/slot bytes. 414 var body []byte 415 for _, value := range []uint32{11, 50000, owner, playfield, 0, 0} { 416 body = binary.BigEndian.AppendUint32(body, value) 417 } 418 body = append(body, byte(page), byte(item.Slot)) 419 low, high := item.templateIDs() 420 definition := item.templateDefinition() 421 stats := [][2]uint32{{23, definition.ID}, {701, item.Quality}, {702, low}, {703, high}} 422 // kkr.dll FUN_100b38b0 interpolates only the local owner's new weapon. 423 // Remote instances need numeric differences from stat23's base definition. 424 for _, stat := range definition.Stats { 425 if stat[0] != 26 && stat[0] != 54 { 426 if value := item.templateStat(stat[0]); value != stat[1] { 427 stats = append(stats, [2]uint32{stat[0], value}) 428 } 429 } 430 } 431 if item.usesAmmo() { 432 stats = append(stats, [2]uint32{26, item.Ammo}) 433 } 434 body = binary.BigEndian.AppendUint32(body, uint32(len(stats)+1)*1009) 435 for _, stat := range stats { 436 body = binary.BigEndian.AppendUint32(body, stat[0]) 437 body = binary.BigEndian.AppendUint32(body, stat[1]) 438 } 439 body = binary.BigEndian.AppendUint32(body, 0) 440 return n3Packet(0x3b1d2268, item.Type, item.ID, body) 441 } 442 443 func equipmentActionPacket(owner uint32, item ownedItem, action uint32) []byte { 444 // kkr.dll FUN_100848d0: template endpoints, QL, count, action and two identities. 445 page := uint32(102) 446 if item.Slot < 16 { 447 page = 101 448 } else if item.Slot >= 33 && item.Slot <= 45 { 449 page = 103 450 } 451 var body []byte 452 low, high := item.templateIDs() 453 for _, value := range []uint32{low, high, item.Quality, 1, action, page, item.Slot, 0, 0} { 454 body = binary.BigEndian.AppendUint32(body, value) 455 } 456 return n3Packet(0x35505644, 50000, owner, body) 457 } 458 459 func equippedItemPackets(character *character) [][]byte { 460 packets := equippedWeaponPackets(character) 461 for _, item := range character.inventory.Items { 462 if item.Slot < 64 && !item.isWieldableWeapon() { 463 packets = append(packets, equipmentActionPacket(character.id, item, 6)) 464 } 465 } 466 return packets 467 }