1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-08 00:39:30 +02:00

Merge remote-tracking branch 'upstream/master'

This commit is contained in:
MaemiKozue 2019-09-18 21:57:24 +02:00
commit 63a70ae071
3 changed files with 265 additions and 3 deletions

View File

@ -2,6 +2,18 @@
local simplex_noise = require "utils.simplex_noise".d2
local function get_brush(size)
local vectors = {}
for x = size * -1, size, 1 do
for y = size * -1, size, 1 do
if math.sqrt(y ^ 2 + x ^ 2) <= size then
vectors[#vectors + 1] = {x, y}
end
end
end
return vectors
end
function noise_vector_entity_path(surface, entity_name, position, base_vector, length, collision)
local seed_1 = math.random(1, 10000000)
local seed_2 = math.random(1, 10000000)
@ -40,14 +52,28 @@ function noise_vector_entity_path(surface, entity_name, position, base_vector, l
return entities
end
function noise_vector_tile_path(surface, tile_name, position, base_vector, length)
function noise_vector_tile_path(surface, tile_name, position, base_vector, length, brush_size, whitelist)
local seed_1 = math.random(1, 10000000)
local seed_2 = math.random(1, 10000000)
local vector = {}
local minimal_movement = 0.5
local tiles = {}
local minimal_movement = 0.65
local brush_vectors = get_brush(brush_size)
for a = 1, length, 1 do
surface.set_tiles({{name = tile_name, position = position}}, true)
for _, v in pairs(brush_vectors) do
local p = {x = position.x + v[1], y = position.y + v[2]}
if whitelist then
if whitelist[surface.get_tile(p).name] then
surface.set_tiles({{name = tile_name, position = p}}, true)
tiles[#tiles + 1] = {name = tile_name, position = p}
end
else
surface.set_tiles({{name = tile_name, position = p}}, true)
tiles[#tiles + 1] = {name = tile_name, position = p}
end
end
local noise = simplex_noise(position.x * 0.1, position.y * 0.1, seed_1)
local noise_2 = simplex_noise(position.x * 0.1, position.y * 0.1, seed_2)
@ -65,6 +91,8 @@ function noise_vector_tile_path(surface, tile_name, position, base_vector, lengt
position = {x = position.x + vector[1], y = position.y + vector[2]}
end
return tiles
end
--/c noise_vector_path(game.player.surface, "tree-04", game.player.position, {0,0})

View File

@ -0,0 +1,87 @@
require "functions.noise_vector_path"
require "functions.boss_unit"
require "maps.island_troopers.terrain"
local function set_next_level()
global.alive_enemies = 0
global.current_stage = 1
global.current_level = global.current_level + 1
global.path_tiles = nil
global.stages = {}
global.stages[1] = {
path_length = 32 + global.current_level * 5,
size = math.random(global.current_level * 2, global.current_level * 5),
}
for i = 1, global.current_level, 1 do
global.stages[#global.stages + 1] = {
path_length = 24 + global.current_level * 5,
size = math.random(global.current_level * 2, global.current_level * 5),
}
end
global.stages[#global.stages + 1] = {
path_length = 128 + global.current_level * 5,
size = false,
}
game.print("Level " .. global.current_level .. " has begun!")
global.gamestate = 2
end
local function wait_until_stage_is_beaten()
if global.alive_enemies > 0 then return end
if global.stages[global.current_stage].size then
global.current_stage = global.current_stage + 1
global.gamestate = 2
return
end
game.print("Level " .. global.current_level .. " complete!!")
global.gamestate = 5
end
local function on_player_joined_game(event)
local player = game.players[event.player_index]
player.insert({name = "pistol", count = 1})
player.insert({name = "uranium-rounds-magazine", count = 128})
player.insert({name = "firearm-magazine", count = 32})
end
local function on_init()
local surface = game.surfaces[1]
surface.request_to_generate_chunks({x = 0, y = 0}, 8)
surface.force_generate_chunk_requests()
--global.level_tiles = {}
global.level_vectors = {}
global.current_level = 0
global.gamestate = 1
game.forces.player.set_spawn_position({0, 2}, surface)
end
local function on_entity_died(event)
if not event.entity.valid then return end
if event.entity.force.name == "enemy" then global.alive_enemies = global.alive_enemies - 1 end
end
local gamestate_functions = {
[1] = set_next_level,
[2] = draw_path_to_next_stage,
[3] = draw_the_island,
[4] = wait_until_stage_is_beaten,
[5] = kill_the_level,
}
local function on_tick()
if game.tick % 2 == 0 then
gamestate_functions[global.gamestate]()
end
end
local event = require 'utils.event'
event.on_init(on_init)
event.add(defines.events.on_tick, on_tick)
event.add(defines.events.on_entity_died, on_entity_died)
event.add(defines.events.on_player_joined_game, on_player_joined_game)

View File

@ -0,0 +1,147 @@
local map_functions = require "tools.map_functions"
local math_random = math.random
local function get_vector()
if global.current_stage == 1 then return {0, -1} end
if global.current_stage == #global.stages then return {0, 1} end
return {1, 0}
end
local function add_enemies(surface, position)
local radius = global.stages[global.current_stage].size
local amount = math.ceil(((global.current_level * 10) / #global.stages) * global.current_stage)
for a = 1, amount, 1 do
local p = {x = position.x + (radius - math.random(0, radius * 2)), y = position.y + (radius - math.random(0, radius * 2))}
if surface.can_place_entity({name = "small-biter", position = p, force = enemy}) then
surface.create_entity({name = "small-biter", position = p, force = enemy})
global.alive_enemies = global.alive_enemies + 1
end
end
if global.current_stage == #global.stages - 1 then
local unit = surface.create_entity({name = "medium-spitter", position = position, force = enemy})
add_boss_unit(unit, global.current_level * 2, 0.55)
global.alive_enemies = global.alive_enemies + 1
end
end
function draw_the_island()
if not global.stages[global.current_stage].size then global.gamestate = 4 return end
local surface = game.surfaces[1]
local position = global.path_tiles[#global.path_tiles].position
map_functions.draw_noise_tile_circle(position, "grass-2", surface, global.stages[global.current_stage].size)
add_enemies(surface, position)
global.gamestate = 4
end
local draw_path_tile_whitelist = {
["water"] = true,
}
function draw_path_to_next_stage()
local surface = game.surfaces[1]
if global.current_stage ~= #global.stages then
if global.current_stage == #global.stages - 1 then
game.print("--Final Stage--")
else
game.print("--Stage " .. global.current_stage .. "--")
end
end
local position = {x = 0, y = 0}
if global.path_tiles then position = global.path_tiles[#global.path_tiles].position end
--game.print(get_vector()[1] .. " " .. get_vector()[2])
global.path_tiles = noise_vector_tile_path(surface, "grass-1", position, get_vector(), global.stages[global.current_stage].path_length, math.random(2, 5), draw_path_tile_whitelist)
--for _, t in pairs(global.path_tiles) do
-- global.level_tiles[#global.level_tiles + 1] = t
--end
global.gamestate = 3
end
local tile_whitelist = {"grass-1", "grass-2", "grass-3", "grass-4"}
local function get_level_tiles(surface)
global.level_tiles = {}
for chunk in surface.get_chunks() do
if chunk.y < 0 and game.forces.player.is_chunk_charted(surface, chunk) then
for _, tile in pairs(surface.find_tiles_filtered({area = {{chunk.x * 32, chunk.y * 32}, {chunk.x * 32 + 32, chunk.y * 32 + 32}}, name = tile_whitelist})) do
local index = math.abs(tile.position.y)
if not global.level_tiles[index] then global.level_tiles[index] = {} end
global.level_tiles[index][#global.level_tiles[index] + 1] = tile
end
end
end
for k, tile_row in pairs(global.level_tiles) do
table.shuffle_table(global.level_tiles[k])
end
end
local function wipe_vision(surface)
for chunk in surface.get_chunks() do
if chunk.y < 0 then game.forces.player.unchart_chunk(chunk, surface) end
end
end
local particles = {"coal-particle", "copper-ore-particle", "iron-ore-particle", "stone-particle"}
local function create_particles(surface, position)
local particle = particles[math_random(1, #particles)]
local m = math_random(10, 30)
local m2 = m * 0.005
for i = 1, 25, 1 do
surface.create_entity({
name = particle,
position = position,
frame_speed = 0.1,
vertical_speed = 0.1,
height = 0.1,
movement = {m2 - (math_random(0, m) * 0.01), m2 - (math_random(0, m) * 0.01)}
})
end
end
function kill_the_level()
local surface = game.surfaces[1]
if not global.level_tiles then get_level_tiles(surface) end
local amount = global.current_level
for i = #global.level_tiles, 1, -1 do
if global.level_tiles[i] then
for k, tile in pairs(global.level_tiles[i]) do
surface.set_tiles({{name = "water", position = tile.position}}, true)
create_particles(surface, tile.position)
global.level_tiles[i][k] = nil
amount = amount - 1
if amount <= 0 then return end
end
global.level_tiles[i] = nil
end
end
if #global.level_tiles == 0 then
wipe_vision(surface)
global.level_tiles = nil
global.gamestate = 1
end
end
local function process_tile(surface, position)
if position.y < 0 then surface.set_tiles({{name = "water", position = position}}, true) return end
if position.y > 4 then surface.set_tiles({{name = "water-green", position = position}}, true) return end
surface.set_tiles({{name = "sand-1", position = position}}, true)
end
local function on_chunk_generated(event)
local left_top = event.area.left_top
local surface = event.surface
for x = 0, 31, 1 do
for y = 0, 31, 1 do
local position = {x = left_top.x + x, y = left_top.y + y}
process_tile(surface, position)
end
end
end
local event = require 'utils.event'
event.add(defines.events.on_chunk_generated, on_chunk_generated)