server/bank.go (6794B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "fmt" 7 "maps" 8 "slices" 9 "time" 10 ) 11 12 // kkr.dll BankEntry constructor 0x10150b30 allocates 102 inventory positions. 13 const bankCapacity = 102 14 15 type bankSession struct { 16 playfield uint32 17 position [3]float32 18 } 19 20 func (inventory playerInventory) carriesUnique(item ownedItem) bool { 21 low, _ := item.templateIDs() 22 for _, carried := range inventory.Items { 23 other, _ := carried.templateIDs() 24 if other == low { 25 return true 26 } 27 if carried.isPortableContainer() && slices.ContainsFunc(inventory.Containers[carried.ID], func(child ownedItem) bool { 28 other, _ := child.templateIDs() 29 return other == low 30 }) { 31 return true 32 } 33 } 34 return false 35 } 36 37 func bankInventoryPacket(player *character) []byte { 38 // BankEntry serializes CASH, XP, PLAYERID, then its normal inventory list. 39 body := binary.BigEndian.AppendUint32(nil, 0) 40 body = binary.BigEndian.AppendUint32(body, 0) 41 body = binary.BigEndian.AppendUint32(body, player.id) 42 body = binary.BigEndian.AppendUint32(body, uint32(len(player.inventory.Bank)+1)*1009) 43 for _, item := range player.inventory.Bank { 44 flags := uint32(3) 45 if item.isPortableContainer() || item.Type == 51053 && item.Template == 28577 { 46 flags = 2 47 } 48 low, high := item.templateIDs() 49 for _, value := range []uint32{item.Slot, flags<<16 | uint32(item.Count), item.Type, item.ID, low, high, item.Quality, 0} { 50 body = binary.BigEndian.AppendUint32(body, value) 51 } 52 } 53 return n3Packet(0x343c287f, 50000, player.id, body) 54 } 55 56 func (server *server) bankAvailable(client *worldClient) bool { 57 if client.bank == nil { 58 return false 59 } 60 player := server.characters[client.id] 61 position := predictedPosition(player, time.Now()) 62 x, y, z := position[0]-client.bank.position[0], position[1]-client.bank.position[1], position[2]-client.bank.position[2] 63 if player.playfield != client.bank.playfield || x*x+y*y+z*z > 25 { 64 client.bank = nil 65 return false 66 } 67 return true 68 } 69 70 func (server *server) handleBankUse(client *worldClient, body []byte) error { 71 if len(body) != 48 { 72 return fmt.Errorf("invalid bank use length") 73 } 74 player := server.characters[client.id] 75 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 76 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 77 binary.BigEndian.Uint32(body[16:20]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 3 || 78 binary.BigEndian.Uint32(body[28:32]) != 1 || binary.BigEndian.Uint32(body[32:36]) != 50000 || 79 binary.BigEndian.Uint32(body[36:40]) != client.id || binary.BigEndian.Uint32(body[40:44]) != 51005 { 80 return nil 81 } 82 sequence := binary.BigEndian.Uint32(body[20:24]) 83 if sequence <= client.useSequence { 84 return nil 85 } 86 client.useSequence = sequence 87 terminal, exists := gamedata.BankTerminals[binary.BigEndian.Uint32(body[44:48])] 88 position := predictedPosition(player, time.Now()) 89 x, y, z := position[0]-terminal.Position[0], position[1]-terminal.Position[1], position[2]-terminal.Position[2] 90 accepted := exists && player.playfield == terminal.Playfield && x*x+y*y+z*z <= 25 && client.equipment == nil && client.trade == nil 91 reply := append([]byte(nil), body[16:]...) 92 binary.BigEndian.PutUint32(reply, 2) 93 if accepted { 94 binary.BigEndian.PutUint32(reply, 1) 95 } 96 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil || !accepted { 97 return err 98 } 99 return server.openBank(client) 100 } 101 102 func (server *server) useRemoteBank(client *worldClient, item ownedItem, body []byte) error { 103 player := server.characters[client.id] 104 // Briefcase Use3 requires Bureaucrat, Psychology >299, title >3 and no combat. 105 accepted := item.Type == 51018 && item.Template == 205736 && item.Quality == 100 && 106 player.profession == 8 && player.titleLevel() > 3 && 107 player.effectiveSkill(162, player.baseStats().TradeRepair.Psychology, gamedata.IPTraining.Weights[162]) > 299 && 108 !server.characterInCombat(player) 109 reply := append([]byte(nil), body[16:]...) 110 binary.BigEndian.PutUint32(reply, 2) 111 if accepted { 112 binary.BigEndian.PutUint32(reply, 1) 113 } 114 if err := client.send(n3Packet(0x52526858, 50000, client.id, reply)); err != nil || !accepted { 115 return err 116 } 117 return server.openBank(client) 118 } 119 120 func (server *server) openBank(client *worldClient) error { 121 if client.trade != nil { 122 return nil 123 } 124 player := server.characters[client.id] 125 if client.shop != nil { 126 if err := server.abortShop(client); err != nil { 127 return err 128 } 129 } 130 client.bank = &bankSession{player.playfield, predictedPosition(player, time.Now())} 131 return client.send(bankInventoryPacket(player)) 132 } 133 134 func (server *server) handleBankClose(client *worldClient, body []byte) error { 135 if len(body) != 36 { 136 return fmt.Errorf("invalid bank close length") 137 } 138 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || 139 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 140 binary.BigEndian.Uint32(body[16:20]) != 106 || binary.BigEndian.Uint64(body[20:28]) != 0 || binary.BigEndian.Uint64(body[28:36]) != 0 { 141 return nil 142 } 143 client.bank = nil 144 return nil 145 } 146 147 func (server *server) handleBankAction(client *worldClient, body []byte) error { 148 if !client.target { 149 return nil 150 } 151 if !server.bankAvailable(client) { 152 client.bank = nil 153 return client.send(characterActionPacket(client.id, [5]uint32{106})) 154 } 155 player := server.characters[client.id] 156 action := binary.BigEndian.Uint32(body[16:20]) 157 slot := binary.BigEndian.Uint32(body[24:28]) 158 index := slices.IndexFunc(player.inventory.Bank, func(item ownedItem) bool { return item.Slot == slot }) 159 if client.equipment != nil || index < 0 { 160 if action == 52 { 161 return server.syncStackPage(client, 105, 0) 162 } 163 return nil 164 } 165 if action == 52 || action == 53 { 166 return server.changeStack(client, index, body) 167 } 168 item := player.inventory.Bank[index] 169 if action != 112 || !item.acceptsDeleteTemplates(binary.BigEndian.Uint32(body[28:32]), binary.BigEndian.Uint32(body[32:36])) { 170 return nil 171 } 172 173 // Deleting a bag destroys its children in the same transaction as its root. 174 deleted := append([]ownedItem{item}, player.inventory.Containers[item.ID]...) 175 inventory := player.inventory 176 inventory.Bank = slices.Delete(slices.Clone(inventory.Bank), index, index+1) 177 if item.isPortableContainer() { 178 inventory.Containers = maps.Clone(inventory.Containers) 179 delete(inventory.Containers, item.ID) 180 if len(inventory.Containers) == 0 { 181 inventory.Containers = nil 182 } 183 } 184 if err := server.saveInventory(player, inventory); err != nil { 185 return err 186 } 187 for handle, bag := range client.backpacks { 188 if bag.id == item.ID { 189 delete(client.backpacks, handle) 190 } 191 } 192 return client.sendItemDeletion(body, deleted) 193 }