Skip to content
Simon edited this page Oct 20, 2019 · 9 revisions

This wiki entry is under construction.

[Work in progress] Guide on using the map_gen/shared/builders

Currently our builders module contains 83 functions assisting our map creation. This wiki entry strives to document the usage of these.

This guide might not be enough information, but it sure beats no information at all. If you need any assistance please visit www.redmew.com/discord we'll be happy to assist in #devtalk or #mapgen

TABLE OF CONTENT

Basic map generation

Basic map generation using our map_gen/shared/generate module consists of the usage of functions returning true or false for a given coordinate. Notice that factorios map generation works before the scenarios', the true or false only determines whether to keep the current map or remove it entirely, leaving void in its place.

Go to top


Example 1.1 (20 x 20 square)

This example generates a 20 x 20 square and leaves everything else as void. Notice how the usage of the boolean true and false affects the map generation.

local function createVoid(x, y)
    if (math.abs(x) <= 10 and math.abs(y) <= 10) then
        return true
    end
    return false
end

return createVoid

Result:

Since the function returns false (translated into void) for all coordinates except -10 <= x <= 10 | -10 <= y <= 10 the result is a 20 x 20 square shape

image


Example 1.2 (20 x 20 square using builder)

local b = require 'map_gen.shared.builders'

local shape = b.rectangle(20,20)

return shape

Useful for testing:

/c local x = 256 game.forces.player.chart(game.player.surface, {lefttop = {x = -x, y = -x}, rightbottom = {x = x, y = x}})
--and
/c global.task_queue_speed = 10 --Threaded work
--or
/c game.speed = 3 --Non-threaded work

The generate module works after the charting has been executed! Wait a while for it to apply

Getting started

Go take a look at https://github.com/Refactorio/RedMew/wiki/Creating-a-new-map

Getting started with the builder you need to understand how to create a new map. Take this example:

local b = require 'map_gen.shared.builders'

local map = b.rectangle(200, 200)

return map

It creates a 200 x 200 square of land and the rest of the map is void.

The important part of this is that a new map should always return a function that takes the following parameters:
@param x number x-coordinate
@param y number y-coordinate
@param world table containing a x and y coordinate for the world (Not affected by any manipulation)
Using the builders functions will always return a function that satisfies this.

In the function section you'll notice the use of the variables shape and map. They are interchangeable and affects nothing. Best pratice is to combine shapes and then add any entites to a function called map before returning it.

Go to top

Functions

Notes

Every shape is a function with the format function(x, y, world), during map generation the shape function is called with the current x and y coordinate. These coordinates are from the center of a tile resulting in always ending with .5 (eg. 10.5 instead of 10) to get the correct coordinate you can use world.x or world.y (eg. x = 10.5 world.x = 10)

Another usage of world is when manipulating a shape (eg. by translation) this changes the x and y coordinates while world.x and world.y will return the true coordinates

Shape creation

Function Description
Rectangle Creates a rectangular shape
Line x Creates a infinite vertical line
Line y Creates a infinite horizontal line
Path Creates an infinite cross
Square Diamond Creates a square diamond
Rectangle Diamond Creates a rectangular diamond
Circle Creates a circle
Oval Creates an oval
Sine wave fill Creates a sine wave and fills the gaps
Sine wave Creates a sine wave with a thickness
Rectangular spiral Creates an infinite rectangular spiral
Circular spiral Creates an infinite circular spiral
Circular growing spiral Creates an infinite growing circular spiral
Circular spiral with n threads Creates a number of threads of infinite circular spirals
Circular growing spiral with n threads Creates a number of infinite growing circular spirals

Go to top

Builders.rectangle

Creates a rectangular shape

@param width int
@param height int

Example

local shape = b.rectangle(16, 8)

image

Builders.line_x

Creates a infinite vertical line

@param tickness int

Example

local shape = b.line_x(10)

image

Builders.line_y

Creates a infinite horizontal line

@param tickness int

Example

local shape = b.line_y(10)

image

Builders.path

Creates a infinite cross Equivalent to combining line_x and line_y

@param tickness int width of the vertical line
@param optional_thickness_height int --optional width of the horizontal line

Example

local shape = b.path(10, 5)

image

Builders.square_diamond

Creates a diamond where width is equal to height
Equivalent to creating a square and rotating it 45 degrees

@param size int

Example

local shape = b.square_diamond(50)

image

Builders.rectangle_diamond

Like square_diamond but with configurable width and height
Equivalent to creating a rectangle and rotating it 45 degrees

@param width int
@param height int

Example

local shape = b.rectangle_diamond(32, 16)

image

Builders.circle

Creates a circle

@param radius int

Example

local shape = b.circle(10)

image

Builders.oval

Like circle but with configurable width and height

@param radius_x int
@param radius_y int

Example

local shape = b.oval(10, 20)

image

Builders.sine_fill

Creates a sine wave and fills it out

@param width int
@param height int the amplitude
@see sine_wave for comparison

Example

local shape = b.sine_fill(20, 10)

image

Builders.sine_wave

Creates a sine wave with a thickness

@param width int --the wave lenght
@param height int --the amplitude
@param thickness int
@see sine_fill for comparison

Example

local shape = b.sine_wave(20, 10, 2)

image

Builders.rectangular_spiral

Creates an infinite rectangular spiral

@param x_size int
@param optional_y_size int optional otherwise equal to x_size

Example

local shape = b.rectangular_spiral(10, 10) 
--equals b.rectangular_spiral(10)

image

Builders.circular_spiral

Creates an infinite circular spiral

@param in_thickness int defines the number of land tiles the spiral contains
@param total_thickness int defines the number of total tiles the spiral spans over (The gap is total_thickness - in_thickness)
@see circular_spiral_grow for comparison

Example

local shape = b.circular_spiral(5, 10) 

image

Builders.circular_spiral_grow

Creates an infinite growing circular spiral

@param in_thickness int defines the number of land tiles the spiral contains
@param total_thickness int defines the number of total tiles the spiral spans over (The gap is total_thickness - in_thickness)
@param grow_factor int defines how quickly the spiral grow. (Lower numbers result in faster growth, larger number in slower.)
@see circular_spiral for comparison

Example

local shape = b.circular_spiral(5, 10, 50) 

image

Builders.circular_spiral_n_threads

Creates a number of threads of infinite circular spirals

@param in_thickness int defines the number of land tiles the spiral contains
@param total_thickness int defines the number of total tiles the spiral spans over (The gap is total_thickness - in_thickness)
@param n_threads int defines the number of threads/spirals
@see circular_spiral for comparison
@see circular_spiral_grow_n_threads for comparison

Example

local shape = b.circular_spiral_n_threads(5, 10, 2) 

image

Builders.circular_spiral_grow_n_threads

Creates a number of infinite growing circular spirals

@param in_thickness int defines the number of land tiles the spiral contains
@param total_thickness int defines the number of total tiles the spiral spans over (The gap is total_thickness - in_thickness)
@param grow_factor int defines how quickly the spiral grow. (Lower numbers result in faster growth, larger number in slower.)
@param n_threads int defines the number of threads/spirals
@see circular_spiral_grow for comparison
@see circular_spiral_n_threads for comparison

Example

local shape = b.circular_spiral_grow_n_threads(5, 10, 50, 2) 

image

Builders.decompress

TBC

Builders.picture

TBC

Shape manipulation

Function Description
Translate Translates a shapes position
Scale Scales a shapes
Rotate Rotates a shape counter clockwise
Flip along x Flips a shape along the x-axis
Flip along y Flips a shape along the y-axis
Flip both x and y Flips a shape along the xy-axis
Combine Any OR combine
Combine All AND combine
Combine Unused : No Docs
Add OR combine. Only two shapes
Subtract Subtracts a shape from the other.
Invert Inverts a shape
Throttle along x Cuts horizontal lines in a shape
Throttle along y Cuts vertical lines in a shape
Throttle along x and y Applies Builders.throttle_x and Builders.throttle_y to a shape
Throttle along world.x and world.y Preferred over Builders.throttle_xy
Choose Applying one of two shapes based on output of another shape
If else Applying one shape based on the output of another shape
Linear grow No Docs
Grow No Docs
Project No Docs
Project pattern No Docs
Project overlap No Docs

Go to top

Builders.translate

Translates a shapes position

@param shape function the function of a shape to be translated (Must have format function(x, y, world) where world is optional)
@param x_offset int
@param y_offset int

Example
Using a rectangle shape

local shape = b.translate(b.rectangle(16, 8), 8, 4)

Player is at position (0, 0)
Without translation (Water added for illustrational purposes)
image
With translation (Water added for illustrational purposes)
image

Builders.scale

Scales a shapes

@param shape function the function of a shape to be scaled (Must have format function(x, y, world) where world is optional)
@param x_scale int
@param y_scale int

Example
Using a rectangle shape

local shape = b.scale(b.rectangle(16, 8), 1, 2)

Player is at position (0, 0)
Without scaling (Water added for illustrational purposes)
image
With scaling (Water added for illustrational purposes)
image

Builders.rotate

Rotates a shape counter clockwise

@param shape function the function of a shape to be rotated (Must have format function(x, y, world) where world is optional)
@param angle int specified in radians (NOT degrees)

Example
Using a rectangle shape

local shape = b.rotate(b.rectangle(16, 8), math.pi/2)

Player is at position (0, 0)
Without rotation (Water added for illustrational purposes)
image
With rotation (Water added for illustrational purposes)
image

Builders.flip_x

Flips a shape along the x-axis Equivalent to rotate by pi

@param shape function the function of a shape to be flipped (Must have format function(x, y, world) where world is optional)

Example
Using a L shape

-- creating the L shape
local pre_shape = b.translate(b.rectangle(16, 8), 4, 0)
pre_shape = b.add(pre_shape, b.rotate(pre_shape, math.pi/2))

--applying flip_x
local shape = b.flip_x(pre_shape)

Player is at position (0, 0)
Without flipping along x (Water added for illustrational purposes)
image
With flipping along x (Water added for illustrational purposes)
image

Builders.flip_y

Flips a shape along the y-axis Equivalent to rotate by -pi

@param shape function the function of a shape to be flipped (Must have format function(x, y, world) where world is optional)

Example
Using a rectangle rotated shape

-- creating the L shape
local pre_shape = b.translate(b.rectangle(16, 8), 4, 0)
pre_shape = b.add(pre_shape, b.rotate(pre_shape, math.pi/2))

--applying flip_y
local shape = b.flip_y(pre_shape)

Player is at position (0, 0)
Without flipping along y (Water added for illustrational purposes)
image
With flipping along y (Water added for illustrational purposes)
image

Builders.flip_xy

Flips a shape along the xy-axis Equivalent to rotate by 2pi

@param shape function the function of a shape to be flipped (Must have format function(x, y, world) where world is optional)

Example
Using a rectangle rotated shape

-- creating the L shape
local pre_shape = b.translate(b.rectangle(16, 8), 4, 0)
pre_shape = b.add(pre_shape, b.rotate(pre_shape), math.pi/2)

--applying flip_y
local shape = b.flip_xy(pre_shape)

Player is at position (0, 0)
Without flipping along xy (Water added for illustrational purposes)
image
With flipping along xy (Water added for illustrational purposes)
image

Builders.any

Combines all shapes in supplied array as if it where evaluated as an OR operation. If any shape returns true for a coordinate, the resulting shape returns true

@param shapes table of functions table/array of all shapes to be combined (Must have format function(x, y, world) where world is optional)
@see Builders.all for comparison

Example
Using 4 rectangles which have been rotated

-- creating the 4 shapes
local shape1 = b.translate(b.rectangle(16, 8), 4, 0)
local shape2 = b.rotate(shape1), math.pi/2)
local shape3 = b.rotate(shape2), math.pi/2)
local shape4 = b.rotate(shape3), math.pi/2)

--Combining using any
local shape = b.any({shape1, shape2, shape3, shape4})

Player is at position (0, 0)
Base shape (Water added for illustrational purposes)
image
Resulting shape (Water added for illustrational purposes)
image

Builders.all

Combines all shapes in supplied array as if it where evaluated as an AND operation If, and only if, all shapes returns true for a coordinate, the resulting shape returns true.

@param shapes table of functions table/array of all shapes to be combined (Must have format function(x, y, world) where world is optional)
@see Builders.any for comparison

Example
Using 4 rectangles which have been rotated

-- creating the 4 shapes
local shape1 = b.translate(b.rectangle(16, 8), 4, 0)
local shape2 = b.rotate(shape1), math.pi/2)
local shape3 = b.rotate(shape2), math.pi/2)
local shape4 = b.rotate(shape3), math.pi/2)

--Combining using all
local shape = b.all({shape1, shape2, shape3, shape4})

Player is at position (0, 0)
Base shape (Water added for illustrational purposes)
image
(Water added for illustrational purposes)
image

Builders.combine

TBC
No map currently uses Builders.combine

Expected behavior:
Works like Builders.any but keeps any entities that have been added to a shape.
Builders.any is using a lazy evaluation and thus terminates at first true.

Alternative usage: Use Builders.any and apply entities after using Builders.apply_entity or Builders.apply_entities

Builders.add

Combines two shapes as if it where evaluated as an OR operation. Equivalent to Builders.any({shape1, shape2})

@param shape1 function the function of the first shape to be combined (Must have format function(x, y, world) where world is optional)
@param shape2 function the function of the second shape to be combined (Must have format function(x, y, world) where world is optional)
@see Builders.any for comparison

Example
Using 2 rectangles to form an L shape

-- creating the L shape
local shape1 = b.translate(b.rectangle(16, 8), 4, 0)
local shape2 = b.rotate(shape1, math.pi/2)

--applying flip_x
local shape = b.add(shape1, shape2)

Result: (Water added for illustrational purposes)
image

Builders.subtract

Subtracts a shape from the other.

@param shape function the function of the shape to be subtracted from (Must have format function(x, y, world) where world is optional)
@param minus_shape function the function of the subtracting shape (Must have format function(x, y, world) where world is optional)

Example
Using 2 rectangles

-- creating the 2 rectangles
local shape1 = b.rectangle(10, 10)
local shape2 = b.rectangle(5, 5)

--applying subtract
local shape = b.subtract(shape1, shape2)

Result: (Water added for illustrational purposes)
image

Builders.invert

Inverts a shape (true becomes false and vice versa)

@param shape function the function of the shape to be inverted (Must have format function(x, y, world) where world is optional)

Example
Using 2 rectangles subtracted (see Builders.subtract example)

-- creating the 2 rectangles
local shape1 = b.rectangle(10, 10)
local shape2 = b.rectangle(5, 5)

--applying subtract
local pre_shape = b.subtract(shape1, shape2)

--applying invert
local shape = b.invert(pre_shape)

Before inversion (Water added for illustrational purposes, acts as false)
image
After inversion (Water added for illustrational purposes, acts as false)
image

Builders.throttle_x

Cuts horizontal lines in a shape

@param shape function the function of the shape to be throttled (Must have format function(x, y, world) where world is optional)
@param x_in int width of tiles unaffected
@param x_size int total width of a single part of the throttled shape (affected tiles width equals (x_size - x_in)/2)

Example
Using a rectangle

-- creating the rectangle
local rectangle = b.rectangle(20, 20)

--applying throttle_x
local shape = b.throttle_x(rectangle, 2, 4)

Before throttle_x (Water added for illustrational purposes, acts as false)
image
After throttle_x (Water added for illustrational purposes, acts as false)
image

Elaboration
Calling builders.throttle_x with x_in as 2 and x_size as 4 results in the rectangle being cut into 20 / x_size <=> 5 pieces each with width x_size <=> 4. Since x_in is 2, two tiles in the width are kept as land (true), while (x_size - x_in) / 2 <=> 1 tile on either side is discarded as water/void (false).

Builders.throttle_y

Cuts vertical lines in a shape

@param shape function the function of the shape to be throttled (Must have format function(x, y, world) where world is optional)
@param y_in int height of tiles unaffected
@param y_size int total height of a single part of the throttled shape (affected tiles height equals (y_size - y_in)/2)

Example
Using a rectangle

-- creating the rectangle
local rectangle = b.rectangle(20, 20)

--applying throttle_y
local shape = b.throttle_y(rectangle, 2, 4)

Before throttle_y (Water added for illustrational purposes, acts as false)
image
After throttle_y (Water added for illustrational purposes, acts as false)
image

Elaboration
Calling builders.throttle_y with y_in as 2 and y_size as 4 results in the rectangle being cut into 20 / y_size <=> 5 pieces each with width y_size <=> 4. Since y_in is 2, two tiles in the height are kept as land (true), while (y_size - y_in) / 2 <=> 1 tile above and below is discarded as water/void (false).

Builders.throttle_xy

Applies Builders.throttle_x and Builders.throttle_y to a shape

@param shape function the function of the shape to be throttled (Must have format function(x, y, world) where world is optional)
@param x_in int width of tiles unaffected
@param x_size int total width of a single part of the throttled shape (affected tiles width equals (x_size - x_in)/2)
@param y_in int height of tiles unaffected
@param y_size int total height of a single part of the throttled shape (affected tiles height equals (y_size - y_in)/2)

Example
Using a rectangle

-- creating the rectangle
local rectangle = b.rectangle(20, 20)

--applying throttle_y
local shape = b.throttle_xy(rectangle, 2, 4, 2, 4)

Before throttle_xy (Water added for illustrational purposes, acts as false)
image
After throttle_xy (Water added for illustrational purposes, acts as false)
image

Builders.throttle_world_xy

TBC
Almost equivalent to Builders.throttle_xy but preferred

Builders.choose

TBC
Given three shapes if first shape returns true apply true_shape if it returns false apply false_shape

Builders.if_else

Given two shapes if first shape returns false apply else_shape

Builders.linear_grow

TBC
No map currently uses Builders.linear_grow

Builders.grow

TBC
The hearts map uses Builders.grow

Builders.project

TBC

Builders.project_pattern

TBC

Builders.project_overlap

TBC

Entity creation

Function Description
Entity Applying a single entity
Entity_function Applying entities based on a custom function
Resource Fills a shape with a resource
Apply entity No Docs
Apply entities No Docs

Go to top

Builders.entity

Returns a table with one entry named name whose value is @param name string if the supplied shape returns true
Use case: Used to apply trees or rocks

local tree = b.entity(b.throttle_world_xy(b.rectangle(20, 10), 1, 3, 1, 3), 'tree-01')

Builders.entity_func

Executes function func if the supplied shape returns true
Use case: Used to apply a random rock or tree instead of a static one.

local rock_names = {'rock-big', 'rock-huge', 'sand-rock-big'}
local function rocks_func()
    local rock = rock_names[math.random(#rock_names)]
    return {name = rock}
end

local rocks = b.entity_func(b.throttle_world_xy(b.rectangle(20, 10), 1, 6, 1, 6), rocks_func)

Builders.resource

Fills a shape with a resource

@param shape function shape to fill with resource
@param resource_type string prototype name of resource. Available types listed below
@param amount_function function --optional function in the format function(x, y), if nil value is set to 404
@param always_place boolean --optional overrides surface.can_place_entity check

Valid resource_type:
iron-ore, copper-ore, stone, coal, uranium-ore, crude-oil

Effect of always_place
image

Example
Simple resource generation using Builders.manhattan_value

-- creating a 100 x 100 square of land
local shape = b.rectangle(100, 100)

-- creating a circular shape with radius 10, to be filled with iron-ore
local ore_shape = b.circle(10)

-- creating the resource entity from the ore_shape using manhattan distance to increase ore count with distance from (0, 0)
local iron_ore = b.resource(ore_shape), 'iron-ore', b.manhattan_value(500, 1), false) 

-- applying resource entity to shape
local map = b.apply_entity(shape, iron_ore)

Example
Advanced resource generation with amount function

-- creates value function that returns a fixed amount
local function value(num)
    return function(x, y) -- the parameter x and y can be omitted in this case
        return num
    end
end

-- creating a 100 x 100 square of land
local shape = b.rectangle(100, 100)

-- creating a circular shape with radius 10, to be filled with iron-ore
local ore_shape = b.circle(10)

-- creating the resource entity from the ore_shape using a custom value function
local iron_ore = b.resource(ore_shape), 'iron-ore', value(500), false) 

-- applying resource entity to shape
local map = b.apply_entity(shape, iron_ore)

Other amount_functions:
Builders.manhattan_value, Builders.euclidean_value, Builders.exponential_value

Builders.apply_entity

Builders.apply_entities

Builders.remove_map_gen_entities_by_filter

Removes map gen entities by filter

@param shape function
@param filter table see https://lua-api.factorio.com/latest/LuaSurface.html#LuaSurface.find_entities_filtered for valid filters. (Area has no effect)

Example

local shape = b.rectangle(10, 10)

shape = b.remove_map_gen_entities_by_filter(shape, {name = 'cliff'})

Removes all cliffs from the shape (In this example a 10 by 10 rectangle)

Builders.remove_entities_by_name

Removes entities previously added by using one of the builders entity functions

@param shape function
@param names table or string of entity names to remove, if only one entity it can be a string.

Builders.remove_map_gen_decoratives

Removes map gen decoratives

@param shape function
@param optional_filter table --optional see https://lua-api.factorio.com/latest/LuaSurface.html#LuaSurface.destroy_decoratives for valid filters (Area has no effect). If no filter, it will remove all decoratives

Example

local shape = b.rectangle(10, 10)

shape = b.remove_map_gen_decoratives_by_filter(shape)

Removes all decoratives from the shape (In this example a 10 by 10 rectangle)

Builders.remove_decoratives_by_name

Removes decoratives previously added by using one of the builders decorative functions

@param shape function
@param names table or string of decorative names to remove, if only one decorative it can be a string.

Builders.remove_map_gen_resources

Removes all resources from a shape

@param shape function

Example

local shape = b.rectangle(10, 10)

shape = b.remove_map_gen_resources(shape)

Removes all resources from the shape (In this example a 10 by 10 rectangle)

Builders.remove_map_gen_trees

Removes all trees from a shape

@param shape function

Example

local shape = b.rectangle(10, 10)

shape = b.remove_map_gen_trees(shape)

Removes all trees from the shape (In this example a 10 by 10 rectangle)

Builders.remove_map_gen_enemies

Removes all entities belonging to the enemy force from a shape Used to remove biters, spitters, worms and spawners from a shape

@param shape function

Example

local shape = b.rectangle(10, 10)

shape = b.remove_map_gen_enemies(shape)

Removes all entities belonging to the enemy force from the shape (In this example a 10 by 10 rectangle)

Patterns

Function Description
Single pattern Applies a single shape infinite
Single pattern overlap Applies a single shape infinite while allowing overlaps
Single x pattern Applies a single shape infinite along the x-axis
Single y pattern Applies a single shape infinite along the y-axis
Single grid pattern Depricated No Docs
Pattern building How to create shape patterns
Grid x pattern No Docs
Grid y pattern No Docs
Grid pattern No Docs

Go to top

Builders.single_pattern

Applies a single shape infinite

@param shape function the function of a shape to be duplicated in a pattern (Must have format function(x, y, world) where world is optional)
@param width int width of one unit of the base shape
@param height int --optional height of one unit of the base shape. If nil it equals to width

Example Using a square

local shape = b.rectangle(10, 10)
local map = b.single_pattern(shape, 15, 15)

Since the shape's dimentions are 10 by 10 and the base pattern is 15 by 15 we end up with some void.

image

Builders.single_pattern_overlap

Applies a single shape infinite while allowing overlaps

@param shape function the function of a shape to be duplicated in a pattern (Must have format function(x, y, world) where world is optional)
@param width int width of one unit of the base shape
@param height int height of one unit of the base shape
@see Builders.single_pattern for comparison

Example (Better example wanted!) Using an L-shape

local pre_shape = b.translate(b.rectangle(16, 8), 4, 0)
local shape = b.add(pre_shape, b.rotate(pre_shape, math.pi/2))

local map = b.single_pattern_overlap(shape, 25, 12)

With overlap image

Without overlap image

Builders.single_x_pattern

Applies a single shape infinite along the x-axis

@param shape function the function of a shape to be duplicated in a pattern (Must have format function(x, y, world) where world is optional)
@param width int width of one unit of the base shape
@see Builders.single_pattern for comparison

Example Using a square

local shape = b.rectangle(10, 10)
local map = b.single_x_pattern(shape, 11)

Since the shape's dimentions are 10 by 10 and the base pattern is 11 by 11 we end up with some void.

image

Builders.single_y_pattern

Applies a single shape infinite along the y-axis

@param shape function the function of a shape to be duplicated in a pattern (Must have format function(x, y, world) where world is optional)
@param height int height of one unit of the base shape
@see Builders.single_pattern for comparison

Example Using a square

local shape = b.rectangle(10, 10)
local map = b.single_y_pattern(shape, 11)

Since the shape's dimentions are 10 by 10 and the base pattern is 11 by 11 we end up with some void.

image

Builders.single_grid_pattern

Depricated
Do not use, will be removed
Equivalent to Builders.single_pattern except:

Must specify both width and height parameters

Pattern building

The grid functions requires a new type of input a pattern. This pattern is a table containing the shapes in a grid like structure.

Example Single row with three colomns

local pattern = {
    {shape1, shape2, shape3}
}
1 2 3
1 shape1 shape2 shape3

Example Two row with two colomns

local pattern = {
    {shape1, shape2},
    {shape2, shape1}
}
1 2
1 shape1 shape2
2 shape2 shape1

You can build the patterns just as you'd like, each row is a new table inside the pattern table, and each column is a new value inside the nested table. For structual purposes it's advised to have a consistent amount of rows and colomns, like all of these examples.

Example Five row with four colomns

local pattern = {
    {shape1, shape2, shape1, shape2},
    {shape2, shape1, shape2, shape1},
    {shape1, shape2, shape1, shape2},
    {shape2, shape1, shape2, shape1},
    {shape1, shape2, shape1, shape2}
}
1 2 3 4
1 shape1 shape2 shape1 shape2
2 shape2 shape1 shape2 shape1
3 shape1 shape2 shape1 shape2
4 shape2 shape1 shape2 shape1
5 shape1 shape2 shape1 shape2

Go to top

Builders.grid_x_pattern

Builders.grid_y_pattern

Builders.grid_pattern

TBC

Example

local b = require 'map_gen.shared.builders'

local water = b.tile('water')
local normal = b.full_shape

local pattern = {
    {normal, water},
    {normal, normal}
}

local map = b.grid_pattern(pattern, 2, 2, 1, 1)
map = b.scale(map, 32, 32)

return map

image

Builders.grid_pattern_overlap

Builders.grid_pattern_full_overlap

Builders.circular_pattern

Builders.single_spiral_pattern

Builders.single_spiral_rotate_pattern

Builders.circular_spiral_pattern

Builders.circular_spiral_grow_pattern

Builders.segment_pattern

Builders.pyramid_pattern

Builders.pyramid_pattern_inner_overlap

Builders.grid_pattern_offset

Builders.change_tile

Builders.set_hidden_tile

Builders.change_collision_tile

Builders.change_map_gen_tile

Builders.change_map_gen_hidden_tile

Builders.change_map_gen_collision_tile

Builders.change_map_gen_collision_hidden_tile

Builders.overlay_tile_land

Builders.fish

Builders.apply_effect

Builders.manhattan_value

Returns the product of the manhattan distance of the current coordinates from origin and a multiplier. A base number is added to this value
Formula: multiplier * (|x| + |y|) + base_value
Laymans' term: the horizontal and vertical distances, between (0, 0) and a given point, added together
image
The red line is the manhattan distance between the two points. This distance is equivalent to the blue and yellow line. The green line is the euclidean distance.

@param base number the base value or minimum value returned
@param mult number a number signifying the multiplication of the manhattan distance

Example
Usage of the manhattan_value

-- returns the base for all coordinates
b.manhattan_value(500, 0) -- Always returns 500

-- returns the base + manhattan distance
b.manhattan_value(500, 1) -- Always returns >= 500 | eg. coordinate (10, 10) gives the manhattan distance of 20, resulting in the manhattan_value of 520.

-- returns the base + mult(manhattan distance)
b.manhattan_value(500, 2) -- Always returns >= 500 | eg. coordinate (10, 10) gives the manhattan distance of 20 (which needs to be multiplied by 2), resulting in the manhattan_value of 540

Builders.euclidean_value

TBC
Return the product of the euclidean distance of the current coordinates from origin and a multiplier. A base number is added to this value
Formula: multiplier * sqrt(x^2 + y^2) + base_value
Laymans' term: the diagonal distance from point (0, 0) to a given coordinate
image
The red line is the manhattan distance between the two points. This distance is equivalent to the blue and yellow line. The green line is the euclidean distance.

@param base number the base value or minimum value returned
@param mult number a number signifying the multiplication of the euclidean distance

Builders.exponential_value

TBC
Formula: base_value + multiplier * (x^2 + y^2)^(exponent/2)

@param base number the base value or minimum value returned
@param mult number a number signifying the multiplication of the exponential value
@param pow number

Builders.prepare_weighted_array

Go to top

Clone this wiki locally