Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Factorio Tournament 2020 scenario script
- -- Contact: https://discord.gg/SUJRG47/
- -- Web: https://games.projects74.ru/
- local tournament_started = false;
- local round_generated = false;
- local first_player_joined = false;
- local teams = {};
- local teams_count = 0;
- local function prepare_lobby_surface()
- local tiles = {};
- local y=-32;
- while y < 32 do
- local x=-32;
- while x < 32 do
- table.insert(tiles, {name="tutorial-grid", position={y, x}});
- x = x+1;
- end
- y = y+1;
- end
- if game.get_surface("lobby") == nil then
- lobby_surface = game.create_surface("lobby", {width=64, height=64})
- lobby_surface.set_chunk_generated_status({-1,-1}, defines.chunk_generated_status.entities);
- lobby_surface.set_chunk_generated_status({0,-1}, defines.chunk_generated_status.entities);
- lobby_surface.set_chunk_generated_status({-1,0}, defines.chunk_generated_status.entities);
- lobby_surface.set_chunk_generated_status({0,0}, defines.chunk_generated_status.entities);
- end
- game.get_surface("lobby").set_tiles(tiles, true);
- end
- function mdump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. mdump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
- end
- local function add_admin_window_elements(frame)
- print("Adding admin window...");
- frame.add{type="button", name="TAdminWindowRebuildLobby", caption="Rebuild lobby"};
- frame.add{type="text-box", name="TAdminWindowFieldRoundData"};
- frame.add{type="button", name="TAdminWindowApplyData", caption="Apply data"};
- frame.add{type="button", name="TAdminWindowApplyDefaultData", caption="Apply default data"};
- frame.add{type="button", name="TAdminWindowStartRound", caption="Start"};
- frame.add{type="button", name="TAdminWindowUpdateSwitcher", caption="Update switcher"};
- local pf = frame.add{type="frame", name="TAdminPrintFrame", direction="horizontal"};
- pf.add{type="button", name="TAdminWindowPrintSurfaces", caption="Print surfaces"};
- pf.add{type="button", name="TAdminWindowPrintForces", caption="Print forces"};
- frame.add{type="button", name="TAdminWindowReload", caption="Reload"};
- frame.add{type="button", name="TAdminWindowClose", caption="Close"};
- end
- local function add_admin_gui(index)
- if not game.players[index].gui.top["TAdmin"] then
- game.players[index].gui.top.add{type="sprite-button", name="TAdmin", sprite="item/power-armor-mk2"};
- end
- if not game.players[index].gui.center["TAdminWindow"] then
- local frame = game.players[index].gui.center.add{type="frame",
- name="TAdminWindow",
- caption="Admin window",
- visible=false,
- direction="vertical"};
- add_admin_window_elements(frame);
- end
- end
- local function add_surface_switcher(index)
- if not game.players[index].gui.top["TView"] then
- game.players[index].gui.top.add{type="sprite-button", name="TView", sprite="utility/surface_editor_icon"};
- local frame = game.players[index].gui.top.add{type="frame",
- name="TViewFrame",
- visible=false,
- direction="horizontal"};
- frame.add{type="slider",
- name="TViewSlider",
- caption="Surfaces",
- minimum_value=0,
- maximum_value=1,
- value=0,
- value_step=1,
- discrete_slider=true,
- discrete_values=true,
- style="notched_slider"};
- frame.add{type="label",
- name="TViewLabel",
- caption="lobby"};
- end
- end
- local function update_surface_switcher(index)
- if game.players[index].admin == false then
- return;
- end
- if not game.players[index].gui.top["TView"] then
- add_surface_switcher(index);
- end
- if round_generated == false then
- game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_minimum_maximum(0, 1);
- else
- game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_minimum_maximum(0, teams_count);
- game.players[index].gui.top.TViewFrame.TViewSlider.set_slider_value_step(1);
- end
- end
- local function handle_round_start()
- seed = game.surfaces["nauvis"].map_gen_settings.seed;
- local surface_counter = 1;
- for n, t in pairs(teams) do
- print("Working on team " .. n);
- print("Creating surface...");
- local created_surface = game.create_surface("T"..surface_counter, game.surfaces["nauvis"].map_gen_settings);
- print("Generating terrain for team "..surface_counter.."...");
- created_surface.request_to_generate_chunks({0, 0}, 2);
- created_surface.force_generate_chunk_requests();
- print("Creating force...");
- local created_force = game.create_force("T"..surface_counter);
- created_force.reset();
- print("Setting players...");
- for np, p in pairs(t.players) do
- print("Processing player " .. p .. " of index " .. np);
- if not game.players[p] then
- print("Warning! Player " .. p .. " not found on server!");
- else
- game.players[p].force = created_force;
- game.players[p].teleport({x=0, y=0}, created_surface);
- end
- end
- teams_count = teams_count + 1;
- surface_counter = surface_counter + 1;
- end
- round_generated = true;
- for _, p in pairs(game.players) do update_surface_switcher(p.index); end
- end
- local function handle_create_gui(index)
- print("Creating gui for " .. game.players[index].name .. " (" .. index .. ")");
- if(game.players[index].admin == true) then
- add_admin_gui(index);
- add_surface_switcher(index);
- end
- end
- local function handle_join_gui(index)
- print("Handling join gui for " .. game.players[index].name .. " (" .. index .. ")");
- end
- local function process_admin_button(event)
- print("Admin clicked button...");
- local index = event.player_index;
- local ename = event.element.name;
- if ename == "TAdminWindowClose" then
- game.players[index].gui.center["TAdminWindow"].visible = false;
- end
- if ename == "TAdmin" then
- game.players[index].gui.center["TAdminWindow"].visible = not game.players[index].gui.center["TAdminWindow"].visible;
- end
- if ename == "TAdminWindowRebuildLobby" then
- prepare_lobby_surface();
- end
- if ename == "TAdminWindowApplyData" then
- teams = game.json_to_table(game.players[index].gui.center.TAdminWindow.TAdminWindowFieldRoundData.text);
- print("Teams set!");
- end
- if ename == "TAdminWindowApplyDefaultData" then
- teams = game.json_to_table("{\"alpha\": {\"players\": [\"f1\",\"f2\",\"f3\"]},\"beta\": {\"players\": [\"andrvaut\",\"kek2\",\"cheburek3\"]},\"gama\": {\"players\": [\"fg1\", \"fg2\",\"fg3\"]}}");
- print("Teams default!");
- end
- if ename == "TAdminWindowStartRound" then
- handle_round_start();
- end
- if ename == "TAdminWindowUpdateSwitcher" then
- for _, p in pairs(game.players) do update_surface_switcher(p.index); end
- end
- if ename == "TAdminWindowPrintSurfaces" then
- print("Surfaces: ");
- for v, p in pairs(game.surfaces) do print(" "..v); end
- end
- if ename == "TAdminWindowPrintForces" then
- print("Forces: ");
- for v, p in pairs(game.forces) do print(" "..v); end
- end
- if ename == "TAdminWindowReload" then
- game.reload_script();
- game.players[index].gui.top.clear();
- game.players[index].gui.center.clear();
- handle_create_gui(index);
- end
- end
- local function get_surface_name_from_slider_value(value)
- if value == 0 then
- return "lobby";
- else
- return "T"..value;
- end
- end
- local function get_surface_from_slider_value(value)
- return game.get_surface(get_surface_name_from_slider_value(value));
- end
- local function get_force_name_from_slider_value(value)
- if value == 0 then
- return "player";
- else
- return "T"..value;
- end
- end
- local function get_force_from_slider_value(value)
- return game.forces[get_force_name_from_slider_value(value)];
- end
- local function process_gui_sliders(event)
- if round_generated == true then
- local index = event.player_index;
- local ename = event.element.name;
- if ename == "TViewSlider" then
- local val = event.element.slider_value;
- local valround = math.floor(val+0.5);
- -- print("Slider change to " .. val .. " for " .. index);
- game.players[index].gui.top.TViewFrame.TViewLabel.caption = get_surface_name_from_slider_value(valround);
- local ns = get_surface_from_slider_value(valround);
- local nf = get_force_from_slider_value(valround);
- if ns == nil then
- print("Requested teleport to unknown surface! " .. val .. " " .. valround .. " " .. index);
- else
- -- game.players[index].teleport(game.players[index].position, ns);
- game.players[index].teleport(game.players[index].position, "lobby");
- game.players[index].force = nf;
- game.players[index].teleport(game.players[index].position, ns);
- end
- end
- end
- end
- local function process_gui_click(event)
- if event.element.valid == false then
- return;
- end
- local index = event.player_index;
- local ename = event.element.name;
- if ename == "TView" then
- game.players[index].gui.top["TViewFrame"].visible = not game.players[index].gui.top["TViewFrame"].visible;
- end
- if(game.players[event.player_index].admin == true) then
- process_admin_button(event);
- end
- end
- local SyncOre = {};
- local SyncOreTotalLast = -1;
- --[[
- local function SyncSurfaces(event)
- if round_generated == true then
- local SyncOreTotalNow = {};
- for i in 1...teams_count do
- table.insert(SyncOreTotalNow, {i, game.surfaces["T"..i].count_entities_filtered(type="resource")});
- end
- local SyncMinimum = math.min(unpack(SyncOreTotalNow));
- if SyncMinimum ~= SyncOreTotalLast then
- end
- end
- end
- ]]--
- -- n {name, position, ammount}
- local function SyncSurfacesGenerated(event)
- if event.surface.name == "nauvis" or event.surface.name == "lobby" then return end
- if count_entities_filtered{area = event.area, type = "resource"} ~= 0 then
- local ore = event.surface.find_entities_filtered{area = event.area, type = "resource"}
- local addedc = 0;
- for i, p in pairs(ore) do
- local found = 0
- for j, k in pairs(SyncOre) do
- if k.position.x == p.position.x and k.position.y == p.position.y then
- found = 1
- break
- end
- end
- if found ~= 1 then
- table.insert(SyncOre, {name = p.name, position = p.position, amount = p.amount})
- addedc = addedc + 1
- end
- end
- print("Generated new chunk, added " .. addedc .. " ore");
- end
- end
- local function PrintSyncTable(index)
- print(mdump(SyncOre));
- end
- commands.add_command("UpdateSurfaceSwitcher", "Updates surface switcher", add_surface_switcher);
- commands.add_command("PrintSyncTable", "PrintSyncTable switcher", PrintSyncTable);
- script.on_event(defines.events.on_gui_click, process_gui_click)
- script.on_event(defines.events.on_gui_value_changed, process_gui_sliders)
- -- script.on_event(defines.events.on_tick, SyncSurfaces)
- script.on_event(defines.events.on_chunk_generated, SyncSurfacesGenerated)
- script.on_event(defines.events.on_player_joined_game, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** joined.");
- if(p.admin == true) then
- p.print("Welcome admin " .. p.name .. " to tournament server!");
- else
- p.print("Welcome " .. p.name .. " to tournament server!");
- end
- handle_join_gui(event.player_index);
- end)
- script.on_event(defines.events.on_player_left_game, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** left.");
- end)
- script.on_event(defines.events.on_player_created, function(event)
- if(first_player_joined == false) then
- prepare_lobby_surface();
- end
- if(tournament_started == false) then
- game.players[event.player_index].teleport({0, 0}, game.surfaces["lobby"]);
- else
- end
- handle_create_gui(event.player_index);
- end)
- script.on_event({defines.events.on_console_chat},
- function (e)
- if not e.player_index then
- return
- end
- if game.players[e.player_index].tag == "" then
- if game.players[e.player_index].admin then
- print('0000-00-00 00:00:00 [DISCORD] (Admin) <' .. game.players[e.player_index].name .. '> ' .. e.message)
- else
- print('0000-00-00 00:00:00 [DISCORD] <' .. game.players[e.player_index].name .. '> ' .. e.message)
- end
- else
- if game.players[e.player_index].admin then
- print('0000-00-00 00:00:00 [DISCORD] (Admin) <' .. game.players[e.player_index].name .. '> ' .. game.players[e.player_index].tag .. " " .. e.message)
- else
- print('0000-00-00 00:00:00 [DISCORD] <' .. game.players[e.player_index].name .. '> ' .. game.players[e.player_index].tag .. " " .. e.message)
- end
- end
- end
- )
- script.on_event(defines.events.on_player_died, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** died.");
- end)
- script.on_event(defines.events.on_player_kicked, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** kicked.");
- end)
- script.on_event(defines.events.on_player_unbanned, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** unbanned.");
- end)
- script.on_event(defines.events.on_player_unmuted, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** unmuted.");
- end)
- script.on_event(defines.events.on_player_banned, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** banned.");
- end)
- script.on_event(defines.events.on_player_muted, function(event)
- local p = game.players[event.player_index];
- print("0000-00-00 00:00:00 [DISCORD] **" .. p.name .. "** muted.");
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement