Advertisement
MaX33333

Untitled

Feb 17th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.47 KB | None | 0 0
  1.  -- Factorio Tournament 2020 scenario script
  2.  -- Contact: https://discord.gg/SUJRG47/
  3.  -- Web: https://games.projects74.ru/
  4.  
  5. local tournament_started = false;
  6. local round_generated = false;
  7. local first_player_joined = false;
  8.  
  9. local teams = {};
  10. local teams_count = 0;
  11.  
  12. local function prepare_lobby_surface()
  13.     local tiles = {};
  14.     local y=-32;
  15.     while y < 32 do
  16.         local x=-32;
  17.         while x < 32 do
  18.             table.insert(tiles, {name="tutorial-grid", position={y, x}});
  19.             x = x+1;
  20.         end
  21.         y = y+1;
  22.     end
  23.     if game.get_surface("lobby") == nil then
  24.         lobby_surface = game.create_surface("lobby", {width=64, height=64})
  25.         lobby_surface.set_chunk_generated_status({-1,-1}, defines.chunk_generated_status.entities);
  26.         lobby_surface.set_chunk_generated_status({0,-1}, defines.chunk_generated_status.entities);
  27.         lobby_surface.set_chunk_generated_status({-1,0}, defines.chunk_generated_status.entities);
  28.         lobby_surface.set_chunk_generated_status({0,0}, defines.chunk_generated_status.entities);
  29.     end
  30.     game.get_surface("lobby").set_tiles(tiles, true);
  31. end
  32.  
  33. function mdump(o)
  34.     if type(o) == 'table' then
  35.         local s = '{ '
  36.         for k,v in pairs(o) do
  37.             if type(k) ~= 'number' then k = '"'..k..'"' end
  38.             s = s .. '['..k..'] = ' .. mdump(v) .. ','
  39.         end
  40.         return s .. '} '
  41.     else
  42.         return tostring(o)
  43.     end
  44. end
  45.  
  46. local function add_admin_window_elements(frame)
  47.     print("Adding admin window...");
  48.     frame.add{type="button", name="TAdminWindowRebuildLobby", caption="Rebuild lobby"};
  49.    
  50.     frame.add{type="text-box", name="TAdminWindowFieldRoundData"};
  51.     frame.add{type="button", name="TAdminWindowApplyData", caption="Apply data"};
  52.     frame.add{type="button", name="TAdminWindowApplyDefaultData", caption="Apply default data"};
  53.     frame.add{type="button", name="TAdminWindowStartRound", caption="Start"};
  54.    
  55.     frame.add{type="button", name="TAdminWindowUpdateSwitcher", caption="Update switcher"};
  56.    
  57.     local pf = frame.add{type="frame", name="TAdminPrintFrame", direction="horizontal"};
  58.     pf.add{type="button", name="TAdminWindowPrintSurfaces", caption="Print surfaces"};
  59.     pf.add{type="button", name="TAdminWindowPrintForces", caption="Print forces"};
  60.    
  61.     frame.add{type="button", name="TAdminWindowReload", caption="Reload"};
  62.     frame.add{type="button", name="TAdminWindowClose", caption="Close"};
  63. end
  64. local function add_admin_gui(index)
  65.     if not game.players[index].gui.top["TAdmin"] then
  66.         game.players[index].gui.top.add{type="sprite-button", name="TAdmin", sprite="item/power-armor-mk2"};
  67.     end
  68.     if not game.players[index].gui.center["TAdminWindow"] then
  69.         local frame = game.players[index].gui.center.add{type="frame",
  70.                                                          name="TAdminWindow",
  71.                                                          caption="Admin window",
  72.                                                          visible=false,
  73.                                                          direction="vertical"};
  74.         add_admin_window_elements(frame);
  75.     end
  76.    
  77. end
  78. local function add_surface_switcher(index)
  79.     if not game.players[index].gui.top["TView"] then
  80.         game.players[index].gui.top.add{type="sprite-button", name="TView", sprite="utility/surface_editor_icon"};
  81.         local frame = game.players[index].gui.top.add{type="frame",
  82.                                                      name="TViewFrame",
  83.                                                      visible=false,
  84.                                                      direction="horizontal"};
  85.         frame.add{type="slider",
  86.               name="TViewSlider",
  87.               caption="Surfaces",
  88.               minimum_value=0,
  89.               maximum_value=1,
  90.               value=0,
  91.               value_step=1,
  92.               discrete_slider=true,
  93.               discrete_values=true,
  94.               style="notched_slider"};
  95.         frame.add{type="label",
  96.               name="TViewLabel",
  97.               caption="lobby"};
  98.     end
  99. end
  100. local function update_surface_switcher(index)
  101.     if game.players[index].admin == false then
  102.         return;
  103.     end
  104.     if not game.players[index].gui.top["TView"] then
  105.         add_surface_switcher(index);
  106.     end
  107.     if round_generated == false then
  108.         game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_minimum_maximum(0, 1);
  109.     else
  110.         game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_minimum_maximum(0, teams_count);
  111.         game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_value_step(1);
  112.     end
  113. end
  114.  
  115. local function handle_round_start()
  116.     seed = game.surfaces["nauvis"].map_gen_settings.seed;
  117.     local surface_counter = 1;
  118.     for n, t in pairs(teams) do
  119.         print("Working on team " .. n);
  120.        
  121.         print("Creating surface...");
  122.         local created_surface = game.create_surface("T"..surface_counter, game.surfaces["nauvis"].map_gen_settings);
  123.         print("Generating terrain for team "..surface_counter.."...");
  124.        
  125.         created_surface.request_to_generate_chunks({0, 0}, 2);
  126.         created_surface.force_generate_chunk_requests();
  127.        
  128.         print("Creating force...");
  129.         local created_force = game.create_force("T"..surface_counter);
  130.         created_force.reset();
  131.        
  132.         print("Setting players...");
  133.         for np, p in pairs(t.players) do
  134.             print("Processing player " .. p .. " of index " .. np);
  135.             if not game.players[p] then
  136.                 print("Warning! Player " .. p .. " not found on server!");
  137.             else
  138.                 game.players[p].force = created_force;
  139.                 game.players[p].teleport({x=0, y=0}, created_surface);
  140.             end
  141.         end
  142.         teams_count = teams_count + 1;
  143.         surface_counter = surface_counter + 1;
  144.     end
  145.     round_generated = true;
  146.     for _, p in pairs(game.players) do update_surface_switcher(p.index); end
  147. end
  148.  
  149.  
  150.  
  151. local function handle_create_gui(index)
  152.     print("Creating gui for " .. game.players[index].name .. " (" .. index .. ")");
  153.     if(game.players[index].admin == true) then
  154.         add_admin_gui(index);
  155.         add_surface_switcher(index);
  156.     end
  157. end
  158. local function handle_join_gui(index)
  159.     print("Handling join gui for " .. game.players[index].name .. " (" .. index .. ")");
  160. end
  161. local function process_admin_button(event)
  162.     print("Admin clicked button...");
  163.     local index = event.player_index;
  164.     local ename = event.element.name;
  165.     if ename == "TAdminWindowClose" then
  166.         game.players[index].gui.center["TAdminWindow"].visible = false;
  167.     end
  168.     if ename == "TAdmin" then
  169.         game.players[index].gui.center["TAdminWindow"].visible = not game.players[index].gui.center["TAdminWindow"].visible;
  170.     end
  171.     if ename == "TAdminWindowRebuildLobby" then
  172.         prepare_lobby_surface();
  173.     end
  174.     if ename == "TAdminWindowApplyData" then
  175.         teams = game.json_to_table(game.players[index].gui.center.TAdminWindow.TAdminWindowFieldRoundData.text);
  176.         print("Teams set!");
  177.     end
  178.     if ename == "TAdminWindowApplyDefaultData" then
  179.         teams = game.json_to_table("{\"alpha\": {\"players\": [\"f1\",\"f2\",\"f3\"]},\"beta\": {\"players\": [\"andrvaut\",\"kek2\",\"cheburek3\"]},\"gama\": {\"players\": [\"fg1\", \"fg2\",\"fg3\"]}}");
  180.         print("Teams default!");
  181.     end
  182.     if ename == "TAdminWindowStartRound" then
  183.         handle_round_start();
  184.     end
  185.     if ename == "TAdminWindowUpdateSwitcher" then
  186.         for _, p in pairs(game.players) do update_surface_switcher(p.index); end
  187.     end
  188.     if ename == "TAdminWindowPrintSurfaces" then
  189.         print("Surfaces: ");
  190.         for v, p in pairs(game.surfaces) do print("   "..v); end
  191.     end
  192.     if ename == "TAdminWindowPrintForces" then
  193.         print("Forces: ");
  194.         for v, p in pairs(game.forces) do print("   "..v); end
  195.     end
  196.     if ename == "TAdminWindowReload" then
  197.         game.reload_script();
  198.         game.players[index].gui.top.clear();
  199.         game.players[index].gui.center.clear();
  200.         handle_create_gui(index);
  201.     end
  202.    
  203. end
  204. local function get_surface_name_from_slider_value(value)
  205.     if value == 0 then
  206.         return "lobby";
  207.     else
  208.         return "T"..value;
  209.     end
  210. end
  211. local function get_surface_from_slider_value(value)
  212.     return game.get_surface(get_surface_name_from_slider_value(value));
  213. end
  214. local function get_force_name_from_slider_value(value)
  215.     if value == 0 then
  216.         return "player";
  217.     else
  218.         return "T"..value;
  219.     end
  220. end
  221. local function get_force_from_slider_value(value)
  222.     return game.forces[get_force_name_from_slider_value(value)];
  223. end
  224. local function process_gui_sliders(event)
  225.     if round_generated == true then
  226.         local index = event.player_index;
  227.         local ename = event.element.name;
  228.         if ename == "TViewSlider" then
  229.             local val = event.element.slider_value;
  230.             local valround = math.floor(val+0.5);
  231.             -- print("Slider change to " .. val .. " for " .. index);
  232.             game.players[index].gui.top.TViewFrame.TViewLabel.caption = get_surface_name_from_slider_value(valround);
  233.             local ns = get_surface_from_slider_value(valround);
  234.             local nf = get_force_from_slider_value(valround);
  235.             if ns == nil then
  236.                 print("Requested teleport to unknown surface! " .. val .. " " .. valround .. " " .. index);
  237.             else
  238.                 -- game.players[index].teleport(game.players[index].position, ns);
  239.                 game.players[index].teleport(game.players[index].position, "lobby");
  240.                 game.players[index].force = nf;
  241.                 game.players[index].teleport(game.players[index].position, ns);
  242.             end
  243.         end
  244.     end
  245. end
  246. local function process_gui_click(event)
  247.     if event.element.valid == false then
  248.         return;
  249.     end
  250.     local index = event.player_index;
  251.     local ename = event.element.name;
  252.     if ename == "TView" then
  253.         game.players[index].gui.top["TViewFrame"].visible = not game.players[index].gui.top["TViewFrame"].visible;
  254.     end
  255.     if(game.players[event.player_index].admin == true) then
  256.         process_admin_button(event);
  257.     end
  258. end
  259.  
  260. local SyncOre = {};
  261. local SyncOreTotalLast = -1;
  262.  
  263. --[[
  264. local function SyncSurfaces(event)
  265.     if round_generated == true then
  266.         local SyncOreTotalNow = {};
  267.         for i in 1...teams_count do
  268.             table.insert(SyncOreTotalNow, {i, game.surfaces["T"..i].count_entities_filtered(type="resource")});
  269.         end
  270.         local SyncMinimum = math.min(unpack(SyncOreTotalNow));
  271.         if SyncMinimum ~= SyncOreTotalLast then
  272.            
  273.         end
  274.     end
  275. end
  276. ]]--
  277.  
  278. -- n {name, position, ammount}
  279.  
  280. local function SyncSurfacesGenerated(event)
  281.     if event.surface.name == "nauvis" or event.surface.name == "lobby" then return end
  282.    
  283.     if count_entities_filtered{area = event.area, type = "resource"} ~= 0 then
  284.         local ore = event.surface.find_entities_filtered{area = event.area, type = "resource"}
  285.         local addedc = 0;
  286.         for i, p in pairs(ore) do
  287.             local found = 0
  288.             for j, k in pairs(SyncOre) do
  289.                 if k.position.x == p.position.x and k.position.y == p.position.y then
  290.                     found = 1
  291.                     break
  292.                 end
  293.             end
  294.             if found ~= 1 then
  295.                 table.insert(SyncOre, {name = p.name, position = p.position, amount = p.amount})
  296.                 addedc = addedc + 1
  297.             end
  298.         end
  299.         print("Generated new chunk, added " .. addedc .. " ore");
  300.     end
  301. end
  302.  
  303. local function PrintSyncTable(index)
  304.     print(mdump(SyncOre));
  305. end
  306.  
  307. commands.add_command("UpdateSurfaceSwitcher", "Updates surface switcher", add_surface_switcher);
  308. commands.add_command("PrintSyncTable", "PrintSyncTable switcher", PrintSyncTable);
  309.  
  310. script.on_event(defines.events.on_gui_click, process_gui_click)
  311. script.on_event(defines.events.on_gui_value_changed, process_gui_sliders)
  312. -- script.on_event(defines.events.on_tick, SyncSurfaces)
  313. script.on_event(defines.events.on_chunk_generated, SyncSurfacesGenerated)
  314.  
  315. script.on_event(defines.events.on_player_joined_game, function(event)
  316.     local p = game.players[event.player_index];
  317.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** joined.");
  318.     if(p.admin == true) then
  319.         p.print("Welcome admin " .. p.name .. " to tournament server!");
  320.     else
  321.         p.print("Welcome " .. p.name .. " to tournament server!");
  322.     end
  323.     handle_join_gui(event.player_index);
  324. end)
  325.  
  326. script.on_event(defines.events.on_player_left_game, function(event)
  327.     local p = game.players[event.player_index];
  328.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** left.");
  329. end)
  330.  
  331. script.on_event(defines.events.on_player_created, function(event)
  332.     if(first_player_joined == false) then
  333.         prepare_lobby_surface();
  334.     end
  335.     if(tournament_started == false) then
  336.         game.players[event.player_index].teleport({0, 0}, game.surfaces["lobby"]);
  337.     else
  338.        
  339.     end
  340.     handle_create_gui(event.player_index);
  341. end)
  342.  
  343. script.on_event({defines.events.on_console_chat},
  344.     function (e)
  345.         if not e.player_index then
  346.             return
  347.         end
  348.         if game.players[e.player_index].tag == "" then
  349.             if game.players[e.player_index].admin then
  350.                 print('0000-00-00 00:00:00 [DISCORD] (Admin) <' .. game.players[e.player_index].name .. '> ' .. e.message)
  351.             else
  352.                 print('0000-00-00 00:00:00 [DISCORD] <' .. game.players[e.player_index].name .. '> ' .. e.message)
  353.             end
  354.         else
  355.             if game.players[e.player_index].admin then
  356.                 print('0000-00-00 00:00:00 [DISCORD] (Admin) <' .. game.players[e.player_index].name .. '> ' .. game.players[e.player_index].tag .. " " .. e.message)
  357.             else
  358.                 print('0000-00-00 00:00:00 [DISCORD] <' .. game.players[e.player_index].name .. '> ' .. game.players[e.player_index].tag .. " " .. e.message)
  359.             end
  360.         end
  361.     end
  362. )
  363.  
  364.  
  365. script.on_event(defines.events.on_player_died, function(event)
  366.     local p = game.players[event.player_index];
  367.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** died.");
  368. end)
  369. script.on_event(defines.events.on_player_kicked, function(event)
  370.     local p = game.players[event.player_index];
  371.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** kicked.");
  372. end)
  373. script.on_event(defines.events.on_player_unbanned, function(event)
  374.     local p = game.players[event.player_index];
  375.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** unbanned.");
  376. end)
  377. script.on_event(defines.events.on_player_unmuted, function(event)
  378.     local p = game.players[event.player_index];
  379.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** unmuted.");
  380. end)
  381. script.on_event(defines.events.on_player_banned, function(event)
  382.     local p = game.players[event.player_index];
  383.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** banned.");
  384. end)
  385. script.on_event(defines.events.on_player_muted, function(event)
  386.     local p = game.players[event.player_index];
  387.     print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** muted.");
  388. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement