server/resource_kit.go (10299B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "maps" 7 "math/rand/v2" 8 "slices" 9 "time" 10 ) 11 12 type resourceKit struct { 13 health [2]uint32 14 nano [2]uint32 15 lockSkill uint32 16 lockSeconds uint32 17 text string 18 } 19 20 func (item ownedItem) resourceKit() (resourceKit, bool) { 21 definition := gamedata.ItemTemplates[item.Template] 22 if item.Type != 51005 || definition == nil || definition.HeaderWords[0] != item.Type || 23 !gamedata.ItemQualitySupported(item.Template, item.Quality) { 24 return resourceKit{}, false 25 } 26 low, high := item.templateIDs() 27 first, second := gamedata.ItemTemplates[low], gamedata.ItemTemplates[high] 28 kit, supported := decodeResourceKit(first) 29 if !supported || first == second { 30 return kit, supported 31 } 32 33 // Both endpoints must implement the same complete restoration and lock contract. 34 upper, supported := decodeResourceKit(second) 35 if !supported || kit.lockSkill != upper.lockSkill || kit.lockSeconds != upper.lockSeconds || kit.text != upper.text || 36 (kit.health[1] == 0) != (upper.health[1] == 0) || (kit.nano[1] == 0) != (upper.nano[1] == 0) { 37 return resourceKit{}, false 38 } 39 // kkr.dll FUN_100f00a0/100f0aa0 interpolate Hit's two resource limits. 40 for index := range 2 { 41 kit.health[index] = uint32(interpolateItemValue(int32(kit.health[index]), int32(upper.health[index]), *first.Quality, *second.Quality, item.Quality)) 42 kit.nano[index] = uint32(interpolateItemValue(int32(kit.nano[index]), int32(upper.nano[index]), *first.Quality, *second.Quality, item.Quality)) 43 } 44 return kit, true 45 } 46 47 func decodeResourceKit(definition *gamedata.Item) (resourceKit, bool) { 48 kit := resourceKit{} 49 // KKR sub_3604D780 schedules ItemDelay in the native item-use action. 50 if definition.HeaderWords[0] != 51005 || itemStat(definition, 76) != 0 || 51 itemStat(definition, 30)&40 != 40 || itemStat(definition, 212) == 0 { 52 return kit, false 53 } 54 55 // Decode the complete immediate-use contract, including activation metadata. 56 var metadataSkill uint32 57 for _, section := range definition.Sections { 58 if section.Type != 2 { 59 continue 60 } 61 for _, function := range *section.Functions { 62 if len(function.Requirements) != 0 || function.TickCount != 1 || function.TickInterval != 0 { 63 return kit, false 64 } 65 if section.Subtype == 10 { 66 if function.ID != 53028 || len(function.Arguments) != 3 || 67 *function.Arguments[0].Integer != 3 || *function.Arguments[2].BytesHex != "01000000" || 68 (function.Target != 1 && function.Target != 3) || metadataSkill != 0 { 69 return kit, false 70 } 71 metadataSkill = uint32(*function.Arguments[1].Integer) 72 continue 73 } 74 if section.Subtype != 0 { 75 return kit, false 76 } 77 switch function.ID { 78 case 53002: 79 if function.Target != 3 || len(function.Arguments) != 4 || *function.Arguments[1].Integer <= 0 || 80 *function.Arguments[2].Integer < *function.Arguments[1].Integer || *function.Arguments[3].Integer != 0 { 81 return kit, false 82 } 83 values := [2]uint32{uint32(*function.Arguments[1].Integer), uint32(*function.Arguments[2].Integer)} 84 switch *function.Arguments[0].Integer { 85 case 27: 86 if kit.health[1] != 0 { 87 return kit, false 88 } 89 kit.health = values 90 case 214: 91 if kit.nano[1] != 0 { 92 return kit, false 93 } 94 kit.nano = values 95 default: 96 return kit, false 97 } 98 case 53030: 99 if function.Target != 3 { 100 return kit, false 101 } 102 case 53033: 103 if kit.lockSkill != 0 || len(function.Arguments) != 3 || *function.Arguments[0].BytesHex != "09000000" || *function.Arguments[2].Integer <= 0 { 104 return kit, false 105 } 106 kit.lockSkill, kit.lockSeconds = uint32(*function.Arguments[1].Integer), uint32(*function.Arguments[2].Integer) 107 case 53044: 108 if function.Target != 3 || kit.text != "" || len(function.Arguments) != 2 || *function.Arguments[1].BytesHex != "0000000000000000000000000000000000000000" { 109 return kit, false 110 } 111 kit.text = *function.Arguments[0].String 112 default: 113 return kit, false 114 } 115 } 116 } 117 118 // SkillTime's target is rewritten from stat 371 by the native resource loader. 119 if metadataSkill != kit.lockSkill || kit.health[1] == 0 && kit.nano[1] == 0 { 120 return kit, false 121 } 122 if kit.lockSkill == 123 && itemStat(definition, 371) == 0 { 123 return kit, true 124 } 125 return kit, (kit.lockSkill == 124 || kit.lockSkill == 131) && itemStat(definition, 371) != 0 126 } 127 128 func (server *server) itemUseRequirementsMet(player, target *character, item ownedItem) bool { 129 subject := player 130 return item.requirementsMet(3, func(requirement gamedata.ItemRequirement) (bool, bool) { 131 if requirement.Operator == 18 { 132 subject = target 133 return true, true 134 } 135 switch requirement.Stat { 136 case 410: 137 // The current combat producer establishes zero/nonzero, not an exact count. 138 if requirement.Operator == 0 && requirement.Value == 0 { 139 return !server.characterInCombat(subject), true 140 } 141 return false, false 142 case 430: 143 if requirement.Operator == 0 && requirement.Value == 2 { 144 return subject.sitting, true 145 } 146 return false, false 147 case 431: 148 // The caller has validated the selected living, connected player subset. 149 if subject != player || requirement.Operator != 22 || requirement.Value != 35 { 150 return false, false 151 } 152 return true, true 153 } 154 return subject.meetsStatRequirement(requirement) 155 }) 156 } 157 158 func (server *server) useResourceKit(client *worldClient, index int, body []byte, kit resourceKit) error { 159 player := server.characters[client.id] 160 page, source := binary.BigEndian.Uint32(body[40:44]), binary.BigEndian.Uint32(body[44:48]) 161 items, bag := player.inventory.Items, uint32(0) 162 if page == 107 { 163 bag = client.backpacks[source>>16].id 164 items = player.inventory.Containers[bag] 165 } 166 item := items[index] 167 target := server.characters[client.selectedTarget[1]] 168 lockRecipient := target 169 if kit.lockSkill == 123 { 170 lockRecipient = player 171 } 172 now := time.Now() 173 accepted := (bag != 0 || item.Slot >= 64 && item.Slot < 94) && client.selectedTarget[0] == 50000 && 174 target != nil && !target.npc && target.health > 0 && target.playfield == player.playfield && 175 server.clients[target.id] != nil && server.clients[target.id].ready && !server.clients[target.id].loading 176 if accepted { 177 accepted = server.itemUseRequirementsMet(player, target, item) 178 accepted = accepted && !now.Before(lockRecipient.skillLocks[kit.lockSkill]) 179 first := predictedPosition(player, now) 180 second := predictedPosition(target, now) 181 x, y, z := first[0]-second[0], first[1]-second[1], first[2]-second[2] 182 distance := float32(item.templateStat(287)) + player.physicalExtent() + target.physicalExtent() 183 accepted = accepted && x*x+y*y+z*z <= distance*distance 184 } 185 if accepted { 186 // Commit both resources, charge and the correct actor's cooldown together. 187 inventory := player.inventory 188 remaining := slices.Clone(items) 189 remaining[index].Count-- 190 if remaining[index].Count == 0 { 191 remaining = slices.Delete(remaining, index, index+1) 192 } 193 if bag == 0 { 194 inventory.Items = remaining 195 } else { 196 inventory.Containers = maps.Clone(inventory.Containers) 197 if len(remaining) == 0 { 198 delete(inventory.Containers, bag) 199 } else { 200 inventory.Containers[bag] = remaining 201 } 202 if len(inventory.Containers) == 0 { 203 inventory.Containers = nil 204 } 205 } 206 deadline := now.Add(time.Duration(lockRecipient.skillLockSeconds(kit.lockSeconds)) * time.Second) 207 vitals := target.vitals() 208 // Uniform inclusive endpoint rolls remain a reconstruction policy. 209 if kit.health[1] != 0 { 210 vitals.Health = min(target.maximumHealth(), vitals.Health+kit.health[0]+rand.Uint32N(kit.health[1]-kit.health[0]+1)) 211 } 212 if kit.nano[1] != 0 { 213 vitals.Nano = min(target.maximumNano(), vitals.Nano+kit.nano[0]+rand.Uint32N(kit.nano[1]-kit.nano[0]+1)) 214 } 215 if err := server.saveItemEffect(player, target, inventory, vitals, skillLockUpdate{lockRecipient.id, kit.lockSkill, deadline}); err != nil { 216 return err 217 } 218 } 219 220 reply := append([]byte(nil), body[16:]...) 221 binary.BigEndian.PutUint32(reply, 2) 222 if accepted { 223 binary.BigEndian.PutUint32(reply, 1) 224 } 225 ackError := client.send(n3Packet(0x52526858, 50000, client.id, reply)) 226 if !accepted { 227 return ackError 228 } 229 230 // The ACK debits one native charge; TemplateAction runs the authored graphics. 231 var action []byte 232 low, high := item.templateIDs() 233 for _, value := range []uint32{low, high, item.Quality, 1, 3, page, source, 50000, target.id} { 234 action = binary.BigEndian.AppendUint32(action, value) 235 } 236 updates := make(map[*worldClient][][]byte) 237 server.queueCombat(updates, player, target, n3Packet(0x35505644, 50000, client.id, action)) 238 if kit.health[1] != 0 { 239 if kit.lockSkill == 123 { 240 // The client reconciles queued weapon impacts against this absolute HP. 241 server.queueHealthSnapshot(updates, target) 242 } else { 243 server.queueQuietHealth(updates, target) 244 } 245 } 246 if item.Count == 1 { 247 var removal []byte 248 for _, value := range []uint32{112, page, source, low, high} { 249 removal = binary.BigEndian.AppendUint32(removal, value) 250 } 251 updates[client] = append(updates[client], n3Packet(0x5e477770, 50000, client.id, removal)) 252 } 253 lockClient := server.clients[lockRecipient.id] 254 updates[lockClient] = append(updates[lockClient], skillLockPacket(lockRecipient.id, kit.lockSkill, kit.lockSeconds)) 255 if kit.nano[1] != 0 { 256 var nano []byte 257 for _, value := range []uint32{214, target.nano} { 258 nano = binary.BigEndian.AppendUint32(nano, value) 259 } 260 updates[server.clients[target.id]] = append(updates[server.clients[target.id]], n3Packet(0x2b333d6e, 50000, target.id, nano)) 261 } 262 if kit.text != "" { 263 // StaticText's local handler is empty; deliver the authored recipient text. 264 feedback := binary.BigEndian.AppendUint32(nil, uint32(len(kit.text))) 265 feedback = append(feedback, kit.text...) 266 feedback = append(feedback, 0, 0) 267 updates[server.clients[target.id]] = append(updates[server.clients[target.id]], n3Packet(0x5f4b442a, 50000, target.id, feedback)) 268 } 269 270 // A failed healer connection must not suppress the healthy patient's outcome. 271 sourcePackets := updates[client] 272 delete(updates, client) 273 server.sendCombatUpdates(updates) 274 if ackError != nil { 275 return ackError 276 } 277 for _, packet := range sourcePackets { 278 if err := client.send(packet); err != nil { 279 return err 280 } 281 } 282 return nil 283 }