Advertisement
antonsavov

Untitled

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