-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems.lua
431 lines (413 loc) · 18 KB
/
items.lua
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
items = {
server = {
container = {}
},
client = {
container = {},
requested = {}
}
}
local imageId2Type = {
armor0Helmet = 'helmet', armor1Helmet = 'helmet',
armor0Chest = 'chest', armor1Chest = 'chest',
armor0Pants = 'pants', armor1Pants = 'pants'
}
for _, v in ipairs{'sword0', 'sword1', 'sword2', 'sword3', 'sword4'} do
imageId2Type[v] = 'sword'
end
function items.server.newItem(data)
if not data.id then
data.id = lume.uuid()
end
if not data.imageId then
data.imageId = 'apple'
end
if not data.type then
data.type = imageId2Type[data.imageId]
data.type = data.type or data.imageId
end
items.server.container[data.id] = data
return data.id
end
function items.server.reset()
items.server.container = {}
end
function items.server.getItem(id)
if id == nil then return nil end
return items.server.container[id]
end
function items.client.reset()
items.client.container = {}
items.client.requested = {}
end
function items.client.getItem(id)
if id == nil then return nil end
local item = items.client.container[id]
if item then
return item
else
if not items.client.requested[id] then
client.nutClient:sendRPC('getItem', bitser.dumps{id=id})
items.client.requested[id] = true
end
return nil
end
end
-- todo: refactor
function items.client.mousepressed(x, y, btn, isTouch, presses)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
local wmx, wmy = camera:screen2world(mx, my)
-- hud
local bag = playerController.player.inventory
local panel = hud.inventoryPanel
local pmx = mx - lume.round(panel.x)
local pmy = my - lume.round(panel.y)
for slotId, slot in ipairs(hud.inventorySlots) do
if pmx >= slot.x and pmx <= slot.x + slot.w
and pmy >= slot.y and pmy <= slot.y + slot.h and panel.open then
uiMouseDown = true
if bag.items[slotId] then
-- use item
if btn == 1 and (love.keyboard.isScancodeDown('lshift') or presses > 1) then
client.useItem{
bagId = bag.id,
slotId = slotId
}
end
-- move items
if btn == 1 then
local heldItem = playerController.heldItem
heldItem.itemId = bag.items[slotId]
heldItem.bagId = bag.id
heldItem.slotId = slotId
heldItem.offset.x = slot.x - pmx
heldItem.offset.y = slot.y - pmy
elseif btn == 2 then
local closestBag = playerController.closestBag
local cqb = playerController.closestQuestBlock
if closestBag.id and closestBag.open then
local bagTo = client.currentState.lootBags[closestBag.id]
for bagSlotId, _ in ipairs(lootBagSlots) do
if bagTo.items[bagSlotId] == nil then
client.moveItem{
from = {
bagId = bag.id,
slotId = slotId
},
to = {
bagId = bagTo.id,
slotId = bagSlotId
}
}
break
end
end
elseif cqb.id and cqb.open then
local qb = client.currentState.entities[cqb.id]
if qb then
local item = items.client.getItem(bag.items[slotId])
if not item then break end
for questSlotId, questItemId in pairs(quests.current.cost) do
local questItem = items.client.getItem(questItemId)
if not questItem then break end
if quests.current.heldItems[questSlotId] == nil
and item.imageId == questItem.imageId then
quests.current.heldItems[questSlotId] = bag.items[slotId]
client.setInventorySlot{
slotId = slotId,
itemId = nil
}
break
end
end
end
end
end
end
end
end
-- lootBags
local closestBag = playerController.closestBag
if closestBag.id and closestBag.open then
local bag = client.currentState.lootBags[closestBag.id]
local img = gfx.ui.bag
local bmx = wmx - (lume.round(bag.x) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(bag.y) - img:getHeight() - 20)
for slotId, slot in ipairs(lootBagSlots) do
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
uiMouseDown = true
if bag.items[slotId] then
if btn == 1 then
local heldItem = playerController.heldItem
heldItem.itemId = bag.items[slotId]
heldItem.bagId = bag.id
heldItem.slotId = slotId
heldItem.offset.x = slot.x - bmx
heldItem.offset.y = slot.y - bmy
elseif btn == 2 then
local p = playerController.player
local item = items.client.getItem(bag.items[slotId])
if not item then break end
for invSlotId, _ in ipairs(hud.inventorySlots) do
local slotType = slot2type[invSlotId]
if p.inventory.items[invSlotId] == nil
and (slotType == nil or slotType == item.type) then
client.moveItem{
from = {
bagId = bag.id,
slotId = slotId
},
to = {
bagId = p.inventory.id,
slotId = invSlotId
}
}
break
end
end
end
end
break
end
end
end
-- quest
local cqb = playerController.closestQuestBlock
if cqb.id and cqb.open then
local qb = client.currentState.entities[cqb.id]
if qb then
local img = gfx.ui.quest
local bmx = wmx - (lume.round(qb.x + 8) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(qb.y + 8) - img:getHeight() - 20)
for slotId, slot in ipairs(questBlockSlots) do
local exists = true
local item = items.client.getItem(quests.current.heldItems[slotId])
if not item then
exists = false
if slotId <= 4 then
item = items.client.getItem(quests.current.cost[slotId])
else
item = items.client.getItem(quests.current.reward[slotId - 4])
end
end
if slotId >= 5 then
local allExist = true
for i, item in ipairs(quests.current.cost) do
if not quests.current.heldItems[i] then
allExist = false
break
end
end
exists = allExist
end
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
uiMouseDown = true
if item and exists then
if btn == 1 then
local heldItem = playerController.heldItem
heldItem.itemId = item.id
heldItem.bagId = 'quest'
heldItem.slotId = slotId
heldItem.offset.x = slot.x - bmx
heldItem.offset.y = slot.y - bmy
elseif btn == 2 then
local bag = playerController.player.inventory
for invSlotId, _ in ipairs(hud.inventorySlots) do
local slotType = slot2type[invSlotId]
if bag.items[invSlotId] == nil
and (slotType == nil or slotType == item.type) then
client.setInventorySlot{
slotId = invSlotId,
itemId = item.id
}
if slot.type == 'cost' then
quests.current.heldItems[slotId] = nil
elseif slot.type == 'reward' then
quests.refresh()
end
break
end
end
end
end
end
end
end
end
end
function items.client.mousereleased(x, y, btn, isTouch, presses)
local mx, my = window2game(x, y)
mx, my = lume.round(mx), lume.round(my)
local wmx, wmy = camera:screen2world(mx, my)
if btn == 1 then
-- hud
local heldItem = playerController.heldItem
local itemIsHeld = true
if heldItem.bagId then
local bagFrom = client.currentState.lootBags[heldItem.bagId]
if heldItem.bagId == 'inventory' then
bagFrom = playerController.player.inventory
end
local bagTo = playerController.player.inventory
local panel = hud.inventoryPanel
local pmx = mx - lume.round(panel.x)
local pmy = my - lume.round(panel.y)
if pmx > 0 and pmx < panel.img:getWidth()
and pmy > 0 and pmy < panel.img:getHeight() then
for slotId, slot in ipairs(hud.inventorySlots) do
if pmx >= slot.x and pmx <= slot.x + slot.w
and pmy >= slot.y and pmy <= slot.y + slot.h then
local item = items.client.getItem(heldItem.itemId)
if not item then break end
local slotType = slot2type[slotId]
if (slotType == nil or slotType == item.type) then
if heldItem.bagId == 'quest' then
client.setInventorySlot{
slotId = slotId,
itemId = heldItem.itemId
}
if heldItem.slotId <= 4 then
quests.current.heldItems[heldItem.slotId] = nil
elseif heldItem.slotId == 5 then
quests.refresh()
end
itemIsHeld = false
break
end
client.moveItem{
from = {
bagId = bagFrom.id,
slotId = heldItem.slotId
},
to = {
bagId = bagTo.id,
slotId = slotId
}
}
itemIsHeld = false
-- move clientside before response (will be corrected/affirmed)
local temp = bagTo.items[slotId]
bagTo.items[slotId] = bagFrom.items[heldItem.slotId]
bagFrom.items[heldItem.slotId] = temp
break
end
end
end
-- move to open slot if dropped in inventory panel
if itemIsHeld then
for invSlotId, _ in ipairs(hud.inventorySlots) do
local item = items.client.getItem(heldItem.itemId)
if not item then break end
local slotType = slot2type[invSlotId]
if bagTo.items[invSlotId] == nil
and (slotType == nil or slotType == item.type) then
if heldItem.bagId == 'quest' then
client.setInventorySlot{
slotId = invSlotId,
itemId = heldItem.itemId
}
if heldItem.slotId <= 4 then
quests.current.heldItems[heldItem.slotId] = nil
elseif heldItem.slotId == 5 then
quests.refresh()
end
itemIsHeld = false
break
end
client.moveItem{
from = {
bagId = bagFrom.id,
slotId = heldItem.slotId
},
to = {
bagId = bagTo.id,
slotId = invSlotId
}
}
itemIsHeld = false
break
end
end
end
end
end
-- lootBags
local closestBag = playerController.closestBag
local heldItem = playerController.heldItem
if closestBag.id and closestBag.open and heldItem.bagId then
local bagFrom = client.currentState.lootBags[heldItem.bagId]
if heldItem.bagId == 'inventory' then
bagFrom = playerController.player.inventory
end
local bagTo = client.currentState.lootBags[closestBag.id]
local img = gfx.ui.bag
local bmx = wmx - (lume.round(bagTo.x) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(bagTo.y) - img:getHeight() - 20)
for slotId, slot in ipairs(lootBagSlots) do
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
if heldItem.bagId == 'quest' then
break
end
client.moveItem{
from = {
bagId = bagFrom.id,
slotId = heldItem.slotId
},
to = {
bagId = bagTo.id,
slotId = slotId
}
}
itemIsHeld = false
-- move clientside before response (will be corrected/affirmed)
local temp = bagTo.items[slotId]
bagTo.items[slotId] = bagFrom.items[heldItem.slotId]
bagFrom.items[heldItem.slotId] = temp
break
end
end
end
-- quest
local cqb = playerController.closestQuestBlock
if cqb.id and cqb.open and heldItem.bagId == 'inventory' then
local qb = client.currentState.entities[cqb.id]
if qb then
local p = playerController.player
local img = gfx.ui.quest
local bmx = wmx - (lume.round(qb.x + 8) - lume.round(img:getWidth()/2))
local bmy = wmy - (lume.round(qb.y + 8) - img:getHeight() - 20)
for slotId=1, 4 do
local slot = questBlockSlots[slotId]
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
local item = items.client.getItem(heldItem.itemId)
if not item then break end
local questItemId = quests.current.cost[slotId]
local questItem = items.client.getItem(questItemId)
if not questItem then break end
if quests.current.heldItems[slotId] == nil
and item.imageId == questItem.imageId then
quests.current.heldItems[slotId] = p.inventory.items[heldItem.slotId]
itemIsHeld = false
client.setInventorySlot{
slotId = heldItem.slotId,
itemId = nil
}
break
end
end
end
end
end
if heldItem.bagId == 'inventory' and itemIsHeld then
client.dropItem{
slotId = heldItem.slotId
}
itemIsHeld = false
end
end
end