server/reclaim.go (5696B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "encoding/binary" 6 "slices" 7 "time" 8 ) 9 10 func (player *character) collectReclaim(now time.Time) { 11 if player.level < 10 { 12 return 13 } 14 15 // kkr.dll FUN_1005fa40 retains equipment and items with the NoDrop flag. 16 inventory := player.inventory 17 inventory.Items = nil 18 inventory.Reclaim = slices.Clone(player.inventory.Reclaim) 19 for _, item := range player.inventory.Items { 20 if item.Slot < 64 || itemStat(gamedata.ItemTemplates[item.Template], 0)&0x04000000 != 0 { 21 inventory.Items = append(inventory.Items, item) 22 } else { 23 // The native DeadTimer fallback is provisional until server timing is known. 24 inventory.Reclaim = append(inventory.Reclaim, reclaimItem{item, now.Add(75 * time.Second)}) 25 } 26 } 27 player.inventory = inventory 28 } 29 30 func (server *server) nearReclaim(client *worldClient) bool { 31 player := server.characters[client.id] 32 position := predictedPosition(player, time.Now()) 33 for _, terminal := range gamedata.InsuranceTerminals.Reclaim { 34 x, y, z := position[0]-terminal.Position[0], position[1]-terminal.Position[1], position[2]-terminal.Position[2] 35 if player.playfield == terminal.Playfield && x*x+y*y+z*z <= 25 { 36 return true 37 } 38 } 39 return false 40 } 41 42 func reclaimInventoryPacket(player *character, slots []uint32) []byte { 43 body := binary.BigEndian.AppendUint32(nil, 1009) 44 count := uint32(1) 45 for slot, id := range slots { 46 if id == 0 { 47 continue 48 } 49 index := slices.IndexFunc(player.inventory.Reclaim, func(entry reclaimItem) bool { return entry.Item.ID == id }) 50 item := player.inventory.Reclaim[index].Item 51 low, high := item.templateIDs() 52 for _, value := range []uint32{uint32(slot), 0x00030000 | uint32(item.Count), item.Type, item.ID, low, high, item.Quality, 0} { 53 body = binary.BigEndian.AppendUint32(body, value) 54 } 55 count++ 56 } 57 binary.BigEndian.PutUint32(body, count*1009) 58 body = binary.BigEndian.AppendUint32(body, player.id) 59 return n3Packet(0x52213420, 50000, player.id, body) 60 } 61 62 func (server *server) handleReclaim(client *worldClient, body []byte) error { 63 player := server.characters[client.id] 64 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 65 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 66 binary.BigEndian.Uint32(body[20:24]) != 0 || binary.BigEndian.Uint32(body[24:28]) != 0 || 67 binary.BigEndian.Uint32(body[28:32]) != 0 || binary.BigEndian.Uint32(body[32:36]) != 0 { 68 return nil 69 } 70 var packet []byte 71 if binary.BigEndian.Uint32(body[16:20]) == 110 { 72 if client.reclaimItems != nil { 73 client.reclaimItems = nil 74 packet = n3Packet(0x5e477770, 50000, client.id, body[16:]) 75 } 76 } else if client.reclaimItems == nil && server.nearReclaim(client) { 77 // Snapshot at most the native proxy's 100 slots; pending items stay durable. 78 client.reclaimItems = make([]uint32, 0, 100) 79 now := time.Now() 80 for _, entry := range player.inventory.Reclaim { 81 if !now.Before(entry.AvailableAt) { 82 client.reclaimItems = append(client.reclaimItems, entry.Item.ID) 83 if len(client.reclaimItems) == 100 { 84 break 85 } 86 } 87 } 88 packet = reclaimInventoryPacket(player, client.reclaimItems) 89 } 90 if packet != nil { 91 return client.send(packet) 92 } 93 return nil 94 } 95 96 func (server *server) takeReclaim(client *worldClient, body []byte) error { 97 player := server.characters[client.id] 98 source := binary.BigEndian.Uint32(body[20:24]) 99 if !client.target || server.clients[client.id] != client || !client.ready || client.loading || player.health == 0 || 100 binary.BigEndian.Uint32(body[4:8]) != 50000 || binary.BigEndian.Uint32(body[8:12]) != client.id || 101 binary.BigEndian.Uint32(body[24:28]) != 50000 || binary.BigEndian.Uint32(body[28:32]) != client.id || 102 binary.BigEndian.Uint32(body[32:36]) != 111 || source >= uint32(len(client.reclaimItems)) { 103 return nil 104 } 105 if !server.nearReclaim(client) { 106 client.reclaimItems = nil 107 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 106, source})); err != nil { 108 return err 109 } 110 return client.send(characterActionPacket(client.id, [5]uint32{110})) 111 } 112 index := slices.IndexFunc(player.inventory.Reclaim, func(entry reclaimItem) bool { return entry.Item.ID == client.reclaimItems[source] }) 113 if index < 0 { 114 // Retain the other snapshot slots while removing an already-collected source. 115 client.reclaimItems[source] = 0 116 packet := reclaimInventoryPacket(player, client.reclaimItems) 117 if err := client.send(characterActionPacket(client.id, [5]uint32{146, 106, source})); err != nil { 118 return err 119 } 120 return client.send(packet) 121 } 122 if time.Now().Before(player.inventory.Reclaim[index].AvailableAt) { 123 return nil 124 } 125 var slot uint32 126 for slot = 64; slot < 94; slot++ { 127 if !slices.ContainsFunc(player.inventory.Items, func(item ownedItem) bool { return item.Slot == slot }) { 128 break 129 } 130 } 131 if slot == 94 || client.equipment != nil || client.trade != nil { 132 return client.send(characterActionPacket(client.id, [5]uint32{146, 106, source})) 133 } 134 135 // Both sides of the transfer commit in one file before the native move reply. 136 inventory := player.inventory 137 item := inventory.Reclaim[index].Item 138 item.Slot = slot 139 inventory.Items = append(slices.Clone(inventory.Items), item) 140 inventory.Reclaim = slices.Delete(slices.Clone(inventory.Reclaim), index, index+1) 141 if len(inventory.Reclaim) == 0 { 142 inventory.Reclaim = nil 143 } 144 if err := server.saveInventory(player, inventory); err != nil { 145 return err 146 } 147 client.reclaimItems[source] = 0 148 reply := append([]byte(nil), body[16:]...) 149 binary.BigEndian.PutUint32(reply[16:20], slot) 150 return client.send(n3Packet(0x47537a24, 50000, client.id, reply)) 151 }