Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------
- -- /lib/Portal ----------------
- -------------------------------
- --- This package keeps functions that deal with the creation, drawing
- -- of portals
- --- A portal is a state machine with states: start, join, closed
- local portal_states_names = {
- start = "start",
- join = "join",
- closed = "closed"
- }
- --local p = {} -- the local table to contain the function
- --portal = p -- the public API in the form of a table
- --------------------------------------------------------------------
- ------------ "START" STATE FUNCTIONS -------------------------------
- local state_start = {}
- state_start.name = portal_states_names.start
- --- enterState_start(self)
- function state_start.enter(self)
- -- self.kew = self.assigned_kew or nil
- -- self:checkKewAvailability()
- self:drawportal()
- end
- --- updateState_start()
- function state_start.update(self)
- -- portal:doPortal = function(self)
- -- if assigned_queue and portal.queue.isInPlay() then return portal:setState(join)
- -- portal:checkKewAvailability()
- end
- --- exitState_start = nil
- state_start.exit = nil --turn to function if action is needed on exiting start state
- --------------------------------------------------------------------
- ------------ "JOIN" STATE FUNCTIONS --------------------------------
- local state_join = {}
- state_join.name = portal_states_names.join
- --- enterState_join(self)
- function state_join.enter(self)
- self:drawportal()
- end
- --- updateState_join(self)
- function state_join.update(self)
- -- portal:doPortal()
- -- if portal.queue.isFull() or portal.queue.isGameover() then return portal:setState(closed)
- end
- -- exitState_join = nil
- state_join.exit = nil --turn to function if action is needed on exiting join state
- ----------------------------------------------------------------------
- ------------ "CLOSED" STATE FUNCTIONS --------------------------------
- local state_closed = {}
- state_closed.name = portal_states_names.closed
- --- enterState_closed(self)
- function state_closed.enter(self)
- self:drawportal()
- -- tp all on the waitlist 1 block up
- -- discard waitlist
- end
- --- updateState_closed(self)
- function state_closed.update(self)
- -- if not assigned_queue and game:getNextQueue() then return self:setState(start)
- -- if assigned_queue and self.queue.isDormant() then return self:setState(start)
- end
- -- exitState_closed = nil
- state_closed.exit = nil --turn to function if action is needed on exiting closed state
- --------------------------------------------------------------------
- -------------------------------
- --- PRIVATE FUNCTIONS ---------
- -------------------------------
- local portal_states = { state_start, state_join, state_closed }
- --- Sets an assigned kew for a portal
- local function _setKew(self, kew)
- self.assigned_kew = kew
- end
- local function _doPortal(self)
- -- if found creative players then change them to adventure mode
- -- if found adventure players then add to portal.waitlist
- -- pop the first player in portal.waitlist and do
- -- portal.queue:addPlayer(player)
- -- {adds player to playlerlist;
- -- if portal.queue:getState() == inplay then
- -- scatterPlayerIntoArea(player {has player.isPlayer}, portal.queue.buildzone.box)}
- end
- --- Update the blocks in a Portal Zone in Minecraft
- -- This is also used to visually "close" a portal to players
- -- @param portal A portal object created with newPortal. A kew.portal can be passed
- -- @param queue_phase (Optional) The phase in which the game queue of the buildzone corresponding to this portal is currently in. The default phase is dormant
- -- TODO remove the dependencies on "registry."
- local function _drawPortal(self) --,queue_phase)
- debug.log("Drawing a portal at: "..self.box.corner_x..", "..self.box.corner_y..", "..self.box.corner_z)
- if self.state == portal_states_names.start then
- --fill the zone with air to clear it
- mcset.fillBox( self.box, registry.BLOCKS.AIR )
- --fill with detector blocks one level below ground
- mcset.fillBox( box_base( self.box ), registry.BLOCKS.DETECT )
- elseif self.state == portal_states_names.join then
- --fill the zone with air to clear it
- mcset.fillBox( self.box, registry.BLOCKS.AIR )
- --fill with detector blocks one level below ground
- mcset.fillBox( box_base( self.box ), registry.BLOCKS.DETECT )
- --mark with a construnction block the middle point
- mcset.setBlock(self.box.base_center_x, self.box.base_center_y + 1, self.box.base_center_z, registry.BLOCKS.CONSTRUCTION)
- elseif self.state == portal_states_names.closed then
- --fill with construction block the ground level to mark as inaccessible
- mcset.fillBox( box_move_y( box_base(self.box), 1),registry.BLOCKS.CONSTRUCTION )
- end
- end
- local function _checkKewAvailability(self)
- -- if not assigned_queue and portal.queue.isFull() then
- -- portal.queue = portal:getNextQueue() {checks in self.queues which is pointer to game.queues assigned in portal:new(game.queues)}
- -- if not portal.queue then return portal:setState(closed)
- end
- --- Creates the abstract object representing a portal
- -- @param active boolean Marks whether the portal has a buildzone attached to it or not
- -- TODO we dont need this, use a pure box object instead
- local function newPortal(_box, kews)
- local _portal = fsm:new(portal_states) -- _portal's prototype is a state machine
- --modify _portal to make it a true portal
- _portal.name = "Portal"
- _portal.box = _box
- _portal.kews = kews
- _portal.assigned_kew = nil
- --assign portal object functions
- _portal.drawPortal = _drawPortal
- _portal.setKew = _setKew
- _portal.doPortal = _doPortal
- _portal.checkKewAvailability = _checkKewAvailability
- --initial state is 'start'
- _portal:setState(_portal.states[portal_states_names.start])
- return _portal
- end
- -------------------------------
- --- PUBLIC API ----------------
- -------------------------------
- function initPortals(settings, kews)
- local portals = {}
- local portal_boxes = getPortalLocations(settings)
- --[[
- local x = settings.computer.x + math.floor(settings.spawnzone_size/2) + portal_offset
- local y = settings.computer.y - 2
- local z = settings.computer.z - math.floor(settings.portal_count.z/2) * ( portal_offset + portal_size) - math.floor(portal_size/2)
- for ix = 0, settings.portal_count.x - 1 do
- for iz=0, settings.portal_count.z - 1 do
- local xpos = x + ix * (portal_offset + portal_size)
- local ypos = y
- local zpos = z + iz * (portal_offset + portal_size)
- --print("making pads", ix, iz, xpos, ypos, zpos)
- --local active = (settings.game_field.countZ > iz and settings.game_field.countX > ix )
- table.insert(portals,newPortal(xpos,ypos,zpos,portal_size,portal_height,kews))
- end
- end
- --]]
- for i,abox in ipairs(portal_boxes) do
- table.insert(portals,newPortal(abox,kews))
- end
- return portals
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement