Room = {}
function Room:new(pos, sqarea)
return setmetatable({pos=pos, sqarea=sqarea, monsters={}}, {__index = Room})
end
function Room:isInside(pos)
if pos.x >= self.pos.x-(self.sqarea-1)/2 and pos.y >= self.pos.y-(self.sqarea-1)/2 and pos.x <= self.pos.x+(self.sqarea-1)/2 and pos.y <= self.pos.y+(self.sqarea-1)/2 then
return true
end
end
function Room:teleportPlayer(player)
if not self.player or not (isPlayer(self.player) and self:isInside(getCreaturePosition(self.player))) then
self.player = player
doTeleportThing(player, self.pos, false)
doSendMagicEffect(self.pos, 10)
return true
end
end
function Room:addMonster(name)
local bool, monsters = self:areMonstersDead()
if not bool then
for i = 1, #monsters do
doRemoveCreature(monsters[i])
end
end
for y = self.pos.y-1, self.pos.y+1 do
for x = self.pos.x-1, self.pos.x+1 do
if x ~= self.pos.x or y ~= self.pos.y then
local _cid = doSummonCreature(name, {x=x, y=y, z=self.pos.z})
table.insert(self.monsters, _cid)
end
end
end
end
function Room:areMonstersDead()
local ret = true
local t = {}
for i, creature in ipairs(self.monsters) do
if isCreature(creature) then
ret = false
table.insert(t, creature)
end
end
return ret, t
end
function isPlayerInside(rooms, player)
local ppos = getCreaturePosition(player)
for i, room in ipairs(rooms) do
if room:isInside(ppos) and room.player == player then
return room
end
end
end
function getFreeRooms(rooms)
local free = {}
for i, room in ipairs(rooms) do
if not room.player or not (isPlayer(room.player) and room:isInside(getCreaturePosition(room.player))) then
table.insert(free, room)
end
end
return free
end
local square = 3 -- SQUARE SIZE OF ROOM MUST BE AN ODD NUMBER! 3x3, 5x5 etc..
-- Room:new(centerpos, squaresize)
local rooms = {
[1] = Room:new({x=277, y=59, z=7}, square),
[2] = Room:new({x=281, y=59, z=7}, square),
[3] = Room:new({x=285, y=59, z=7}, square),
}
local successchance = 1/10 -- 1 out of 10 tries, 10%
local successpos = {x=270, y=56, z=7} -- pos of final room
local monsters = {"wolf", "troll"} -- monsters
function onStepIn(cid, item, pos, frompos)
if isPlayer(cid) then
local freeRooms = getFreeRooms(rooms)
local isinside = isPlayerInside(rooms, cid)
if (isinside and #freeRooms > 0) or #freeRooms > 1 then
if isinside and not isinside:areMonstersDead() then
doTeleportThing(cid, frompos, true)
return doPlayerSendCancel(cid, "You got to kill all monsters first.")
end
local successrd = math.random(1, 100)
if isinside and successrd <= successchance*100 then
doTeleportThing(cid, successpos)
doSendMagicEffect(successpos, 10)
else
local rd = math.random(#freeRooms)
freeRooms[rd]:teleportPlayer(cid)
freeRooms[rd]:addMonster(monsters[math.random(#monsters)])
end
else
doTeleportThing(cid, frompos, true)
return doPlayerSendCancel(cid, "All rooms are full now, please wait.")
end
end
end