Advertisement
antonsavov

Untitled

Mar 1st, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.90 KB | None | 0 0
  1. local VERSION = '0.8.4 - goals added'
  2. os.loadAPI('tracker')
  3. os.loadAPI('json')
  4.  
  5. print("Starting 20.000 Blocks")
  6.  
  7. DEBUG_MODE = false
  8. local monitor = peripheral.wrap("right")
  9. local ox,oy,oz = commands.getBlockPosition()
  10.  
  11. SPAWN_VILLAGER = false
  12. DO_GRID = true
  13. GRID_HOLE_CHANCE = 0
  14. NUMBER_OF_BUILDZONES = 6
  15. NUMBER_OF_VOCAB = 10
  16. GAME_LENGTH = 600
  17. VOCAB_WIDTH = 27
  18. VOCAB_HEIGHT = 19
  19. BUILDZONE_WIDTH = 62 -- the actual zone width is 63 blocks, but for some reason due to how minecraft handles relative coordinates this has to be 62
  20. BUILDZONE_FLOOR_HEIGHT = -1
  21. SPAWN = {
  22. x=12110,
  23. y=57,
  24. z=-1777,
  25. a1=90,
  26. a2=0
  27. }
  28. FIRSTZONE= {
  29. x=11685,
  30. y=57,
  31. z=-1869
  32. }
  33. WAITZONE = {
  34. x=12093,
  35. y=56,
  36. z=-1779,
  37. w=5,
  38. l=5
  39. }
  40. VICTORY_HEIGHT = 30
  41. VICTORY_TOTAL = 50
  42. VICTORY_SPECIFIC = {name='garden',count=101}
  43.  
  44. DEFAULT_REWARD = 'minecraft:emerald 2 0 {display:{Name:"Emerald",Lore:[Trade this to the villager for wool]}}'
  45. WOOL_PER_EMERALD = 64
  46. PICKAXE_USES = 10
  47.  
  48. BLOCKS = {
  49. CAMP_FLOOR = {block='minecraft:sandstone',data=0},
  50. CONSTRUCTION = {block='minecraft:diamond_ore',data=0},
  51. RING = {block='minecraft:diamond_ore',data=0},
  52. DETECT = {block='minecraft:red_sandstone',data=0},
  53. DETECT_DEAD = {block='minecraft:obsidian',data=0},
  54. VOCAB_DETECT = {block="minecraft:quartz_block",data=0},
  55. VOCAB_REPLACE = {block="minecraft:coal_block",data=0},
  56. VICTORY_MARKER = {block="minecraft:emerald_block",data=0},
  57. PLUG = {block="minecraft:lapis_ore",data=0},
  58. PHVFLOOR = {block="minecraft:stone_slab",data=3},
  59. BUILDING_HOUSE = {block="minecraft:wool",data=0},
  60. BUILDING_GARDEN = {block="minecraft:wool",data=13},
  61. BUILDING_WATER = {block="minecraft:wool",data=1},
  62. }
  63.  
  64. --contains a list of all posible Buildzones (build a bit later)
  65. LOCS = {}
  66.  
  67. if DEBUG_MODE then
  68. GAME_LENGTH = 12000
  69. NUMBER_OF_BUILDZONES = 1
  70. end
  71.  
  72. --creates a grid of absolute references (0,0) (1,0)
  73. function buildGrid(w,h)
  74. local grid = {}
  75. for x=0,w-1 do
  76. for z=0,h-1 do
  77. table.insert(grid,{x=x,z=z})
  78. end
  79. end
  80. return grid
  81. end
  82.  
  83. --sets where and how big the PHV area is
  84. --second number is along the long edge of PHV starting at the spawn side
  85. --first number is along the short edge of PHV starting from the park side
  86. if DEBUG_MODE then
  87. FIRSTZONE= {
  88. x=5,
  89. y=57,
  90. z=-7
  91. }
  92.  
  93. LOCS = buildGrid(1,3)
  94. else
  95. LOCS = buildGrid(11,27)
  96. end
  97.  
  98. --call these to return a proper minecraft item string including adventure mode properties
  99. function houseBlock(quant)
  100. text = BLOCKS.BUILDING_HOUSE.block..' '..quant..' '..BLOCKS.BUILDING_HOUSE.data..' {display:{Name:"House Construction Block",Lore:[Place this against a Diamond to build a house]},CanPlaceOn:["'..BLOCKS.BUILDING_HOUSE.block..'","'..BLOCKS.PLUG.block..'","'..BLOCKS.DETECT.block..'"]}'
  101. return text
  102. end
  103.  
  104. function waterBlock(quant)
  105. text = BLOCKS.BUILDING_WATER.block..' '..quant..' '..BLOCKS.BUILDING_WATER.data..' {display:{Name:"Water Construction Block",Lore:[Place this against a Diamond to build a pond or pool]},CanPlaceOn:["'..BLOCKS.BUILDING_HOUSE.block..'","'..BLOCKS.PLUG.block..'","'..BLOCKS.DETECT.block..'"]}'
  106. return text
  107. end
  108.  
  109. function gardenBlock(quant)
  110. text = BLOCKS.BUILDING_GARDEN.block..' '..quant..' '..BLOCKS.BUILDING_GARDEN.data..' {display:{Name:"Garden Construction Block",Lore:[Place this against a Diamond to build a pond or pool]},CanPlaceOn:["'..BLOCKS.BUILDING_HOUSE.block..'","'..BLOCKS.PLUG.block..'","'..BLOCKS.DETECT.block..'"]}'
  111. return text
  112. end
  113.  
  114. STARTING_ITEMS = {
  115. houseBlock(30), gardenBlock(30),
  116. 'stone_pickaxe 1 '..131-PICKAXE_USES..' {CanDestroy:["'..BLOCKS.BUILDING_HOUSE.block..'"],display:{Name:"Construction Remover",Lore:[Use this to remove up to '..PICKAXE_USES..' pieces of construction material]}}'
  117.  
  118. }
  119.  
  120. REWARDS = {}
  121. --Urban singlehouse rewards (costs 4 house, -2+1 activators)
  122. REWARDS[1] = gardenBlock(4)
  123. REWARDS[2] = gardenBlock(4)
  124. REWARDS[3] = gardenBlock(4)
  125. REWARDS[4] = gardenBlock(4)
  126. --Urban businesses rewards (costs 6 house, -2+1 activators)
  127. REWARDS[5] = gardenBlock(7)
  128. REWARDS[6] = gardenBlock(7)
  129. REWARDS[7] = gardenBlock(7)
  130. REWARDS[8] = gardenBlock(7)
  131. --Urban Tech&Science Spaces bridge rewards (costs 6 house, -3+2 activators)
  132. REWARDS[9] = gardenBlock(10)
  133. REWARDS[10] = gardenBlock(10)
  134. --Green Tech&Science Spaces garden bridge
  135. REWARDS[11] = gardenBlock(7)
  136. REWARDS[12] = gardenBlock(7)
  137. --House extention rewards (costs 4 house, 1 activators)
  138. REWARDS[13] = houseBlock(6)
  139. REWARDS[14] = houseBlock(6)
  140. REWARDS[15] = houseBlock(6)
  141. REWARDS[16] = houseBlock(6)
  142. --riser 1
  143. REWARDS[17] = houseBlock(1)
  144. --riser 2
  145. REWARDS[18] = houseBlock(1)
  146. --filler garden
  147. REWARDS[19] = houseBlock(1)
  148. --filler house plaza
  149. REWARDS[20] = gardenBlock(3)
  150. --extention-farm (like extension rise) (costs 6 garden, -2 activators)
  151. REWARDS[21] = houseBlock(3)
  152. REWARDS[22] = houseBlock(3)
  153. REWARDS[23] = houseBlock(3)
  154. REWARDS[24] = houseBlock(3)
  155. --extension-garden (like extension row house)rewards (costs 3 garden, 1 activators)
  156. REWARDS[25] = "minecraft:emerald"
  157. REWARDS[26] = "minecraft:emerald"
  158. REWARDS[27] = "minecraft:emerald"
  159. REWARDS[28] = "minecraft:emerald"
  160. --L-shaped small business agriculture (costs 6 garden, -3+2 activators)
  161. REWARDS[29] = "minecraft:emerald 2"
  162. REWARDS[30] = "minecraft:emerald 2"
  163. REWARDS[31] = "minecraft:emerald 2"
  164. REWARDS[32] = "minecraft:emerald 2"
  165. --L-shaped small business urban (costs 6 housing, -3+2 activators)
  166. REWARDS[33] = gardenBlock(6)
  167. REWARDS[34] = gardenBlock(6)
  168. REWARDS[35] = gardenBlock(6)
  169. REWARDS[36] = gardenBlock(6)
  170. --allotments (costs 4 garden, -2 activators)
  171. REWARDS[37] = houseBlock(2)
  172. REWARDS[38] = houseBlock(2)
  173. REWARDS[39] = houseBlock(2)
  174. REWARDS[40] = houseBlock(2)
  175.  
  176. DEFAULT_NAME = 'default-vocab'
  177. VOCAB_NAMES = {
  178. 'freestand-house',
  179. 'freestand-house',
  180. 'freestand-house',
  181. 'freestand-house',
  182. 'dbl-ext-house',
  183. 'dbl-ext-house',
  184. 'dbl-ext-house',
  185. 'dbl-ext-house',
  186. 'bridge-house',
  187. 'bridge-house',
  188. 'bridge-garden',
  189. 'bridge-garden',
  190. 'ext-house',
  191. 'ext-house',
  192. 'ext-house',
  193. 'ext-house',
  194. 'riser-1',
  195. 'riser-2',
  196. 'city-garden',
  197. 'city-plaza',
  198. 'dbl-ext-garden',
  199. 'dbl-ext-garden',
  200. 'dbl-ext-garden',
  201. 'dbl-ext-garden',
  202. 'ext-garden',
  203. 'ext-garden',
  204. 'ext-garden',
  205. 'ext-garden',
  206. 'corner-garden',
  207. 'corner-garden',
  208. 'corner-garden',
  209. 'corner-garden',
  210. 'corner-house',
  211. 'corner-house',
  212. 'corner-house',
  213. 'corner-house',
  214. 'freestand-garden',
  215. 'freestand-garden',
  216. 'freestand-garden',
  217. 'freestand-garden'
  218. }
  219.  
  220.  
  221.  
  222. ----------------------------------------------------------------------------------------------------------------
  223. -- the data structure below represents the catalogue of vocabulary as built in minecraft
  224. -- id - is unique for each vocab, starting from 1 going to 40 currently
  225. -- column and row - represent the "physical" location of the vocab model in the catalogue in the minecraft world
  226. -- nameEN and name DE - are names for english and german. for now names must not have spaces, use dashes (-) instead
  227. -- typeEN and typeDE - are the type names, also in english and german such as tech&science spaces
  228. -- height - is how many blocks is this structure tall
  229. -- slots - is how many potential detector stones is this structure occupying as a building, ex. 2 for houses, 3 for corner extenstions, 0 for plazas
  230. -- green - is true or false and is used for easier calculation of the GREENEST GOAL
  231. -- greenSlots - is how many slots are green and is used for easier calculation of the GREENEST GOAL
  232. VOCABS_DATA = {
  233. {id=1, column=1, row=1,nameEN="House",nameDE="Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=false, greenSlots=0},
  234. {id=2, column=1, row=2,nameEN="House",nameDE="Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=false, greenSlots=0},
  235. {id=3, column=1, row=3,nameEN="House",nameDE="Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=false, greenSlots=0},
  236. {id=4, column=1, row=4,nameEN="House",nameDE="Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=false, greenSlots=0},
  237. --
  238. {id=5, column=2, row=1,nameEN="Gym",nameDE="Fitnessstudio", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=false, greenSlots=0},
  239. {id=6, column=2, row=2,nameEN="Office-Building",nameDE="Bürogebäude", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=false, greenSlots=0},
  240. {id=7, column=2, row=3,nameEN="Hotel",nameDE="Hotel", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=false, greenSlots=0},
  241. {id=8, column=2, row=4 ,nameEN="Bank",nameDE="Bank", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=false, greenSlots=0},
  242. --
  243. {id=9, column=3, row=1,nameEN="Library",nameDE="Bibliothek", typeEN="Tech & Science Spaces",typeDE="Wissenschaftsgebäude",height=16, slots=1, green=false, greenSlots=0},
  244. {id=10, column=3, row=2,nameEN="FabLab",nameDE="Innovationszentrum", typeEN="Tech & Science Spaces",typeDE="Wissenschaftsgebäude",height=16, slots=1, green=false, greenSlots=0},
  245. {id=11, column=3, row=3,nameEN="Aquaponic-Greenhouse",nameDE="Aquaponik-Gewächshaus", typeEN="Tech & Science Spaces",typeDE="Wissenschaftsgebäude",height=16, slots=1, green=true, greenSlots=1},
  246. {id=12, column=3, row=4,nameEN="Science-Bridge",nameDE="Wissenschaftsbrücke", typeEN="Tech & Science Spaces",typeDE="Wissenschaftsgebäude",height=16, slots=1, green=true, greenSlots=1},
  247. --
  248. {id=13, column=4, row=1,nameEN="House-Extension",nameDE="Hauserweiterung", typeEN="Extensions",typeDE="Erweiterungen",height=10, slots=1, green=false, greenSlots=0},
  249. {id=14, column=4, row=2,nameEN="House-Extension",nameDE="Hauserweiterung", typeEN="Extensions",typeDE="Erweiterungen",height=10, slots=1, green=false, greenSlots=0},
  250. {id=15, column=4, row=3,nameEN="House-Extension",nameDE="Hauserweiterung", typeEN="Extensions",typeDE="Erweiterungen",height=10, slots=1, green=false, greenSlots=0},
  251. {id=16, column=4, row=4,nameEN="House-Extension",nameDE="Hauserweiterung", typeEN="Extensions",typeDE="Erweiterungen",height=10, slots=1, green=false, greenSlots=0},
  252. --
  253. {id=17, column=5, row=1,nameEN="Double-Floor-Riser",nameDE="Verschiebung-zwei-hoch", typeEN="",typeDE="vertikale Verschiebungen",height=0, slots=0, green=false, greenSlots=0},
  254. {id=18, column=5, row=2,nameEN="Single-Floor-Riser",nameDE="Verschiebung-einen-hoch", typeEN="",typeDE="vertikale Verschiebungen",height=0, slots=0, green=false, greenSlots=0},
  255. {id=19, column=5, row=3,nameEN="City-Plaza",nameDE="städtischer Platz", typeEN="Plazas",typeDE="Plätze",height=0, slots=0, green=false, greenSlots=0},
  256. {id=20, column=5, row=4,nameEN="Park-Plaza",nameDE="grüner Platz", typeEN="Plazas",typeDE="Plätze",height=0, slots=0, green=true, greenSlots=1},
  257. ---
  258. {id=21, column=6, row=1,nameEN="Green-Gym",nameDE="grünes Fitnessstudio", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=true, greenSlots=5},
  259. {id=22, column=6, row=2,nameEN="Green-Office-Building",nameDE="grünes Bürogebäude", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=true, greenSlots=5},
  260. {id=23, column=6, row=3,nameEN="Green-Hotel",nameDE="grünes Hotel", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=true, greenSlots=5},
  261. {id=24, column=6, row=4,nameEN="Green-Bank",nameDE="grünes Bank", typeEN="Businesses",typeDE="Geschäfte",height=20, slots=5, green=true, greenSlots=5},
  262. ---
  263. {id=25, column=7, row=1,nameEN="House-Garden",nameDE="Privatgarten", typeEN="Extensions",typeDE="Erweiterungen",height=0, slots=0, green=true, greenSlots=1},
  264. {id=26, column=7, row=2,nameEN="House-Garden",nameDE="Privatgarten", typeEN="Extensions",typeDE="Erweiterungen",height=0, slots=0, green=true, greenSlots=1},
  265. {id=27, column=7, row=3,nameEN="House-Garden",nameDE="Privatgarten", typeEN="Extensions",typeDE="Erweiterungen",height=0, slots=0, green=true, greenSlots=1},
  266. {id=28, column=7, row=4,nameEN="House-Garden",nameDE="Privatgarten", typeEN="Extensions",typeDE="Erweiterungen",height=0, slots=0, green=true, greenSlots=1},
  267. ---
  268. {id=29, column=8, row=1,nameEN="Open-Аir-Cinema",nameDE="Freiluftkino", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=true, greenSlots=3},
  269. {id=30, column=8, row=2,nameEN="Open-Air-Swimming-Pool",nameDE="Freibad", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=true, greenSlots=3},
  270. {id=31, column=8, row=3,nameEN="Green-Cafe",nameDE="grünes Café", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=true, greenSlots=3},
  271. {id=32, column=8, row=4,nameEN="Palm-Tree-House",nameDE="Palmengarten", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=true, greenSlots=3},
  272. ----
  273. {id=33, column=9, row=1,nameEN="Cinema",nameDE="Kino", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=false, greenSlots=0},
  274. {id=34, column=9, row=2,nameEN="Conference-Hall",nameDE="Konferenzhalle", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=false, greenSlots=0},
  275. {id=35, column=9, row=3,nameEN="Café",nameDE="Café", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=false, greenSlots=0},
  276. {id=36, column=9, row=4,nameEN="Art-Gallery",nameDE="Kunstgalerie", typeEN="Gathering Spaces",typeDE="Treffpunkte",height=8, slots=3, green=false, greenSlots=0},
  277. ---
  278. {id=37, column=10, row=1,nameEN="Green-Roof-House",nameDE="grünes Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=true, greenSlots=2},
  279. {id=38, column=10, row=2,nameEN="Green-Roof-House",nameDE="grünes Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=true, greenSlots=2},
  280. {id=39, column=10, row=3,nameEN="Green-Roof-House",nameDE="grünes Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=true, greenSlots=2},
  281. {id=40, column=10, row=4,nameEN="Green-Roof-House",nameDE="grünes Einfamilienhaus", typeEN="Houses",typeDE="Häuser",height=10, slots=2, green=true, greenSlots=2},
  282. }
  283.  
  284.  
  285. --constructor for player object so that data structure is consistant
  286. function newPlayerData(name,x,y,z)
  287. local p = {
  288. name = name,
  289. x=x,
  290. y=y,
  291. z=z
  292. }
  293. return p
  294. end
  295.  
  296. --return a list of all players in the game world as player objects
  297. local function getAllPos(selector)
  298. local result, message = commands.tp("@a["..selector.."]","~ ~ ~")
  299. local names = {}
  300. if result == true then
  301. for i,result in ipairs(message) do
  302. local wordpattern = "[^, ]+"
  303. local numberpattern = "[%-% ]%d+[%.]%d+"
  304. local words,numbers = {},{}
  305.  
  306. for word in string.gmatch(result, wordpattern) do
  307. table.insert(words,word)
  308. end
  309.  
  310. for number in string.gmatch(result, numberpattern) do table.insert(numbers,number) end
  311.  
  312. local coords = {
  313. x = math.floor(numbers[1]),
  314. y = math.floor(numbers[2]),
  315. z = math.floor(numbers[3])
  316. }
  317. local name = words[2]
  318. table.insert(names,newPlayerData(name,coords.x,coords.y,coords.z))
  319. --print("Player Found - getAllPos")
  320. end
  321. end
  322. return names
  323. end
  324.  
  325. --sort table by random
  326. local function shuffleTable( t )
  327. local rand = math.random
  328. assert( t, "shuffleTable() expected a table, got nil" )
  329. local iterations = #t
  330. local j
  331.  
  332. for i = iterations, 2, -1 do
  333. j = rand(i)
  334. t[i], t[j] = t[j], t[i]
  335. end
  336. end
  337.  
  338. --returns a list of player objects containing all players who are standing on the given block, and who also are in the given selection
  339. local function getAllOnBlockType(block,selector)
  340. local result, message = commands.exec("execute @a["..selector.."] ~ ~ ~ detect ~ ~-1 ~ "..block.." -1 tp @p[r=1] ~ ~ ~")
  341. local names = {}
  342. if result == true then
  343. for i,result in ipairs(message) do
  344. local wordpattern = "[^, ]+"
  345. local numberpattern = "[%-% ]%d+[%.]%d+"
  346. local words,numbers = {},{}
  347.  
  348. for word in string.gmatch(result, wordpattern) do table.insert(words,word) end
  349. for number in string.gmatch(result, numberpattern) do table.insert(numbers,number) end
  350.  
  351. if numbers[1] and numbers[2] and numbers[3] then
  352. local coords = {
  353. x = math.floor(numbers[1]),
  354. y = math.floor(numbers[2]),
  355. z = math.floor(numbers[3])
  356. }
  357. local name = words[2]
  358. table.insert(names,newPlayerData(name,coords.x,coords.y,coords.z))
  359. print("Found a player - getOnBlock")
  360. else
  361. print("Error: Coordinate Numbers were missing")
  362. end
  363. end
  364. end
  365. return names
  366. end
  367.  
  368. --gives a player a squid egg with their name on it. Removes all spawn eggs first
  369. local function giveSquid(name)
  370. commands.clear(name,'spawn_egg')
  371. commands.give(name,'spawn_egg 1 94 {CanPlaceOn:["'..BLOCKS.PHVFLOOR.block..'","'..BLOCKS.CAMP_FLOOR.block..'"],display:{Name:"Heimkehrer - '..name..'",Lore:[Use this on the floor to return to spawn]}}')
  372. end
  373.  
  374. --returns a list of squid objects and their names
  375. local function getSquids()
  376. local result, message = commands.exec("execute @e[type=Squid] ~ ~ ~ tp @e[r=1] ~ ~ ~")
  377. local names = {}
  378. if result == true then
  379. for i,result in ipairs(message) do
  380. local wordpattern = "[^, ]+"
  381. local numberpattern = "[%-% ]%d+[%.]%d+"
  382. local words,numbers = {},{}
  383.  
  384. for word in string.gmatch(result, wordpattern) do table.insert(words,word) print(word) end
  385. for number in string.gmatch(result, numberpattern) do table.insert(numbers,number) end
  386.  
  387. if numbers[1] and numbers[2] and numbers[3] then
  388. local coords = {
  389. x = math.floor(numbers[1]),
  390. y = math.floor(numbers[2]),
  391. z = math.floor(numbers[3])
  392. }
  393. local name = words[4]
  394. table.insert(names,newPlayerData(name,coords.x,coords.y,coords.z))
  395. print("Found a player - getOnBlock "..name)
  396. else
  397. print("Error: Coordinate Numbers were missing")
  398. end
  399. end
  400. end
  401. return names
  402. end
  403.  
  404. local function removePlayerFromKew(game,playername)
  405. for _,kew in pairs(game.queues) do
  406. for index, player in ipairs(kew.playerlist) do
  407. if player.name == playername then
  408. table.remove(kew.playerlist,index)
  409. end
  410. end
  411. if #kew.playerlist == 0 and kew.phase == 2 then
  412. --game can be ended as no players are left
  413. kew.timer = 0
  414. end
  415. end
  416. end
  417.  
  418. local function movePlayerToSpawn(playername)
  419. respawnPlayer(playername)
  420. commands.async.tp(playername,SPAWN.x,SPAWN.y,SPAWN.z,SPAWN.a1,SPAWN.a2)
  421. commands.async.tellraw(playername,'["",{"text":"TELEPORTED: You used your Heimkehrer","color":"blue"}]')
  422. commands.async.tellraw(playername,'["",{"text":"DE: TELEPORTED: You used your Heimkehrer","color":"orange"}]')
  423. end
  424.  
  425. --takes a list of squids and deals with all those players, moving them back to spawn and removing them from game
  426. --also gives them a new squid
  427. local function dealWithSquidders(game,squids)
  428. for _,squid in pairs(squids) do
  429. --remove player from current game if in game
  430. removePlayerFromKew(game,squid.name)
  431. --teleport them back to spawn
  432. movePlayerToSpawn(squid.name)
  433. --give a new squid
  434. giveSquid(squid.name)
  435. --particle effects
  436. --teleport message
  437. end
  438. --kill all squids
  439. if #squids>0 then
  440. commands.tp("@e[type=Squid]",100000,200,100000)
  441. commands.kill("@e[type=Squid]")
  442. os.sleep(#squids*0.2)
  443. end
  444. end
  445.  
  446.  
  447.  
  448.  
  449. --creates a villager with special items
  450. local function spawnVillager(x,y,z)
  451. 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"]}}} ]}}')
  452. end
  453.  
  454. --displays a subtitle of time to a selection of players
  455. local function displayTime(selector,minutes,seconds)
  456. --commands.title("@a["..selector.."]","subtitle",'{text:"Time left: '..minutes..":"..seconds..'",color:red,bold:false,underlined:false,italic:false,strikethrough:false,obfuscated:false}')
  457. commands.async.xp("-1000000L","@a["..selector.."]")
  458. local secondstot = (minutes * 60) + seconds
  459. commands.async.xp(tostring(secondstot).."L","@a["..selector.."]")
  460. end
  461.  
  462. --displays a title to a selection of players
  463. local function displayTitle(selector,text)
  464. commands.async.title("@a["..selector.."]","title",'{text:"'..text..'"}')
  465. end
  466.  
  467. --simply runs displayTitle on a list of players
  468. local function displayTitleToGroup(playerlist,text)
  469. for i,player in ipairs(playerlist) do
  470. displayTitle("name="..player.name,text)
  471. end
  472. end
  473.  
  474. --simply runs displayTime on a list of players
  475. local function displayTimeToGroup(playerlist,minutes,seconds)
  476. for i,player in ipairs(playerlist) do
  477. displayTime("name="..player.name,minutes,seconds)
  478. end
  479. end
  480.  
  481. --teleports a list of players to an exact place and sends them a message about it
  482. local function teleportToPoint(x,y,z,playerlist,clear,textEN, textDE)
  483. for i,player in ipairs(playerlist) do
  484. player.x = x
  485. player.y = y
  486. player.z = z
  487. commands.async.gamemode(2,player.name)
  488. if clear then
  489. commands.async.clear(player.name,"wool")
  490. commands.async.clear(player.name,"stone_pickaxe")
  491. end
  492. commands.tp("@a[name="..player.name.."]",x,y,z)
  493. commands.async.tellraw(player.name,'["",{"text":"'..textEN..'","color":"blue"}]')
  494. commands.async.tellraw(player.name,'["",{"text":"'..textDE..'","color":"orange"}]')
  495. end
  496. end
  497.  
  498. --teleports a list of players to a given buildzone
  499. local function teleportToZone(buildzone,playerlist,textEN, textDE)
  500. teleportToPoint(buildzone.x+2+(buildzone.w/2),buildzone.y+5,buildzone.z+2+(buildzone.w/2),playerlist,true,textEN, textDE)
  501.  
  502. end
  503.  
  504. --gives the same list of items to a list of players
  505. local function giveItems(playerlist,itemlist)
  506. local given = 0
  507. for i,player in ipairs(playerlist) do
  508. --commands.async.clear(player.name)
  509. for j,item in ipairs(itemlist) do
  510. commands.async.give("@a[name="..player.name.."]",item)
  511. given = given +1
  512. end
  513. giveSquid(player.name)
  514. end
  515. return given
  516. end
  517.  
  518. --a multi builder which uses the vocab constructor to create sets of vocab
  519. local function makeVocabZones(quant,w)
  520. local x,y,z = ox,oy,oz+6
  521. local result = {}
  522. local id = 1
  523. for i=0,quant-1 do
  524. for k=0,3 do
  525. local zpos = i-4
  526. local ypos = k
  527. --print("vocab at X")
  528. --print(x-(2*w)-6)
  529. --print("and Z")
  530. --print(z+((w+1)*zpos))
  531. local nextVocab = newVocabZone(
  532. x-(2*w)-6,y+(ypos*(VOCAB_HEIGHT+3)),
  533. z+((w+1)*zpos),
  534. w,
  535. id,
  536. REWARDS[id] or DEFAULT_REWARD,
  537. VOCABS_DATA[id].nameEN or DEFAULT_NAME,
  538. VOCABS_DATA[id].nameDE or DEFAULT_NAME,
  539. VOCABS_DATA[id].typeEN,
  540. VOCABS_DATA[id].typeDE,
  541. VOCABS_DATA[id].height,
  542. VOCABS_DATA[id].slots,
  543. VOCABS_DATA[id].green,
  544. VOCABS_DATA[id].greenSlots
  545. )
  546. table.insert(result,nextVocab)
  547. id = id +1
  548. end
  549. end
  550. return result
  551. end
  552.  
  553. --finds the next free location(or buildzone) for a new game in a given area.
  554. --giving force as True will mean it overrides the first location no matter what
  555. local function findNextLoc(width,zones,force)
  556. local x,y,z = 0,0,0
  557. for i,loc in ipairs(LOCS) do
  558. x,y,z = FIRSTZONE.x+(loc.x*(width+1)),FIRSTZONE.y,FIRSTZONE.z+(-loc.z*(width+1))
  559. local result,message = commands.testforblock(x,y+BUILDZONE_FLOOR_HEIGHT,z,"minecraft:air")
  560. if force then result = true end
  561. local zonefree = true
  562. for i,zone in ipairs(zones) do
  563. if zone.x == x and zone.z == z then
  564. zonefree = false
  565. end
  566. end
  567. --print("next position free is ",loc.x*width,oy,loc.z*width)
  568. --if result then print("true") else print("false") end
  569. if result and zonefree then
  570. --print("using loc: ",loc.x,loc.z)
  571. return x,y,z
  572. end
  573. end
  574. return nil,nil,nil
  575.  
  576. end
  577.  
  578. --relocates a buildzone to a new coordinate safely
  579. --avoids overlapping with other buildzones
  580. function moveBuildzone(buildzone,zones)
  581. local x,y,z = findNextLoc(buildzone.w,zones)
  582. if x and y and z then
  583. print("moved buildzone from "..buildzone.x..","..buildzone.z.." to "..x..","..y)
  584. local w = buildzone.w
  585. buildzone.x,buildzone.y,buildzone.z = x,y+BUILDZONE_FLOOR_HEIGHT,z
  586. buildzone.selector = "x="..x..",y="..tostring(y-1)..",z="..z..",dx="..w..",dy=256,dz="..w
  587. buildzone.structures = {} --a list of all vocabularies which have been contructed
  588. else
  589. print("buildzone at "..buildzone.x..","..buildzone.z.." stayed where it is")
  590. end
  591. end
  592.  
  593. --multi builder to create sets of buildzones using the buildzone constructor
  594. local function makeBuildzones(quant,vocab,width,height)
  595. local result = {}
  596. for i=1,quant do
  597. local x,y,z = findNextLoc(width,result)
  598. if x and y and z then
  599. --print("made new buildzone at",x,y,z)
  600. table.insert(result,newBuildZone(x,y+height,z,width,vocab))
  601. else
  602. --print("failed to make new buildzone")
  603. end
  604. end
  605.  
  606. local remaining = NUMBER_OF_BUILDZONES - #result
  607.  
  608. for i=1, remaining do
  609. local x,y,z = findNextLoc(width,result,true)
  610. if x and y and z then
  611. --print("forced new buildzone at",x,y,z)
  612. table.insert(result,newBuildZone(x,y+height,z,width,vocab))
  613. else
  614. print("failed to force new buildzone")
  615. end
  616. end
  617.  
  618.  
  619. return result
  620. end
  621.  
  622. --vocab constructor old version - DELETE IN NEXT COMMIT. Enforces some data structure
  623. function newVocabZoneOld(x,y,z,w,reward,name)
  624. local nvz = {}
  625. nvz.x ,nvz.y ,nvz.z ,nvz.w = x,y,z,w
  626.  
  627. nvz.cx = nvz.x - nvz.w - 2
  628. nvz.cy = nvz.y
  629. nvz.cz = nvz.z
  630. nvz.name = name
  631. nvz.reward = reward
  632.  
  633. return nvz
  634.  
  635. end
  636.  
  637. --vocab constructor. Enforces some data structure
  638. function newVocabZone(x,y,z,w,id, reward,nameEN, nameDE, typeEN, typeDE, height, slots,green,greenSlots)
  639. local nvz = {}
  640. nvz.x ,nvz.y ,nvz.z ,nvz.w = x,y,z,w
  641.  
  642. nvz.cx = nvz.x - nvz.w - 2
  643. nvz.cy = nvz.y
  644. nvz.cz = nvz.z
  645. nvz.nameEN = nameEN
  646. nvz.reward = reward
  647. --- new stuff
  648. nvz.id = id
  649. nvz.nameDE = nameDE
  650. nvz.typeEN = typeEN
  651. nvz.typeDE = typeDE
  652. nvz.height = height
  653. nvz.slots = slots
  654. nvz.green = green
  655. nvz.greenSlots = greenSlots
  656.  
  657. return nvz
  658.  
  659. end
  660.  
  661. --buildzone constructor. Enforces some data structure
  662. function newBuildZone(x,y,z,w,vocabZones)
  663. local nbz = {}
  664. nbz.x ,nbz.y ,nbz.z ,nbz.w = x,y,z,w
  665. nbz.selector = "x="..x..",y="..(y-5)..",z="..z..",dx="..w..",dy=256,dz="..w
  666. nbz.structures = {} --a list of all vocabularies names which have been contructed
  667. nbz.buildings = {} --a list of all vocabularies with full data (x,y,z,id,name) which have been contructed
  668. nbz.filledSlots = 0 --to count how many slots have been filled with buildings. the matrix is 7x7x20 slots. one slot is 9x9x9 blocks big
  669. nbz.greenSlots = 0 --to count how many of the slots are green. the matrix is 7x7x20 slots. one slot is 9x9x9 blocks big
  670. nbz.variety = {} -- this stores how many buildings of each type are there. it is indexed on vocab.id and the value is the number of buildings from type vocab.id
  671. nbz.waitingForCheck = {}
  672. nbz.highest = 0
  673. nbz.vocab = vocabZones
  674.  
  675. return nbz
  676. end
  677.  
  678. --kew constructor. Enforces some data structure
  679. function newQueue(buildzone,maxplayers)
  680. local q = {}
  681. q.timer = 1
  682. q.phase = 1
  683.  
  684. q.victory = false
  685. q.phases = {
  686. {
  687. name = "Selecting Players",
  688. length = 25,
  689. displaylength = 15
  690. },
  691. {
  692. name = "Game In Progress",
  693. length = GAME_LENGTH,
  694. displaylength = 70
  695. },
  696. {
  697. name = "Round Complete",
  698. length = 35,
  699. displaylength = 5
  700. }
  701. }
  702. q.playerlist = {}
  703. q.maxplayers = maxplayers
  704. q.buildzone = buildzone
  705.  
  706. return q
  707. end
  708.  
  709.  
  710. --creates a ring of blocks using coordinates
  711. function fillRing(x,y,z,w,block)
  712. commands.fill(x,y,z,x+w,y,z,block)
  713. commands.fill(x+w,y,z,x+w,y,z+w,block)
  714. commands.fill(x,y,z+w,x+w,y,z+w,block)
  715. commands.fill(x,y,z+w,x,y,z,block)
  716. end
  717.  
  718. function setup()
  719. local game = {}
  720. game.vocab = {}
  721. game.builds = {}
  722. game.queues = {}
  723. game.waitlist = {}
  724. game.spawn = SPAWN
  725. game.lastClock = os.clock()
  726. game.nowTime = os.clock()
  727.  
  728. --kill all villagers
  729. commands.exec("kill @e[type=Villager]")
  730.  
  731. --buildzone and vocabzone creation
  732. game.vocab = makeVocabZones(NUMBER_OF_VOCAB,VOCAB_WIDTH)
  733.  
  734. game.builds = makeBuildzones(NUMBER_OF_BUILDZONES,game.vocab,BUILDZONE_WIDTH,BUILDZONE_FLOOR_HEIGHT)
  735.  
  736. for i,build in ipairs(game.builds) do
  737. table.insert(game.queues,newQueue(build,4))
  738. end
  739.  
  740. for i,vz in ipairs(game.vocab) do
  741. local x,y,z,w = vz.x,vz.y,vz.z,vz.w
  742. local cx,cy,cz = vz.cx,vz.cy,vz.cz
  743.  
  744. local detector, message1 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT.block)
  745. local blocker, message2 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD.block)
  746. if not (detector or blocker) then
  747. for nx=0,2 do
  748. for nz=0,2 do
  749. commands.setblock(x+(nx*9)+4,y-1,z+(nz*9)+4,BLOCKS.VOCAB_REPLACE.block)
  750. commands.setblock(cx+(nx*9)+4,cy-1,cz+(nz*9)+4,BLOCKS.VOCAB_DETECT.block)
  751. end
  752. end
  753. commands.setblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD.block)
  754.  
  755. end
  756.  
  757. end
  758.  
  759. for i, name in ipairs(VOCAB_NAMES) do
  760. commands.scoreboard("objectives","add",name,"dummy")
  761. end
  762.  
  763. commands.gamerule("doDaylightCycle",false)
  764. commands.gamerule("keepInventory",true)
  765. commands.gamerule("doTileDrops",false)
  766. commands.gamerule("logAdminCommands",false)
  767. commands.gamerule("commandBlockOutput",false)
  768. commands.time("set",6000)
  769. commands.scoreboard("objectives","add","highscores","dummy","Best Neighbourhoods")
  770. commands.scoreboard("objectives","add","VillagerLife","dummy")
  771. commands.scoreboard("objectives","add","built","dummy", "Structures Built")
  772. commands.scoreboard("objectives","add","highest","dummy", "Personal Highest")
  773. commands.scoreboard("objectives","add","played","dummy","Games Played")
  774.  
  775. commands.title("@a","times",0,30,30) -- what does this do?
  776. math.randomseed( os.time() )
  777. commands.scoreboard("objectives","setdisplay","sidebar","highscores")
  778. commands.scoreboard("objectives","setdisplay","list","played")
  779.  
  780. if DEBUG_MODE then
  781. for i,build in ipairs(game.builds) do
  782. build.phase = 2
  783. build.timer = 500
  784. end
  785. end
  786.  
  787. print("Computer Co-ordinates ",ox,oy,oz)
  788. print("20.000 Blocks Active!")
  789.  
  790. return game
  791. end
  792.  
  793. function checkForSquids(game)
  794. --execute on all squids
  795. local squidders = getSquids()
  796. dealWithSquidders(game,squidders)
  797. end
  798.  
  799. --main game loop
  800. --runs the game object through each of these update steps in order
  801. function update(game)
  802. local elapsed = updateClock(game)
  803. --update players
  804. checkPlayers(game)
  805. doTimerUpdates(game,elapsed)
  806. doPhaseUpdates(game)
  807. doPhaseEnds(game)
  808. checkBoundaries(game)
  809. checkForSquids(game)
  810. if #game.waitlist > 0 then allocateWaiters(game) end
  811. end
  812.  
  813. --calculates elapsed time during a game tick
  814. function updateClock(game)
  815. game.nowTime = os.clock()
  816. local elapsed = game.nowTime - game.lastClock
  817. game.lastClock = game.nowTime
  818. return elapsed
  819. end
  820.  
  821. --updates all kews in the game object based on elapsed time
  822. function doTimerUpdates(game,elapsed)
  823. for i,kew in ipairs(game.queues) do
  824. kew.timer = kew.timer - elapsed
  825. end
  826. end
  827.  
  828. --check players are inside their buildzone and move them back if not
  829. function checkBoundaries(game)
  830. for i,kew in ipairs(game.queues) do
  831. if kew.phase ==2 then
  832. --boundaries
  833. local x_min = kew.buildzone.x
  834. local x_max = kew.buildzone.x+kew.buildzone.w
  835. local z_min = kew.buildzone.z
  836. local z_max = kew.buildzone.z+kew.buildzone.w
  837.  
  838. local toBeCorrected = {}
  839.  
  840. for j,player in ipairs(kew.playerlist) do
  841. local listOfOne = getAllPos('m=2,name='..player.name)
  842. if listOfOne and listOfOne[1] then
  843. player.x = listOfOne[1].x
  844. player.y = listOfOne[1].y
  845. player.z = listOfOne[1].z
  846. local changed = false
  847. if player.x > x_max then
  848. changed = true
  849. player.x = x_max-2
  850. end
  851. if player.x < x_min then
  852. changed = true
  853. player.x = x_min+2
  854. end
  855. if player.z > z_max then
  856. changed = true
  857. player.z = z_max-2
  858. end
  859. if player.z < z_min then
  860. changed = true
  861. player.z = z_min+2
  862. end
  863. if changed then teleportToPoint(player.x,kew.buildzone.y,player.z,{player},false,"TELEPORTED: Please stay inside the building zone until your game has ended", "DE: TELEPORTED: Please stay inside the building zone until your game has ended") end
  864. end
  865. end
  866. end
  867. end
  868. end
  869.  
  870.  
  871. --here you can set the logic which determines if a buildzone was valuable.
  872. --Return true if it is crap and should be replaced
  873. function checkIfBuildzoneIsCrap(buildzone)
  874. if #buildzone.structures < 5 then
  875. print("Buildzone was crap")
  876. return true
  877. end
  878. print("Buildzone was ok")
  879. return false
  880. end
  881.  
  882.  
  883. --Everything that happens after a buildzone was completed
  884. --due to time limit.
  885. function cleanAfterVictory(buildzone)
  886. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,BLOCKS.VICTORY_MARKER.block)
  887. fillRing(buildzone.x,buildzone.y,buildzone.z,buildzone.w,"minecraft:air",0,"replace",BLOCKS.CONSTRUCTION.block,BLOCKS.CONSTRUCTION.data)
  888. for h=0,VICTORY_HEIGHT do
  889. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air 0","replace",BLOCKS.DETECT.block,BLOCKS.DETECT.data)
  890. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air 0","replace",BLOCKS.PLUG.block,BLOCKS.PLUG.data)
  891. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air 0","replace",BLOCKS.BUILDING_GARDEN.block,BLOCKS.BUILDING_GARDEN.data)
  892. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air 0","replace",BLOCKS.BUILDING_HOUSE.block,BLOCKS.BUILDING_HOUSE.data)
  893. end
  894. commands.async.fill(buildzone.x,buildzone.y-1,buildzone.z,buildzone.x+buildzone.w,buildzone.y-1,buildzone.z+buildzone.w,BLOCKS.CAMP_FLOOR.block,BLOCKS.CAMP_FLOOR.data,"replace",BLOCKS.PLUG.block,BLOCKS.PLUG.data)
  895. commands.async.fill(buildzone.x,buildzone.y,buildzone.z,buildzone.x+buildzone.w,buildzone.y,buildzone.z+buildzone.w,BLOCKS.PHVFLOOR.block,BLOCKS.PHVFLOOR.data,"replace","minecraft:air","0")
  896.  
  897. local wasCrap = checkIfBuildzoneIsCrap(buildzone)
  898. if wasCrap then
  899. --mark this buildzone for replacement
  900. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,"minecraft:air")
  901. end
  902. end
  903.  
  904. --these happen every tick and require the game object
  905. --updates are performed on each Queue (kew) and within each
  906. --of those on each buildzone.
  907. function doPhaseUpdates(game)
  908. for i,kew in ipairs(game.queues) do
  909.  
  910. local minutes = string.format("%02d",math.floor(kew.timer/60))
  911. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  912. if kew.timer <= 0 then
  913. minutes = "00"
  914. seconds = "00"
  915. end
  916.  
  917. if kew.phase == 1 then
  918. --waiting phase
  919. if #kew.playerlist == kew.maxplayers and kew.timer > 5 then kew.timer = 5 end
  920. if not DEBUG_MODE and #kew.playerlist == 0 then kew.timer = kew.phases[1].length end
  921.  
  922. displayTitleToGroup(kew.playerlist,"Game starting!")
  923. displayTimeToGroup(kew.playerlist,minutes,seconds)
  924. --show countdown
  925. elseif kew.phase == 2 then
  926. --playing phase
  927. if #kew.playerlist == 0 then timer = 0 end -- finish if all players quit
  928. -- do vocab logic
  929. local victory = updatePlayedZone(kew.buildzone) --currently victory updatePlayedZone returns always false
  930. --
  931. displayTimeToGroup(kew.playerlist,minutes,seconds)
  932.  
  933. elseif kew.phase == 3 then
  934. --end phase
  935. displayTitleToGroup(kew.playerlist,"Use Heimkehrer to return")
  936. displayTimeToGroup(kew.playerlist,minutes,seconds)
  937.  
  938.  
  939. end
  940. end
  941. end
  942.  
  943. --this runs after a buildzone is completed
  944. --it should tally structure types and set scoreboard highscores
  945. --it also rewards all participants with more played score
  946. function processHighscores(kew)
  947. local buildzone = kew.buildzone
  948.  
  949.  
  950.  
  951. --add score to players who finished this game
  952. for _,player in ipairs(kew.playerlist) do
  953. commands.async.scoreboard("players","add",player.name,"played",1)
  954. end
  955. end
  956.  
  957. --function to export a buildzone detail once it is complete
  958. --requires a kew so it can access the playerlist
  959. local function exportKewData(kew)
  960. local buildzone = kew.buildzone
  961. local saved = {}
  962. saved.position =
  963. {
  964. x=buildzone.x,
  965. y=buildzone.y,
  966. z=buildzone.z
  967. }
  968. saved.completed = os.time()
  969. saved.players = {}
  970. for _, player in ipairs(kew.playerlist) do
  971. table.insert(saved.players,player.name)
  972. end
  973. saved.structures = buildzone.structures
  974. saved.totals = tracker.tallyTable(buildzone.structures)
  975. saved.highest = buildzone.highest - buildzone.y
  976.  
  977. fs.makeDir("/records")
  978. local file = fs.open("/records/"..buildzone.x.."_"..buildzone.z,"w")
  979. file.write(json.encodePretty(saved))
  980. file.close()
  981. end
  982.  
  983. --this code runs ONCE at the end of each phase
  984. --what actually happens is specific to which phase the
  985. --particular kew is in.
  986. function doPhaseEnds(game)
  987. for i,kew in ipairs(game.queues) do
  988. if kew.timer <= 0 then
  989. if kew.phase == 1 then
  990. --waiting phase ends goto play phase
  991.  
  992. moveBuildzone(kew.buildzone,game.builds)
  993. teleportToZone(kew.buildzone,kew.playerlist,"TELEPORTED: Your game has started.", "DE: TELEPORTED: Your game has started.")--teleport selected players
  994. cleanBuildzone(kew.buildzone)
  995. prepareBuildzone(kew.buildzone)--prepare build zone
  996. giveItems(kew.playerlist,STARTING_ITEMS) --give starting items
  997. displayTitle(kew.buildzone.selector,"Build!")
  998. kew.victory = false
  999. displayTime(kew.buildzone.selector,0,0)
  1000. kew.phase = 2
  1001. kew.timer = kew.phases[2].length
  1002. elseif kew.phase == 2 then
  1003. --playing phase ends goto end phase
  1004. processHighscores(kew)
  1005. exportKewData(kew)
  1006. cleanAfterVictory(kew.buildzone)
  1007. kew.phase = 3
  1008. displayTime(kew.buildzone.selector,0,0)
  1009. kew.timer = kew.phases[3].length
  1010. elseif kew.phase == 3 then
  1011. --end phase ends goto waiting phase
  1012. removePlayersFromKew(game,kew)
  1013. kew.phase = 1
  1014. displayTime(kew.buildzone.selector,0,0)
  1015. kew.timer = kew.phases[1].length
  1016. end
  1017. end
  1018. end
  1019. end
  1020.  
  1021.  
  1022. --Replaces everything that is needed to start the game. Does not rebuild the floor, or clear anything away.
  1023. --based on the settings it creates a full grid, or a partial grid, or no grid
  1024. --it also places the ring, although this is disabled for now
  1025. function prepareBuildzone(buildzone)
  1026. local bz = buildzone
  1027. local x,y,z,w = bz.x,bz.y,bz.z,bz.w
  1028. --commands.fill(x,y-1,z,x+w,y-1,z+w,BLOCKS.CAMP_FLOOR)
  1029. fillRing(buildzone.x,buildzone.y,buildzone.z,buildzone.w,BLOCKS.CONSTRUCTION.block)
  1030. if DO_GRID then
  1031. for x=0,6 do
  1032. for z=0,6 do
  1033. local rand = math.random()*100
  1034. --local result, text = commands.testforblock(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+4,BLOCKS.CAMP_FLOOR.block)
  1035. if rand > GRID_HOLE_CHANCE then --and result then
  1036. commands.async.setblock(bz.x+(x*9)+4,bz.y,bz.z+(z*9)+4,BLOCKS.DETECT.block,BLOCKS.DETECT.data,"replace","minecraft:air")
  1037. commands.async.fill(bz.x+(x*9)+1,bz.y-1,bz.z+(z*9)+4,bz.x+(x*9)+7,bz.y-1,bz.z+(z*9)+4,BLOCKS.PLUG.block)
  1038. commands.async.fill(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+1,bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+7,BLOCKS.PLUG.block)
  1039. --commands.async.setblock(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+4,BLOCKS.CAMP_FLOOR.block)
  1040. end
  1041. end
  1042. end
  1043. end
  1044. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,BLOCKS.CONSTRUCTION.block)
  1045. end
  1046.  
  1047. --deletes everything inside the buildzone
  1048. function cleanBuildzone(buildzone)
  1049. print("Cleaning buildzone")
  1050. for h=0,VICTORY_HEIGHT+20 do
  1051. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air")
  1052. end
  1053. end
  1054.  
  1055. --moves all players assigned to the queue into the waiting area for the provided game.
  1056. --It also changes them to Adventure mode and clears their inventory
  1057. function respawnPlayers(game,kew)
  1058. teleportToPoint(game.spawn.x,game.spawn.y,game.spawn.z,kew.playerlist,true,"TELEPORTED: Your game ended so you have been returned to the Spawn Point", "DE: TELEPORTED: Your game ended so you have been returned to the Spawn Point") --put players back in spawn area
  1059. for i,player in ipairs(kew.playerlist) do
  1060. --table.insert(game.waitlist,player)
  1061. respawnPlayer(player,"")
  1062. end
  1063. kew.playerlist = {}
  1064. end
  1065.  
  1066. function removePlayersFromKew(game,kew)
  1067. for _, player in ipairs(kew.playerlist) do
  1068. commands.tell(player.name,"Your game is over. Use your Heimkehrer to return to spawn")
  1069. end
  1070. kew.playerlist = {}
  1071. end
  1072.  
  1073. function respawnPlayer(playername,message)
  1074. commands.tell(playername,message)
  1075. commands.async.gamemode(2,playername)
  1076. commands.async.clear(playername,"minecraft:wool")
  1077. commands.async.clear(playername,"minecraft:stone_pickaxe")
  1078. end
  1079.  
  1080. function checkForPlayerInBuildzone(player,buildzone)
  1081. local result,message = commands.testfor('@a[name='..player.name..','..buildzone.selector..']')
  1082. return result
  1083. end
  1084.  
  1085. function checkForPlayerInWaitzone(player)
  1086. local selector = "x="..WAITZONE.x..",y="..WAITZONE.y..",z="..WAITZONE.z..",dx="..WAITZONE.w..",dy=256,dz="..WAITZONE.l
  1087. local result,message = commands.testfor('@a[name='..player.name..','..selector..']')
  1088. return result
  1089. end
  1090.  
  1091. function checkPlayers(game)
  1092. local selector = "x="..WAITZONE.x..",y="..WAITZONE.y..",z="..WAITZONE.z..",dx="..WAITZONE.w..",dy=256,dz="..WAITZONE.l
  1093. local loggedIn = getAllPos('m=2,'..selector)
  1094. --refresh waitlist
  1095. game.waitlist = loggedIn
  1096. --check currently playing players
  1097. for l,kew in ipairs(game.queues) do
  1098. for i,builder in ipairs(kew.playerlist) do
  1099. local isPlaying = checkForPlayerInBuildzone(builder,kew.buildzone)
  1100. --remove players who are already in kews from the waitlist
  1101. for j, player in ipairs(loggedIn) do
  1102. if player.name == builder.name then
  1103. table.remove(loggedIn,j)
  1104. end
  1105. end
  1106. --if the game is in progress and the player is not found then remove them from the gamekew
  1107. if not isPlaying and kew.phase == 2 then
  1108. --table.remove(kew.playerlist,i)
  1109. --print("Removed "..builder.name.." from game in progress")
  1110. end
  1111. end
  1112. end
  1113. end
  1114.  
  1115. --adds players who wait in the orange room to slots in waiting buildzones
  1116. function allocateWaiters(game)
  1117. --find free slots
  1118. local freeslots = {}
  1119. for i, kew in ipairs(game.queues) do
  1120. if kew.phase == 1 and #kew.playerlist < kew.maxplayers then
  1121. local slots = kew.maxplayers - #kew.playerlist
  1122. for j=1,slots do
  1123. table.insert(freeslots,kew)
  1124. end
  1125. end
  1126. end
  1127.  
  1128. --RE-ENABLE SECOND SHUFFLETABLE IF YOU WANT RANDOM PLAYER MATCHUPS
  1129. shuffleTable(game.waitlist)
  1130. --shuffleTable(freeslots)
  1131.  
  1132. while #freeslots > 0 and #game.waitlist > 0 do
  1133. local player = table.remove(game.waitlist,1)
  1134. local freeslot = table.remove(freeslots,1).playerlist
  1135. table.insert(freeslot,player)
  1136. end
  1137. end
  1138.  
  1139. --PARTICLE FUNCTIONS
  1140. --particles shown to player while their shape is being checked for match
  1141. function searchParticle(x,y,z)
  1142. commands.async.particle("fireworksSpark",x,y,z,0.01,3,0.01,0.01,100)
  1143. end
  1144. -- particles shown to player on successful vocab match
  1145. function successParticle(x,y,z)
  1146. commands.async.particle("happyVillager",x,y,z,2,2,2,1,1000)
  1147. commands.async.playsound("random.levelup","@a",x,y,z,1,1.2)
  1148. end
  1149. --- particles shown to player on failed vocab match
  1150. function failParticle(x,y,z)
  1151. commands.async.particle("reddust",x,y,z,0.1,0.1,1.5,1,200)
  1152. commands.async.particle("reddust",x,y,z,1.5,0.1,0.1,1,200)
  1153. commands.async.playsound("random.bowhit","@a",x,y,z,1,0.8)
  1154. end
  1155.  
  1156. --makes sure that incoming check requests dont exist already
  1157. function addToChecklist(player,buildzone)
  1158. for _, detector in ipairs(buildzone.waitingForCheck) do
  1159. if detector.x == player.x and detector.y == player.y and detector.z == player.z then
  1160. return false
  1161. end
  1162. end
  1163. table.insert(buildzone.waitingForCheck,player)
  1164. return true
  1165. end
  1166.  
  1167. --removes all barrier blocks from a buildzone which are used to carve space in vocabs
  1168. function cleanBarriers(buildzone)
  1169. for h=0,200 do
  1170. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air",0,"replace","minecraft:barrier")
  1171. end
  1172. end
  1173.  
  1174. --The main chunk of code which deals with stepping on detector blocks and reading the vocab
  1175. function updatePlayedZone(buildzone)
  1176. local victory = false
  1177. local buildzoneSelector = buildzone.selector
  1178. --get all players on diamond, add them to the list of things to check
  1179. local detectLocations = getAllOnBlockType(BLOCKS.DETECT.block,buildzoneSelector)
  1180. for _, player in ipairs(detectLocations) do
  1181. addToChecklist(player,buildzone)
  1182. end
  1183.  
  1184. --DEAL WITH THE DETECTOR AT THE TOP OF THE LIST IF THERE IS ONE
  1185. if #buildzone.waitingForCheck > 0 then
  1186. --DO PARTICLE EFFECTS IF A DETECTING BLOCK THAT IS DETECTING
  1187. for i,loc in ipairs(buildzone.waitingForCheck) do
  1188. searchParticle(loc.x,loc.y+1,loc.z)
  1189. end
  1190. local totalResult = false
  1191. local checked = table.remove(buildzone.waitingForCheck,1)
  1192. local x,y,z,name = checked.x,checked.y,checked.z,checked.name
  1193. for i,vocab in pairs(buildzone.vocab) do
  1194. 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")
  1195. if result then
  1196. --clone in the correct vocab
  1197. 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")
  1198. if DEBUG_MODE then
  1199. print(clonemes[1])
  1200. end
  1201. commands.async.give(name,vocab.reward)
  1202. -- announce vocab success in English
  1203. commands.async.tellraw(name,'["",{"text":"You built a '..vocab.nameEN.. ' and received more building materials!!","color":"green"}]')
  1204. -- announce vocab success in German
  1205. commands.async.tellraw(name,'["",{"text":"DE: You built a '..vocab.nameDE.. ' and received more building materials!!","color":"orange"}]')
  1206. --clear out barrier blocks
  1207. cleanBarriers(buildzone)
  1208.  
  1209. --ADD THE NEW STRUCTURE TO THE RECORDS
  1210. table.insert(buildzone.structures,vocab.nameEN) ---CHANGE HERE to make it record the place of the vocab too x, y, z and vocab id
  1211. local building = {id=vocab.id,xpos=x,ypos=y,zpos=z,name=vocab.nameEN}
  1212. table.insert(buildzone.buildings, building)
  1213. --if vocab.green then
  1214. buildzone.greenSlots = buildzone.greenSlots + vocab.greenSlots
  1215. --end
  1216. buildzone.filledSlots = buildzone.filledSlots + vocab.slots
  1217. local newHeight = y + vocab.height - buildzone.y-1 -- the Y coordinate of the highest block above the ground. Our world has its ground at 55 which is in buildzone.y, subtracting 1 to compensate for player height
  1218. if newHeight > buildzone.highest then
  1219. buildzone.highest = newHeight
  1220. end
  1221. -- count variety
  1222. print("adding variety: "..vocab.id)
  1223. if buildzone.variety[vocab.id] then
  1224. print("increasing existing item")
  1225. buildzone.variety[vocab.id] = buildzone.variety[vocab.id] + 1
  1226. else
  1227. print("adding new item")
  1228. buildzone.variety[vocab.id] = 1
  1229. end
  1230.  
  1231. --- CHECK FOR PERSONAL RECORDS
  1232. --- check if the new structure is the highest
  1233. --- CHANGE here to live detect the contribution of the new structure to the 4 goals and update them
  1234.  
  1235. local personalbest = tracker.getScore(name,"highest")
  1236. if personalbest.count < newHeight then
  1237. --commands.async.tell(name,"You just topped your personal record for highest structure!")
  1238. commands.async.scoreboard("players","add",name,"highest",1)
  1239. -- announce success in English
  1240. commands.async.tellraw(name,'["",{"text":"You just topped your personal record for highest structure!","color":"green"}]')
  1241. -- announce success in German
  1242. commands.async.tellraw(name,'["",{"text":"DE: You just topped your personal record for highest structure!","color":"orange"}]')
  1243. end
  1244.  
  1245. ---
  1246. ---
  1247. -- CHECK if placing the current structure would result in beating a server-wide record on the 4 goals
  1248. --calculate total slots - FOR GOAL "DENSEST NEIGHBOURHOOD"
  1249. local most = tracker.getScore("Densest_[percent]","highscores")
  1250. local Kint = math.floor(100 * buildzone.filledSlots / 49) -- Kint is the density index made from built area (filledSlots) over ground area (7x7 slots = 49)
  1251. if Kint > most.count then
  1252. commands.async.scoreboard("players","set","Densest_[percent]","highscores",Kint)
  1253. commands.async.say("@a","The record for the Densest Neighbourhood has been topped!")
  1254. end
  1255.  
  1256. -- FOR THE GOAL "MOST DIVERSE NEIGHBOURHOOD"
  1257. -- here we need to count how many varieties of buildings there are
  1258. --local structures = tracker.tallyTable(buildzone.structures) -- this counts the variety of buildings in a game
  1259. local mostDiverse = tracker.getScore("Most-Diverse_[out-of-40]","highscores")
  1260. print("variety count is: "..#buildzone.variety)
  1261. if #buildzone.variety > mostDiverse.count then
  1262. commands.async.scoreboard("players","set","Most-Diverse_[out-of-40]","highscores",#buildzone.variety)
  1263. commands.async.say("@a","The record for the Most Diverse Neighbourhood has been topped!")
  1264. end
  1265.  
  1266. -- FOR THE GOAL "GREENEST NEIGHBOURHOOD"
  1267. -- here we need to count the number of green vocabs
  1268. local greenest = tracker.getScore("Greenest_[percent]","highscores")
  1269. local Gint = math.floor(100*buildzone.greenSlots/49) --Gint is the green index, made from green area (greenSlots) over ground area (7x7 slots = 49)
  1270. if Gint > greenest.count then
  1271. commands.async.scoreboard("players","set","Greenest_[percent]","highscores",Gint)
  1272. -- announce success in English
  1273. commands.async.tellraw("@a",'["",{"text":"Awesome! '..name.. ' just topped the record for GREENEST NEIGHBOURHOOD!","color":"green"}]')
  1274. -- announce success in German
  1275. commands.async.tellraw("@a",'["",{"text":"DE: Awesome! '..name.. ' just topped the record for GREENEST NEIGHBOURHOOD!","color":"orange"}]')
  1276. ---commands.async.say("@a","The record for the Densest Neighbourhood has been topped!")
  1277. end
  1278.  
  1279. --calculate highest placement -- FOR THE GOAL "TALLEST NEIGHBOURHOOD"
  1280. local highest = tracker.getScore("Tallest_[meters]","highscores")
  1281. if buildzone.highest > highest.count then
  1282. commands.async.scoreboard("players","set","Tallest_[meters]","highscores",buildzone.highest)
  1283. commands.async.say("@a","The record for the Tallest Negihbourhood has been topped!")
  1284. end
  1285.  
  1286.  
  1287. --increase the "how many structures did i build" score for the building player
  1288. commands.async.scoreboard("players","add",name,"built",1)
  1289. commands.async.scoreboard("players","add",name,vocab.name,1) -- use maybe vocab ID instead of name
  1290.  
  1291. totalResult = true
  1292. break
  1293.  
  1294. end
  1295. end
  1296. if totalResult then
  1297. --yey win, do a happy time
  1298. successParticle(x,y,z)
  1299. else
  1300. --no vocab found so do a fail particle
  1301. --announce in English
  1302. commands.async.tellraw(name,'["",{"text":"Your structure is not built correctly, try a different shape.","color":"red"}]')
  1303. -- announce in German
  1304. commands.async.tellraw(name,'["",{"text":"DE: Your structure is not built correctly, try a different shape.","color":"orange"}]')
  1305. failParticle(x,y-1,z)
  1306. end
  1307. end
  1308. return victory
  1309. end
  1310.  
  1311. --Display game information on the monitor
  1312. function debugDisplay(game)
  1313. monitor.clear()
  1314. if blink then
  1315. monitor.setCursorPos(1,1)
  1316. monitor.setTextColor(colors.red)
  1317. monitor.write("Running")
  1318. monitor.setTextColor(colors.white)
  1319. redstone.setOutput("top",true)
  1320. blink = false
  1321. else
  1322. redstone.setOutput("top",false)
  1323. blink = true
  1324. end
  1325. local line = 2
  1326.  
  1327. for i,kew in ipairs(game.queues) do
  1328. monitor.setCursorPos(1,line)
  1329. local minutes = string.format("%02d",math.floor(kew.timer/60))
  1330. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  1331. monitor.write("Buildzone "..i.." | Phase: "..kew.phase.." | Time: "..minutes..":"..seconds)
  1332. monitor.setCursorPos(1,line+1)
  1333. for i,player in ipairs(kew.playerlist) do
  1334. monitor.write(player.name.." ")
  1335. end
  1336. line = line +2
  1337. end
  1338.  
  1339. monitor.setCursorPos(1,10)
  1340. for i,player in ipairs(game.waitlist) do
  1341. monitor.write(player.name.." ")
  1342. end
  1343. end
  1344.  
  1345. --BEGIN RUNTIME CODE
  1346. local blink = true
  1347. local game = setup()
  1348. while true do
  1349. update(game)
  1350. if monitor then debugDisplay(game) end
  1351. commands.async.weather("clear",10000)
  1352.  
  1353. local resetbutton = redstone.getInput("left")
  1354. if resetbutton then
  1355. print("reset!")
  1356. for i,kew in ipairs(game.queues) do
  1357. if kew.phase == 2 then
  1358. kew.timer = 5
  1359. end
  1360. end
  1361. end
  1362. sleep(2)
  1363. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement