aorecomp

Anarchy Online classic in the browser

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


server/stacks.go (6863B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"maps"
      7 	"slices"
      8 )
      9 
     10 func (item ownedItem) isStackable() bool {
     11 	definition := gamedata.ItemTemplates[item.Template]
     12 	return item.Type == 51005 && definition != nil && itemStat(definition, 30)&(1<<9) != 0
     13 }
     14 
     15 func (server *server) changeStack(client *worldClient, index int, body []byte) error {
     16 	player := server.characters[client.id]
     17 	inventory := player.inventory
     18 	items := inventory.Items
     19 	bag := uint32(0)
     20 	first, end := uint32(64), uint32(94)
     21 	page := binary.BigEndian.Uint32(body[20:24])
     22 	if page == 105 {
     23 		items, first, end = inventory.Bank, 0, bankCapacity
     24 	} else if page == 107 {
     25 		bag = client.backpacks[binary.BigEndian.Uint32(body[24:28])>>16].id
     26 		items, first, end = inventory.Containers[bag], 0, 21
     27 	}
     28 	if !items[index].isStackable() {
     29 		return nil
     30 	}
     31 	items = slices.Clone(items)
     32 	if binary.BigEndian.Uint32(body[16:20]) == 52 {
     33 		// Action 52 carries the count retained in the original, already split locally.
     34 		retained := binary.BigEndian.Uint32(body[32:36])
     35 		if retained == uint32(items[index].Count) && binary.BigEndian.Uint32(body[28:32]) == 0 {
     36 			if bag != 0 {
     37 				// The same retained count can also come from a stale native row.
     38 				client.backpacks[binary.BigEndian.Uint32(body[24:28])>>16] = backpackHandle{bag, false}
     39 				return client.send(backpackClosePacket(client.id, bag))
     40 			}
     41 			if page == 104 {
     42 				return server.syncStacks(client)
     43 			}
     44 			return nil
     45 		}
     46 		if binary.BigEndian.Uint32(body[28:32]) != 0 || retained == 0 || retained > uint32(items[index].Count) {
     47 			return server.syncStackPage(client, page, bag)
     48 		}
     49 		var slot uint32
     50 		for slot = first; slot < end; slot++ {
     51 			if !slices.ContainsFunc(items, func(item ownedItem) bool { return item.Slot == slot }) {
     52 				break
     53 			}
     54 		}
     55 		if slot == end {
     56 			return server.syncStackPage(client, page, bag)
     57 		}
     58 		id, err := server.reserveItems(1)
     59 		if err != nil {
     60 			return err
     61 		}
     62 		item := items[index]
     63 		inventory.LastID = id
     64 		item.ID, item.Slot, item.Count = inventory.LastID, slot, item.Count-uint16(retained)
     65 		items[index].Count = uint16(retained)
     66 		items = append(items, item)
     67 	} else {
     68 		// The native join response keeps the first slot and removes the second.
     69 		otherSlot := binary.BigEndian.Uint32(body[32:36])
     70 		if bag != 0 {
     71 			if otherSlot>>16 != binary.BigEndian.Uint32(body[24:28])>>16 {
     72 				return nil
     73 			}
     74 			otherSlot &= 65535
     75 		}
     76 		other := slices.IndexFunc(items, func(item ownedItem) bool { return item.Slot == otherSlot })
     77 		if other < 0 && page == 104 && otherSlot >= 64 && otherSlot < 94 && binary.BigEndian.Uint32(body[28:32]) == 104 {
     78 			// A previously consumed source still needs the kept stack's current count.
     79 			for _, packet := range equipmentCorrectionPackets(client.id, items[index].Slot) {
     80 				if err := client.send(packet); err != nil {
     81 					return err
     82 				}
     83 			}
     84 			return server.syncStacks(client)
     85 		}
     86 		if binary.BigEndian.Uint32(body[28:32]) != binary.BigEndian.Uint32(body[20:24]) || other < 0 || other == index || items[other].Type != items[index].Type ||
     87 			items[other].Quality != items[index].Quality {
     88 			return nil
     89 		}
     90 		low, high := items[index].templateIDs()
     91 		otherLow, otherHigh := items[other].templateIDs()
     92 		if low != otherLow || high != otherHigh {
     93 			return nil
     94 		}
     95 		count := uint32(items[index].Count) + uint32(items[other].Count)
     96 		// Native ushort addition wraps; prevent loss at its representation ceiling.
     97 		// This is not evidence of the original server's maximum permitted stack.
     98 		if count > 65535 {
     99 			// The ordinary grid hides the removed source while awaiting a reply.
    100 			if page == 104 {
    101 				for _, packet := range equipmentCorrectionPackets(client.id, items[other].Slot) {
    102 					if err := client.send(packet); err != nil {
    103 						return err
    104 					}
    105 				}
    106 			}
    107 			return nil
    108 		}
    109 		items[index].Count = uint16(count)
    110 		items = slices.Delete(items, other, other+1)
    111 	}
    112 
    113 	// Publish quantities and any new identity only after one atomic inventory save.
    114 	if page == 105 {
    115 		inventory.Bank = items
    116 	} else if bag == 0 {
    117 		inventory.Items = items
    118 	} else {
    119 		inventory.Containers = maps.Clone(inventory.Containers)
    120 		inventory.Containers[bag] = items
    121 	}
    122 	if err := server.saveInventory(player, inventory); err != nil {
    123 		return err
    124 	}
    125 	server.nextItem = max(server.nextItem, inventory.LastID)
    126 	return client.send(n3Packet(0x5e477770, 50000, client.id, body[16:]))
    127 }
    128 
    129 func (server *server) syncStackPage(client *worldClient, page, bag uint32) error {
    130 	if page == 105 {
    131 		return client.send(bankInventoryPacket(server.characters[client.id]))
    132 	}
    133 	if bag == 0 {
    134 		return server.syncStacks(client)
    135 	}
    136 	for index, handle := range client.backpacks {
    137 		if handle.id == bag {
    138 			return client.send(backpackInventoryPacket(client.id, bag, index, server.characters[client.id].inventory.Containers[bag]))
    139 		}
    140 	}
    141 	return nil
    142 }
    143 
    144 func (server *server) syncStacks(client *worldClient) error {
    145 	return server.syncStackSource(client, 0)
    146 }
    147 
    148 func (server *server) syncStackSource(client *worldClient, source uint32) error {
    149 	// Preserve live individual items and equipped modifiers while rebuilding
    150 	// supported stacks and a replaced source, removing optimistic split clones.
    151 	player := server.characters[client.id]
    152 	var occupied [30]bool
    153 	for _, item := range player.inventory.Items {
    154 		if item.Slot >= 64 && !item.isStackable() && item.Slot != source {
    155 			occupied[item.Slot-64] = true
    156 		}
    157 	}
    158 	for slot := uint32(64); slot < 94; slot++ {
    159 		if occupied[slot-64] {
    160 			continue
    161 		}
    162 		var body []byte
    163 		for _, value := range []uint32{112, 104, slot, 0, 0} {
    164 			body = binary.BigEndian.AppendUint32(body, value)
    165 		}
    166 		if err := client.send(n3Packet(0x5e477770, 50000, client.id, body)); err != nil {
    167 			return err
    168 		}
    169 	}
    170 
    171 	// Item delivery chooses first-free; move each restored item to its saved slot.
    172 	items := slices.Clone(player.inventory.Items)
    173 	slices.SortFunc(items, func(first, second ownedItem) int { return int(first.Slot) - int(second.Slot) })
    174 	for _, item := range items {
    175 		if item.Slot < 64 || !item.isStackable() && item.Slot != source {
    176 			continue
    177 		}
    178 		if item.Slot == source && (item.Type == 51018 || item.isPortableContainer()) {
    179 			for index, handle := range client.backpacks {
    180 				if handle.id == item.ID && handle.open {
    181 					client.backpacks[index] = backpackHandle{item.ID, false}
    182 					if err := client.send(backpackClosePacket(client.id, item.ID)); err != nil {
    183 						return err
    184 					}
    185 				}
    186 			}
    187 			if err := client.send(n3Packet(0x36510078, item.Type, item.ID, nil)); err != nil {
    188 				return err
    189 			}
    190 		}
    191 		if err := client.send(inventoryAdditionPacket(player, item)); err != nil {
    192 			return err
    193 		}
    194 		slot := uint32(64)
    195 		for occupied[slot-64] {
    196 			slot++
    197 		}
    198 		if slot != item.Slot {
    199 			if err := client.send(itemMovePacket(client.id, slot, item.Slot)); err != nil {
    200 				return err
    201 			}
    202 		}
    203 		occupied[item.Slot-64] = true
    204 	}
    205 	return nil
    206 }