diff --git a/maps/planet_prison.lua b/maps/planet_prison.lua index 41e86e0c..a61a6860 100644 --- a/maps/planet_prison.lua +++ b/maps/planet_prison.lua @@ -2,15 +2,17 @@ global.this = {} local _global = require("utils.global") local _evt = require("utils.event") local _map = require("tools.map_functions") +local _timers = require("planet_prison.mod.timers") local _common = require("planet_prison.mod.common") local _layers = require("planet_prison.mod.layers") local _ai = require("planet_prison.mod.ai") local _bp = require("planet_prison.mod.bp") +local _afk = require("planet_prison.mod.afk") global.this._config = require("planet_prison.config") global.this.maps = { { - name = "Flooded metropolia", + name = "flooded-metropolia", height = 2000, width = 2000, water = 1, @@ -47,6 +49,118 @@ global.this.maps = { } } +local function noise_hostile_hook(ent, common) + ent.force = "enemy" + if ent.name == "character" then + local shade = common.rand_range(20, 200) + ent.color = { + r = shade, + g = shade, + b = shade + } + + if common.rand_range(1, 5) == 1 then + ent.insert({name="shotgun", count=1}) + ent.insert({name="shotgun-shell", count=20}) + else + ent.insert({name="pistol", count=1}) + ent.insert({name="firearm-magazine", count=20}) + end + else + ent.insert({name="firearm-magazine", count=200}) + end +end + +local function noise_set_neutral_hook(ent) + ent.force = "neutral" +end + +local industrial_zone_layers = { + { + type = "LuaTile", + name = "concrete", + objects = { + "concrete", + }, + elevation = 0.3, + resolution = 0.2, + hook = nil, + deps = nil, + }, + { + type = "LuaTile", + name = "stones", + objects = { + "stone-path", + }, + elevation = 0.2, + resolution = 0.4, + hook = nil, + deps = nil, + }, + { + type = "LuaTile", + name = "shallows", + objects = { + "water-shallow", + }, + elevation = 0.7, + resolution = 0.01, + hook = nil, + deps = nil, + }, + { + type = "LuaEntity", + name = "scrap", + objects = { + "mineable-wreckage", + }, + elevation = 0.5, + resolution = 0.1, + hook = nil, + deps = nil, + }, + { + type = "LuaEntity", + name = "walls", + objects = { + "stone-wall" + }, + elevation = 0.5, + resolution = 0.09, + hook = noise_set_neutral_hook, + deps = nil, + }, + { + type = "LuaEntity", + name = "hostile", + objects = { + "character", + "gun-turret", + }, + elevation = 0.92, + resolution = 0.99, + hook = noise_hostile_hook, + deps = _common, + }, + { + type = "LuaEntity", + name = "structures", + objects = { + "big-electric-pole", + "medium-electric-pole", + }, + elevation = 0.9, + resolution = 0.9, + hook = noise_set_neutral_hook, + deps = nil, + }, +} + +global.this.presets = { + ["flooded-metropolia"] = industrial_zone_layers, +} + global.this.entities_cache = nil global.this.surface = nil global.this.last_friend = nil @@ -101,26 +215,21 @@ local function init_merchant_bp(entity, _) end end -local function noise_hostile_hook(ent) - ent.force = "enemy" - if ent.name == "character" then - ent.insert({name="pistol", count=1}) - ent.insert({name="firearm-magazine", count=20}) - else - ent.insert({name="firearm-magazine", count=200}) - end -end - -local function noise_set_neutral_hook(ent) - ent.force = "neutral" -end global.this.bp = { player_ship = require("planet_prison.bp.player_ship"), merchant = require("planet_prison.bp.merchant") } local function init_game() - global.this.surface = game.create_surface("arena", pick_map()) + _common.init() + _layers.init() + _bp.init() + _ai.init() + _timers.init() + + local map = pick_map() + local preset = global.this.presets[map.name] + global.this.surface = game.create_surface("arena", map) global.this.surface.min_brightness = 0 global.this.surface.ticks_per_day = 25000 * 4 global.this.perks = {} @@ -131,26 +240,62 @@ local function init_game() game.difficulty_settings.technology_price_multiplier = 0.1 game.difficulty_settings.research_queue_setting = "always" - _layers.init() _layers.set_collision_mask({"water-tile"}) - _layers.add_noise_layer("LuaTile", "concrete", {"concrete"}, 0.3, 0.2) - _layers.add_noise_layer("LuaTile", "stones", {"stone-path"}, 0.2, 0.4) - _layers.add_noise_layer("LuaTile", "shallows", {"water-shallow"}, 0.6, 0.005) - _layers.add_noise_layer("LuaEntity", "scrap", {"mineable-wreckage"}, 0.5, 0.1) - _layers.add_noise_layer("LuaEntity", "walls", {"stone-wall"}, 0.5, 0.09) - _layers.add_noise_layer("LuaEntity", "hostile", {"character", - "gun-turret"}, 0.92, 0.99) - _layers.add_noise_layer("LuaEntity", "structures", {"big-electric-pole", - "medium-electric-pole"}, 0.9, 0.9) - _layers.add_noise_layer_hook("structures", noise_set_neutral_hook) - _layers.add_noise_layer_hook("walls", noise_set_neutral_hook) - _layers.add_noise_layer_hook("hostile", noise_hostile_hook) + + for _, layer in pairs(preset) do + _layers.add_noise_layer(layer.type, layer.name, + layer.objects, layer.elevation, + layer.resolution) + if layer.hook ~= nil then + _layers.add_noise_layer_hook(layer.name, layer.hook) + end + + if layer.deps ~= nil then + _layers.add_noise_layer_dependency(layer.name, layer.deps) + end + end + _bp.push_blueprint("player_ship", global.this.bp.player_ship) _bp.set_blueprint_hook("player_ship", init_player_ship_bp) _bp.push_blueprint("merchant", global.this.bp.merchant) _bp.set_blueprint_hook("merchant", init_merchant_bp) end +local function explode_ship(deps) + local bp = deps.modules.bp + local layers = deps.modules.layers + for _, ent in pairs(bp.reference_get_entities(deps.ship)) do + if not ent.valid then + goto continue + end + + local explosion = { + name = "massive-explosion", + position = ent.position + } + deps.surf.create_entity(explosion) + + ::continue:: + end + + local bb = bp.reference_get_bounding_box(deps.ship) + layers.remove_excluding_bounding_box(bb) + bp.destroy_reference(deps.surf, deps.ship) + rendering.destroy(deps.id) +end + +local function explode_ship_update(left, deps) + local common = deps.modules.common + for _, ent in pairs(deps.ship.entities) do + if not ent.valid then + return false + end + end + + rendering.set_text(deps.id, common.get_time(left)) + return true +end + local function do_spawn_point(player) local point = { x = _common.get_axis(player.position, "x"), @@ -158,44 +303,77 @@ local function do_spawn_point(player) } local instance = _bp.build(player.surface, "player_ship", point, player) _layers.push_excluding_bounding_box(instance.bb) + + local left = global.this._config.self_explode + local object = { + text = _common.get_time(left), + surface = player.surface, + color = { + r = 255, + g = 20, + b = 20 + }, + target = { + x = point.x - 2, + y = point.y - 3, + }, + scale = 2.0 + } + + local entry = { + id = rendering.draw_text(object), + ship = instance, + modules = { + bp = _bp, + layers = _layers, + common = _common, + timers = _timers, + func = _bp.destroy_reference, + }, + surf = player.surface, + } + local timer = _timers.set_timer(left, explode_ship) + _timers.set_timer_on_update(timer, explode_ship_update) + _timers.set_timer_dependency(timer, entry) + _timers.set_timer_start(timer) end local function get_non_obstructed_position(s, radius) - while true do - local chunk = s.get_random_chunk() + local chunk + + for i = 1, 32 do + chunk = s.get_random_chunk() chunk.x = chunk.x * 32 chunk.y = chunk.y * 32 - for x = 1, radius do - for y = 1, radius do - local tile = s.get_tile({chunk.x + x, chunk.y + y}) - if not tile.collides_with("ground-tile") then - goto continue - end + local search_info = { + position = chunk, + radius = radius, + } + + local tiles = s.find_tiles_filtered(search_info) + for _, tile in pairs(tiles) do + if string.find(tile.name, "water") ~= nil + or string.find(tile.name, "out") ~= nil then + goto continue end end - local search_info = { + search_info = { position = chunk, radius = radius, force = {"neutral", "enemy"}, invert = true } local ents = s.find_entities_filtered(search_info) - if not ents then - return chunk - end - - if #ents == 0 then - return chunk - end - - if ents[1].name == "character" then - return chunk + if not ents or #ents == 0 then + break end ::continue:: end + + return chunk end local function redraw_gui(p) @@ -205,7 +383,7 @@ local function redraw_gui(p) local perks = global.this.perks[p.name] local chat_type = "Global chat" if not perks.chat_global then - chat_type = "Buddies chat" + chat_type = "NAP chat" end local button = { @@ -255,9 +433,13 @@ local function on_gui_click(e) local p = game.players[e.player_index] local perks = global.this.perks[p.name] + if not elem.valid then + return + end + if elem.name == "chat_toggle" then if perks.chat_global then - elem.caption = "Buddies chat" + elem.caption = "NAP chat" perks.chat_global = false else elem.caption = "Global chat" @@ -412,16 +594,8 @@ local function cause_event(s) merchant_event(s) end -local function unlink_old_blueprints(name) - local query = { - timestamp = game.tick, - } - - local refs = _bp.unlink_references_filtered(name, query) - for _, ref in pairs(refs) do - local bb = _bp.reference_get_bounding_box(ref) - _layers.remove_excluding_bounding_box(bb) - end +local function kill_player(p) + p.character.die() end local function on_tick() @@ -436,12 +610,16 @@ local function on_tick() _ai.do_job(surf, _ai.command.seek_and_destroy_player) end - if (game.tick + 1) % 500 == 0 then - unlink_old_blueprints("player_ship") - end - _layers.do_job(surf) cause_event(s) + + if (game.tick + 1) % 100 == 0 then + _afk.on_inactive_players(90, kill_player) + end + + if (game.tick + 1) % 60 == 0 then + _timers.do_job() + end end local function make_ore_patch(e) @@ -539,23 +717,23 @@ local function on_player_dropped_item(e) end if p.force.get_cease_fire(peer.name) then - p.print(string.format("The %s is your buddy already", peer.name)) + p.print(string.format("You're in the NAP with %s already", peer.name)) return end if global.this.last_friend[peer.name] == p.name then p.force.set_cease_fire(peer.name, true) peer.force.set_cease_fire(p.name, true) - p.print(string.format("%s is now your buddy", peer.name)) - peer.print(string.format("%s is now your buddy", p.name)) + p.print(string.format("The NAP was formed with %s", peer.name)) + peer.print(string.format("The NAP was formed with %s", p.name)) global.this.last_friend[p.name] = "" global.this.last_friend[peer.name] = "" return end global.this.last_friend[p.name] = peer.name - p.print(string.format("You want %s to be your buddy", peer.name)) - peer.print(string.format("The %s wants to be your buddy", p.name)) + p.print(string.format("You want to form the NAP with %s", peer.name)) + peer.print(string.format("The %s wants to form NAP with you", p.name)) elseif ent.stack.name == "coal" then local ent_list = p.surface.find_entities_filtered({ name = "character", @@ -579,7 +757,7 @@ local function on_player_dropped_item(e) end if not p.force.get_cease_fire(peer.name) then - p.print(string.format("The %s is not your buddy", p.name)) + p.print(string.format("You don't have the NAP with %s", p.name)) return end @@ -588,8 +766,8 @@ local function on_player_dropped_item(e) global.this.last_friend[p.name] = "" global.this.last_friend[peer.name] = "" - p.print(string.format("The %s is no longer your buddy", peer.name)) - peer.print(string.format("The %s is no longer your buddy", p.name)) + p.print(string.format("You're no longer in the NAP with %s", peer.name)) + peer.print(string.format("You're no longer in the NAP with %s", p.name)) end end @@ -616,6 +794,29 @@ local function on_entity_damaged(e) ent.force.set_friend(e.force, false) e.force.set_friend(ent.force, false) end + + if ent.name == "character" then + local hp = 1.0 - ent.get_health_ratio() + local particles = 45 * hp + local coeff = _common.rand_range(-20, 20) / 100.0 + for i = 1, particles do + local blood = { + name = "blood-particle", + position = { + x = ent.position.x, + y = ent.position.y, + }, + movement = { + (_common.rand_range(-20, 20) / 100.0) + coeff, + (_common.rand_range(-20, 20) / 100.0) + coeff, + }, + frame_speed = 0.01, + vertical_speed = 0.02, + height = 0.01, + } + ent.surface.create_entity(blood) + end + end end local function merchant_death(e) @@ -665,6 +866,19 @@ local function hostile_death(e) return true end +local function character_death(e) + local ent = e.entity + if ent.name ~= "character" then + return false + end + + local explosion = { + name = "blood-explosion-big", + position = ent.position, + } + ent.surface.create_entity(explosion) +end + local function on_entity_died(e) if not e.entity.valid then return @@ -675,6 +889,7 @@ local function on_entity_died(e) end hostile_death(e) + character_death(e) end @@ -748,7 +963,7 @@ local function create_console_message(p, message) if global.this.perks[p.name].chat_global then msg_fmt = "[color=red]global:[/color] %s %s" else - msg_fmt = "[color=green]buddies:[/color] %s %s" + msg_fmt = "[color=green]nap:[/color] %s %s" end return string.format(msg_fmt, prefix, p_msg) diff --git a/maps/planet_prison/config.lua b/maps/planet_prison/config.lua index 456fade2..b5e57032 100644 --- a/maps/planet_prison/config.lua +++ b/maps/planet_prison/config.lua @@ -2,8 +2,12 @@ local public = {} public.player_ship_loot = { { - name = "firearm-magazine", - count = 60, + name = "piercing-rounds-magazine", + count = 35, + }, + { + name = "grenade", + count = 2, }, { name = "submachine-gun", @@ -31,6 +35,8 @@ public.player_ship_loot = { } } +public.self_explode = 60 * 60 * 10 + public.wreck_loot = { ["iron-plate"] = { rare = 0.1, @@ -104,13 +110,6 @@ public.technologies = { ["explosive-rocketry"] = false, ["flamethrower"] = false, ["fusion-reactor-equipment"] = false, - ["inserter-capacity-bonus-1"] = false, - ["inserter-capacity-bonus-2"] = false, - ["inserter-capacity-bonus-3"] = false, - ["inserter-capacity-bonus-4"] = false, - ["inserter-capacity-bonus-5"] = false, - ["inserter-capacity-bonus-6"] = false, - ["inserter-capacity-bonus-7"] = false, ["kovarex-enrichment-process"] = false, ["land-mine"] = false, ["logistics-3"] = false, @@ -140,6 +139,27 @@ public.technologies = { ["stronger-explosives-5"] = false, ["stronger-explosives-6"] = false, ["stronger-explosives-7"] = false, + ["physical-projectile-damage-4"] = false, + ["physical-projectile-damage-5"] = false, + ["physical-projectile-damage-6"] = false, + ["physical-projectile-damage-7"] = false, + ["weapon-shooting-speed-4"] = false, + ["weapon-shooting-speed-5"] = false, + ["weapon-shooting-speed-6"] = false, + ["energy-weapons-damage-1"] = false, + ["energy-weapons-damage-2"] = false, + ["energy-weapons-damage-3"] = false, + ["energy-weapons-damage-4"] = false, + ["energy-weapons-damage-5"] = false, + ["energy-weapons-damage-6"] = false, + ["energy-weapons-damage-7"] = false, + ["laser-turret-speed-1"] = false, + ["laser-turret-speed-2"] = false, + ["laser-turret-speed-3"] = false, + ["laser-turret-speed-4"] = false, + ["laser-turret-speed-5"] = false, + ["laser-turret-speed-6"] = false, + ["laser-turret-speed-7"] = false, ["tanks"] = false, ["uranium-ammo"] = false, ["uranium-processing"] = false, @@ -190,12 +210,12 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 500 + amount = 100 } }, offer = { - type = "unlock-recipe", - recipe = "modular-armor" + type = "give-item", + item = "heavy-armor" } }, { @@ -203,7 +223,20 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 5000 + amount = 500 + } + }, + offer = { + type = "give-item", + item = "modular-armor" + } + }, + { + price = { + { + type = "item", + name = "coin", + amount = 12000 } }, offer = { @@ -211,19 +244,6 @@ public.merchant_offer = { item = "power-armor" } }, - { - price = { - { - type = "item", - name = "coin", - amount = 10000 - } - }, - offer = { - type = "give-item", - item = "power-armor-mk2" - } - }, { price = { { @@ -268,7 +288,7 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 300 + amount = 700 } }, offer = { @@ -289,19 +309,6 @@ public.merchant_offer = { item = "fusion-reactor-equipment" } }, - { - price = { - { - type = "item", - name = "coin", - amount = 1000 - } - }, - offer = { - type = "give-item", - item = "personal-laser-defense-equipment" - } - }, { price = { { @@ -346,7 +353,7 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 100 + amount = 300 } }, offer = { @@ -359,7 +366,7 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 400 + amount = 2000 } }, offer = { @@ -372,7 +379,7 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 20 + amount = 50 } }, offer = { @@ -380,6 +387,32 @@ public.merchant_offer = { item = "flamethrower-ammo" } }, + { + price = { + { + type = "item", + name = "coin", + amount = 1 + } + }, + offer = { + type = "give-item", + item = "firearm-magazine" + } + }, + { + price = { + { + type = "item", + name = "coin", + amount = 5 + } + }, + offer = { + type = "give-item", + item = "piercing-rounds-magazine" + } + }, { price = { { @@ -398,7 +431,7 @@ public.merchant_offer = { { type = "item", name = "coin", - amount = 100 + amount = 50 } }, offer = { @@ -487,7 +520,7 @@ public.merchant_offer = { } public.manual = [[ -[font=heading-1]Planet Prison (1.0.3) - Manual[/font] +[font=heading-1]Planet Prison (1.0.4) - Manual[/font] [font=default-bold]You did naughty things and was sent to this planet with a one way ticket. Once an industrial site, turned into non-hospitable planet due to pollution and war. Among other inmates, there are still bandits scavenging through the junk looking for rare items. This is an ultimate survival scenario with very hostile environment. @@ -498,9 +531,9 @@ This is an ultimate survival scenario with very hostile environment. - Flee by a rocket is a win. (Put a car into a rocket and enter the rocket). - The light is your best friend. [/font] -[font=heading-1]Buddies[/font] -[font=default-bold]Grab a raw fish [img=item/raw-fish] and drop it on someone with [virtual-signal=signal-Z] button (in default setting). This way you request an inmate to become your buddy.[/font] -[font=default-bold]Grab a coal piece [img=item/coal] and drop it on someone with [virtual-signal=signal-Z] button (in default setting). This way you discard buddy status with an inname.[/font] +[font=heading-1]NAP contractors (A non-aggression pact)[/font] +[font=default-bold]Grab a raw fish [img=item/raw-fish] and drop it on someone with [virtual-signal=signal-Z] button (in default setting). This way you request an inmate to get in NAP with you.[/font] +[font=default-bold]Grab a coal piece [img=item/coal] and drop it on someone with [virtual-signal=signal-Z] button (in default setting). This way you discard NAP with an inname.[/font] [font=heading-1]Coins[/font] [font=default-bold]Coins [img=item/coin] are the main medium that you use in the market. You obtain them by researching stuff and pvp activites.[/font] diff --git a/maps/planet_prison/mod/afk.lua b/maps/planet_prison/mod/afk.lua new file mode 100644 index 00000000..e3ee2629 --- /dev/null +++ b/maps/planet_prison/mod/afk.lua @@ -0,0 +1,19 @@ +local public = {} +local _evt = require("utils.event") + +--[[ +on_inactive_players - Performs operation on inactive players from the game +if they exceed time. +@param time - Maximum time a player can be inactive. +@param func - Callback that will be called. +--]] +public.on_inactive_players = function(time, func) + for _, p in pairs(game.connected_players) do + local afk = p.afk_time / 60 / 60 + if afk >= time then + func(p) + end + end +end + +return public diff --git a/maps/planet_prison/mod/ai.lua b/maps/planet_prison/mod/ai.lua index 24e8ce6f..a5789aca 100644 --- a/maps/planet_prison/mod/ai.lua +++ b/maps/planet_prison/mod/ai.lua @@ -1,9 +1,6 @@ -local public, this = {}, {} -local _global = require("utils.global") +local public = {} local _common = require(".common") -_global.register(this, function(t) this = t end) - public.command = { --[[ @param args nil @@ -16,6 +13,15 @@ public.command = { seek_and_destroy_player = 1, } +--[[ +init - Initialize the module. +--]] +public.init = function() + if global.this == nil then + global.this = {} + end +end + local function _get_direction(src, dest) local src_x = _common.get_axis(src, "x") local src_y = _common.get_axis(src, "y") diff --git a/maps/planet_prison/mod/bp.lua b/maps/planet_prison/mod/bp.lua index 85869784..8c7d498d 100644 --- a/maps/planet_prison/mod/bp.lua +++ b/maps/planet_prison/mod/bp.lua @@ -1,9 +1,13 @@ -local this, public = {}, {} -local _global = require("utils.global") +local public = {} local _common = require(".common") -_global.register(this, function(t) this = t end) -this._bps = {} +public.init = function() + if global.this == nil then + global.this = {} + end + + global.this._bps = {} +end --[[ push_blueprint - Pushes blueprint into a list. @@ -16,7 +20,7 @@ public.push_blueprint = function(name, bp) hook = nil, refs = {} } - this._bps[name] = entry + global.this._bps[name] = entry end --[[ @@ -30,12 +34,12 @@ public.set_blueprint_hook = function(name, hook) return end - if this._bps[name] == nil then + if global.this._bps[name] == nil then log("bp.set_blueprint_hook: unrecognized blueprint") return end - this._bps[name].hook = hook + global.this._bps[name].hook = hook end --[[ @@ -48,7 +52,7 @@ public.get_references = function(name) return {} end - local object = this._bps[name] + local object = global.this._bps[name] if object == nil then log("bp.get_references: unrecognized blueprint") return {} @@ -67,7 +71,7 @@ public.get_references = function(name) return end - local object = this._bps[name] + local object = global.this._bps[name] if object == nil then log("bp.get_references: unrecognized blueprint") return @@ -116,7 +120,7 @@ public.unlink_references_filtered = function(name, query) return end - local object = this._bps[name] + local object = global.this._bps[name] if object == nil then log("bp.get_references: unrecognized blueprint") return @@ -154,7 +158,7 @@ public.destroy_references_filtered = function(surf, name, query) return end - local object = this._bps[name] + local object = global.this._bps[name] if object == nil then log("bp.get_references: unrecognized blueprint") return @@ -196,6 +200,45 @@ public.destroy_references = function(surf, name) public.destroy_references_filtered(surf, name, {}) end +global._bp_destroy_reference = function(surf, ref) + for _, ent in pairs(ref.entities) do + if ent.valid then + ent.destroy() + end + end + + local tiles = {} + for _, tile in pairs(ref.tiles) do + if tile.valid then + goto continue + end + + tile.name = "concrete" + table.insert(tiles, tile) + ::continue:: + end + + surf.set_tiles(tiles) +end + +--[[ +destroy_reference - Destroys reference of a blueprint at given surface. +@param surf - Surface on which blueprints are placed. +@param reference - Any valid reference. +--]] +public.destroy_reference = function(surf, reference) + for _, meta in pairs(global.this._bps) do + for i = 1, #meta.refs do + local ref = meta.refs[i] + if reference.id == ref.id then + global._bp_destroy_reference(surf, ref) + table.remove(meta.refs, i) + return + end + end + end +end + local function _build_tiles(surf, point, tiles) local _tiles = {} @@ -262,7 +305,7 @@ public.build = function(surf, name, point, args) return end - local object = this._bps[name] + local object = global.this._bps[name] if object == nil then log("bp.set_blueprint_hook: unrecognized blueprint") return @@ -321,6 +364,7 @@ public.build = function(surf, name, point, args) end instance.bb = _common.merge_bounding_boxes(bbs) + instance.id = game.tick table.insert(object.refs, instance) return instance diff --git a/maps/planet_prison/mod/common.lua b/maps/planet_prison/mod/common.lua index 7c5c3d98..879e92dd 100644 --- a/maps/planet_prison/mod/common.lua +++ b/maps/planet_prison/mod/common.lua @@ -1,6 +1,10 @@ -local public, this = {}, {} -local _global = require("utils.global") -_global.register(this, function(t) this = t end) +local public = {} + +public.init = function() + if global.this == nil then + global.this = {} + end +end --[[ rand_range - Return random integer within the range. @@ -8,11 +12,11 @@ rand_range - Return random integer within the range. @param stop - Stop range. --]] public.rand_range = function(start, stop) - if not this.rng then - this.rng = game.create_random_generator() + if not global.this.rng then + global.this.rng = game.create_random_generator() end - return this.rng(start, stop) + return global.this.rng(start, stop) end --[[ @@ -264,4 +268,25 @@ public.merge_bounding_boxes = function(bbs) return box end +--[[ +get_time - Return strigified time of a tick. +@param ticks - Just a ticks. +--]] +public.get_time = function(ticks) + local seconds = math.floor((ticks / 60) % 60) + local minutes = math.floor((ticks / 60 / 60) % 60) + local hours = math.floor(ticks / 60 / 60 / 60) + + local time + if hours > 0 then + time = string.format("%02d:%01d:%02d", hours, minutes, seconds) + elseif minutes > 0 then + time = string.format("%02d:%02d", minutes, seconds) + else + time = string.format("00:%02d", seconds) + end + + return time +end + return public diff --git a/maps/planet_prison/mod/layers.lua b/maps/planet_prison/mod/layers.lua index f1157e3d..ca6e9935 100644 --- a/maps/planet_prison/mod/layers.lua +++ b/maps/planet_prison/mod/layers.lua @@ -1,16 +1,16 @@ local public = {} -global.this = {} -local _global = require("utils.global") local _common = require(".common") local _simplex = require(".simplex_noise") -_global.register(global.this, function(t) global.this = t end) -global.this._grid = {} -global.this._exclusions = {} -global.this._layers = {} -global.this._collision_mask = {} - public.init = function() + if global.this == nil then + global.this = {} + end + + global.this._grid = {} + global.this._exclusions = {} + global.this._layers = {} + global.this._collision_mask = {} _simplex.init() end --[[ @@ -60,6 +60,7 @@ public.add_noise_layer = function(type, name, objects, elevation, resolution) resolution = resolution, cache = {}, hook = nil, + deps = nil, } table.insert(global.this._layers, layer) @@ -79,6 +80,20 @@ public.add_noise_layer_hook = function(name, hook) end end +--[[ +add_noise_layer_dependency - Adds dependency to the layer. It can be any +lua variable. This dependency then is injected into hook. +@param deps - Dependencies, any variable. +--]] +public.add_noise_layer_dependency = function(name, deps) + for _, layer in pairs(global.this._layers) do + if layer.name == name then + layer.deps = deps + break + end + end +end + --[[ set_collision_mask - Set which tiles should be ignored. @param mask - Table of collision masks. @@ -93,6 +108,7 @@ end local function _do_job_entity(surf, layer) local hook = layer.hook + local deps = layer.deps for _, object in pairs(layer.cache) do if object.name == "character" or object.name == "gun-turret" then if not surf.can_place_entity(object) then @@ -106,7 +122,7 @@ local function _do_job_entity(surf, layer) end if hook then - hook(ent) + hook(ent, deps) end ::continue:: diff --git a/maps/planet_prison/mod/timers.lua b/maps/planet_prison/mod/timers.lua new file mode 100644 index 00000000..745fdb0a --- /dev/null +++ b/maps/planet_prison/mod/timers.lua @@ -0,0 +1,98 @@ +local public = {} +local _global = require('utils.global') + +public.init = function() + if global.this == nil then + global.this = {} + end + + global.this.timers = {} +end + +--[[ +set_timer - Sets a timer. +@param left - Time left on the timer in ticks. +@param hook - Action executed after timer is elapsed. +--]] +public.set_timer = function(left, hook) + local id = game.tick + local entry = { + left = left, + hook_finish = hook, + hook_update = nil, + deps = nil, + running = false, + last_update = 0, + } + + global.this.timers[id] = entry + return id +end + +--[[ +set_timer_on_update - Adds a hook that is executed everytime a +timers is updated. +@param id - Id of the timer. +@param hook - Hook that will be executed per update. +--]] +public.set_timer_on_update = function(id, hook) + global.this.timers[id].hook_update = hook +end + +--[[ +set_timer_dependency - Adds dependency into user callback. +@param id - Id of the timer, +@param deps - Dependency of timer to add. +--]] +public.set_timer_dependency = function(id, deps) + global.this.timers[id].deps = deps +end + +--[[ +set_timer_start - Sets the timer to run. +@param id - Id of a timer. +--]] +public.set_timer_start = function(id) + global.this.timers[id].running = true + global.this.timers[id].last_update = game.tick +end + +--[[ +kill_timer - Effectivly kills the timer. +@param id - Timer id. +--]] +public.kill_timer = function(id) + global.this.timers[id] = nil +end + +--[[ +do_job - Execute timer logic within a tick. +--]] +public.do_job = function() + for id, entry in pairs(global.this.timers) do + if entry.running == false then + goto continue + end + + entry.left = entry.left - (game.tick - entry.last_update) + if entry.left > 0 then + entry.last_update = game.tick + + if entry.hook_update ~= nil then + if not entry.hook_update(entry.left, entry.deps) then + goto premature_finish + end + end + + goto continue + end + + ::premature_finish:: + entry.hook_finish(entry.deps) + global.this.timers[id] = nil + + ::continue:: + end +end + +return public