Advertisement
antonsavov

WIP Kew

Jan 10th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.85 KB | None | 0 0
  1. -------------------------------
  2. -- /lib/Kew -------------------
  3. -------------------------------
  4.  
  5. --- A Kew (short for Queue) is a state machine with states: dormant, inplay, gameover; unavailable
  6.  
  7. local kew_states_names = {
  8.     dormant     = "dormant",
  9.     inplay      = "inplay",
  10.     gameover    = "gameover",
  11.     unavailable = "unavailable"
  12. }
  13.  
  14. -----------------------------------------------------------------------
  15. ------------ "DORMANT" STATE DEFINITION -------------------------------
  16.  
  17. --- enterState_dormant(self)
  18. local state_dormant = {}
  19.  
  20. state_dormant.name = kew_states_names.dormant
  21.  
  22. --- dormant_state.enter(self)
  23. function state_dormant.enter(self)
  24.     -- self.buildzone = self.assigned_buildzone or self:getNextBuildzone()
  25.     -- if self.buildzone == nil then return self:setState(unavailable) end
  26. end
  27.  
  28. --- updateState_dormant(self)
  29. function state_dormant.update(self)
  30.     -- if self:hasPlayers() then return self:setState(inplay) end
  31. end
  32.  
  33. --- exitState_dormant
  34. state_dormant.exit = nil --make this a function if action is needed on exiting dormant state
  35.  
  36. ---------------------------------------------------------------------
  37. ------------ "INPLAY" STATE DEFINITION -------------------------------
  38.  
  39. local state_inplay = {}
  40.  
  41. state_inplay.name = kew_states_names.inplay
  42.  
  43. --- enterState_inplay(self)
  44. function state_inplay.enter(self)
  45.     -- scatterPlayerIntoArea(queue.playerlist {has playerlist.isPlayerList}, queue.buildzone.box)
  46.     -- setStateDuration = playphaselength
  47.     -- queue:cleanBuildzone()
  48.     -- queue:buildInitialCondition() (former prepareBuildzone())
  49. end
  50.  
  51. --- state_inplay.update(self)
  52. function state_inplay.update(self)
  53.     -- updatetime()
  54.     -- TODO updateVillagers(box,self.villager_code) --this function can be in another package, either players/NPCs or mcset
  55.     -- if time is up then return queue:setState(gameover)
  56.     -- if active players are found outside the buildzone they are tp-ed back with a message (TODO use tp Armorstand to playername to avoid glitches)
  57.     -- queue:addLocationToDetectionList(getPlayersOnDetectionBlock(box))
  58.     -- do particle effects for all locations in detection list
  59.     -- pop the first player in queue.detectionlist and do:
  60.     --  check for match in catalogue
  61.     --  if match then clone element; give reward items; update scores; do particle effect; announce in chat; saveToFile; end
  62.     --  else do fail particle effect; annoucne in chat; end
  63.     -- if queue:goalachieved() then return queue:setState(gameover)
  64.     -- queue:dealwithhomecomers():
  65.     -- -    queue:addHomecomingList(getHomecomers(only for players whose name is in .playerlist))
  66.     -- -    pop the first player in queue.homecominglist and do:
  67.     -- -        remove from active playlist by setstate(player) to left with homecomer
  68.     -- -        givehomecomer
  69.     -- -        tp to spawn
  70.     -- if playerlist only inactive players then return queue:setState(dormant)
  71. end
  72.  
  73. --- state_inplay.exit(self)
  74. function state_inplay.exit(self)
  75.     -- saveToFile
  76.     -- saveOnline
  77. end
  78.  
  79. -----------------------------------------------------------------------
  80. ------------ "GAMEOVER" STATE DEFINITION ------------------------------
  81.  
  82. local state_gameover = {}
  83.  
  84. state_gameover.name = kew_states_names.gameover
  85.  
  86. --- state_gameover.enter(self)
  87. function state_gameover.enter(self)
  88.     -- announce game end to all active players and that they can leave with homecomers
  89.     -- cover floor with pavement
  90. end
  91.  
  92. --- state_gameover.update(self)
  93. function state_gameover.update(self)
  94.     -- queue:dealwithhomecomers()
  95. end
  96.  
  97. --- state_gameover.exit
  98. state_gameover.exit = nil --make this a function if action is needed on exiting gameover state
  99.  
  100. -----------------------------------------------------------------------
  101. ------------ "UNAVAILABLE" STATE DEFINITION ---------------------------
  102.  
  103. local state_unavailable = {}
  104.  
  105. state_unavailable.name = kew_states_names.unavailable
  106.  
  107. --- state_unavailable.enter
  108. state_unavailable.enter = nil --make this a function if action is needed on entering unavailable state
  109.  
  110. --- state_unavailable.update
  111. state_unavailable.update = nil --make this a function if action is needed on updating unavailable state
  112.  
  113. --- state_unavailable.exit
  114. state_unavailable.exit = nil --make this a function if action is needed on exiting unavailable state
  115.  
  116. -----------------------------------------------------------------------
  117.  
  118. --local kew = {}
  119.  
  120. --GameQueue = kew
  121.  
  122. --[[
  123. ---------------------------
  124. --- STATE FUNCTIONS
  125. --- the functions for onEnter, update and onExit for each of the 3 states
  126.  
  127. --- DORMANT STATE FUNCTIONS
  128.  
  129. ---
  130. local function enterState_dormant(kew)
  131.     debug.log("Entering the dormant state for queue "..kew.id)
  132.    
  133.     kew.phase = registry.QUEUE_PHASE.DORMANT
  134.     --displayTime(kew.buildzone.selector,0,0)
  135.     kew.timer = kew.phases[kew.phase].length
  136.     portal.drawPortal(kew.portal, kew.phase)
  137.    
  138.     debug.log("Dormant state for queue "..kew.id.. " - entered!")
  139. end
  140.  
  141. ---
  142. local function updateState_dormant(kew)
  143.     debug.log("Updating the dormant state for queue "..kew.id)
  144.     --checkportal(kew.portal)
  145.     -- if playerlist is not empty then kew:setstate(playing)
  146.     debug.log("Dormant state for queue "..kew.id.. " - updated!")
  147. end
  148.  
  149. ---
  150. local function exitState_dormant(kew)
  151.     debug.log("exiting the dormant state for queue "..kew.id)
  152.     -- TODO if it doesnt have an assigned buildzone it picks the next one available from game.buildzones
  153.     debug.log("dormant state for queue "..kew.id.. " - exited!")
  154. end
  155.  
  156. --- PLAYING STATE FUNCTIONS
  157.  
  158.  
  159. ---
  160. local function enterState_playing(kew)
  161.     debug.log("Entering the playing state for queue "..kew.id)
  162.     --displayTime(kew.buildzone.selector,0,0)
  163.     kew.phase = registry.QUEUE_PHASE.PLAYING
  164.     cleanBuildzone(kew.buildzone)
  165.     prepareBuildzone(kew.buildzone)--prepare build zone
  166.     --giveItems(kew.playerlist,registry.STARTING_ITEMS) --Starting items are given as players join now, not all at once
  167.     displayTitleToPlayers(kew.playerlist,"BUILD!","Place your resources and stand on a detector block.")
  168.     kew.victory = false
  169.    
  170.     kew.timer = kew.phases[kew.phase].length
  171.     portal.drawPortal(kew.portal,kew.phase)
  172.     debug.log("Playing state for queue "..kew.id.. " - entered!")
  173. end
  174.  
  175. ---
  176. local function updateState_playing(kew)
  177.     debug.log("Updating the playing state for queue "..kew.id)
  178.     --checkportal(kew.portal)
  179.     debug.log("Playing state for queue "..kew.id.. " - updated!")  
  180. end
  181.  
  182. ---
  183. local function exitState_playing(kew)
  184.     debug.log("exiting the playing state for queue "..kew.id)
  185.     processHighscores(kew)
  186.     exportKewData(kew,true) -- saves the final state of the game and writes it to the online database
  187.     cleanAfterGameOver(kew)
  188.    
  189.     debug.log("playing state for queue "..kew.id.. " - exited!")
  190. end
  191.  
  192. --- GAMEOVER STATE FUNCTIONS
  193.  
  194. ---
  195. local function enterState_gameover(kew)
  196.     debug.log("Entering the gameover state for queue "..kew.id)
  197.     kew.phase = registry.QUEUE_PHASE.GAMEOVER
  198.     --displayTime(kew.buildzone.selector,0,0)
  199.     kew.timer = kew.phases[kew.phase].length
  200.     displayTitleToPlayers(kew.playerlist,"Times Up!","Use HOMECOMER to return to spawn")
  201.     portal.drawPortal(kew.portal,kew.phase)
  202.     debug.log("Gameover state for queue "..kew.id.. " - entered!")
  203. end
  204.  
  205. ---
  206. local function updateState_gameover(kew)
  207.     debug.log("Updating the gameover state for queue "..kew.id)
  208.    
  209.     debug.log("gameover state for queue "..kew.id.. " - updated!")
  210. end
  211.  
  212. ---
  213. local function exitState_gameover(kew)
  214.     debug.log("exiting the gameover state for queue "..kew.id)
  215.     removePlayersFromKew(kew)
  216.     --
  217.     debug.log("gameover state for queue "..kew.id.. " - exited!")
  218. end
  219. --]]
  220. ---------------------------------------
  221.  
  222.  
  223. local kew_states = {state_dormant, state_inplay, state_gameover, state_unavailable }
  224.  
  225.  
  226. --- Kew constructor. Enforces some data structure
  227. -- Kews are used for timekeeping and to record which players are
  228. -- interacting where. Each queue can only have 1 buildzone, but may have
  229. -- many players. Buildzones may move location, but Kews do not.
  230. -- @param buildzone The buildzone object to associate with this Kew
  231. -- @param maxplayers How many players are allowed to play in the buildzone
  232. -- @param portal A portal object created with newPortal()
  233. -- @return The constructed Kew Object
  234. local function newKew(settings, id, buildzones)
  235.     -- {TODO assigned buildzone is passed with queue:new(buildzone) (we can check for object being a buildzone with bz.isBuildzone)}
  236.     --debug.log("creating a new game queue for buildzone #"..buildzone.locid)
  237.     debug.log("creating a new game queue")
  238.     local _kew = fsm:new(kew_states)-- _kew's prototype is a state machine
  239.     --modify _kew to make it a true kew
  240.     _kew.name = "Kew"
  241.     --assign kew object functions
  242.     _kew.addPlayer = _addPlayer
  243.     _kew.getNextBuildzone = _getNextBuildzone
  244.     _kew.setBuildzone = _setBuildzone
  245.     --
  246.     _kew.id = id
  247.     _kew.assigned_buildzone = nil
  248.     _kew.buildzones = buildzones
  249.     --[[ handle these with setstate()
  250.     q.timer = 1
  251.     q.phase = registry.QUEUE_PHASE.DORMANT
  252.    
  253.     q.victory = false
  254.     q.phases = {
  255.         {
  256.             name = "Selecting Players",
  257.             length = registry.PHASE_LENGTH_WAIT
  258.             --displaylength = 15 --this field is not used
  259.         },
  260.         {
  261.             name = "Game In Progress",
  262.             length = registry.PHASE_LENGTH_GAME
  263.             --displaylength = 70 --this field is not used
  264.         },
  265.         {
  266.             name = "Round Complete",
  267.             length = registry.PHASE_LENGTH_OVER
  268.             --displaylength = 5 --this field is not used
  269.         }
  270.     }
  271.     --]]
  272.     --TODO reset the playerlist in onEnter_playing()
  273.     _kew.playerlist = {} --needs a metatable to know how to convert to string
  274.     setmetatable(_kew.playerlist, {__tostring = playerListToString}) -- TODO put this as playerlist:new() in the player package
  275.    
  276.     _kew.maxplayers = settings.maxplayers
  277.     --_kew.portal = portal_box
  278.    
  279.    
  280.    
  281.    
  282.     --TODO this needs to be set and reset in onEnter_playing()
  283.     local timestamp = math.floor(os.clock())
  284.     _kew.filename = timestamp.."at".._kew.buildzone.x.."_".._kew.buildzone.z
  285.    
  286.    
  287.     _kew:setState(_kew.states[kew_states_names.dormant])
  288.     return q
  289. end
  290.  
  291. local function _getNextBuildzone(self)
  292.     --  if not assigned_buildzone and queue.buildzone == nil then queue.buildzone = queue:getNextBuildzone() {checks in self.buildzones which is pointer to game.buildzones assigned in queue:new(game.buildzones) (we can check for object being a buildzonelist with bz.isBuildzoneList) }
  293.     --                  if queue.buildzone == nil then queue:setState(unavailable)
  294. end
  295.  
  296. local function _addPlayer(self, player)
  297.     --      {adds player to playlerlist;
  298.     --      if portal.queue:getState() == inplay then
  299.     --          scatterPlayerIntoArea(player {has player.isPlayer}, portal.queue.buildzone.box)}
  300. end
  301.  
  302. --- Sets an assigned kew for a portal
  303. local function _setBuildzone(self, _buildzone)
  304.     self.assigned_buildzone = _buildzone
  305. end
  306. ------------------------------------------------------------------------------------
  307. --- PUBLIC API ---------------------------------------------------------------------
  308. ------------------------------------------------------------------------------------
  309.  
  310.  
  311.  
  312. ----------------------------
  313. --- GAME QUEUE FUNCTIONS
  314. ---------------------------
  315.  
  316. ---
  317. function initKews(settings)
  318. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement