Advertisement
antonsavov

WIP Portal

Jan 8th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.74 KB | None | 0 0
  1. -------------------------------
  2. -- /lib/Portal ----------------
  3. -------------------------------
  4. --- This package keeps functions that deal with the creation, drawing
  5. -- of portals
  6.  
  7. --- A portal is a state machine with states: start, join, closed
  8. local portal_states_names = {
  9.     start   = "start",
  10.     join    = "join",
  11.     closed  = "closed"
  12. }
  13. --local p = {} -- the local table to contain the function
  14. --portal = p -- the public API in the form of a table
  15.  
  16. --------------------------------------------------------------------
  17. ------------ "START" STATE FUNCTIONS -------------------------------
  18.  
  19. local state_start = {}
  20.  
  21. state_start.name = portal_states_names.start
  22.  
  23. --- enterState_start(self)
  24. function state_start.enter(self)
  25.     -- self.kew = self.assigned_kew or nil
  26.     -- self:checkKewAvailability()
  27.     self:drawportal()
  28. end
  29.  
  30. --- updateState_start()
  31. function state_start.update(self)
  32.     --  portal:doPortal = function(self)
  33.     --  if assigned_queue and portal.queue.isInPlay() then return portal:setState(join)
  34.     --  portal:checkKewAvailability()
  35. end
  36.  
  37. --- exitState_start = nil
  38. state_start.exit = nil --turn to function if action is needed on exiting start state
  39.  
  40. --------------------------------------------------------------------
  41. ------------ "JOIN" STATE FUNCTIONS --------------------------------
  42.  
  43. local state_join = {}
  44.  
  45. state_join.name = portal_states_names.join
  46.  
  47. --- enterState_join(self)
  48. function state_join.enter(self)
  49.     self:drawportal()
  50. end
  51.  
  52. --- updateState_join(self)
  53. function state_join.update(self)
  54.     -- portal:doPortal()
  55.     -- if portal.queue.isFull() or portal.queue.isGameover() then return portal:setState(closed)
  56. end
  57.  
  58. --  exitState_join = nil
  59. state_join.exit = nil --turn to function if action is needed on exiting join state
  60.  
  61. ----------------------------------------------------------------------
  62. ------------ "CLOSED" STATE FUNCTIONS --------------------------------
  63.  
  64. local state_closed = {}
  65.  
  66. state_closed.name = portal_states_names.closed
  67.  
  68. --- enterState_closed(self)
  69. function state_closed.enter(self)
  70.     self:drawportal()
  71.     -- tp all on the waitlist 1 block up
  72.     -- discard waitlist
  73. end
  74.  
  75. --- updateState_closed(self)
  76. function state_closed.update(self)
  77.     -- if not assigned_queue and game:getNextQueue() then return self:setState(start)
  78.     -- if assigned_queue and self.queue.isDormant() then return self:setState(start)
  79. end
  80.  
  81. --  exitState_closed = nil
  82. state_closed.exit = nil --turn to function if action is needed on exiting closed state
  83.  
  84. --------------------------------------------------------------------
  85.  
  86. -------------------------------
  87. --- PRIVATE FUNCTIONS ---------
  88. -------------------------------
  89.  
  90. local portal_states = { state_start, state_join, state_closed }
  91.  
  92. --- Sets an assigned kew for a portal
  93. local function _setKew(self, kew)
  94.     self.assigned_kew = kew
  95. end
  96.  
  97. local function _doPortal(self)
  98.     --  if found creative players then change them to adventure mode
  99.     --  if found adventure players then add to portal.waitlist
  100.     --  pop the first player in portal.waitlist and do
  101.     --      portal.queue:addPlayer(player)
  102.     --      {adds player to playlerlist;
  103.     --      if portal.queue:getState() == inplay then
  104.     --          scatterPlayerIntoArea(player {has player.isPlayer}, portal.queue.buildzone.box)}
  105. end
  106.  
  107. --- Update the blocks in a Portal Zone in Minecraft
  108. -- This is also used to visually "close" a portal to players
  109. -- @param portal A portal object created with newPortal. A kew.portal can be passed
  110. -- @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
  111. -- TODO remove the dependencies on "registry."
  112. local function _drawPortal(self) --,queue_phase)
  113.     debug.log("Drawing a portal at: "..self.box.corner_x..", "..self.box.corner_y..", "..self.box.corner_z)
  114.     if self.state == portal_states_names.start then
  115.         --fill the zone with air to clear it
  116.         mcset.fillBox( self.box, registry.BLOCKS.AIR )
  117.         --fill with detector blocks one level below ground
  118.         mcset.fillBox( box_base( self.box ), registry.BLOCKS.DETECT )
  119.     elseif self.state == portal_states_names.join then
  120.         --fill the zone with air to clear it
  121.         mcset.fillBox( self.box, registry.BLOCKS.AIR )
  122.         --fill with detector blocks one level below ground
  123.         mcset.fillBox( box_base( self.box ), registry.BLOCKS.DETECT )
  124.         --mark with a construnction block the middle point
  125.         mcset.setBlock(self.box.base_center_x, self.box.base_center_y + 1, self.box.base_center_z, registry.BLOCKS.CONSTRUCTION)
  126.     elseif self.state == portal_states_names.closed then
  127.         --fill with construction block the ground level to mark as inaccessible
  128.         mcset.fillBox( box_move_y( box_base(self.box), 1),registry.BLOCKS.CONSTRUCTION )
  129.     end
  130. end
  131.  
  132. local function _checkKewAvailability(self)
  133.     -- if not assigned_queue and portal.queue.isFull() then
  134.     --      portal.queue = portal:getNextQueue() {checks in self.queues which is pointer to game.queues assigned in portal:new(game.queues)}
  135.     -- if not portal.queue then return portal:setState(closed)
  136. end
  137.  
  138. --- Creates the abstract object representing a portal
  139. -- @param active boolean Marks whether the portal has a buildzone attached to it or not
  140. -- TODO we dont need this, use a pure box object instead
  141. local function newPortal(_box, kews)
  142.     local _portal = fsm:new(portal_states) -- _portal's prototype is a state machine
  143.     --modify _portal to make it a true portal
  144.     _portal.name = "Portal"
  145.     _portal.box = _box
  146.     _portal.kews = kews
  147.     _portal.assigned_kew = nil
  148.     --assign portal object functions
  149.     _portal.drawPortal = _drawPortal
  150.     _portal.setKew = _setKew
  151.     _portal.doPortal = _doPortal
  152.     _portal.checkKewAvailability = _checkKewAvailability
  153.     --initial state is 'start'
  154.     _portal:setState(_portal.states[portal_states_names.start])
  155.     return _portal
  156. end
  157.  
  158. -------------------------------
  159. --- PUBLIC API ----------------
  160. -------------------------------
  161.  
  162. function initPortals(settings, kews)
  163.     local portals = {}
  164.     local portal_boxes = getPortalLocations(settings)
  165.     --[[
  166.     local x = settings.computer.x + math.floor(settings.spawnzone_size/2) + portal_offset
  167.     local y = settings.computer.y - 2
  168.     local z = settings.computer.z - math.floor(settings.portal_count.z/2) * ( portal_offset + portal_size) - math.floor(portal_size/2)
  169.     for ix = 0, settings.portal_count.x - 1 do
  170.         for iz=0, settings.portal_count.z - 1 do
  171.             local xpos = x + ix * (portal_offset + portal_size)
  172.             local ypos = y
  173.             local zpos = z + iz * (portal_offset + portal_size)            
  174.             --print("making pads", ix, iz, xpos, ypos, zpos)
  175.             --local active = (settings.game_field.countZ > iz and settings.game_field.countX > ix )
  176.             table.insert(portals,newPortal(xpos,ypos,zpos,portal_size,portal_height,kews))
  177.         end
  178.     end
  179.     --]]
  180.     for i,abox in ipairs(portal_boxes) do
  181.         table.insert(portals,newPortal(abox,kews))
  182.     end
  183.     return portals
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement