Autolaunch for modless server/single player

Find multiplayer games.
Tools/scripts to run a dedicated server.
Post Reply
pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

Autolaunch for modless server/single player

Post by pieppiep »

I've written a rocket autolauncher for servers where you don't want any mods.
This works by editing the control.lua inside the savefile.
The script is

Code: Select all

local function on_init() 
	global.silos = global.silos or find_silos()
	global.ticks = global.ticks or 0
end
script.on_init(on_init)

local function on_creation(event)
	global.silos = global.silos or find_silos()
	local entity = event.created_entity
	if entity.type == "rocket-silo" then
		add_unique_value(global.silos, entity)
	end
end
script.on_event(defines.events.on_built_entity, on_creation)
script.on_event(defines.events.on_robot_built_entity, on_creation)

local function on_destruction(event)
	global.silos = global.silos or find_silos()
	local entity = event.entity
	if entity.type == "rocket-silo" then
		remove_value(global.silos, entity)
	end
end
script.on_event(defines.events.on_entity_died, on_destruction)
script.on_event(defines.events.on_robot_pre_mined, on_destruction)
script.on_event(defines.events.on_preplayer_mined_item, on_destruction)

local function on_tick()
	global.silos = global.silos or find_silos()
	global.ticks = (global.ticks or 0) + 1
	
	if global.ticks >= 60 then
		for k, silo in pairs(global.silos) do
			if silo.valid then
				if silo.get_item_count("satellite") > 0 then
					silo.launch_rocket()
				end
			else
				table.remove(global.silos, k)
			end
		end
		global.ticks = 0
	end
end
script.on_event(defines.events.on_tick, on_tick)

function add_unique_value(list, newValue)
	for _, value in pairs(list) do
		if value == newValue then
			return
		end
	end
	table.insert(list, newValue)
end

function remove_value(list, oldValue)
	for i, value in pairs(list) do
		if value == oldValue then
			table.remove(list, i)
		end
	end
end

function find_silos()
	local silos = {}
	for _, surface in pairs(game.surfaces) do
		for _, silo in pairs(surface.find_entities_filtered({type="rocket-silo"})) do
			add_unique_value(silos, silo)
		end
	end
	return silos
end
I have tested it a little with another player and it should be desync free.

Currently I'm playing a single player game without mods to see how many rockets I can sent before 0.15 launches with this script in the savefile.

SilverB1rd
Inserter
Inserter
Posts: 47
Joined: Fri Mar 17, 2017 9:19 pm
Contact:

Re: Autolaunch for modless server/single player

Post by SilverB1rd »

you can save a global variable by using the event.tick value to run your on_tick check

Code: Select all

local function on_tick(event) -- get event parameter
   if event.tick%60 == 0 then -- % is modulo, returns the remainder after dividing by a number, so %60 returns 0 through 59
   ...
   end
end

pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

Re: Autolaunch for modless server/single player

Post by pieppiep »

Oh, nice :)

Btw, I forget to say, I've only tested this with savegame, haven't tryed scenarios.
I can't test scenario before the weekend.

Also, this script is what I've added to the standard control.lua in the savegame I have for single player game.
You need to keep the original code also, so the total will be,

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
  local player = game.players[event.player_index]
  player.insert{name="iron-plate", count=8}
  player.insert{name="pistol", count=1}
  player.insert{name="firearm-magazine", count=10}
  player.insert{name="burner-mining-drill", count = 1}
  player.insert{name="stone-furnace", count = 1}
  player.force.chart(player.surface, {{player.position.x - 200, player.position.y - 200}, {player.position.x + 200, player.position.y + 200}})
  if (#game.players <= 1) then
    game.show_message_dialog{text = {"msg-intro"}}
  else
    player.print({"msg-intro"})
  end
end)

script.on_event(defines.events.on_player_respawned, function(event)
  local player = game.players[event.player_index]
  player.insert{name="pistol", count=1}
  player.insert{name="firearm-magazine", count=10}
end)

script.on_event(defines.events.on_rocket_launched, function(event)
  local force = event.rocket.force
  if event.rocket.get_item_count("satellite") == 0 then
    if (#game.players <= 1) then
      game.show_message_dialog{text = {"gui-rocket-silo.rocket-launched-without-satellite"}}
    else
      for index, player in pairs(force.players) do
        player.print({"gui-rocket-silo.rocket-launched-without-satellite"})
      end
    end
    return
  end
  if not global.satellite_sent then
    global.satellite_sent = {}
  end
  if global.satellite_sent[force.name] then
    global.satellite_sent[force.name] = global.satellite_sent[force.name] + 1   
  else
    game.set_game_state{game_finished=true, player_won=true, can_continue=true}
    global.satellite_sent[force.name] = 1
  end
  for index, player in pairs(force.players) do
    if player.gui.left.rocket_score then
      player.gui.left.rocket_score.rocket_count.caption = tostring(global.satellite_sent[force.name])
    else
      local frame = player.gui.left.add{name = "rocket_score", type = "frame", direction = "horizontal", caption={"score"}}
      frame.add{name="rocket_count_label", type = "label", caption={"", {"rockets-sent"}, ":"}}
      frame.add{name="rocket_count", type = "label", caption=tostring(global.satellite_sent[force.name])}
    end
  end
end)





local function on_init() 
	global.silos = global.silos or find_silos()
	global.ticks = global.ticks or 0
end
script.on_init(on_init)

local function on_creation(event)
	global.silos = global.silos or find_silos()
	local entity = event.created_entity
	if entity.type == "rocket-silo" then
		add_unique_value(global.silos, entity)
	end
end
script.on_event(defines.events.on_built_entity, on_creation)
script.on_event(defines.events.on_robot_built_entity, on_creation)

local function on_destruction(event)
	global.silos = global.silos or find_silos()
	local entity = event.entity
	if entity.type == "rocket-silo" then
		remove_value(global.silos, entity)
	end
end
script.on_event(defines.events.on_entity_died, on_destruction)
script.on_event(defines.events.on_robot_pre_mined, on_destruction)
script.on_event(defines.events.on_preplayer_mined_item, on_destruction)

local function on_tick()
	global.silos = global.silos or find_silos()
	global.ticks = (global.ticks or 0) + 1
	
	if global.ticks >= 60 then
		for k, silo in pairs(global.silos) do
			if silo.valid then
				if silo.get_item_count("satellite") > 0 then
					silo.launch_rocket()
				end
			else
				table.remove(global.silos, k)
			end
		end
		global.ticks = 0
	end
end
script.on_event(defines.events.on_tick, on_tick)

function add_unique_value(list, newValue)
	for _, value in pairs(list) do
		if value == newValue then
			return
		end
	end
	table.insert(list, newValue)
end

function remove_value(list, oldValue)
	for i, value in pairs(list) do
		if value == oldValue then
			table.remove(list, i)
		end
	end
end

function find_silos()
	local silos = {}
	for _, surface in pairs(game.surfaces) do
		for _, silo in pairs(surface.find_entities_filtered({type="rocket-silo"})) do
			add_unique_value(silos, silo)
		end
	end
	return silos
end

Post Reply

Return to “Multiplayer / Dedicated Server”