aboutsummaryrefslogtreecommitdiff
path: root/internal/world/ground.go
blob: 2d5ea7ba91fdbb20fc372a65dd91f73db77db9dc (plain)
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
package world

import (
	"strings"
)

const DropDespawnTicks = 1000
const ReserveTicks = 100

type GroundItemInfo struct {
	ItemID       string
	Quantity     int
	ReservedFor  string
	ReserveTimer int
	DespawnTimer int
	IsSpawn      bool
}

type groundEntry struct {
	itemID       string
	quantity     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,
			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.mu.Lock()
	defer w.mu.Unlock()
	e := &groundEntry{
		itemID:       itemID,
		quantity:     qty,
		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.mu.Lock()
	defer w.mu.Unlock()
	e := &groundEntry{
		itemID:       itemID,
		quantity:     qty,
		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) {
	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, false
	}

	// Pass 2: take. Every entry reachable here is takeable, so no
	// mid-loop abort is possible.
	removed := 0
	remaining := qty
	for _, e := range entries {
		if remaining <= 0 {
			break
		}
		if e.quantity <= 0 || !strings.EqualFold(e.itemID, itemID) {
			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, true
}

// 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
		}
		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 _, 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 = ""
				}
			}
		}
	}
}

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.reserveTimer <= 0 || other.reservedFor == "") &&
			other.despawnTimer <= 0 {
			target.quantity += other.quantity
			other.quantity = 0
		}
	}
}