Skip to content

Instantly share code, notes, and snippets.

@Jezza
Last active July 6, 2016 13:13
Show Gist options
  • Save Jezza/fcf87255533a7f82deea187da57e9ddd to your computer and use it in GitHub Desktop.
Save Jezza/fcf87255533a7f82deea187da57e9ddd to your computer and use it in GitHub Desktop.
-- resource: the value that will be used when creating the entity. Such as 'stone', or 'iron-ore'
-- length: the length of one side of the square that will be generated, the player will be the top left of the square. Default is 20.
-- amount: the quantity per ore. Default is 50000.
-- to: The value that will be used when generating the base land within the area. Default is 'grass'
function create(resource, length, amount, to)
if (resource == nil)
return "Please specify a resource to use. Eg: /c create('stone') or /c create('iron-ore')"
end
local r = length or 20; -- If no length is provided, just assume a small 20, that way it doesn't destroy massive areas of land.
local p = game.player; -- Store the player in a local variable for faster access.
local pos = p.position; -- Grab and store the player's position that will be used throughout the function.
local s = p.surface; -- Grab the surface that the player exists on. This is the "world" that you see.
p.force.chart(s, {lefttop={x=pos.x,y=pos.y}, rightbottom={x=pos.x+r, y=pos.y+r}}); -- Make sure that the game has generated the chunks by charting them into the player's force.
local re = to or 'grass'
local p={}; -- Create a table that will be used within this for-loop. This holds all the data that will become the surface.
for x=0,r do
for y=0,r do
p[#p + 1] = {name=re, position={pos.x+x, pos.y+y}} -- Create a simple object that can represent the map. In this case, we use grass and store the position that this 'data' represents.
end
end;
s.set_tiles(p); -- Tell the map to replace the current data with the data that we just built. In short, it says replace this part of the map with this. In our case, it's grass.
local area = {{pos.x, pos.y},{pos.x+r,pos.y+r}}; -- Store the area as a table, as no point creating it 3 times over. This area is simply the area we want to replace.
local found = s.find_entities_filtered{area=area,type='resource'}; -- Grab all of the entities that meet a certain requirement within the predefined area from before. Here we're looking for entities that are of the type 'resource', which can be anything from iron ore, to stone, etc.
for i = 1, #found do
found[i].destroy() -- Simply loop through each entity found and destroy it.
end
found = s.find_entities_filtered{area=area,type='tree'} -- Similiar to the above call, we need to filter out entities that meet certain requirements. In this case, it's entities that are of the type 'tree'.
for i = 1, #found do
found[i].destroy() -- As with above, we want to destroy the entries, so loop through and call destroy().
end
found = s.find_entities_filtered{area=area,name='stone-rock'} -- As before, filtering out entities that we wish to destroy, and here we want to destroy the big rocks that you tend to shoot or hit with your vehicle of choice.
for i = 1, #found do
found[i].destroy() -- Again, destroying the entity. This makes the game remove the entity from the map.
end
local amount = amount or 50000; -- If no amount is specified, then simply provide a default of 50,000.
for y=0,r do -- The creation loop. Loop on a 2D plane and start creating the resource.
for x=0,r do
s.create_entity({name=resource, amount=amount, position={pos.x+x, pos.y+y}}) -- Fairly straight forward. This simply tells the surface ('world') to "Create this entity, at these coordinates."
end
end
end
/c function create(resource,radius,amount,replacement)local r=radius or 20;local p=game.player;local pos=p.position;local s=p.surface;p.force.chart(s,{lefttop={x=pos.x,y=pos.y},rightbottom={x=pos.x+r,y=pos.y+r}});local re = replacement or 'grass';local p={};for x=0,r do for y=0,r do p[#p+1]={name=re,position={pos.x+x,pos.y+y}};end end;s.set_tiles(p);local area = {{pos.x, pos.y},{pos.x+r,pos.y+r}};local found = s.find_entities_filtered{area=area,type='resource'};for i = 1, #found do found[i].destroy()end found = s.find_entities_filtered{area=area,type='tree'};for i = 1, #found do found[i].destroy()end found=s.find_entities_filtered{area=area,name='stone-rock'};for i=1,#found do found[i].destroy()end local amount = amount or 50000;for y=0,r do for x=0,r do s.create_entity({name=resource, amount=amount, position={pos.x+x, pos.y+y}})end end end
@Jezza
Copy link
Author

Jezza commented May 24, 2016

It should all be relatively well laid out and explained.
If anyone wants anymore added to it, feel free to poke me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment