From 185b21c7a50946821cecafdf8b3d04b5e139b417 Mon Sep 17 00:00:00 2001 From: MewMew Date: Fri, 20 Sep 2019 15:59:43 +0200 Subject: [PATCH] new module --- maps/island_troopers/main.lua | 1 + maps/island_troopers/terrain.lua | 21 ++++++- modules/shopping_chests.lua | 105 +++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 modules/shopping_chests.lua diff --git a/maps/island_troopers/main.lua b/maps/island_troopers/main.lua index 3985072a..f842a2f2 100644 --- a/maps/island_troopers/main.lua +++ b/maps/island_troopers/main.lua @@ -1,6 +1,7 @@ require "functions.noise_vector_path" require "functions.boss_unit" require "maps.island_troopers.terrain" +require "modules.shopping_chests" local function set_next_level() global.alive_enemies = 0 diff --git a/maps/island_troopers/terrain.lua b/maps/island_troopers/terrain.lua index a70110ea..550912fd 100644 --- a/maps/island_troopers/terrain.lua +++ b/maps/island_troopers/terrain.lua @@ -4,7 +4,16 @@ 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} + local position = global.path_tiles[#global.path_tiles].position + local island_size = global.stages[global.current_stage].size + local y_modifier = 0 + if math.abs(position.y) < 32 + island_size * 3 then + y_modifier = math.random(50, 100) * -0.01 + else + y_modifier = math.random(50, 100) * 0.01 + end + game.print(y_modifier) + return {1, y_modifier} end local function add_enemies(surface, position) @@ -54,6 +63,12 @@ function draw_path_to_next_stage() --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) + if global.current_stage ~= #global.stages and global.current_stage > 1 then + if math_random(1, 3) == 1 then + noise_vector_tile_path(surface, "grass-1", position, {0, 1}, global.stages[#global.stages].path_length, math.random(2, 5), draw_path_tile_whitelist) + end + end + --for _, t in pairs(global.path_tiles) do -- global.level_tiles[#global.level_tiles + 1] = t --end @@ -89,7 +104,7 @@ 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 + for i = 1, 8, 1 do surface.create_entity({ name = particle, position = position, @@ -105,7 +120,7 @@ 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 + local amount = global.current_level * 2 for i = #global.level_tiles, 1, -1 do if global.level_tiles[i] then for k, tile in pairs(global.level_tiles[i]) do diff --git a/modules/shopping_chests.lua b/modules/shopping_chests.lua new file mode 100644 index 00000000..fe6fa5ce --- /dev/null +++ b/modules/shopping_chests.lua @@ -0,0 +1,105 @@ +local shop_list = { + ["iron-ore"] = 1, + ["copper-ore"] = 1, + ["uranium-ore"] = 3, + ["coal"] = 1, + ["stone"] = 1, + ["wood"] = 0.75, + ["crude-oil-barrel"] = 6, + ["empty-barrel"] = 5, + ["landfill"] = 2.5, +} + +function create_shopping_chest(surface, position, destructible) + global.shopping_chests[#global.shopping_chests + 1] = surface.create_entity({name = "logistic-chest-requester", position = position, force = "shopping_chests"}) + global.shopping_chests[#global.shopping_chests].minable = false + if not destructible then global.shopping_chests[#global.shopping_chests].destructible = false end +end + +function create_dump_chest(surface, position, destructible) + global.dump_chests[#global.dump_chests + 1] = surface.create_entity({name = "logistic-chest-passive-provider", position = position, force = "shopping_chests"}) + global.dump_chests[#global.dump_chests].minable = false + if not destructible then global.dump_chests[#global.dump_chests].destructible = false end +end + +local function get_affordable_item_count(name, count) + if global.credits >= count * shop_list[name] then + return count + end + count = math.floor(global.credits / shop_list[name]) + return count +end + +local function process_shopping_chest(k, chest) + if not chest.valid then global.shopping_chests[k] = nil return end + if global.credits <= 0 then return end + local requested_item_stack = chest.get_request_slot(1) + if not requested_item_stack then return end + if not shop_list[requested_item_stack.name] then + chest.surface.create_entity({name = "flying-text", position = {chest.position.x - 2, chest.position.y}, text = requested_item_stack.name .. " is not for sale", color = {r = 200, g = 160, b = 30}}) + return + end + local inventory = chest.get_inventory(defines.inventory.chest) + --if not inventory.can_insert(requested_item_stack) then return end + local current_count = inventory.get_item_count(requested_item_stack.name) + if current_count >= requested_item_stack.count then return end + local count = requested_item_stack.count - current_count + count = get_affordable_item_count(requested_item_stack.name, count) + if count < 1 then return end + local inserted_amount = inventory.insert({name = requested_item_stack.name, count = count}) + if inserted_amount == 0 then return end + local spent_credits = inserted_amount * shop_list[requested_item_stack.name] + global.credits = global.credits - spent_credits + chest.surface.create_entity({name = "flying-text", position = chest.position, text = "-" .. spent_credits .. " ø", color = {r = 200, g = 160, b = 30}}) +end + +local function process_dump_chest(k, chest) + if not chest.valid then global.dump_chests[k] = nil return end + local inventory = chest.get_inventory(defines.inventory.chest) + if inventory.is_empty() then return end + for k, price in pairs(shop_list) do + local removed = inventory.remove(k) + global.credits = global.credits + (removed * shop_list[k]) + end +end + +local function gui() + for _, player in pairs(game.connected_players) do + if player.gui.top.credits_button then player.gui.top.credits_button.destroy() end + local element = player.gui.top.add({type = "frame", name = "credits_button", caption = global.credits .. " ø", tooltip = "Credits of the factory."}) + local style = element.style + style.minimal_height = 38 + style.maximal_height = 38 + style.minimal_width = 140 + style.top_padding = 2 + style.left_padding = 4 + style.right_padding = 4 + style.bottom_padding = 2 + style.font_color = {r = 155, g = 85, b = 25} + style.font = "default-large-bold" + end +end + +local function tick() + for k, chest in pairs(global.shopping_chests) do + process_shopping_chest(k, chest) + end + for k, chest in pairs(global.dump_chests) do + process_dump_chest(k, chest) + end + gui() + global.credits = global.credits + math.random(1,5) +end + +local function on_init() + global.shopping_chests = {} + global.dump_chests = {} + global.credits = 0 + game.create_force("shopping_chests") + game.forces.player.set_friend("shopping_chests", true) + game.forces.shopping_chests.set_friend("player", true) +end + +local event = require 'utils.event' +event.on_nth_tick(120, tick) +event.on_init(on_init) \ No newline at end of file