Skip to content

Commit

Permalink
fix multi quests, quick-take from quest cost
Browse files Browse the repository at this point in the history
parameterized committed Mar 1, 2019
1 parent dfd549d commit b49e52e
Showing 4 changed files with 87 additions and 112 deletions.
2 changes: 1 addition & 1 deletion client.lua
Original file line number Diff line number Diff line change
@@ -124,7 +124,6 @@ function client.connect(ip, port)
slimeBalls.reset()
items.client.reset()
damageText.reset()
quests.refresh()
collectgarbage()
end
client.currentState = client.newState()
@@ -133,6 +132,7 @@ function client.connect(ip, port)

clientRealm:load()
playerController.load()
quests.refresh()
end

function client.newState()
180 changes: 76 additions & 104 deletions items.lua
Original file line number Diff line number Diff line change
@@ -10,22 +10,27 @@ items = {
}

function items.server.newItem(data)
data.id = lume.uuid()

data.type = data.imageId
if isSword[data.imageId] then
data.type = 'sword'
end
if data.imageId == 'armor0Helmet' or data.imageId == 'armor1Helmet' then
data.type = 'helmet'
if not data.id then
data.id = lume.uuid()
end
if data.imageId == 'armor0Chest' or data.imageId == 'armor1Chest' then
data.type = 'chest'
if not data.imageId then
data.imageId = 'apple'
end
if data.imageId == 'armor0Pants' or data.imageId == 'armor1Pants' then
data.type = 'pants'
if not data.type then
data.type = data.imageId
if isSword[data.imageId] then
data.type = 'sword'
end
if data.imageId == 'armor0Helmet' or data.imageId == 'armor1Helmet' then
data.type = 'helmet'
end
if data.imageId == 'armor0Chest' or data.imageId == 'armor1Chest' then
data.type = 'chest'
end
if data.imageId == 'armor0Pants' or data.imageId == 'armor1Pants' then
data.type = 'pants'
end
end

items.server.container[data.id] = data
return data.id
end
@@ -94,6 +99,7 @@ function items.client.mousepressed(x, y, btn, isTouch, presses)
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
@@ -111,6 +117,25 @@ function items.client.mousepressed(x, y, btn, isTouch, presses)
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
@@ -165,113 +190,60 @@ function items.client.mousepressed(x, y, btn, isTouch, presses)
end

-- quest
if btn == 1 then
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
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
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
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
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
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
end
end
end
end
end
elseif btn == 2 then
local cqb = playerController.closestQuestBlock
if cqb.id and cqb.open then
local qb = client.currentState.entities[cqb.id]
if qb then
-- give items
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
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

-- take reward
local allExist = true
for i, item in ipairs(quests.current.cost) do
if not quests.current.heldItems[i] then
allExist = false
break
end
end
if allExist 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)
local slot = questBlockSlots[5]
if bmx >= slot.x and bmx <= slot.x + slot.w
and bmy >= slot.y and bmy <= slot.y + slot.h then
uiMouseDown = true
local itemId = quests.current.reward[1]
local item = items.client.getItem(itemId)
if item then
elseif btn == 2 then
local bag = playerController.player.inventory
for invSlotId, _ in ipairs(hud.inventorySlots) do
local slotType = slot2type[invSlotId]
-- bag = player inventory
if bag.items[invSlotId] == nil
and (slotType == nil or slotType == item.type) then
client.setInventorySlot{
slotId = invSlotId,
itemId = itemId
itemId = item.id
}
quests.refresh()
if slot.type == 'cost' then
quests.current.heldItems[slotId] = nil
elseif slot.type == 'reward' then
quests.refresh()
end
break
end
end
14 changes: 7 additions & 7 deletions quests.lua
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ for k, v in ipairs{'cost', 'reward'} do
end
end

quests.current = {cost={}, reward={}, heldItems={}}
function quests.refresh()
quests.current = {cost={}, reward={}, heldItems={}}
local choices = {
@@ -24,26 +25,25 @@ function quests.refresh()
}
for _=1, 2 do
choice = lume.weightedchoice(choices)
local itemData = {imageId=choice}
local itemData = {id=lume.uuid(), imageId=choice}
if choice == 'sword0' then
itemData.atk = math.max(5, math.floor(love.math.randomNormal()*2+10))
elseif choice =='sword1' then
itemData.atk = math.max(5, math.floor(love.math.randomNormal()*2+12))
end
local itemId = items.server.newItem(itemData)
table.insert(quests.current.cost, itemId)
client.newItem(itemData)
table.insert(quests.current.cost, itemData.id)
end
local choices = {sword2=40, sword3=40, sword4=20}
choice = lume.weightedchoice(choices)
local itemData = {imageId=choice}
local itemData = {id=lume.uuid(), imageId=choice}
if choice =='sword2' then
itemData.atk = math.max(5, math.floor(love.math.randomNormal()*2+14))
elseif choice =='sword3' then
itemData.atk = math.max(5, math.floor(love.math.randomNormal()*2+16))
elseif choice =='sword4' then
itemData.atk = math.max(5, math.floor(love.math.randomNormal()*2+18))
end
local itemId = items.server.newItem(itemData)
table.insert(quests.current.reward, itemId)
client.newItem(itemData)
table.insert(quests.current.reward, itemData.id)
end
quests.refresh()
3 changes: 3 additions & 0 deletions server.lua
Original file line number Diff line number Diff line change
@@ -169,6 +169,9 @@ function server.start(port, singleplayer)
setInventorySlot = function(self, data, clientId)
local p = server.currentState.players[clientId]
p.inventory.items[data.slotId] = data.itemId
end,
newItem = function(self, data, clientId)
items.server.newItem(data)
end
}
for k, v in pairs(bitserRPCs) do

0 comments on commit b49e52e

Please sign in to comment.