1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
package world
import (
"strings"
)
const DropDespawnTicks = 1000
const ReserveTicks = 100
type GroundItemInfo struct {
ItemID string
Quantity int
Quality int
MaxQuality int
ReservedFor string
ReserveTimer int
DespawnTimer int
IsSpawn bool
}
type groundEntry struct {
itemID string
quantity int
quality int
maxQuality int
isSpawn bool
respawnTimer int // >0 = counting down to respawn
respawnQty int
respawnDelay int
despawnTimer int // >0 = counting down to despawn (dropped items)
reservedFor string
reserveTimer int // >0 = counting down reservation
}
func (w *World) GroundItemsDetailed(roomID int) []GroundItemInfo {
w.mu.Lock()
defer w.mu.Unlock()
var out []GroundItemInfo
for _, e := range w.groundItems[roomID] {
if e.quantity <= 0 {
continue
}
info := GroundItemInfo{
ItemID: e.itemID,
Quantity: e.quantity,
Quality: e.quality,
MaxQuality: e.maxQuality,
DespawnTimer: e.despawnTimer,
IsSpawn: e.isSpawn,
}
if e.reserveTimer > 0 && e.reservedFor != "" {
info.ReservedFor = e.reservedFor
info.ReserveTimer = e.reserveTimer
}
out = append(out, info)
}
return out
}
func (w *World) GroundItems(roomID int) map[string]int {
w.mu.Lock()
defer w.mu.Unlock()
out := make(map[string]int)
for _, e := range w.groundItems[roomID] {
if e.quantity > 0 {
out[e.itemID] += e.quantity
}
}
return out
}
func (w *World) AddReservedItem(roomID int, itemID string, qty int, owner string) {
w.AddReservedItemQ(roomID, itemID, qty, owner, 0, 0)
}
// AddReservedItemQ is AddReservedItem with the item's quality preserved (a
// non-stackable max_quality item dropped on death/trigger keeps its charge).
func (w *World) AddReservedItemQ(roomID int, itemID string, qty int, owner string, quality, maxQuality int) {
w.mu.Lock()
defer w.mu.Unlock()
e := &groundEntry{
itemID: itemID,
quantity: qty,
quality: quality,
maxQuality: maxQuality,
despawnTimer: DropDespawnTicks,
reservedFor: owner,
reserveTimer: ReserveTicks,
}
w.groundItems[roomID] = append(w.groundItems[roomID], e)
}
func (w *World) AddGroundItem(roomID int, itemID string, qty int) {
w.AddGroundItemQ(roomID, itemID, qty, 0, 0)
}
// AddGroundItemQ is AddGroundItem with the item's quality preserved. A
// dropped partially-used max_quality item must not come back full on pickup.
func (w *World) AddGroundItemQ(roomID int, itemID string, qty, quality, maxQuality int) {
w.mu.Lock()
defer w.mu.Unlock()
e := &groundEntry{
itemID: itemID,
quantity: qty,
quality: quality,
maxQuality: maxQuality,
despawnTimer: DropDespawnTicks,
}
w.groundItems[roomID] = append(w.groundItems[roomID], e)
}
// RemoveReservedItem removes up to qty of itemID from the room's ground,
// refusing entries reserved for another player. It is ALL-OR-NOTHING:
// when any part of the quantity is unobtainable (a reserved-for-others
// entry, or too little accessible stock) nothing is taken and (0, false)
// is returned. Callers must never lose already-removed items to a failed
// pickup, so the removal is verified before any state is touched.
func (w *World) RemoveReservedItem(roomID int, itemID string, qty int, owner string) (int, bool) {
removed, _, _, ok := w.removeReservedItem(roomID, itemID, qty, owner)
return removed, ok
}
// RemoveReservedItemQ is RemoveReservedItem that also reports the quality of
// the entry the pickup actually came from, so a partially-used non-stackable
// item keeps its charge through a drop/pickup round trip.
func (w *World) RemoveReservedItemQ(roomID int, itemID string, qty int, owner string) (removed, quality, maxQuality int, ok bool) {
return w.removeReservedItem(roomID, itemID, qty, owner)
}
func (w *World) removeReservedItem(roomID int, itemID string, qty int, owner string) (removed, quality, maxQuality int, ok bool) {
w.mu.Lock()
defer w.mu.Unlock()
entries := w.groundItems[roomID]
// Pass 1: verify the full quantity is takable by this owner without
// mutating anything, so a blocked pickup can never leave the ground
// half-emptied with nothing delivered.
available := 0
for _, e := range entries {
if available >= qty {
break
}
if e.quantity <= 0 || !strings.EqualFold(e.itemID, itemID) {
continue
}
if e.reserveTimer > 0 && e.reservedFor != "" && e.reservedFor != owner {
continue
}
available += e.quantity
}
if available < qty {
return 0, 0, 0, false
}
// Pass 2: take. Every entry reachable here is takeable, so no
// mid-loop abort is possible. The ownership check MUST match pass 1:
// without it a player could consume a stack reserved for someone else
// when a public stack and a reserved stack coexist (the reserved entry
// comes first in the slice, so the take loop would drain it first).
remaining := qty
tookQuality, tookMaxQuality := 0, 0
tookFromEntry := false
for _, e := range entries {
if remaining <= 0 {
break
}
if e.quantity <= 0 || !strings.EqualFold(e.itemID, itemID) {
continue
}
if e.reserveTimer > 0 && e.reservedFor != "" && e.reservedFor != owner {
continue
}
take := e.quantity
if take > remaining {
take = remaining
}
if !tookFromEntry {
tookQuality, tookMaxQuality = e.quality, e.maxQuality
tookFromEntry = true
}
e.quantity -= take
removed += take
remaining -= take
if e.isSpawn && e.quantity <= 0 && e.respawnDelay > 0 {
e.respawnTimer = e.respawnDelay
}
}
return removed, tookQuality, tookMaxQuality, true
}
// AccessibleGroundQuality reports the quality of the first ground entry this
// owner may take, so a pickup can stamp the item it is about to create. The
// boolean is false when no accessible entry exists.
func (w *World) AccessibleGroundQuality(roomID int, itemID, owner string) (quality, maxQuality int, ok bool) {
w.mu.Lock()
defer w.mu.Unlock()
for _, e := range w.groundItems[roomID] {
if e.quantity <= 0 || !strings.EqualFold(e.itemID, itemID) {
continue
}
if e.reserveTimer > 0 && e.reservedFor != "" && e.reservedFor != owner {
continue
}
return e.quality, e.maxQuality, true
}
return 0, 0, false
}
// RemoveAvailableItem removes up to maxQty of itemID from the room's
// ground, skipping entries reserved for another player, and returns the
// quantity actually removed. Unlike RemoveReservedItem it is not
// all-or-nothing: callers pass a generous upper bound (credit pickups use
// a sentinel far above any possible stock) and take whatever is accessible
// at the moment, so a request larger than the available stock still
// succeeds.
func (w *World) RemoveAvailableItem(roomID int, itemID string, maxQty int, owner string) int {
w.mu.Lock()
defer w.mu.Unlock()
removed := 0
remaining := maxQty
for _, e := range w.groundItems[roomID] {
if remaining <= 0 {
break
}
if e.quantity <= 0 || !strings.EqualFold(e.itemID, itemID) {
continue
}
if e.reserveTimer > 0 && e.reservedFor != "" && e.reservedFor != owner {
continue
}
take := e.quantity
if take > remaining {
take = remaining
}
e.quantity -= take
removed += take
remaining -= take
if e.isSpawn && e.quantity <= 0 && e.respawnDelay > 0 {
e.respawnTimer = e.respawnDelay
}
}
return removed
}
// AccessibleGround returns the ground items in roomID this owner may take:
// entries reserved for another player are excluded. Pickup flows use this
// to size requests and match names against takeable stock only.
func (w *World) AccessibleGround(roomID int, owner string) map[string]int {
w.mu.Lock()
defer w.mu.Unlock()
out := make(map[string]int)
for _, e := range w.groundItems[roomID] {
if e.quantity <= 0 {
continue
}
if e.reserveTimer > 0 && e.reservedFor != "" && e.reservedFor != owner {
continue
}
out[e.itemID] += e.quantity
}
return out
}
func (w *World) RemoveGroundItem(roomID int, itemID string, qty int) int {
w.mu.Lock()
defer w.mu.Unlock()
removed := 0
remaining := qty
for _, e := range w.groundItems[roomID] {
if remaining <= 0 {
break
}
if !strings.EqualFold(e.itemID, itemID) {
continue
}
if e.quantity <= 0 {
continue
}
// This helper has no owner, so it must not consume another player's
// reserved entry (its siblings are reservation-aware).
if e.reserveTimer > 0 && e.reservedFor != "" {
continue
}
take := e.quantity
if take > remaining {
take = remaining
}
e.quantity -= take
removed += take
remaining -= take
if e.isSpawn && e.quantity <= 0 && e.respawnDelay > 0 {
e.respawnTimer = e.respawnDelay
}
}
return removed
}
func (w *World) tickGroundItems() {
for roomID, entries := range w.groundItems {
for _, e := range entries {
if e.respawnTimer > 0 {
e.respawnTimer--
if e.respawnTimer <= 0 {
e.quantity = e.respawnQty
w.mergeNearbyItems(entries, e)
}
}
if e.despawnTimer > 0 {
e.despawnTimer--
if e.despawnTimer <= 0 {
e.quantity = 0
}
}
if e.reserveTimer > 0 {
e.reserveTimer--
if e.reserveTimer <= 0 {
e.reservedFor = ""
}
}
}
// Prune dead entries: without this every drop/loot/firemaking action
// appends an entry that lives for the process lifetime and is scanned
// on every tick and pickup. A spawn entry awaiting respawn keeps its
// slot; everything else with no quantity is gone for good.
kept := entries[:0]
for _, e := range entries {
if e.quantity <= 0 && e.respawnTimer <= 0 {
continue
}
kept = append(kept, e)
}
if len(kept) == 0 {
delete(w.groundItems, roomID)
} else {
w.groundItems[roomID] = kept
}
}
}
func (w *World) mergeNearbyItems(entries []*groundEntry, target *groundEntry) {
for _, other := range entries {
if other != target && other.quantity > 0 && strings.EqualFold(other.itemID, target.itemID) &&
other.quality == target.quality && other.maxQuality == target.maxQuality &&
(other.reserveTimer <= 0 || other.reservedFor == "") &&
other.despawnTimer <= 0 {
target.quantity += other.quantity
other.quantity = 0
}
}
}
|