local BREAD_COUNTER_STORAGE = 1700040
local BREAD_ACHIEVEMENT_STORAGE = 170004
local BREAD_GOAL = 200
function onAddItem(moveitem, tileitem, position)
if moveitem:getId() == 2693 then
moveitem:transform(2689)
position:sendMagicEffect(CONST_ME_HITBYFIRE)
-- Hitta spelaren som "troligen" la degen (den närmsta spelaren)
local spectators = Game.getSpectators(position, false, true, 1, 1, 0, 0)
local closestPlayer = nil
local minDist = 2
for _, player in ipairs(spectators) do
if player:isPlayer() then
local dist = math.max(math.abs(player:getPosition().x - position.x), math.abs(player:getPosition().y - position.y))
if dist < minDist then
minDist = dist
closestPlayer = player
end
end
end
if closestPlayer then
local count = closestPlayer:getStorageValue(BREAD_COUNTER_STORAGE)
if count < 0 then count = 0 end
count = count + 1
closestPlayer:setStorageValue(BREAD_COUNTER_STORAGE, count)
if count >= BREAD_GOAL and closestPlayer:getStorageValue(BREAD_ACHIEVEMENT_STORAGE) ~= 1 then
closestPlayer:setStorageValue(BREAD_ACHIEVEMENT_STORAGE, 1)
closestPlayer:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Achievement Unlocked: Master of Crust!")
end
end
elseif moveitem:getId() == 6277 then
moveitem:transform(2687, 12)
position:sendMagicEffect(CONST_ME_HITBYFIRE)
end
return true
end