Advertisement
antonsavov

Untitled

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