Restart the game on event

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Sauerkraut
Inserter
Inserter
Posts: 42
Joined: Fri Jul 17, 2015 3:37 pm
Contact:

Restart the game on event

Post by Sauerkraut »

This does not seem to be possible at the moment. At least I could not find any method to do so.

I would like to restart the game when the rocket is launched and change some map generation settings, like changing the available biomes and changing ressources. i thought of something like this:

Code: Select all

require "defines"

scrip.on_event(defines.events.on_rocket_launched, function(event)
	-- maybe manipulate world generation
	game.restart() --this does not exist
end)
Is there another way to completely reset all technology, wipe the discovered map, destroy all entities and let the player effectively restart?

Thanks in advance!

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5148
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Restart the game on event

Post by Klonan »

Sauerkraut wrote: Is there another way to completely reset all technology, wipe the discovered map, destroy all entities and let the player effectively restart?

Yep, you could do

Code: Select all

function new_map(name)
  game.create_surface(name,map_settings{})
  for k, force in pairs (game.forces)
    force.set_spawn_position({0,0}, game.surfaces[name])
    for i, technology in pairs (force.technologies) do
      technology.researched = false
    end
    force.reset_recipes()
  end
  for k, player in pairs (game.players) do
    player.teleport({0,0}, game.surfaces[name]
  end
    game.delete_surface(game.surfaces["nauvis"])
end
 
Something like that, it willl delete the original map, and make a new one
I haven't tested this, but should be a good starting point

User avatar
Sauerkraut
Inserter
Inserter
Posts: 42
Joined: Fri Jul 17, 2015 3:37 pm
Contact:

Re: Restart the game on event

Post by Sauerkraut »

Thanks Klonan! :D

I actually came up with the same idea of creating a new surface, however, I can't figure out the way I have to pass map_settings argument the argument. map_settings is a nil value, so I figured you meant game.map_settings? However, that one does not work either. It tells me i'm attempting to call a field with a table value (?)

I did not know that delete_surface existed, I thought surfaces cannot be deleted? After some trying the game also told me "Given surface can not be deleted." So I assume it's not possible? What exactly would happen if i just let nauvis be, would it eat a lot of ressources? Atleast the savefile would be unnecessary big. I could maybe delete all entities on nauvis, that may save some ressources....

I changed the code a bit, here is what it looks now, if anyone is interested:

Code: Select all

function replace_map(name,old)
	game.create_surface(name,game.map_settings{})
	for k, force in pairs (game.forces) do
		force.set_spawn_position({0,0}, game.surfaces[name])
		for i, technology in pairs (force.technologies) do
			technology.researched = false
		end
		force.reset_recipes()
	end
	for k, player in pairs (game.players) do
		player.teleport({0,0}, game.surfaces[name])
	end
    game.delete_surface(game.surfaces[old])
end

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5148
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Restart the game on event

Post by Klonan »

map_settings is an array of settings,

Which looks like this:

Code: Select all

surface.map_gen_settings = 
{
  terrain_segmentation="normal",
  water="normal",
  autoplace_controls=
  {
    coal=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    },
    ["copper-ore"]=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    },
    ["crude-oil"]=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    },
    ["enemy-base"]=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    },
    ["iron-ore"]=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    },
    stone=
    {
      frequency="normal",
      size="normal",
      richness="normal"
    }
  },
  seed=3457826546,
  width=2000000,
  height=2000000,
  starting_area="normal",
  peaceful_mode=false
}
But you can do like

Code: Select all

local new_map_settings = game.players[1].surface.map_gen_settings
game.create_surface("New_surface_name", new_map_settings)

User avatar
Sauerkraut
Inserter
Inserter
Posts: 42
Joined: Fri Jul 17, 2015 3:37 pm
Contact:

Re: Restart the game on event

Post by Sauerkraut »

Ah, okay, right. I was already wondering why the API said game.map_settings is a MapSettings but create_surface() requires MapGenSettings. Okay, so those are two different things ;P

Great, now that that works I'll look into autoplace, biomes and map generations! Maybe I'll make another dumb post about that, who knows ;)

Post Reply

Return to “Modding help”