Advertisement
Guest User

Untitled

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