Advertisement
antonsavov

Untitled

Mar 1st, 2017
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 52.16 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. print("debug: starting setup function.")
  720. local game = {}
  721. game.vocab = {}
  722. game.builds = {}
  723. game.queues = {}
  724. game.waitlist = {}
  725. game.spawn = SPAWN
  726. game.lastClock = os.clock()
  727. game.nowTime = os.clock()
  728.  
  729. --kill all villagers
  730. commands.exec("kill @e[type=Villager]")
  731.  
  732. --buildzone and vocabzone creation
  733. print("debug-setup: making vocab zones.")
  734. game.vocab = makeVocabZones(NUMBER_OF_VOCAB,VOCAB_WIDTH)
  735. print("debug-setup: making building zones.")
  736. game.builds = makeBuildzones(NUMBER_OF_BUILDZONES,game.vocab,BUILDZONE_WIDTH,BUILDZONE_FLOOR_HEIGHT)
  737.  
  738. for i,build in ipairs(game.builds) do
  739. table.insert(game.queues,newQueue(build,4))
  740. end
  741. print("debug-setup: testing for properly setup vocabs.")
  742. for i,vz in ipairs(game.vocab) do
  743. local x,y,z,w = vz.x,vz.y,vz.z,vz.w
  744. local cx,cy,cz = vz.cx,vz.cy,vz.cz
  745.  
  746. local detector, message1 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT.block)
  747. local blocker, message2 = commands.testforblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD.block)
  748. if not (detector or blocker) then
  749. for nx=0,2 do
  750. for nz=0,2 do
  751. commands.setblock(x+(nx*9)+4,y-1,z+(nz*9)+4,BLOCKS.VOCAB_REPLACE.block)
  752. commands.setblock(cx+(nx*9)+4,cy-1,cz+(nz*9)+4,BLOCKS.VOCAB_DETECT.block)
  753. end
  754. end
  755. commands.setblock(x+(math.floor(w/2)),y,z+(math.floor(w/2)),BLOCKS.DETECT_DEAD.block)
  756.  
  757. end
  758.  
  759. end
  760.  
  761. print("debug-setup: adding scoreboards.")
  762. for i, name in ipairs(VOCAB_NAMES) do
  763. commands.scoreboard("objectives","add",name,"dummy")
  764. end
  765.  
  766. commands.gamerule("doDaylightCycle",false)
  767. commands.gamerule("keepInventory",true)
  768. commands.gamerule("doTileDrops",false)
  769. commands.gamerule("logAdminCommands",false)
  770. commands.gamerule("commandBlockOutput",false)
  771. commands.time("set",6000)
  772. commands.scoreboard("objectives","add","highscores","dummy","Best Neighbourhoods")
  773. commands.scoreboard("objectives","add","VillagerLife","dummy")
  774. commands.scoreboard("objectives","add","built","dummy", "Structures Built")
  775. commands.scoreboard("objectives","add","highest","dummy", "Personal Highest")
  776. commands.scoreboard("objectives","add","played","dummy","Games Played")
  777.  
  778. commands.title("@a","times",0,30,30) -- what does this do?
  779.  
  780. math.randomseed( os.time() )
  781. commands.scoreboard("objectives","setdisplay","sidebar","highscores")
  782. commands.scoreboard("objectives","setdisplay","list","played")
  783. print("time is: "..os.time())
  784. if DEBUG_MODE then
  785. for i,build in ipairs(game.builds) do
  786. build.phase = 2
  787. build.timer = 500
  788. end
  789. end
  790.  
  791. print("Computer Co-ordinates ",ox,oy,oz)
  792. print("20.000 Blocks Active!")
  793.  
  794. return game
  795. end
  796.  
  797. function checkForSquids(game)
  798. --execute on all squids
  799. local squidders = getSquids()
  800. dealWithSquidders(game,squidders)
  801. end
  802.  
  803. --main game loop
  804. --runs the game object through each of these update steps in order
  805. function update(game)
  806. local elapsed = updateClock(game)
  807. --update players
  808. checkPlayers(game)
  809. doTimerUpdates(game,elapsed)
  810. doPhaseUpdates(game)
  811. doPhaseEnds(game)
  812. checkBoundaries(game)
  813. checkForSquids(game)
  814. if #game.waitlist > 0 then allocateWaiters(game) end
  815. end
  816.  
  817. --calculates elapsed time during a game tick
  818. function updateClock(game)
  819. game.nowTime = os.clock()
  820. local elapsed = game.nowTime - game.lastClock
  821. game.lastClock = game.nowTime
  822. return elapsed
  823. end
  824.  
  825. --updates all kews in the game object based on elapsed time
  826. function doTimerUpdates(game,elapsed)
  827. for i,kew in ipairs(game.queues) do
  828. kew.timer = kew.timer - elapsed
  829. end
  830. end
  831.  
  832. --check players are inside their buildzone and move them back if not
  833. function checkBoundaries(game)
  834. for i,kew in ipairs(game.queues) do
  835. if kew.phase ==2 then
  836. --boundaries
  837. local x_min = kew.buildzone.x
  838. local x_max = kew.buildzone.x+kew.buildzone.w
  839. local z_min = kew.buildzone.z
  840. local z_max = kew.buildzone.z+kew.buildzone.w
  841.  
  842. local toBeCorrected = {}
  843.  
  844. for j,player in ipairs(kew.playerlist) do
  845. local listOfOne = getAllPos('m=2,name='..player.name)
  846. if listOfOne and listOfOne[1] then
  847. player.x = listOfOne[1].x
  848. player.y = listOfOne[1].y
  849. player.z = listOfOne[1].z
  850. local changed = false
  851. if player.x > x_max then
  852. changed = true
  853. player.x = x_max-2
  854. end
  855. if player.x < x_min then
  856. changed = true
  857. player.x = x_min+2
  858. end
  859. if player.z > z_max then
  860. changed = true
  861. player.z = z_max-2
  862. end
  863. if player.z < z_min then
  864. changed = true
  865. player.z = z_min+2
  866. end
  867. 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
  868. end
  869. end
  870. end
  871. end
  872. end
  873.  
  874.  
  875. --here you can set the logic which determines if a buildzone was valuable.
  876. --Return true if it is crap and should be replaced
  877. function checkIfBuildzoneIsCrap(buildzone)
  878. if #buildzone.structures < 5 then
  879. print("Buildzone was crap")
  880. return true
  881. end
  882. print("Buildzone was ok")
  883. return false
  884. end
  885.  
  886.  
  887. --Everything that happens after a buildzone was completed
  888. --due to time limit.
  889. function cleanAfterVictory(buildzone)
  890. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,BLOCKS.VICTORY_MARKER.block)
  891. fillRing(buildzone.x,buildzone.y,buildzone.z,buildzone.w,"minecraft:air",0,"replace",BLOCKS.CONSTRUCTION.block,BLOCKS.CONSTRUCTION.data)
  892. for h=0,VICTORY_HEIGHT do
  893. 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)
  894. 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)
  895. 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)
  896. 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)
  897. end
  898. 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)
  899. 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")
  900.  
  901. local wasCrap = checkIfBuildzoneIsCrap(buildzone)
  902. if wasCrap then
  903. --mark this buildzone for replacement
  904. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,"minecraft:air")
  905. end
  906. end
  907.  
  908. --these happen every tick and require the game object
  909. --updates are performed on each Queue (kew) and within each
  910. --of those on each buildzone.
  911. function doPhaseUpdates(game)
  912. for i,kew in ipairs(game.queues) do
  913.  
  914. local minutes = string.format("%02d",math.floor(kew.timer/60))
  915. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  916. if kew.timer <= 0 then
  917. minutes = "00"
  918. seconds = "00"
  919. end
  920.  
  921. if kew.phase == 1 then
  922. --waiting phase
  923. if #kew.playerlist == kew.maxplayers and kew.timer > 5 then kew.timer = 5 end
  924. if not DEBUG_MODE and #kew.playerlist == 0 then kew.timer = kew.phases[1].length end
  925.  
  926. displayTitleToGroup(kew.playerlist,"Game starting!")
  927. displayTimeToGroup(kew.playerlist,minutes,seconds)
  928. --show countdown
  929. elseif kew.phase == 2 then
  930. --playing phase
  931. if #kew.playerlist == 0 then timer = 0 end -- finish if all players quit
  932. -- do vocab logic
  933. local victory = updatePlayedZone(kew.buildzone) --currently victory updatePlayedZone returns always false
  934. --
  935. displayTimeToGroup(kew.playerlist,minutes,seconds)
  936.  
  937. elseif kew.phase == 3 then
  938. --end phase
  939. displayTitleToGroup(kew.playerlist,"Use Heimkehrer to return")
  940. displayTimeToGroup(kew.playerlist,minutes,seconds)
  941.  
  942.  
  943. end
  944. end
  945. end
  946.  
  947. --this runs after a buildzone is completed
  948. --it should tally structure types and set scoreboard highscores
  949. --it also rewards all participants with more played score
  950. function processHighscores(kew)
  951. local buildzone = kew.buildzone
  952.  
  953.  
  954.  
  955. --add score to players who finished this game
  956. for _,player in ipairs(kew.playerlist) do
  957. commands.async.scoreboard("players","add",player.name,"played",1)
  958. end
  959. end
  960.  
  961. --function to export a buildzone detail once it is complete
  962. --requires a kew so it can access the playerlist
  963. local function exportKewData(kew)
  964. local buildzone = kew.buildzone
  965. local saved = {}
  966. saved.position =
  967. {
  968. x=buildzone.x,
  969. y=buildzone.y,
  970. z=buildzone.z
  971. }
  972. saved.completed = os.time()
  973. saved.players = {}
  974. for _, player in ipairs(kew.playerlist) do
  975. table.insert(saved.players,player.name)
  976. end
  977. saved.structures = buildzone.structures
  978. saved.totals = tracker.tallyTable(buildzone.structures)
  979. saved.highest = buildzone.highest - buildzone.y
  980.  
  981. fs.makeDir("/records")
  982. local file = fs.open("/records/"..buildzone.x.."_"..buildzone.z,"w")
  983. file.write(json.encodePretty(saved))
  984. file.close()
  985. end
  986.  
  987. --this code runs ONCE at the end of each phase
  988. --what actually happens is specific to which phase the
  989. --particular kew is in.
  990. function doPhaseEnds(game)
  991. for i,kew in ipairs(game.queues) do
  992. if kew.timer <= 0 then
  993. if kew.phase == 1 then
  994. --waiting phase ends goto play phase
  995.  
  996. moveBuildzone(kew.buildzone,game.builds)
  997. teleportToZone(kew.buildzone,kew.playerlist,"TELEPORTED: Your game has started.", "DE: TELEPORTED: Your game has started.")--teleport selected players
  998. cleanBuildzone(kew.buildzone)
  999. prepareBuildzone(kew.buildzone)--prepare build zone
  1000. giveItems(kew.playerlist,STARTING_ITEMS) --give starting items
  1001. displayTitle(kew.buildzone.selector,"Build!")
  1002. kew.victory = false
  1003. displayTime(kew.buildzone.selector,0,0)
  1004. kew.phase = 2
  1005. kew.timer = kew.phases[2].length
  1006. elseif kew.phase == 2 then
  1007. --playing phase ends goto end phase
  1008. processHighscores(kew)
  1009. exportKewData(kew)
  1010. cleanAfterVictory(kew.buildzone)
  1011. kew.phase = 3
  1012. displayTime(kew.buildzone.selector,0,0)
  1013. kew.timer = kew.phases[3].length
  1014. elseif kew.phase == 3 then
  1015. --end phase ends goto waiting phase
  1016. removePlayersFromKew(game,kew)
  1017. kew.phase = 1
  1018. displayTime(kew.buildzone.selector,0,0)
  1019. kew.timer = kew.phases[1].length
  1020. end
  1021. end
  1022. end
  1023. end
  1024.  
  1025.  
  1026. --Replaces everything that is needed to start the game. Does not rebuild the floor, or clear anything away.
  1027. --based on the settings it creates a full grid, or a partial grid, or no grid
  1028. --it also places the ring, although this is disabled for now
  1029. function prepareBuildzone(buildzone)
  1030. local bz = buildzone
  1031. local x,y,z,w = bz.x,bz.y,bz.z,bz.w
  1032. --commands.fill(x,y-1,z,x+w,y-1,z+w,BLOCKS.CAMP_FLOOR)
  1033. fillRing(buildzone.x,buildzone.y,buildzone.z,buildzone.w,BLOCKS.CONSTRUCTION.block)
  1034. if DO_GRID then
  1035. for x=0,6 do
  1036. for z=0,6 do
  1037. local rand = math.random()*100
  1038. --local result, text = commands.testforblock(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+4,BLOCKS.CAMP_FLOOR.block)
  1039. if rand > GRID_HOLE_CHANCE then --and result then
  1040. commands.async.setblock(bz.x+(x*9)+4,bz.y,bz.z+(z*9)+4,BLOCKS.DETECT.block,BLOCKS.DETECT.data,"replace","minecraft:air")
  1041. 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)
  1042. 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)
  1043. --commands.async.setblock(bz.x+(x*9)+4,bz.y-1,bz.z+(z*9)+4,BLOCKS.CAMP_FLOOR.block)
  1044. end
  1045. end
  1046. end
  1047. end
  1048. commands.async.setblock(buildzone.x,buildzone.y,buildzone.z,BLOCKS.CONSTRUCTION.block)
  1049. end
  1050.  
  1051. --deletes everything inside the buildzone
  1052. function cleanBuildzone(buildzone)
  1053. print("Cleaning buildzone")
  1054. for h=0,VICTORY_HEIGHT+20 do
  1055. commands.async.fill(buildzone.x,buildzone.y+h,buildzone.z,buildzone.x+buildzone.w,buildzone.y+h,buildzone.z+buildzone.w,"minecraft:air")
  1056. end
  1057. end
  1058.  
  1059. --moves all players assigned to the queue into the waiting area for the provided game.
  1060. --It also changes them to Adventure mode and clears their inventory
  1061. function respawnPlayers(game,kew)
  1062. 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
  1063. for i,player in ipairs(kew.playerlist) do
  1064. --table.insert(game.waitlist,player)
  1065. respawnPlayer(player,"")
  1066. end
  1067. kew.playerlist = {}
  1068. end
  1069.  
  1070. function removePlayersFromKew(game,kew)
  1071. for _, player in ipairs(kew.playerlist) do
  1072. commands.tell(player.name,"Your game is over. Use your Heimkehrer to return to spawn")
  1073. end
  1074. kew.playerlist = {}
  1075. end
  1076.  
  1077. function respawnPlayer(playername,message)
  1078. commands.tell(playername,message)
  1079. commands.async.gamemode(2,playername)
  1080. commands.async.clear(playername,"minecraft:wool")
  1081. commands.async.clear(playername,"minecraft:stone_pickaxe")
  1082. end
  1083.  
  1084. function checkForPlayerInBuildzone(player,buildzone)
  1085. local result,message = commands.testfor('@a[name='..player.name..','..buildzone.selector..']')
  1086. return result
  1087. end
  1088.  
  1089. function checkForPlayerInWaitzone(player)
  1090. local selector = "x="..WAITZONE.x..",y="..WAITZONE.y..",z="..WAITZONE.z..",dx="..WAITZONE.w..",dy=256,dz="..WAITZONE.l
  1091. local result,message = commands.testfor('@a[name='..player.name..','..selector..']')
  1092. return result
  1093. end
  1094.  
  1095. function checkPlayers(game)
  1096. local selector = "x="..WAITZONE.x..",y="..WAITZONE.y..",z="..WAITZONE.z..",dx="..WAITZONE.w..",dy=256,dz="..WAITZONE.l
  1097. local loggedIn = getAllPos('m=2,'..selector)
  1098. --refresh waitlist
  1099. game.waitlist = loggedIn
  1100. --check currently playing players
  1101. for l,kew in ipairs(game.queues) do
  1102. for i,builder in ipairs(kew.playerlist) do
  1103. local isPlaying = checkForPlayerInBuildzone(builder,kew.buildzone)
  1104. --remove players who are already in kews from the waitlist
  1105. for j, player in ipairs(loggedIn) do
  1106. if player.name == builder.name then
  1107. table.remove(loggedIn,j)
  1108. end
  1109. end
  1110. --if the game is in progress and the player is not found then remove them from the gamekew
  1111. if not isPlaying and kew.phase == 2 then
  1112. --table.remove(kew.playerlist,i)
  1113. --print("Removed "..builder.name.." from game in progress")
  1114. end
  1115. end
  1116. end
  1117. end
  1118.  
  1119. --adds players who wait in the orange room to slots in waiting buildzones
  1120. function allocateWaiters(game)
  1121. --find free slots
  1122. local freeslots = {}
  1123. for i, kew in ipairs(game.queues) do
  1124. if kew.phase == 1 and #kew.playerlist < kew.maxplayers then
  1125. local slots = kew.maxplayers - #kew.playerlist
  1126. for j=1,slots do
  1127. table.insert(freeslots,kew)
  1128. end
  1129. end
  1130. end
  1131.  
  1132. --RE-ENABLE SECOND SHUFFLETABLE IF YOU WANT RANDOM PLAYER MATCHUPS
  1133. shuffleTable(game.waitlist)
  1134. --shuffleTable(freeslots)
  1135.  
  1136. while #freeslots > 0 and #game.waitlist > 0 do
  1137. local player = table.remove(game.waitlist,1)
  1138. local freeslot = table.remove(freeslots,1).playerlist
  1139. table.insert(freeslot,player)
  1140. end
  1141. end
  1142.  
  1143. --PARTICLE FUNCTIONS
  1144. --particles shown to player while their shape is being checked for match
  1145. function searchParticle(x,y,z)
  1146. commands.async.particle("fireworksSpark",x,y,z,0.01,3,0.01,0.01,100)
  1147. end
  1148. -- particles shown to player on successful vocab match
  1149. function successParticle(x,y,z)
  1150. commands.async.particle("happyVillager",x,y,z,2,2,2,1,1000)
  1151. commands.async.playsound("random.levelup","@a",x,y,z,1,1.2)
  1152. end
  1153. --- particles shown to player on failed vocab match
  1154. function failParticle(x,y,z)
  1155. commands.async.particle("reddust",x,y,z,0.1,0.1,1.5,1,200)
  1156. commands.async.particle("reddust",x,y,z,1.5,0.1,0.1,1,200)
  1157. commands.async.playsound("random.bowhit","@a",x,y,z,1,0.8)
  1158. end
  1159.  
  1160. --makes sure that incoming check requests dont exist already
  1161. function addToChecklist(player,buildzone)
  1162. for _, detector in ipairs(buildzone.waitingForCheck) do
  1163. if detector.x == player.x and detector.y == player.y and detector.z == player.z then
  1164. return false
  1165. end
  1166. end
  1167. table.insert(buildzone.waitingForCheck,player)
  1168. return true
  1169. end
  1170.  
  1171. --removes all barrier blocks from a buildzone which are used to carve space in vocabs
  1172. function cleanBarriers(buildzone)
  1173. for h=0,200 do
  1174. 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")
  1175. end
  1176. end
  1177.  
  1178. --The main chunk of code which deals with stepping on detector blocks and reading the vocab
  1179. function updatePlayedZone(buildzone)
  1180. local victory = false
  1181. local buildzoneSelector = buildzone.selector
  1182. --get all players on diamond, add them to the list of things to check
  1183. local detectLocations = getAllOnBlockType(BLOCKS.DETECT.block,buildzoneSelector)
  1184. for _, player in ipairs(detectLocations) do
  1185. addToChecklist(player,buildzone)
  1186. end
  1187.  
  1188. --DEAL WITH THE DETECTOR AT THE TOP OF THE LIST IF THERE IS ONE
  1189. if #buildzone.waitingForCheck > 0 then
  1190. --DO PARTICLE EFFECTS IF A DETECTING BLOCK THAT IS DETECTING
  1191. for i,loc in ipairs(buildzone.waitingForCheck) do
  1192. searchParticle(loc.x,loc.y+1,loc.z)
  1193. end
  1194. local totalResult = false
  1195. local checked = table.remove(buildzone.waitingForCheck,1)
  1196. local x,y,z,name = checked.x,checked.y,checked.z,checked.name
  1197. for i,vocab in pairs(buildzone.vocab) do
  1198. 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")
  1199. if result then
  1200. --clone in the correct vocab
  1201. 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")
  1202. if DEBUG_MODE then
  1203. print(clonemes[1])
  1204. end
  1205. commands.async.give(name,vocab.reward)
  1206. -- announce vocab success in English
  1207. commands.async.tellraw(name,'["",{"text":"You built a '..vocab.nameEN.. ' and received more building materials!!","color":"green"}]')
  1208. -- announce vocab success in German
  1209. commands.async.tellraw(name,'["",{"text":"DE: You built a '..vocab.nameDE.. ' and received more building materials!!","color":"orange"}]')
  1210. --clear out barrier blocks
  1211. cleanBarriers(buildzone)
  1212.  
  1213. --ADD THE NEW STRUCTURE TO THE RECORDS
  1214. table.insert(buildzone.structures,vocab.nameEN) ---CHANGE HERE to make it record the place of the vocab too x, y, z and vocab id
  1215. local building = {id=vocab.id,xpos=x,ypos=y,zpos=z,name=vocab.nameEN}
  1216. table.insert(buildzone.buildings, building)
  1217. --if vocab.green then
  1218. buildzone.greenSlots = buildzone.greenSlots + vocab.greenSlots
  1219. --end
  1220. buildzone.filledSlots = buildzone.filledSlots + vocab.slots
  1221. 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
  1222. if newHeight > buildzone.highest then
  1223. buildzone.highest = newHeight
  1224. end
  1225. -- count variety
  1226. print("adding variety: "..vocab.id)
  1227. if buildzone.variety[vocab.id] then
  1228. print("increasing existing item")
  1229. buildzone.variety[vocab.id] = buildzone.variety[vocab.id] + 1
  1230. else
  1231. print("adding new item")
  1232. buildzone.variety[vocab.id] = 1
  1233. end
  1234.  
  1235. --- CHECK FOR PERSONAL RECORDS
  1236. --- check if the new structure is the highest
  1237. --- CHANGE here to live detect the contribution of the new structure to the 4 goals and update them
  1238.  
  1239. local personalbest = tracker.getScore(name,"highest")
  1240. if personalbest.count < newHeight then
  1241. --commands.async.tell(name,"You just topped your personal record for highest structure!")
  1242. commands.async.scoreboard("players","add",name,"highest",1)
  1243. -- announce success in English
  1244. commands.async.tellraw(name,'["",{"text":"You just topped your personal record for highest structure!","color":"green"}]')
  1245. -- announce success in German
  1246. commands.async.tellraw(name,'["",{"text":"DE: You just topped your personal record for highest structure!","color":"orange"}]')
  1247. end
  1248.  
  1249. ---
  1250. ---
  1251. -- CHECK if placing the current structure would result in beating a server-wide record on the 4 goals
  1252. --calculate total slots - FOR GOAL "DENSEST NEIGHBOURHOOD"
  1253. local most = tracker.getScore("Densest_[percent]","highscores")
  1254. local Kint = math.floor(100 * buildzone.filledSlots / 49) -- Kint is the density index made from built area (filledSlots) over ground area (7x7 slots = 49)
  1255. if Kint > most.count then
  1256. commands.async.scoreboard("players","set","Densest_[percent]","highscores",Kint)
  1257. commands.async.say("@a","The record for the Densest Neighbourhood has been topped!")
  1258. end
  1259.  
  1260. -- FOR THE GOAL "MOST DIVERSE NEIGHBOURHOOD"
  1261. -- here we need to count how many varieties of buildings there are
  1262. --local structures = tracker.tallyTable(buildzone.structures) -- this counts the variety of buildings in a game
  1263. local mostDiverse = tracker.getScore("Most-Diverse_[out-of-40]","highscores")
  1264. print("variety count is: "..#buildzone.variety)
  1265. if #buildzone.variety > mostDiverse.count then
  1266. commands.async.scoreboard("players","set","Most-Diverse_[out-of-40]","highscores",#buildzone.variety)
  1267. commands.async.say("@a","The record for the Most Diverse Neighbourhood has been topped!")
  1268. end
  1269.  
  1270. -- FOR THE GOAL "GREENEST NEIGHBOURHOOD"
  1271. -- here we need to count the number of green vocabs
  1272. local greenest = tracker.getScore("Greenest_[percent]","highscores")
  1273. local Gint = math.floor(100*buildzone.greenSlots/49) --Gint is the green index, made from green area (greenSlots) over ground area (7x7 slots = 49)
  1274. if Gint > greenest.count then
  1275. commands.async.scoreboard("players","set","Greenest_[percent]","highscores",Gint)
  1276. -- announce success in English
  1277. commands.async.tellraw("@a",'["",{"text":"Awesome! '..name.. ' just topped the record for GREENEST NEIGHBOURHOOD!","color":"green"}]')
  1278. -- announce success in German
  1279. commands.async.tellraw("@a",'["",{"text":"DE: Awesome! '..name.. ' just topped the record for GREENEST NEIGHBOURHOOD!","color":"orange"}]')
  1280. ---commands.async.say("@a","The record for the Densest Neighbourhood has been topped!")
  1281. end
  1282.  
  1283. --calculate highest placement -- FOR THE GOAL "TALLEST NEIGHBOURHOOD"
  1284. local highest = tracker.getScore("Tallest_[meters]","highscores")
  1285. if buildzone.highest > highest.count then
  1286. commands.async.scoreboard("players","set","Tallest_[meters]","highscores",buildzone.highest)
  1287. commands.async.say("@a","The record for the Tallest Negihbourhood has been topped!")
  1288. end
  1289.  
  1290.  
  1291. --increase the "how many structures did i build" score for the building player
  1292. commands.async.scoreboard("players","add",name,"built",1)
  1293. commands.async.scoreboard("players","add",name,vocab.name,1) -- use maybe vocab ID instead of name
  1294.  
  1295. totalResult = true
  1296. break
  1297.  
  1298. end
  1299. end
  1300. if totalResult then
  1301. --yey win, do a happy time
  1302. successParticle(x,y,z)
  1303. else
  1304. --no vocab found so do a fail particle
  1305. --announce in English
  1306. commands.async.tellraw(name,'["",{"text":"Your structure is not built correctly, try a different shape.","color":"red"}]')
  1307. -- announce in German
  1308. commands.async.tellraw(name,'["",{"text":"DE: Your structure is not built correctly, try a different shape.","color":"orange"}]')
  1309. failParticle(x,y-1,z)
  1310. end
  1311. end
  1312. return victory
  1313. end
  1314.  
  1315. --Display game information on the monitor
  1316. function debugDisplay(game)
  1317. monitor.clear()
  1318. if blink then
  1319. monitor.setCursorPos(1,1)
  1320. monitor.setTextColor(colors.red)
  1321. monitor.write("Running")
  1322. monitor.setTextColor(colors.white)
  1323. redstone.setOutput("top",true)
  1324. blink = false
  1325. else
  1326. redstone.setOutput("top",false)
  1327. blink = true
  1328. end
  1329. local line = 2
  1330.  
  1331. for i,kew in ipairs(game.queues) do
  1332. monitor.setCursorPos(1,line)
  1333. local minutes = string.format("%02d",math.floor(kew.timer/60))
  1334. local seconds = string.format("%02d",math.floor(kew.timer - (minutes*60)))
  1335. monitor.write("Buildzone "..i.." | Phase: "..kew.phase.." | Time: "..minutes..":"..seconds)
  1336. monitor.setCursorPos(1,line+1)
  1337. for i,player in ipairs(kew.playerlist) do
  1338. monitor.write(player.name.." ")
  1339. end
  1340. line = line +2
  1341. end
  1342.  
  1343. monitor.setCursorPos(1,10)
  1344. for i,player in ipairs(game.waitlist) do
  1345. monitor.write(player.name.." ")
  1346. end
  1347. end
  1348.  
  1349. --BEGIN RUNTIME CODE
  1350. local blink = true
  1351. local game = setup()
  1352. while true do
  1353. update(game)
  1354. if monitor then debugDisplay(game) end
  1355. commands.async.weather("clear",10000)
  1356.  
  1357. local resetbutton = redstone.getInput("left")
  1358. if resetbutton then
  1359. print("reset!")
  1360. for i,kew in ipairs(game.queues) do
  1361. if kew.phase == 2 then
  1362. kew.timer = 5
  1363. end
  1364. end
  1365. end
  1366. sleep(2)
  1367. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement