-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisland_generator.lua
144 lines (118 loc) · 4.22 KB
/
island_generator.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function count_alive_neighbors(tiles, depth, _x, _y)
count = 0
for y = -depth, depth do
for x = -depth, depth do
if not ((y == 0 and x == 0) or (x + _x < 0 or x + _x >= 31 or y + _y < 0 or y + _y >= 31)) then -- don't select ourself or out-of-bounds
if(tiles[y + _y][x + _x]) then count = count + 1 end
end
end
end
return count
end
local function generate_initial()
tiles = {}
for y = 0, 31 do
tiles[y] = {}
for x = 0, 31 do
tiles[y][x] = getRandomIntInclusive(1, 100) <= 55
if (x == 0 or x == 31 or y == 0 or y == 31) then -- kill edges
tiles[y][x] = false
end
end
end
return tiles
end
local function iterate_tiles(tiles)
new_tiles = tiles
for y = 0, 31 do
for x = 0, 31 do
count = count_alive_neighbors(tiles, 1, x, y)
if (count >= 5 and not tiles[y][x]) then new_tiles[y][x] = true
elseif (count <= 3 and tiles[y][x]) then new_tiles[y][x] = false
end
end
end
return new_tiles
end
local function generate_island_tiles(iterations)
tiles = generate_initial()
for i = 1, iterations do
tiles = iterate_tiles(tiles)
end
return tiles
end
local function get_ore_type()
ratio = getRandomIntInclusive(1, 100)
if (ratio <= 15) then
return "iron-ore" -- 15% iron
elseif (ratio <= 30) then
return "copper-ore" -- 15% copper
elseif (ratio <= 45) then
return "coal" -- 15% coal
elseif (ratio <= 78) then
return "stone" -- 33% stone
elseif (ratio <= 83) then
return "uranium-ore" -- 5% uranium
else
return nil -- 17% empty
end
end
local function get_entity_type()
return "tree-01"
end
local function get_total_amount(one, two, three, four, x, y)
amount = 0
if (one >= 8) then amount = amount + costCalc(x,y)*2 end
if (two >= 24) then amount = amount + costCalc(x,y)*4 end
if (three >= 48) then amount = amount + costCalc(x,y)*6 end
if (four >= 80) then amount = amount + costCalc(x,y)*8 end
return amount
end
function get_island_tiles(surface, x1, y1, x2, y2)
ore_type = get_ore_type()
entity_type = get_entity_type()
tiles = generate_island_tiles(getRandomIntInclusive(1,3))
transformed_tiles = {}
ore = get_ore_type()
do_oil = getRandomIntInclusive(1, 10) <= 2
do_enemies = getRandomIntInclusive(1, 100) <= 25
if (ore == "uranium-ore") then do_enemies = true end
for y = 0, 31 do
for x = 0, 31 do
-- spawn ore
count_one = count_alive_neighbors(tiles, 0, x, y) -- >= 8
count_two = count_alive_neighbors(tiles, 2, x, y) -- >= 24
count_three = count_alive_neighbors(tiles, 3, x, y) -- >= 48
count_four = count_alive_neighbors(tiles, 4, x, y) -- >= 80
if (ore) then
if (count_one == nil) then count_one = 0 end
if (count_two == nil) then count_two = 0 end
if (count_three == nil) then count_three = 0 end
if (count_four == nil) then count_four = 0 end
amount = get_total_amount(count_one, count_two, count_three, count_four, x + x1, y + y1)
if (amount > 0) then
spawn_entity(surface, ore, amount, x + x1, y + y1)
end
spawn_tree(surface, x + x1, y + y1)
if (do_oil) then
spawn_oil(surface, x + x1, y + y1)
end
if (do_enemies) then
spawn_enemy(surface, x + x1, y + y1)
end
end
-- spawn entities
-- transform tile
tile_type = "grass"
if (not tiles[y][x]) then
if (count_two <= 5) then
tile_type = "deepwater"
else
tile_type = "water"
end
end
table.insert(transformed_tiles, {name=tile_type, position={x+x1, y+y1}})
end
end
return transformed_tiles
end