Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.62 KB | None | 0 0
  1. local VERSION = '0.4.5 - feetarecool'
  2. DEBUG_MODE = true
  3.  
  4. commands.gamerule("doDaylightCycle",false)
  5. commands.gamerule("doTileDrops",false)
  6. commands.gamerule("logAdminCommands",false)
  7. commands.gamerule("commandBlockOutput",false)
  8. commands.time("set",6000)
  9.  
  10. local monitor = peripheral.wrap("right")
  11. local ox,oy,oz = commands.getBlockPosition()
  12. commands.scoreboard("objectives","add","VillagerLife","dummy")
  13. print("Starting 20,000 Blocks")
  14. print("Computer Co-ordinates ",ox,oy,oz)
  15. commands.title("@a","times",0,30,30)
  16. math.randomseed( os.time() )
  17.  
  18. SPAWN_VILLAGER = false
  19. DO_GRID = true
  20. GRID_HOLE_CHANCE = 0
  21. NUMBER_OF_BUILDZONES = 1
  22. NUMBER_OF_VOCAB = 9
  23. VOCAB_WIDTH = 27
  24. VOCAB_HEIGHT = 19
  25. BUILDZONE_WIDTH = 62
  26. BUILDZONE_FLOOR_HEIGHT = -1
  27. SPAWN = {x=ox,y=oy+2,z=oz-10}
  28. VICTORY_HEIGHT = 30
  29. VICTORY_TOTAL = 50
  30. VICTORY_SPECIFIC = {name='garden',count=101}
  31.  
  32. DEFAULT_REWARD = 'minecraft:emerald 2 0 {display:{Name:"Emerald",Lore:[Trade this to the villager for wool]}}'
  33. WOOL_PER_EMERALD = 64
  34. PICKAXE_USES = 10
  35.  
  36. BLOCKS = {
  37. CAMP_FLOOR = 'minecraft:sandstone',
  38. CONSTRUCTION = 'minecraft:diamond_ore',
  39. RING = 'minecraft:diamond_ore',
  40. DETECT = 'minecraft:mycelium',
  41. DETECT_DEAD = 'minecraft:obsidian',
  42. VOCAB_DETECT = "minecraft:quartz_block",
  43. VOCAB_REPLACE = "minecraft:coal_block",
  44. VICTORY_MARKER = "minecraft:emerald_block",
  45. PLUG = "minecraft:lapis_ore"
  46. }
  47.  
  48.  
  49. function houseBlock(quant)
  50. text = 'minecraft:wool '..quant..' 0 {display:{Name:"House Construction Block",Lore:[Place this against a Diamond to build a house]},CanPlaceOn:["minecraft:diamond_block","minecraft:wool","minecraft:lapis_ore","minecraft:mycelium"]}'
  51. return text
  52. end
  53.  
  54. function waterBlock(quant)
  55. text = 'minecraft:wool '..quant..' 11 {display:{Name:"Water Construction Block",Lore:[Place this against a Diamond to build a pond or pool]},CanPlaceOn:["minecraft:diamond_block","minecraft:wool","minecraft:lapis_ore","minecraft:mycelium"]}'
  56. return text
  57. end
  58.  
  59. function gardenBlock(quant)
  60. text = 'minecraft:wool '..quant..' 13 {display:{Name:"Garden Construction Block",Lore:[Place this against a Diamond to build a pond or pool]},CanPlaceOn:["minecraft:diamond_block","minecraft:wool","minecraft:lapis_ore","minecraft:mycelium"]}'
  61. return text
  62. end
  63.  
  64. STARTING_ITEMS = {
  65. houseBlock(15),
  66. 'stone_pickaxe 1 '..131-PICKAXE_USES..' {CanDestroy:["minecraft:wool"],display:{Name:"Wool Remover",Lore:[Use this to remove up to '..PICKAXE_USES..' pieces of wool]}}'
  67. }
  68.  
  69. REWARDS = {}
  70. --plain singlehouse rewards
  71. REWARDS[1] = waterBlock(4)
  72. REWARDS[2] = waterBlock(4)
  73. REWARDS[3] = waterBlock(4)
  74. REWARDS[4] = waterBlock(4)
  75. --extension rise rewards
  76. REWARDS[5] = gardenBlock(9)
  77. REWARDS[6] = gardenBlock(9)
  78. REWARDS[7] = gardenBlock(9)
  79. REWARDS[8] = gardenBlock(9)
  80. --bridge rewards
  81. REWARDS[9] = gardenBlock(4)
  82. REWARDS[10] = gardenBlock(4)
  83. --Filler rewards (very low)
  84. REWARDS[11] = houseBlock(1)
  85. REWARDS[12] = houseBlock(1)
  86. --extention row house rewards
  87. REWARDS[13] = gardenBlock(4)
  88. REWARDS[14] = gardenBlock(4)
  89. REWARDS[15] = gardenBlock(4)
  90. REWARDS[16] = gardenBlock(4)
  91. --extension pond rewards
  92. REWARDS[17] = gardenBlock(3)
  93. REWARDS[18] = gardenBlock(3)
  94. REWARDS[19] = gardenBlock(3)
  95. REWARDS[20] = gardenBlock(3)
  96. --extention pool rewards
  97. REWARDS[21] = houseBlock(3)
  98. REWARDS[22] = houseBlock(3)
  99. REWARDS[23] = houseBlock(3)
  100. REWARDS[24] = houseBlock(3)
  101. --extension garden rewards
  102. REWARDS[25] = waterBlock(3)
  103. REWARDS[26] = waterBlock(3)
  104. REWARDS[27] = waterBlock(3)
  105. REWARDS[28] = waterBlock(3)
  106. --extention pool rewards
  107. REWARDS[29] = houseBlock(3)
  108. REWARDS[30] = houseBlock(3)
  109. REWARDS[31] = houseBlock(3)
  110. REWARDS[32] = houseBlock(3)
  111. --extention major rewards
  112. REWARDS[33] = houseBlock(3)
  113. REWARDS[34] = houseBlock(3)
  114. REWARDS[35] = houseBlock(3)
  115. REWARDS[36] = houseBlock(3)
  116.  
  117.  
  118. DEFAULT_NAME = 'default-vocab'
  119. VOCAB_NAMES = {
  120. 'single-house-1',
  121. 'single-house-2',
  122. 'single-house-3',
  123. 'single-house-4',
  124. 'extension-rise-1',
  125. 'extension-rise-2',
  126. 'extension-rise-3',
  127. 'extension-rise-4',
  128. 'bridge-1',
  129. 'bridge-2',
  130. 'filler-water-1',
  131. 'filler-garden-1',
  132. 'extension-row-1',
  133. 'extension-row-2',
  134. 'extension-row-3',
  135. 'extension-row-4',
  136. 'extension-pond-1',
  137. 'extension-pond-2',
  138. 'extension-pond-3',
  139. 'extension-pond-4',
  140. 'extension-pool-1',
  141. 'extension-pool-2',
  142. 'extension-pool-3',
  143. 'extension-pool-4',
  144. 'extension-garden-1',
  145. 'extension-garden-2',
  146. 'extension-garden-3',
  147. 'extension-garden-4',
  148. 'extension-plaza-1',
  149. 'extension-plaza-2',
  150. 'extension-plaza-3',
  151. 'extension-plaza-4',
  152. 'extension-major-1',
  153. 'extension-major-2',
  154. 'extension-major-3',
  155. 'extension-major-4'
  156. }
  157. LOCS = {}
  158.  
  159. --########################### LOGIC CODE
  160.  
  161. function buildGrid(w)
  162. local grid = {}
  163. for x=0,w-1 do
  164. for z=0,w-1 do
  165. table.insert(grid,{x=x,z=z})
  166. print("zone loc at: ",x,z)
  167. end
  168. end
  169. return grid
  170. end
  171.  
  172.  
  173. LOCS = buildGrid(2)
  174.  
  175. function newPlayerData(name,x,y,z)
  176. local p = {
  177. name = name,
  178. x=x,
  179. y=y,
  180. z=z
  181. }
  182. return p
  183. end
  184.  
  185. local function getAllPos(selector)
  186. local result, message = commands.tp("@a["..selector.."]","~ ~ ~")
  187. local names = {}
  188. if result == true then
  189. for i,result in ipairs(message) do
  190. local wordpattern = "[^, ]+"
  191. local numberpattern = "[%-% ]%d+[%.]%d+"
  192. local words,numbers = {},{}
  193.  
  194. for word in string.gmatch(result, wordpattern) do
  195. table.insert(words,word)
  196. end
  197.  
  198. for number in string.gmatch(result, numberpattern) do table.insert(numbers,number) end
  199.  
  200. local coords = {
  201. x = math.floor(numbers[1]),
  202. y = math.floor(numbers[2]),
  203. z = math.floor(numbers[3])
  204. }
  205. local name = words[2]
  206. table.insert(names,newPlayerData(name,coords.x,coords.y,coords.z))
  207. end
  208. end
  209. return names
  210. end
  211.  
  212. local function shuffleTable( t )
  213. local rand = math.random
  214. assert( t, "shuffleTable() expected a table, got nil" )
  215. local iterations = #t
  216. local j
  217.  
  218. for i = iterations, 2, -1 do
  219. j = rand(i)
  220. t[i], t[j] = t[j], t[i]
  221. end
  222. end
  223.  
  224. local function getAllOnBlockType(block,selector)
  225. local result, message = commands.exec("execute @a["..selector.."] ~ ~ ~ detect ~ ~-1 ~ "..block.." -1 tp @p[r=1] ~ ~ ~")
  226. local names = {}
  227. if result == true then
  228. for i,result in ipairs(message) do
  229. local wordpattern = "[^, ]+"
  230. local numberpattern = "[%-% ]%d+[%.]%d+"
  231. local words,numbers = {},{}
  232.  
  233. for word in string.gmatch(result, wordpattern) do table.insert(words,word) end
  234. for number in string.gmatch(result, numberpattern) do table.insert(numbers,number) end
  235.  
  236. if numbers[1] and numbers[2] and numbers[3] then
  237. local coords = {
  238. x = math.floor(numbers[1]),
  239. y = math.floor(numbers[2]),
  240. z = math.floor(numbers[3])
  241. }
  242. local name = words[2]
  243. table.insert(names,newPlayerData(name,coords.x,coords.y,coords.z))
  244. else
  245. print("Error: Coordinate Numbers were missing")
  246. end
  247. end
  248. end
  249. return names
  250. end
  251.  
  252. local function spawnVillager(x,y,z)
  253. commands.summon("Villager",x,y,z,'{Invulnerable:1,CustomName:Wool_Seller,Profession:2,Career:1,CareerLevel:6,Offers:{Recipes:[ {buy:{id:emerald,Count:1},sell:{id:wool,Count:'..WOOL_PER_EMERALD..',tag:{CanPlaceOn:["minecraft:diamond_block","minecraft:clay","minecraft:wool","minecraft:stained_hardened_clay"]}}}, {buy:{id:emerald,Count:1},sell:{id:stone_pickaxe,Count:1,Damage:'..131-PICKAXE_USES..',tag:{CanDestroy:["minecraft:wool"]}}} ]}}')
  254. end
  255.  
  256. local function displayTime(selector,minutes,seconds)
  257. commands.title("@a["..selector.."]","subtitle",'{text:"'..minutes..":"..seconds..'",color:red,bold:false,underlined:false,italic:false,strikethrough:false,obfuscated:false}')
  258. end
  259.  
  260. local function displayTitle(selector,text)
  261. commands.title("@a["..selector.."]","title",'{text:"'..text..'"}')
  262. end
  263.  
  264. local function displayTitleToGroup(playerlist,text)
  265. for i,player in ipairs(playerlist) do
  266. displayTitle("name="..player.name,text)
  267. end
  268. end
  269.  
  270. local function displayTimeToGroup(playerlist,minutes,seconds)
  271. for i,player in ipairs(playerlist) do
  272. displayTime("name="..player.name,minutes,seconds)
  273. end
  274. end
  275.  
  276. local function teleportToPoint(x,y,z,playerlist)
  277. for i,player in ipairs(playerlist) do
  278. player.x = x
  279. player.y = y
  280. player.z = z
  281. commands.tp("@a[name="..player.name.."]",x,y,z)
  282. end
  283. end
  284.  
  285. local function teleportToZone(buildzone,playerlist)
  286. teleportToPoint(buildzone.x+(buildzone.w/2),buildzone.y+2,buildzone.z+(buildzone.w/2),playerlist)
  287. end
  288.  
  289. local function giveItems(playerlist,itemlist)
  290. local given = 0
  291. for i,player in ipairs(playerlist) do
  292. commands.async.clear(player.name)
  293. for j,item in ipairs(itemlist) do
  294. commands.async.give("@a[name="..player.name.."]",item)
  295. given = given +1
  296. end
  297. end
  298. return given
  299. end
  300.  
  301. local function makeVocabZones(quant,w)
  302. local x,y,z = ox,oy,oz+6
  303. local result = {}
  304. local namecount = 1
  305. for i=0,quant-1 do
  306. for k=0,3 do
  307. local zpos = i-4
  308. local ypos = k
  309. local nextVocab = newVocabZone(x-(2*w)-6,y+(ypos*(VOCAB_HEIGHT+3)),z+((w+1)*zpos),w,REWARDS[namecount] or DEFAULT_REWARD,VOCAB_NAMES[namecount] or DEFAULT_NAME)
  310. table.insert(result,nextVocab)
  311. namecount = namecount +1
  312. end
  313. end
  314. return result
  315. end
  316.  
  317. local function findNextLoc(width)
  318. local x,y,z = 0,0,0
  319. --local ox,oy,oz = 10000,oy,10000
  320. for i,loc in ipairs(LOCS) do
  321. x,y,z = ox+(loc.x*(width+1)),oy,oz+6+(loc.z*(width+1))
  322. local result,message = commands.testforblock(x,y+BUILDZONE_FLOOR_HEIGHT,z,"minecraft:air")
  323. --print("next position free is ",loc.x*width,oy,loc.z*width)
  324. --if result then print("true") else print("false") end
  325. if result then
  326. print("using loc: ",loc.x,loc.z)
  327. return x,y,z
  328. end
  329. end
  330. return ox,oy,oz+6
  331.  
  332. end
  333.  
  334. function moveBuildzone(buildzone)
  335. local x,y,z = findNextLoc(buildzone.w)
  336. local w = buildzone.w
  337. buildzone.x,buildzone.y,buildzone.z = x,y+BUILDZONE_FLOOR_HEIGHT,z
  338. buildzone.selector = "x="..x..",y="..y..",z="..z..",dx="..w..",dy=256,dz="..w
  339. buildzone.structures = {} --a list of all vocabularies which have been contructed
  340. end
  341.  
  342.  
  343. local function makeBuildzones(quant,vocab,width,height)
  344. --local x,y,z = ox+(VOCAB_WIDTH*3),oy,oz+6
  345. local x,y,z = ox,oy,oz+6
  346.  
  347. x,y,z = findNextLoc(width)
  348.  
  349. return {
  350. newBuildZone(x,y+height,z,width,vocab)
  351. }
  352.  
  353. --local result = {}
  354. --for i=0,quant-1 do
  355. --table.insert(result,newBuildZone(x,y+height,z+(i*(width+20)),width,vocab))
  356. --end
  357. --return result
  358. end
  359.  
  360. --########################### GAME MANAGER CODE
  361.  
  362.  
  363.  
  364. function newVocabZone(x,y,z,w,reward,name)
  365. local nvz = {}
  366. nvz.x ,nvz.y ,nvz.z ,nvz.w = x,y,z,w
  367.  
  368. nvz.cx = nvz.x - nvz.w - 2
  369. nvz.cy = nvz.y
  370. nvz.cz = nvz.z
  371. nvz.name = name
  372. nvz.reward = reward
  373.  
  374. return nvz
  375.  
  376. end
  377.  
  378. function newBuildZone(x,y,z,w,vocabZones)
  379. local nbz = {}
  380. nbz.x ,nbz.y ,nbz.z ,nbz.w = x,y,z,w
  381. nbz.selector = "x="..x..",y="..y..",z="..z..",dx="..w..",dy=256,dz="..w
  382. nbz.structures = {} --a list of all vocabularies which have been contructed
  383. nbz.vocab = vocabZones
  384.  
  385. return nbz
  386. end
  387.  
  388. function newQueue(buildzone,maxplayers)
  389. local q = {}
  390. q.timer = 1
  391. q.phase = 1
  392.  
  393. q.victory = false
  394. q.phases = {
  395. {
  396. name = "Selecting Players",
  397. length = 25,
  398. displaylength = 15
  399. },
  400. {
  401. name = "Game In Progress",
  402. length = 5000,
  403. displaylength = 70
  404. },
  405. {
  406. name = "Round Complete",
  407. length = 15,
  408. displaylength = 5
  409. }
  410. }
  411. q.playerlist = {}
  412. q.maxplayers = maxplayers
  413. q.buildzone = buildzone
  414.  
  415. if DEBUG_MODE then
  416. --q.phase = 2
  417. --q.timer = 5000
  418. end
  419.  
  420. return q
  421. end
  422.  
  423. --[[function fillRing(x,y,z,w,block)
  424. commands.fill(x-1,y,z-1,x+1+w,y,z-1,block)
  425. commands.fill(x+1+w,y,z-1,x+1+w,y,z+1+w,block)
  426. commands.fill(x-1,y,z+1+w,x+1+w,y,z+1+w,block)
  427. commands.fill(x-1,y,z+1+w,x-1,y,z-1,block)
  428. end]]
  429.  
  430. function fillRing(x,y,z,w,block)
  431. commands.fill(x,y,z,x+w,y,z,block)
  432. commands.fill(x+w,y,z,x+w,y,z+w,block)
  433. commands.fill(x,y,z+w,x+w,y,z+w,block)
  434. commands.fill(x,y,z+w,x,y,z,block)
  435. end
  436.  
  437. function setup()
  438. local game = {}
  439. game.vocab = {}
  440. game.builds = {}
  441. game.queues = {}
  442. game.waitlist = {}
  443. game.spawn = SPAWN
  444. game.lastClock = os.clock()
  445. game.nowTime = os.clock()
  446.  
  447. --kill all villagers
  448. commands.exec("kill @e[type=Villager]")
  449.  
  450. --buildzone and vocabzone creation
  451. game.vocab = makeVocabZones(NUMBER_OF_VOCAB,VOCAB_WIDTH)
  452.  
  453. game.builds = makeBuildzones(NUMBER_OF_BUILDZONES,game.vocab,BUILDZONE_WIDTH,BUILDZONE_FLOOR_HEIGHT)
  454.  
  455. for i,build in ipairs(game.builds) do
  456. table.insert(game.queues,newQueue(build,4))
  457. cleanBuildzone(build)
  458. prepareBuildzone(build)
  459. end
  460.  
  461. for i,vz in ipairs(game.vocab) do
  462. local x,y,z,w = vz.x,vz.y,vz.z,vz.w
  463. local cx,cy,cz = vz.cx,vz.cy,vz.cz
  464. --commands.fill(x,y-1,z,x+w-1,y-1,z+w-1,BLOCKS.VOCAB_DETECT)
  465. --commands.fill(cx,cy-1,z,cx+w-1,cy-1,cz+w-1,BLOCKS.VOCAB_REPLACE)
  466. --commands.fill(x,y-1,z,x+w-1,y+10,z+w-1,'minecraft:dirt',"replace","minecraft:air")
  467. --commands.fill(cx,cy-1,z,cx+w-1,cy+10,cz+w-1,'minecraft:dirt',"replace","minecraft:air")
  468. --commands.setblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.VOCAB_REPLACE)
  469. --commands.setblock(x+(math.floor(w/2)),y-1,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD)
  470. --commands.setblock(cx+(math.floor(w/2)),cy-1,cz+(math.floor(w/2)),BLOCKS.VOCAB_DETECT)
  471.  
  472. local detector, message1 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT)
  473. local blocker, message2 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD)
  474. if not (detector or blocker) then
  475. for nx=0,2 do
  476. for nz=0,2 do
  477. commands.setblock(x+(nx*9)+4,y-1,z+(nz*9)+4,BLOCKS.VOCAB_REPLACE)
  478. --commands.setblock(x+(nx*9)+4,y,z+(nz*9)+4,BLOCKS.DETECT_DEAD)
  479. commands.setblock(cx+(nx*9)+4,cy-1,cz+(nz*9)+4,BLOCKS.VOCAB_DETECT)
  480. end
  481. end
  482. commands.setblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD)
  483.  
  484. end
  485.  
  486. end
  487.  
  488. if DEBUG_MODE then
  489. --commands.setblock(x,y,z,'minecraft:redstone_block')
  490. --commands.setblock(cx,cy,cz,'minecraft:emerald_block')
  491. for i,build in ipairs(game.builds) do
  492. build.phase = 2
  493. build.timer = 5000
  494. end
  495. end
  496.  
  497. return game
  498. end
  499.  
  500. function update(game)
  501. local elapsed = updateClock(game)
  502. --update players
  503. checkPlayers(game)
  504. --if not DEBUG_MODE then
  505. doTimerUpdates(game,elapsed)
  506. --end
  507. doPhaseUpdates(game)
  508. doPhaseEnds(game)
  509. checkBoundaries(game)
  510. cullVillagers()
  511. if #game.waitlist > 0 then allocateWaiters(game) end
  512. end
  513.  
  514. function cullVillagers()
  515. --add lifetime to villagers
  516. commands.exec("scoreboard players add @e[type=Villager] VillagerLife 1")
  517. --cull old villagers
  518. commands.exec("tp @e[type=Villager,score_VillagerLife_min=50] ~ ~256 ~")
  519. commands.exec("kill @e[type=Villager,score_VillagerLife_min=50]")
  520.  
  521. end
  522.  
  523.  
  524. function updateClock(game)
  525. game.nowTime = os.clock()
  526. local elapsed = game.nowTime - game.lastClock
  527. game.lastClock = game.nowTime
  528. return elapsed
  529. end
  530.  
  531. function doTimerUpdates(game,elapsed)
  532. for i,kew in ipairs(game.queues) do
  533. kew.timer = kew.timer - elapsed
  534. end
  535. end
  536.  
  537. function checkBoundaries(game)
  538. for i,kew in ipairs(game.queues) do
  539. if kew.phase > 1 then
  540. --boundaries
  541. local x_min = kew.buildzone.x
  542. local x_max = kew.buildzone.x+kew.buildzone.w
  543. local z_min = kew.buildzone.z
  544. local z_max = kew.buildzone.z+kew.buildzone.w
  545.  
  546. local toBeCorrected = {}
  547.  
  548. for j,player in ipairs(kew.playerlist) do
  549. local changed = false
  550. if player.x > x_max then
  551. changed = true
  552. player.x = x_max
  553. end
  554. if player.x < x_min then
  555. changed = true
  556. player.x = x_min
  557. end
  558. if player.z > z_max then
  559. changed = true
  560. player.z = z_max
  561. end
  562. if player.z < z_min then
  563. changed = true
  564. player.z = z_min
  565. end
  566. if changed then teleportToPoint(player.x,kew.buildzone.y,player.z,{player}) end
  567. end
  568. end
  569. end
  570. end
  571.  
  572. function cleanAfterVictory(buildzone)
  573. commands.setblock(buildzone.x,buildzone.y,buildzone.z,BLOCKS.VICTORY_MARKER)
  574. fillRing(buildzone.x,buildzone.y-1,buildzone.z,buildzone.w,BLOCKS.CAMP_FLOOR)
  575. for h=0,VICTORY_HEIGHT do
  576. commands.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air 0","replace","minecraft:diamond_block 0")
  577. end
  578. end
  579.  
  580. function doPhaseUpdates(game)
  581. for i,kew in ipairs(game.queues) do
  582.  
  583. local minutes = string.format("%02d",math.floor(kew.timer/60))
  584. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  585. if kew.timer <= 0 then
  586. minutes = "00"
  587. seconds = "00"
  588. end
  589.  
  590. if kew.phase == 1 then
  591. --waiting phase
  592. if #kew.playerlist == kew.maxplayers and kew.timer > 5 then kew.timer = 5 end
  593. if not DEBUG_MODE and #kew.playerlist == 0 then kew.timer = kew.phases[1].length end
  594.  
  595. displayTitleToGroup(kew.playerlist,"Waiting for Game to Start")
  596. displayTimeToGroup(kew.playerlist,minutes,seconds)
  597. --show countdown
  598. elseif kew.phase == 2 then
  599. --playing phase
  600. if #kew.playerlist == 0 then timer = 0 end -- finish if all players quit
  601. local victory = updatePlayedZone(kew.buildzone) -- do vocab logic
  602. --check for victory or defeat
  603. if victory then
  604. kew.victory = true
  605. --tell all in zone that victory was achieved
  606. if kew.timer > 5 then kew.timer = 5 end
  607. cleanAfterVictory(kew.buildzone)
  608.  
  609. end
  610. --show countdown when time is almost done
  611. if kew.victory then
  612. displayTitle(kew.buildzone.selector,"Success!")
  613. elseif kew.timer < kew.phases[2].displaylength then
  614. displayTitle(kew.buildzone.selector,"Hurry Up!")
  615. displayTime(kew.buildzone.selector,minutes,seconds)
  616. end
  617. elseif kew.phase == 3 then
  618. --end phase
  619.  
  620. if #kew.playerlist == 0 then kew.timer = 0 end --if playerlist == 0 then timer = 0
  621. if kew.victory then --show congratulation message if kew.victory
  622. displayTitle(kew.buildzone.selector,"You Win!")
  623. else
  624. displayTitle(kew.buildzone.selector,"Time's Up!")
  625. end
  626. displayTime(kew.buildzone.selector,minutes,seconds)
  627. end
  628. end
  629. end
  630.  
  631. function forceObserverStatus(x,y,z,playerlist)
  632. for i,player in ipairs(playerlist) do
  633. commands.gamemode('spectator',player.name)
  634. commands.tp(x,y,z,-45,45,0,player.name)
  635. end
  636. end
  637.  
  638. function doPhaseEnds(game)
  639. for i,kew in ipairs(game.queues) do
  640. if kew.timer <= 0 then
  641. if kew.phase == 1 then
  642. --waiting phase ends goto play phase
  643.  
  644. moveBuildzone(kew.buildzone)
  645. cleanBuildzone(kew.buildzone)
  646. prepareBuildzone(kew.buildzone)--prepare build zone
  647. teleportToZone(kew.buildzone,kew.playerlist)--teleport selected players
  648. giveItems(kew.playerlist,STARTING_ITEMS) --give starting items
  649. displayTitle(kew.buildzone.selector,"Build!")
  650. kew.victory = false
  651. kew.phase = 2
  652. kew.timer = kew.phases[2].length
  653. elseif kew.phase == 2 then
  654. --playing phase ends goto end phase
  655. --move players to observation point and mode
  656. forceObserverStatus(kew.buildzone.x,kew.buildzone.y+25,kew.buildzone.z,kew.playerlist)
  657. kew.phase = 3
  658. kew.timer = kew.phases[3].length
  659. elseif kew.phase == 3 then
  660. --end phase ends goto waiting phase
  661. fillRing(kew.buildzone.x,kew.buildzone.y+VICTORY_HEIGHT,kew.buildzone.z,kew.buildzone.w,"minecraft:air")
  662. respawnPlayers(game,kew)
  663. kew.phase = 1
  664. kew.timer = kew.phases[1].length
  665. end
  666. end
  667. end
  668. end
  669.  
  670.  
  671. --Replaces everything that is needed to start the game. Does not rebuild the floor, or clear anything away.
  672. --based on the settings it creates a full grid, or a partial grid, or no grid
  673. --it also places the ring, although this is disabled for now
  674. function prepareBuildzone(buildzone)
  675. local bz = buildzone
  676. local x,y,z,w = bz.x,bz.y,bz.z,bz.w
  677. --commands.fill(x,y-1,z,x+w,y-1,z+w,BLOCKS.CAMP_FLOOR)
  678. fillRing(buildzone.x,buildzone.y-1,buildzone.z,buildzone.w,BLOCKS.CONSTRUCTION)
  679. if DO_GRID then
  680. for x=0,6 do
  681. for z=0,6 do
  682. local rand = math.random()*100
  683. if rand > GRID_HOLE_CHANCE then
  684. commands.setblock(bz.x+(x*9)+4,bz.y,bz.z+(z*9)+4,BLOCKS.DETECT)
  685. commands.fill(bz.x+(x*9)+2,bz.y-1,bz.z+(z*9)+4,bz.x+(x*9)+6,bz.y-1,bz.z+(z*9)+4,BLOCKS.PLUG)
  686. commands.fill(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+2,bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+6,BLOCKS.PLUG)
  687. end
  688. end
  689. end
  690. end
  691. fillRing(buildzone.x,buildzone.y+VICTORY_HEIGHT,buildzone.z,buildzone.w,BLOCKS.RING)
  692. end
  693.  
  694. --deletes everything inside the buildzone, except the ring and the floor
  695. function cleanBuildzone(buildzone)
  696. print("Cleaning buildzone")
  697. for h=0,VICTORY_HEIGHT+20 do
  698. commands.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air")
  699. end
  700. end
  701.  
  702. --moves all players assigned to the queue into the waiting area for the provided game.
  703. --It also changes them to Adventure mode and clears their inventory
  704. function respawnPlayers(game,kew)
  705. teleportToPoint(game.spawn.x,game.spawn.y,game.spawn.z,kew.playerlist) --put players back in spawn area
  706. for i,player in ipairs(kew.playerlist) do
  707. table.insert(game.waitlist,player)
  708. commands.gamemode('adventure',player.name)
  709. commands.clear(player.name)
  710. end
  711. kew.playerlist = {}
  712. end
  713.  
  714. function checkPlayers(game)
  715. local loggedIn = getAllPos("m=2,name=!Abregado")
  716.  
  717. --print("detected players "..#loggedIn)
  718.  
  719. --check current waiting players
  720. for i,waiter in ipairs(game.waitlist) do
  721. local isPlaying = false
  722. for j, player in ipairs(loggedIn) do
  723. if player.name == waiter.name then
  724. isPlaying = true
  725. table.remove(loggedIn,j)
  726. --break
  727. end
  728. end
  729. if not isPlaying then
  730. table.remove(game.waitlist,i)
  731. print("Removed "..waiter.name.." from waiting list")
  732. end
  733.  
  734. end
  735.  
  736. --check currently playing players
  737. for l,kew in ipairs(game.queues) do
  738. for i,builder in ipairs(kew.playerlist) do
  739. local isPlaying = false
  740. for j, player in ipairs(loggedIn) do
  741. if player.name == builder.name then
  742. isPlaying = true
  743. builder.x = player.x
  744. builder.y = player.y
  745. builder.z = player.z
  746. table.remove(loggedIn,j)
  747. --break
  748. end
  749. end
  750. if not isPlaying then
  751. table.remove(kew.playerlist,i)
  752. print("Removed "..builder.name.." from game in progress")
  753. end
  754.  
  755. end
  756. end
  757.  
  758. --add remaining players to waitlist
  759. for i,newPlayer in ipairs(loggedIn) do
  760. newPlayer.x = game.spawn.x
  761. newPlayer.y = game.spawn.y
  762. newPlayer.z = game.spawn.z
  763. commands.gamemode('adventure',newPlayer.name)
  764. teleportToPoint(newPlayer.x,newPlayer.y,newPlayer.z,{newPlayer})
  765. table.insert(game.waitlist,newPlayer)
  766. print("Added "..newPlayer.name.." to waitlist")
  767. end
  768. end
  769.  
  770.  
  771. function allocateWaiters(game)
  772. --find free slots
  773. local freeslots = {}
  774. for i, kew in ipairs(game.queues) do
  775. if kew.phase == 1 and #kew.playerlist < kew.maxplayers then
  776. local slots = kew.maxplayers - #kew.playerlist
  777. for j=1,slots do
  778. table.insert(freeslots,kew)
  779. end
  780. end
  781. end
  782.  
  783. shuffleTable(game.waitlist)
  784. shuffleTable(freeslots)
  785.  
  786. while #freeslots > 0 and #game.waitlist > 0 do
  787. local player = table.remove(game.waitlist,1)
  788. local freeslot = table.remove(freeslots,1).playerlist
  789. table.insert(freeslot,player)
  790. end
  791. end
  792.  
  793. function updatePlayedZone(buildzone)
  794. local victory = false
  795. local buildzoneSelector = buildzone.selector
  796. --get all players on diamond
  797. local detectLocations = getAllOnBlockType(BLOCKS.DETECT,buildzoneSelector)
  798.  
  799. --DO PARTICLE EFFECTS IF ON A DETECTING BLOCK
  800. for i,loc in ipairs(detectLocations) do
  801. if i == 1 then
  802. commands.particle("fireworksSpark",loc.x,loc.y+1,loc.z,0.01,0.01,0.01,0.1,10)
  803. end
  804. commands.particle("happyVillager",loc.x,loc.y+1,loc.z,0.1,0.1,0.1,0.1,10)
  805. end
  806.  
  807. if #detectLocations > 0 then
  808. local x,y,z,name = detectLocations[1].x,detectLocations[1].y,detectLocations[1].z,detectLocations[1].name
  809. --print(#buildzone.vocab.." vocabs in detected zone")
  810. for i,vocab in ipairs(buildzone.vocab) do
  811. local result,message = commands.testforblocks(vocab.x,vocab.y,vocab.z,vocab.x+vocab.w,vocab.y+VOCAB_HEIGHT,vocab.z+vocab.w,x-math.floor(vocab.w/2),y-1,z-math.floor(vocab.w/2),"masked")
  812. --print(message[1])
  813. if result then
  814. --commands.say("Found vocab "..i)
  815. print(name.." built vocab "..i)
  816. local cloneres,clonemes = commands.clone(vocab.cx,vocab.cy,vocab.cz,vocab.cx+vocab.w,vocab.cy+VOCAB_HEIGHT,vocab.cz+vocab.w,x-math.floor(vocab.w/2),y-1,z-math.floor(vocab.w/2),"masked")
  817. if DEBUG_MODE then
  818. print(clonemes[1])
  819. end
  820. commands.give(name,vocab.reward)
  821. commands.playsound("random.levelup",name)
  822. commands.tellraw(name,'["",{"text":"You built a '..vocab.name.. ' and received more building materials!!","color":"green"}]')
  823.  
  824. table.insert(buildzone.structures,vocab.name)
  825. local count = 0
  826. for i,struct in ipairs(buildzone.structures) do
  827. if struct == VICTORY_SPECIFIC.name then
  828. count = count +1
  829. end
  830. --print(struct)
  831. end
  832.  
  833. --SPECIFIC VICTORY CODE
  834. if count >= VICTORY_SPECIFIC.count then
  835. victory = true
  836. print("Specific count victory achieved")
  837. end
  838.  
  839. --TOTAL STRUCTURES VICTORY CODE
  840. if #buildzone.structures > VICTORY_TOTAL then
  841. victory = true
  842. print("Victory")
  843. for i,struct in ipairs(buildzone.structures) do
  844. print(struct)
  845. end
  846. end
  847.  
  848.  
  849. --HEIGHT VICTORY CODE
  850. if detectLocations[1].y >= buildzone.y+VICTORY_HEIGHT then
  851. victory = true
  852. print("Victory was Triggered at",detectLocations[1].x,detectLocations[1].y,detectLocations[1].z)
  853. end
  854.  
  855.  
  856.  
  857.  
  858. break
  859. end
  860.  
  861.  
  862.  
  863.  
  864. end
  865.  
  866. table.remove(detectLocations,1)
  867. end
  868.  
  869. --check that each buildzone has a villager
  870. local hasVillager,message = commands.testfor("@e[type=Villager,"..buildzoneSelector.."]")
  871. if hasVillager == false and SPAWN_VILLAGER then
  872. spawnVillager(buildzone.x+(buildzone.w/4),buildzone.y+1,buildzone.z+(buildzone.w/4))
  873. end
  874.  
  875. return victory
  876. end
  877.  
  878. function debugDisplay(game)
  879. monitor.clear()
  880. if blink then
  881. monitor.setCursorPos(1,1)
  882. monitor.setTextColor(colors.red)
  883. monitor.write("Running")
  884. monitor.setTextColor(colors.white)
  885. redstone.setOutput("top",true)
  886. blink = false
  887. else
  888. redstone.setOutput("top",false)
  889. blink = true
  890. end
  891. local line = 2
  892.  
  893. for i,kew in ipairs(game.queues) do
  894. monitor.setCursorPos(1,line)
  895. local minutes = string.format("%02d",math.floor(kew.timer/60))
  896. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  897. monitor.write("Buildzone "..i.." | Phase: "..kew.phase.." | Time: "..minutes..":"..seconds)
  898. monitor.setCursorPos(1,line+1)
  899. for i,player in ipairs(kew.playerlist) do
  900. monitor.write(player.name.." ")
  901. end
  902. line = line +2
  903. end
  904.  
  905. monitor.setCursorPos(1,10)
  906. for i,player in ipairs(game.waitlist) do
  907. monitor.write(player.name.." ")
  908. end
  909.  
  910. end
  911. local blink = true
  912. local game = setup()
  913. while true do
  914. update(game)
  915. if monitor then debugDisplay(game) end
  916. commands.weather("clear",10000)
  917. sleep(2)
  918. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement