Advertisement
Guest User

chess tournament board 24/7 10p. AntiGrief

a guest
Jul 25th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.50 KB | None | 0 0
  1. -- chess tournament board 24/7 for up to 10 players. AntiGrief protection
  2.  
  3. boards = {'d6d31d','29986b','350b4c','ce9240','46214c'} -- 5 big chess boards !!! keep rotation: 1,3,5 black-white; 2,4 white-black
  4. c_boards = {'a57fa0','3d9229','5bb8d4','904854','643c97'} -- 5 control boards
  5. clocks = {{'f6aacc','288fdc'},{'efc50a','700748'},{'e20ad0','e8f20b'},{'46f159','9fd6e1'},{'e95d30','788f0c'}} -- 2 clocks per board
  6.  
  7. zones = {'cf272b','0bad24','3e111d','2219c3','e2619c'}
  8. play_zones = {'474235','4623c7','ebca8f','c8f47f','461759'}
  9. colors = {'Teal','Green', 'Orange','Red', 'Brown','Yellow', 'Blue','Purple', 'White','Pink'} -- 2 players per board
  10.  
  11. turn_markers = {'d96edd','e9262a','94abbc','e738ff','c14ed3'}
  12. basic_timer = 300
  13.  
  14. chess_pos = {{},{},{},{},{}}
  15. chess_guids = {{},{},{},{},{}}
  16.  
  17. -- ================================================================
  18.  
  19. function onload() -- spawn select timer buttons, disable boards and clocks
  20.   for i=1,#boards,1 do -- 5
  21.     getObjectFromGUID(boards[i]).interactable = false
  22.     getObjectFromGUID(c_boards[i]).interactable = false
  23.     getObjectFromGUID(clocks[i][1]).interactable = false
  24.     getObjectFromGUID(clocks[i][2]).interactable = false
  25.     getObjectFromGUID(turn_markers[i]).interactable = false
  26.  
  27.     local z = getObjectFromGUID(zones[i]).getObjects() -- save chess positions in case of reset
  28.     for j, k in ipairs(z) do
  29.       chess_pos[i][j] = k.getPosition()
  30.       chess_guids[i][j] = k.getGUID()
  31.       set_script(chess_guids[i][j],i) -- make players not able to interact with chess on different board
  32.       k.lock()
  33.       k.interactable = false
  34.     end
  35.  
  36.     spawn_pre_buttons(i) -- spawn select timer: 5,10,15.. mins. i= board
  37.  
  38.   end
  39. end
  40.  
  41.  
  42. function set_script(x,n) -- chess protection
  43.   local a = colors[n*2-1]
  44.   local b = colors[n*2]
  45.   local temp = 'pos = {}    \nvel = {}    \nang = {}'
  46.   temp = temp .. '\nfunction onPickedUp(player_color)    \nif player_color ~= \''..a..'\' and player_color ~= \''..b..'\' then      \npos = self.getPosition()    \nrot = self.getRotation()    \n\nself.use_snap_points = false    \nend   \nend \n'
  47.   temp = temp .. '\nfunction onDropped(player_color)   \nif player_color ~= \''..a..'\' and player_color ~= \''..b..'\' then    \nself.setPosition(pos)   \nself.setRotation({0,0,0})   \nang = self.getAngularVelocity()   \nvel = self.getVelocity()    \nself.addTorque({-ang[\'x\'],-ang[\'y\'],-ang[\'z\']}, 4)   \nself.addForce({-vel[\'x\'],-vel[\'y\'],-vel[\'z\']}, 4)    \nend   \nself.use_snap_points = true   \nend'
  48.  
  49.   getObjectFromGUID(x).setLuaScript(temp)
  50.   --getObjectFromGUID(x).setLuaScript('') -- debug
  51. end
  52.  
  53. -- ================================================================
  54.  
  55. -- select time for board related clocks
  56. function spawn_pre_buttons(x) -- x = board (1-5)
  57.   set_timer(x,basic_timer) -- in case of restart
  58.   getObjectFromGUID(c_boards[x]).clearButtons()
  59.  
  60.   local buttonParam = {['click_function'] = 'none', ['label'] = 'Set clock timer', ['function_owner'] = self, ['position'] = {0, 0.1, -1}, ['rotation'] =  {0, 0, 0}, ['width'] = 1200, ['height'] = 200, ['font_size'] = 150}
  61.   getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  62.  
  63.   for i=1,6,1 do -- spawn select timer buttons on board x
  64.     local buttonParam = {['click_function'] = 'timer'..i, ['label'] = (5*i)..'m', ['function_owner'] = self, ['position'] = {0.6*((-1)^i), 0.1, -0.4+0.5*math.floor((i-1)/2)}, ['rotation'] =  {0, 0, 0}, ['width'] = 500, ['height'] = 200, ['font_size'] = 150}
  65.     getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  66.   end
  67.   local buttonParam = {['click_function'] = 'timer7', ['label'] = '60m!', ['function_owner'] = self, ['position'] = {0, 0.1, -0.4+0.5*3}, ['rotation'] =  {0, 0, 0}, ['width'] = 500, ['height'] = 200, ['font_size'] = 150}
  68.   getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  69. end
  70.  
  71.  
  72.  
  73. function timer1(obj, player) -- c_board and player color
  74.   local t = 300
  75.   timer_supp(obj.getGUID(), player, t)
  76. end
  77.  
  78. function timer2(obj, player)
  79.   local t = 600
  80.   timer_supp(obj.getGUID(), player, t)
  81. end
  82.  
  83. function timer3(obj, player)
  84.   local t = 900
  85.   timer_supp(obj.getGUID(), player, t)
  86. end
  87.  
  88. function timer4(obj, player)
  89.   local t = 1200
  90.   timer_supp(obj.getGUID(), player, t)
  91. end
  92.  
  93. function timer5(obj, player)
  94.   local t = 1500
  95.   timer_supp(obj.getGUID(), player, t)
  96. end
  97.  
  98. function timer6(obj, player)
  99.   local t = 1800
  100.   timer_supp(obj.getGUID(), player, t)
  101. end
  102.  
  103. function timer7(obj, player)
  104.   local t = 3600
  105.   timer_supp(obj.getGUID(), player, t)
  106. end
  107.  
  108. -- allows to keep 7 timer functions instead of 35 (7 x5 boards)
  109. function timer_supp(obj_guid, player, t) -- c_board_guid color timer_time
  110.   local succ, temp_n = protection(obj_guid, player)
  111.   if succ == 0 then
  112.     return
  113.   end
  114.   spawn_ready(temp_n)
  115.   set_timer(temp_n, t)
  116.   spawn_buttons(temp_n)
  117.   spawn_reset_buttons(temp_n,0)
  118. end
  119.  
  120. function set_timer(x,y)
  121.   getObjectFromGUID(clocks[x][1]).Clock.setValue(y)
  122.   getObjectFromGUID(clocks[x][2]).Clock.setValue(y)
  123. end
  124.  
  125. -- ================================================================
  126.  
  127. function protection(obj_guid,player)
  128.   for q,w in ipairs(colors) do -- 10
  129.     if player == w then -- protection
  130.       local temp_n = math.ceil(q/2) -- (1-5)
  131.       if c_boards[temp_n] == obj_guid then
  132.         return 1,temp_n,q -- protection passed
  133.       end
  134.       break
  135.     end
  136.   end
  137.   printToColor('You\'r not allowed to access that board', player, {1,1,1})
  138.   return 0,0,0 -- 0 protection restriction
  139. end
  140.  
  141. -- ================================================================
  142.  
  143. function spawn_ready(x)
  144.   local buttonParam = {['click_function'] = 'Ready', ['label'] = 'Ready?', ['function_owner'] = self, ['position'] = {0, 0.3, 6}, ['rotation'] =  {0, 90, 0}, ['width'] = 1500, ['height'] = 300, ['font_size'] = 150}
  145.   getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  146. end
  147.  
  148.  
  149. function Ready(obj,player)
  150.   local succ, temp_n = protection(obj.getGUID(), player)
  151.   if succ == 0 then
  152.     return
  153.   end
  154.   spawn_buttons(temp_n)
  155.   spawn_reset_buttons(temp_n,0)
  156.   spawn_pass_buttons(temp_n,1)  -- White always goes first
  157.   getObjectFromGUID(clocks[temp_n][1]).Clock.pauseStart()
  158.  
  159.   for i=1, #chess_guids[temp_n], 1 do
  160.     if chess_guids[temp_n][i] ~= boards[temp_n] then
  161.       getObjectFromGUID(chess_guids[temp_n][i]).unlock()
  162.       getObjectFromGUID(chess_guids[temp_n][i]).interactable = true
  163.     end
  164.   end
  165. end
  166.  
  167. -- ================================================================
  168.  
  169. function spawn_buttons(x) -- x 1-5
  170.   getObjectFromGUID(c_boards[x]).clearButtons()
  171.  
  172.   local buttonParam = {['click_function'] = 'none', ['label'] = 'Reset board?', ['function_owner'] = self, ['position'] = {0, 0.1, -1}, ['rotation'] =  {0, 0, 0}, ['width'] = 1200, ['height'] = 200, ['font_size'] = 150}
  173.   getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  174. end
  175.  
  176. function spawn_pass_buttons(x)
  177.   local q = 1
  178.   if getObjectFromGUID(clocks[x][2]).Clock.paused == false then
  179.     q = 2
  180.   end
  181.  
  182.   local buttonParam = {['click_function'] = 'Pass', ['label'] = 'Pass your Turn', ['function_owner'] = self, ['position'] = {4*((-1)^q), 0.3, 6}, ['rotation'] =  {0, -90*((-1)^q), 0}, ['width'] = 1500, ['height'] = 300, ['font_size'] = 150}
  183.   getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  184. end
  185.  
  186.  
  187. function spawn_reset_buttons(x,w)
  188.   if w == 0 then
  189.     local buttonParam = {['click_function'] = 'Reset1', ['label'] = 'ask to\nRestart', ['function_owner'] = self, ['position'] = {-0.8, 0.1, 0.2}, ['rotation'] =  {0, 90, 0}, ['width'] = 800, ['height'] = 300, ['font_size'] = 120}
  190.     getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  191.     local buttonParam = {['click_function'] = 'Reset1', ['label'] = 'ask to\nRestart', ['function_owner'] = self, ['position'] = {0.8, 0.1, 0.2}, ['rotation'] =  {0, -90, 0}, ['width'] = 800, ['height'] = 300, ['font_size'] = 120}
  192.     getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  193.   elseif w == 1 then
  194.     local buttonParam = {['click_function'] = 'Reset2', ['label'] = 'do you agree\nto Restart?', ['function_owner'] = self, ['position'] = {-0.8, 0.1, 0.2}, ['rotation'] =  {0, 90, 0}, ['width'] = 800, ['height'] = 300, ['font_size'] = 120}
  195.     getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  196.   else
  197.     local buttonParam = {['click_function'] = 'Reset2', ['label'] = 'do you agree\nto Restart?', ['function_owner'] = self, ['position'] = {0.8, 0.1, 0.2}, ['rotation'] =  {0, -90, 0}, ['width'] = 800, ['height'] = 300, ['font_size'] = 120}
  198.     getObjectFromGUID(c_boards[x]).createButton(buttonParam)
  199.   end
  200. end
  201.  
  202. -- ================================================================
  203.  
  204. function Pass(obj,player)
  205.   local succ, temp_n, q = protection(obj.getGUID(), player)
  206.   if succ == 0 then
  207.     return
  208.   end
  209.   getObjectFromGUID(clocks[temp_n][1]).Clock.pauseStart()
  210.   getObjectFromGUID(clocks[temp_n][2]).Clock.pauseStart()
  211.   spawn_buttons(temp_n)
  212.   spawn_pass_buttons(temp_n)
  213.   spawn_reset_buttons(temp_n,0)
  214.  
  215.   flip_turn_marker(1,temp_n)
  216. end
  217.  
  218.  
  219. function flip_turn_marker(s,temp_n)
  220.   local tempy = {}
  221.   if s == 1 then
  222.     tempy = getObjectFromGUID(turn_markers[temp_n]).getRotation()
  223.     tempy['z'] = math.abs(tempy['z']) - 180
  224.     getObjectFromGUID(turn_markers[temp_n]).setRotation(tempy)
  225.   else
  226.     tempy = getObjectFromGUID(turn_markers[temp_n]).getRotation()
  227.     tempy['z'] = 0
  228.     getObjectFromGUID(turn_markers[temp_n]).setRotation(tempy)
  229.   end
  230. end
  231.  
  232. -- ================================================================
  233.  
  234. function Reset1(obj,player)
  235.   local succ, temp_n,q = protection(obj.getGUID(), player)
  236.   if succ == 0 then
  237.     return
  238.   end
  239.  
  240.   if Player[colors[q-1+2*(q%2)]].seated == true then
  241.     spawn_buttons(temp_n)
  242.     if getObjectFromGUID(clocks[temp_n][2]).Clock.paused == true then -- in case game did'nt start
  243.       spawn_pass_buttons(temp_n,1)
  244.     else
  245.       spawn_pass_buttons(temp_n,2)
  246.     end
  247.     spawn_reset_buttons(temp_n,q%2+1)
  248.   else
  249.     getObjectFromGUID(c_boards[temp_n]).clearButtons()
  250.     spawn_pre_buttons(temp_n)
  251.     reset_chess(temp_n)
  252.   end
  253. end
  254.  
  255.  
  256. function Reset2(obj,player)
  257.   local succ, temp_n,q = protection(obj.getGUID(), player)
  258.   if succ == 0 then
  259.     return
  260.   end
  261.   getObjectFromGUID(c_boards[temp_n]).clearButtons()
  262.   spawn_pre_buttons(temp_n)
  263.   reset_chess(temp_n)
  264. end
  265.  
  266.  
  267. function reset_chess(temp_n)
  268.   for i=1, #chess_guids[temp_n], 1 do
  269.     local z = getObjectFromGUID(chess_guids[temp_n][i])
  270.     z.setPosition(chess_pos[temp_n][i])
  271.     z.setRotation({0,0,0})
  272.     z.lock()
  273.     z.interactable = false
  274.   end
  275.   flip_turn_marker(0,temp_n)
  276. end
  277.  
  278.  
  279. -- ================================================================
  280.  
  281. -- special event tracker
  282. function onObjectLeaveScriptingZone(zone, leave_obj)
  283.   for i=1, #play_zones, 1 do
  284.     if play_zones[i] == zone.getGUID() then
  285.       for j=1,#chess_guids[i],1 do
  286.         if chess_guids[i][j] == leave_obj.getGUID() then
  287.  
  288.           local offset = 15.8 -- to get rid of linking coords
  289.           if leave_obj.getDescription() == 'White' then
  290.             leave_obj.setPosition({chess_pos[i][j]['x'] +offset*((-1)^i), chess_pos[i][j]['y'], chess_pos[i][j]['z']})
  291.           else
  292.             leave_obj.setPosition({chess_pos[i][j]['x'] -offset*((-1)^i), chess_pos[i][j]['y'], chess_pos[i][j]['z']})
  293.           end
  294.           leave_obj.setRotation({0,0,0})
  295.           local ang = leave_obj.getAngularVelocity()
  296.           local vel = leave_obj.getVelocity()
  297.           leave_obj.addTorque({-ang['x'],-ang['y'],-ang['z']}, 4)
  298.           leave_obj.addForce({-vel['x'],-vel['y'],-vel['z']}, 4)
  299.  
  300.           break
  301.         end
  302.       end
  303.       break
  304.     end
  305.   end
  306. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement