diff --git a/control.lua b/control.lua index 15436ec5..e66122ee 100644 --- a/control.lua +++ b/control.lua @@ -107,6 +107,9 @@ require 'modules.autostash' --![[East VS West Survival PVP, where you breed biters with science flasks]]-- --require 'maps.biter_hatchery.main' +--![[Fight in a world where everyone are prisoners]] +--require 'maps.planet_prison' + --![[Chop trees to gain resources]]-- --require 'maps.choppy' --require 'maps.choppy_dx' @@ -201,7 +204,6 @@ require 'modules.autostash' --require 'maps.wave_defense' --require 'maps.crossing' --require 'maps.anarchy' ---require 'maps.planet_prison' --require 'maps.blue_beach' --require 'maps.nightfall' --require 'maps.pitch_black.main' diff --git a/maps/planet_prison.lua b/maps/planet_prison.lua index 187b63de..51ca29b6 100644 --- a/maps/planet_prison.lua +++ b/maps/planet_prison.lua @@ -1,1436 +1,1441 @@ -global.this = {} -local _global = require("utils.global") -local _evt = require("utils.event") -local _server = require("utils.server") -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") -local _claims = require("planet_prison.mod.claims") -global.this._config = require("planet_prison.config") +local Global = require('utils.global') +local Event = require('utils.event') +local Server = require('utils.server') +local MapFuntions = require('tools.map_functions') +local TimersFunctions = require('planet_prison.mod.timers') +local CommonFunctions = require('planet_prison.mod.common') +local LayersFunctions = require('planet_prison.mod.layers') +local AIFunctions = require('planet_prison.mod.ai') +local Blueprints = require('planet_prison.mod.bp') +local AfkFunctions = require('planet_prison.mod.afk') +local ClaimsFunctions = require('planet_prison.mod.claims') +local MapConfig = require('planet_prison.config') +local Token = require 'utils.token' +local Task = require 'utils.task' -global.this.maps = { - { - name = "flooded-metropolia", - height = 2000, - width = 2000, - water = 1, - terrain_segmentation = 8, - property_expression_names = { - moisture = 0, - temperature = 30. - }, - cliff_settings = { - richness = 0, - }, - starting_area = "none", - autoplace_controls = { - ["iron-ore"] = { - frequency = 0, - }, - ["copper-ore"] = { - frequency = 0, - }, - ["uranium-ore"] = { - frequency = 0, - }, - ["stone"] = { - frequency = 0, - }, - ["coal"] = { - frequency = 0, - }, - ["crude-oil"] = { - frequency = 1000, - size = 1, - }, - ["trees"] = { - frequency = 4, - }, - ["enemy-base"] = { - frequency = 0, - } - }, - } +local this = {} +local Public = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) + +this.maps = { + { + name = 'flooded-metropolia', + height = 2000, + width = 2000, + water = 1, + terrain_segmentation = 8, + property_expression_names = { + moisture = 0, + temperature = 30. + }, + cliff_settings = { + richness = 0 + }, + starting_area = 'none', + autoplace_controls = { + ['iron-ore'] = { + frequency = 0 + }, + ['copper-ore'] = { + frequency = 0 + }, + ['uranium-ore'] = { + frequency = 0 + }, + ['stone'] = { + frequency = 0 + }, + ['coal'] = { + frequency = 0 + }, + ['crude-oil'] = { + frequency = 1000, + size = 1 + }, + ['trees'] = { + frequency = 4 + }, + ['enemy-base'] = { + frequency = 0 + } + } + } } -global.this.assign_camouflage = function(ent, common) - local shade = common.rand_range(20, 200) - ent.color = { - r = shade, - g = shade, - b = shade - } - ent.disable_flashlight() +local assign_camouflage = function(ent, common) + local shade = common.rand_range(20, 200) + ent.color = { + r = shade, + g = shade, + b = shade + } + ent.disable_flashlight() end -local function noise_hostile_hook(ent, common) - ent.force = "enemy" - if ent.name == "character" then - global.this.assign_camouflage(ent, common) +local set_noise_hostile_hook = + Token.register( + function(data) + local ent = data + if not ent or not ent.valid then + return + end + ent.force = 'enemy' + if ent.name == 'character' then + assign_camouflage(ent, CommonFunctions) - 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 + if CommonFunctions.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 set_neutral_to_entity = + Token.register( + function(data) + local entity = data.entity + entity.force = 'neutral' + end +) + +local fetch_common = + Token.register( + function() + return CommonFunctions + 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, - }, + { + 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 = { + 'crash-site-spaceship-wreck-medium-1' + }, + elevation = 0.5, + resolution = 0.1, + hook = nil, + deps = nil + }, + { + type = 'LuaEntity', + name = 'walls', + objects = { + 'stone-wall' + }, + elevation = 0.5, + resolution = 0.09, + hook = set_neutral_to_entity, + deps = nil + }, + { + type = 'LuaEntity', + name = 'hostile', + objects = { + 'character', + 'gun-turret' + }, + elevation = 0.92, + resolution = 0.99, + hook = set_noise_hostile_hook, + deps = fetch_common + }, + { + type = 'LuaEntity', + name = 'structures', + objects = { + 'big-electric-pole', + 'medium-electric-pole' + }, + elevation = 0.9, + resolution = 0.9, + hook = set_neutral_to_entity, + deps = nil + } } -global.this.presets = { - ["flooded-metropolia"] = industrial_zone_layers, +this.presets = { + ['flooded-metropolia'] = industrial_zone_layers } -global.this.entities_cache = nil -global.this.surface = nil -global.this.last_friend = nil +this.entities_cache = nil +this.surface = nil +this.last_friend = nil local function pick_map() - return global.this.maps[_common.rand_range(1, #global.this.maps)] + return this.maps[CommonFunctions.rand_range(1, #this.maps)] end local function find_force(name) - for _, f in pairs(game.forces) do - if f.name == name then - return f - end - end + for _, f in pairs(game.forces) do + if f.name == name then + return f + end + end - return nil + return nil end local function init_player_ship_bp(entity, player) - entity.force = player.force - if entity.name == "crash-site-chest-1" then - for _, stack in pairs(global.this._config.player_ship_loot) do - entity.insert(stack) - end - end - - if entity.name == "crash-site-generator" then - entity.electric_buffer_size = 2000 - entity.power_production = 2000 - end + entity.force = player.force + if entity.name == 'crash-site-chest-1' then + for _, stack in pairs(MapConfig.player_ship_loot) do + entity.insert(stack) + end + end end -global.this.events = { - merchant = { - alive = false, - moving = false, - spawn_tick = 0, - embark_tick = 0, - position = { x = 0, y = 0 }, - offer = global.this._config.merchant_offer, - } +this.events = { + merchant = { + alive = false, + moving = false, + spawn_tick = 0, + embark_tick = 0, + position = {x = 0, y = 0}, + offer = MapConfig.merchant_offer + } } local function init_merchant_bp(entity, _) - entity.force = "merchant" - entity.rotatable = false - entity.minable = false - if entity.name ~= "market" then - entity.operable = false - else - for _, entry in pairs(global.this.events.merchant.offer) do - entity.add_market_item(entry) - end - end + entity.force = 'merchant' + entity.rotatable = false + entity.minable = false + if entity.name ~= 'market' then + entity.operable = false + else + for _, entry in pairs(this.events.merchant.offer) do + entity.add_market_item(entry) + end + end end local function create_orbit_group() - local orbit = game.permissions.create_group("orbit") - for _, perm in pairs(global.this._config.permission_orbit) do - orbit.set_allows_action(perm, false) - end + local orbit = game.permissions.create_group('orbit') + for _, perm in pairs(MapConfig.permission_orbit) do + orbit.set_allows_action(perm, false) + end end -global.this.bp = { - player_ship = require("planet_prison.bp.player_ship"), - merchant = require("planet_prison.bp.merchant") +this.bp = { + player_ship = require('planet_prison.bp.player_ship'), + merchant = require('planet_prison.bp.merchant') } local function init_game() - _common.init() - _layers.init() - _bp.init() - _ai.init() - _timers.init() - _claims.init(global.this._config.claim_markers, - global.this._config.claim_max_distance) + CommonFunctions.init() + LayersFunctions.init() + Blueprints.init() + AIFunctions.init() + TimersFunctions.init() + ClaimsFunctions.init(MapConfig.claim_markers, MapConfig.claim_max_distance) - local map = pick_map() - local preset = global.this.presets[map.name] - global.this.surface = game.create_surface("arena", map) - global.this.surface.brightness_visual_weights = { - 1 / 0.85, - 1 / 0.85, - 1 / 0.85 - } - global.this.surface.ticks_per_day = 25000 * 4 - global.this.perks = {} - global.this.events.merchant.spawn_tick = game.tick + 5000 - global.this.events.raid_groups = {} - global.this.events.raid_init = false - global.this.events.annihilation = false - global.this.events.reset_time = nil + local map = pick_map() + local preset = this.presets[map.name] + this.surface = game.create_surface('arena', map) + this.surface.brightness_visual_weights = { + 1 / 0.85, + 1 / 0.85, + 1 / 0.85 + } + this.surface.ticks_per_day = 25000 * 4 + this.perks = {} + this.events.merchant.spawn_tick = game.tick + 5000 + this.events.raid_groups = {} + this.events.raid_init = false + this.events.annihilation = false + this.events.reset_time = nil - create_orbit_group() - game.map_settings.pollution.enabled = false - game.map_settings.enemy_evolution.enabled = false - game.difficulty_settings.technology_price_multiplier = 0.1 - game.difficulty_settings.research_queue_setting = "always" + create_orbit_group() + game.map_settings.pollution.enabled = false + game.map_settings.enemy_evolution.enabled = false + game.difficulty_settings.technology_price_multiplier = 0.1 + game.difficulty_settings.research_queue_setting = 'always' - _layers.set_collision_mask({"water-tile"}) + LayersFunctions.set_collision_mask({'water-tile'}) - 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 + for _, layer in pairs(preset) do + local token = Token.register(layer.hook) + LayersFunctions.add_noise_layer(layer.type, layer.name, layer.objects, layer.elevation, layer.resolution) + if layer.hook ~= nil then + LayersFunctions.add_noise_layer_hook(layer.name, token) + end - if layer.deps ~= nil then - _layers.add_noise_layer_dependency(layer.name, layer.deps) - end - end + local token2 = Token.register(layer.deps) - _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) + if layer.deps ~= nil then + LayersFunctions.add_noise_layer_dependency(layer.name, token2) + end + end + + Blueprints.push_blueprint('player_ship', this.bp.player_ship) + Blueprints.set_blueprint_hook('player_ship', init_player_ship_bp) + Blueprints.push_blueprint('merchant', this.bp.merchant) + Blueprints.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 explode_ship_update = + Token.register( + function(data) + local ship = data.ship + local id = data.id + local time_left = data.time_left + for _, ent in pairs(ship.entities) do + if not ent.valid then + return false + end + end - local explosion = { - name = "massive-explosion", - position = ent.position - } - deps.surf.create_entity(explosion) + rendering.set_text(id, CommonFunctions.get_time(time_left)) + return true + end +) - ::continue:: - end +local explode_ship = + Token.register( + function(data) + local ship = data.ship + local id = data.id + local surface = data.surface + local time_left = data.time_left + for _, ent in pairs(Blueprints.reference_get_entities(ship)) do + if not ent.valid then + goto 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 explosion = { + name = 'massive-explosion', + position = ent.position + } + surface.create_entity(explosion) -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 + ::continue:: + end - rendering.set_text(deps.id, common.get_time(left)) - return true -end + local bb = Blueprints.reference_get_bounding_box(ship) + LayersFunctions.remove_excluding_bounding_box(bb) + Blueprints.destroy_reference(surface, ship) + rendering.destroy(id) + Task.set_timeout_in_ticks(60, explode_ship_update, {time_left = time_left, ship = ship}) + Task.start_queue() + end +) local function do_spawn_point(player) - local point = { - x = _common.get_axis(player.position, "x"), - y = _common.get_axis(player.position, "y") - 2 - } - local instance = _bp.build(player.surface, "player_ship", point, player) - _layers.push_excluding_bounding_box(instance.bb) + local point = { + x = CommonFunctions.get_axis(player.position, 'x'), + y = CommonFunctions.get_axis(player.position, 'y') - 2 + } + local instance = Blueprints.build(player.surface, 'player_ship', point, player) + LayersFunctions.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 time_left = MapConfig.self_explode + local object = { + text = CommonFunctions.get_time(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) + Task.set_timeout_in_ticks(60, explode_ship, {time_left = time_left, ship = instance, id = rendering.draw_text(object), surface = player.surface}) end local function get_non_obstructed_position(s, radius) - local chunk + local chunk - for i = 1, 32 do - chunk = s.get_random_chunk() - chunk.x = chunk.x * 32 - chunk.y = chunk.y * 32 + for i = 1, 32 do + chunk = s.get_random_chunk() + chunk.x = chunk.x * 32 + chunk.y = chunk.y * 32 - local search_info = { - position = chunk, - radius = radius, - } + 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 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 - search_info = { - position = chunk, - radius = radius, - force = {"neutral", "enemy"}, - invert = true - } - local ents = s.find_entities_filtered(search_info) - if not ents or #ents == 0 then - break - end + search_info = { + position = chunk, + radius = radius, + force = {'neutral', 'enemy'}, + invert = true + } + local ents = s.find_entities_filtered(search_info) + if not ents or #ents == 0 then + break + end - ::continue:: - end + ::continue:: + end - return chunk -end - -local function switchable_perk(caption, status) - if status then - return string.format("[color=0,80,0]%s[/color]", caption) - end - - return string.format("[color=80,0,0]%s[/color]", caption) + return chunk end local function draw_normal_gui(player) - local button - local merchant = global.this.events.merchant - if merchant.alive then - button = { - type = "button", - name = "merchant_find", - caption = "Merchant", - } - player.gui.left.add(button) - end + local button + local merchant = this.events.merchant + if merchant.alive then + button = { + type = 'button', + name = 'merchant_find', + caption = 'Merchant' + } + player.gui.left.add(button) + end - button = { - type = "button", - name = "flashlight_toggle", - caption = "Toggle flashlight" - } - player.gui.left.add(button) + button = { + type = 'button', + name = 'flashlight_toggle', + caption = 'Toggle flashlight' + } + player.gui.left.add(button) end local function draw_common_gui(player) - local perks = global.this.perks[player.name] - local chat_type = "Global chat" - if not perks.chat_global then - chat_type = "NAP chat" - end + local perks = this.perks[player.name] + local chat_type = 'Global chat' + if not perks.chat_global then + chat_type = 'NAP chat' + end - local button = { - type = "button", - name = "manual_toggle", - caption = "Manual" - } - player.gui.left.add(button) + local button = { + type = 'button', + name = 'manual_toggle', + caption = 'Manual' + } + player.gui.left.add(button) - button = { - type = "button", - name = "chat_toggle", - caption = chat_type, - } - player.gui.left.add(button) + button = { + type = 'button', + name = 'chat_toggle', + caption = chat_type + } + player.gui.left.add(button) end local function draw_orbit_gui(player) - local button = { - type = "button", - name = "annihilate", - caption = "Annihilate" - } - player.gui.left.add(button) + local button = { + type = 'button', + name = 'annihilate', + caption = 'Annihilate' + } + player.gui.left.add(button) end local function redraw_gui(player) - player.gui.left.clear() - draw_common_gui(player) - if player.spectator == true then - draw_orbit_gui(player) - else - draw_normal_gui(player) - end + player.gui.left.clear() + draw_common_gui(player) + if player.spectator == true then + draw_orbit_gui(player) + else + draw_normal_gui(player) + end end local function print_merchant_position(player) - local position = global.this.events.merchant.position - local perks = global.this.perks[player.name] - if not perks.minimap then - player.print(string.format(">> You were able to spot him %s from your location", - _common.get_readable_direction(player.position, position))) - else - player.print(string.format(">> You received a broadcast with [gps=%d,%d] coordinates", position.x, position.y)) - end + local position = this.events.merchant.position + local perks = this.perks[player.name] + if not perks.minimap then + player.print(string.format('>> You were able to spot him %s from your location', CommonFunctions.get_readable_direction(player.position, position))) + else + player.print(string.format('>> You received a broadcast with [gps=%d,%d] coordinates', position.x, position.y)) + end end local function on_tick_reset() - if global.this.events.reset_time == nil then - return - end + if this.events.reset_time == nil then + return + end - if global.this.events.reset_time > game.tick then - return - end + if this.events.reset_time > game.tick then + return + end - _server.start_scenario('planet_prison') - global.this.events.reset_time = nil + Server.start_scenario('planet_prison') + this.events.reset_time = nil end local function annihilate(caller) - global.this.events.annihilation = true - for _, player in pairs(game.connected_players) do - if player.name == caller.name then - goto continue - end + this.events.annihilation = true + for _, player in pairs(game.connected_players) do + if player.name == caller.name then + goto continue + end - local coeff - for i = 1, 5 do - if i % 2 == 0 then - coeff = -1 - else - coeff = 1 - end + local coeff + for i = 1, 5 do + if i % 2 == 0 then + coeff = -1 + else + coeff = 1 + end - local query = { - name = "atomic-rocket", - position = { - player.position.x - 100, - player.position.y - 100, - }, - target = { - player.position.x + (8 * i * coeff), - player.position.y + (8 * i * coeff), - }, - speed = 0.1, - } + local query = { + name = 'atomic-rocket', + position = { + player.position.x - 100, + player.position.y - 100 + }, + target = { + player.position.x + (8 * i * coeff), + player.position.y + (8 * i * coeff) + }, + speed = 0.1 + } - player.surface.create_entity(query) - player.print(">> Annihilation in progress...") - end - ::continue:: - end + player.surface.create_entity(query) + player.print('>> Annihilation in progress...') + end + ::continue:: + end - global.this.events.reset_time = game.tick + (60 * 15) + this.events.reset_time = game.tick + (60 * 15) end local function on_gui_click(e) - local elem = e.element - local p = game.players[e.player_index] - local perks = global.this.perks[p.name] + local elem = e.element + local p = game.players[e.player_index] + local perks = this.perks[p.name] - if not elem.valid then - return - end + if not elem.valid then + return + end - if elem.name == "chat_toggle" then - if perks.chat_global then - elem.caption = "NAP chat" - perks.chat_global = false - else - elem.caption = "Global chat" - perks.chat_global = true - end - elseif elem.name == "flashlight_toggle" then - if perks.flashlight_enable then - perks.flashlight_enable = false - p.character.disable_flashlight() - else - perks.flashlight_enable = true - p.character.enable_flashlight() - end - elseif elem.name == "merchant_find" then - print_merchant_position(p) - elseif elem.name == "manual_toggle" then - local children = p.gui.center.children - if #children >= 1 then - p.gui.center.clear() - return - end + if elem.name == 'chat_toggle' then + if perks.chat_global then + elem.caption = 'NAP chat' + perks.chat_global = false + else + elem.caption = 'Global chat' + perks.chat_global = true + end + elseif elem.name == 'flashlight_toggle' then + if perks.flashlight_enable then + perks.flashlight_enable = false + if p.character and p.character.valid then + p.character.disable_flashlight() + end + else + perks.flashlight_enable = true + if p.character and p.character.valid then + p.character.enable_flashlight() + end + end + elseif elem.name == 'merchant_find' then + print_merchant_position(p) + elseif elem.name == 'manual_toggle' then + local children = p.gui.center.children + if #children >= 1 then + p.gui.center.clear() + return + end - local text_box = { - type = "text-box", - text = global.this._config.manual - } - text_box = p.gui.center.add(text_box) - text_box.style.minimal_width = 512 - text_box.read_only = true - text_box.word_wrap = true - elseif elem.name == "annihilate" then - if global.this.events.annihilation == true then - return - end + local text_box = { + type = 'text-box', + text = MapConfig.manual + } + text_box = p.gui.center.add(text_box) + text_box.style.minimal_width = 512 + text_box.read_only = true + text_box.word_wrap = true + elseif elem.name == 'annihilate' then + if this.events.annihilation == true then + return + end - elem.destroy() - annihilate(p) - end -end - -local function get_random_name() - while true do - local id = _common.rand_range(100, 999) - local name = string.format("#%d", id) - if global.this.perks[name] == nil then - return name - end - end + elem.destroy() + annihilate(p) + end end local function init_player(p) - p.teleport({0, 0}, "arena") - local s = p.surface - local position = get_non_obstructed_position(s, 10) + p.teleport({0, 0}, 'arena') + local s = p.surface + local position = get_non_obstructed_position(s, 10) - global.this.perks[p.name] = nil - p.teleport(position, "arena") - --p.name = get_random_name() --player name is read only - p.force = game.create_force(p.name) - p.force.set_friend("neutral", true) - global.this.perks[p.name] = { - flashlight_enable = true, - minimap = false, - chat_global = true, - } + this.perks[p.name] = nil + p.teleport(position, 'arena') + --p.name = get_random_name() --player name is read only + p.force = game.create_force(p.name) + p.force.set_friend('neutral', true) + this.perks[p.name] = { + flashlight_enable = true, + minimap = false, + chat_global = true + } - local merch = find_force("merchant") - if merch then - p.force.set_friend(merch, true) - merch.set_friend(p.force, true) - end + local merch = find_force('merchant') + if merch then + p.force.set_friend(merch, true) + merch.set_friend(p.force, true) + end - p.force.research_queue_enabled = true - for _, tech in pairs(p.force.technologies) do - for name, status in pairs(global.this._config.technologies) do - if tech.name == name then - tech.researched = status - tech.enabled = status - end - end - end + p.force.research_queue_enabled = true + for _, tech in pairs(p.force.technologies) do + for name, status in pairs(MapConfig.technologies) do + if tech.name == name then + tech.researched = status + tech.enabled = status + end + end + end - p.minimap_enabled = false - redraw_gui(p) - do_spawn_point(p) + p.minimap_enabled = false + redraw_gui(p) + do_spawn_point(p) end local function on_player_joined_game(e) - local p = game.players[e.player_index] - init_player(p) + local p = game.players[e.player_index] + init_player(p) end local function _build_merchant_bp(surf, position) - local instance = _bp.build(surf, "merchant", position, nil) - _layers.push_excluding_bounding_box(instance.bb) + local instance = Blueprints.build(surf, 'merchant', position, nil) + LayersFunctions.push_excluding_bounding_box(instance.bb) end local function _remove_merchant_bp(surf) - local refs = _bp.get_references("merchant") - local bb = _bp.reference_get_bounding_box(refs[1]) - _layers.remove_excluding_bounding_box(bb) - _bp.destroy_references(surf, "merchant") - global.this.events.merchant.position = { - x = 0, - y = 0 - } + local refs = Blueprints.get_references('merchant') + local bb = Blueprints.reference_get_bounding_box(refs[1]) + LayersFunctions.remove_excluding_bounding_box(bb) + Blueprints.destroy_references(surf, 'merchant') + this.events.merchant.position = { + x = 0, + y = 0 + } end local function spawn_merchant(s) - local merchant = global.this.events.merchant - local position = get_non_obstructed_position(s, 10) - local merch - if not merchant.moving then - merch = game.create_force("merchant") - else - merch = find_force("merchant") - end + local merchant = this.events.merchant + local position = get_non_obstructed_position(s, 10) + local merch + if not merchant.moving then + merch = game.create_force('merchant') + else + merch = find_force('merchant') + end - merchant.position = position - merchant.alive = true - merchant.moving = false - merchant.embark_tick = game.tick + 90000 - _build_merchant_bp(s, position) + merchant.position = position + merchant.alive = true + merchant.moving = false + merchant.embark_tick = game.tick + 90000 + _build_merchant_bp(s, position) - s.print(">> Merchant appeared in the area") - for _, p in pairs(game.players) do - p.force.set_friend(merch, true) - merch.set_friend(p.force, true) - print_merchant_position(p) - redraw_gui(p) - end + s.print('>> Merchant appeared in the area') + for _, p in pairs(game.players) do + p.force.set_friend(merch, true) + merch.set_friend(p.force, true) + print_merchant_position(p) + redraw_gui(p) + end end local function embark_merchant(s) - global.this.events.merchant.alive = false - global.this.events.merchant.moving = true - global.this.events.merchant.spawn_tick = game.tick + 10000 + this.events.merchant.alive = false + this.events.merchant.moving = true + this.events.merchant.spawn_tick = game.tick + 10000 - s.print(">> Merchant is moving to new location") - _remove_merchant_bp(s) - for _, player in pairs(game.players) do - redraw_gui(player) - end + s.print('>> Merchant is moving to new location') + _remove_merchant_bp(s) + for _, player in pairs(game.players) do + redraw_gui(player) + end end local function merchant_event(s) - local e = global.this.events - local m = e.merchant - if not m.alive and m.spawn_tick <= game.tick then - spawn_merchant(s) - end + local e = this.events + local m = e.merchant + if not m.alive and m.spawn_tick <= game.tick then + spawn_merchant(s) + end - if m.alive and not m.moving and m.embark_tick <= game.tick then - embark_merchant(s) - end + if m.alive and not m.moving and m.embark_tick <= game.tick then + embark_merchant(s) + end end local function _get_outer_points(surf, x, y, deps) - local inner = deps.inner - local points = deps.points + local inner = deps.inner + local points = deps.points - local point = { - x = x, - y = y, - } + local point = { + x = x, + y = y + } - if _common.point_in_bounding_box(point, inner) then - return - end + if CommonFunctions.point_in_bounding_box(point, inner) then + return + end - local tile = surf.get_tile(point) - if string.find(tile.name, "water") ~= nil - or string.find(tile.name, "out") ~= nil then - return - end + local tile = surf.get_tile(point) + if string.find(tile.name, 'water') ~= nil or string.find(tile.name, 'out') ~= nil then + return + end - table.insert(points, point) + table.insert(points, point) end local function _calculate_attack_costs(surf, bb) - local query = { - area = bb, - force = { - "enemy", - "neutral", - "player", - }, - invert = true, - } - local objects = surf.find_entities_filtered(query) - if next(objects) == nil then - log("B") - return 0 - end + local query = { + area = bb, + force = { + 'enemy', + 'neutral', + 'player' + }, + invert = true + } + local objects = surf.find_entities_filtered(query) + if next(objects) == nil then + log('B') + return 0 + end - local cost = 0 - local costs = global.this._config.base_costs - for _, obj in pairs(objects) do - for name, coeff in pairs(costs) do - if obj.name == name then - cost = cost + coeff - end - end - end + local cost = 0 + local costs = MapConfig.base_costs + for _, obj in pairs(objects) do + for name, coeff in pairs(costs) do + if obj.name == name then + cost = cost + coeff + end + end + end - return cost + return cost end local function _get_raid_info(surf, bb) - local pick = nil - local cost = _calculate_attack_costs(surf, bb) - for _, entry in pairs(global.this._config.raid_costs) do - if entry.cost <= cost then - pick = entry - else - break - end - end + local pick = nil + local cost = _calculate_attack_costs(surf, bb) + for _, entry in pairs(MapConfig.raid_costs) do + if entry.cost <= cost then + pick = entry + else + break + end + end - return pick + return pick end local function _create_npc_group(claim, surf) - local inner = _common.create_bounding_box_by_points(claim) - local info = _get_raid_info(surf, inner) - if info == nil then - return {} - end + local inner = CommonFunctions.create_bounding_box_by_points(claim) + local info = _get_raid_info(surf, inner) + if info == nil then + return {} + end - local outer = _common.deepcopy(inner) - _common.enlarge_bounding_box(outer, 10) + local outer = CommonFunctions.deepcopy(inner) + CommonFunctions.enlarge_bounding_box(outer, 10) - local points = {} - local deps = { - points = points, - inner = inner, - } - _common.for_bounding_box_extra(surf, outer, _get_outer_points, deps) + local points = {} + local deps = { + points = points, + inner = inner + } + CommonFunctions.for_bounding_box_extra(surf, outer, _get_outer_points, deps) - local agents = {} - for i, point in ipairs(points) do - if _common.rand_range(1, info.chance) ~= 1 then - goto continue - end + local agents = {} + for i, point in ipairs(points) do + if CommonFunctions.rand_range(1, info.chance) ~= 1 then + goto continue + end - local query = { - name = "character", - position = point - } + local query = { + name = 'character', + position = point + } - local agent = surf.create_entity(query) - local stash = {} - for attr, value in pairs(info.gear[(i % #info.gear) + 1]) do - local prop = { - name = value - } + local agent = surf.create_entity(query) + local stash = {} + for attr, value in pairs(info.gear[(i % #info.gear) + 1]) do + local prop = { + name = value + } - if attr == "ammo" then - prop.count = 20 - elseif attr == "weap" then - prop.count = 1 - elseif attr == "armor" then - prop.count = 1 - end + if attr == 'ammo' then + prop.count = 20 + elseif attr == 'weap' then + prop.count = 1 + elseif attr == 'armor' then + prop.count = 1 + end - table.insert(stash, prop) - end + table.insert(stash, prop) + end - for _, stack in pairs(stash) do - agent.insert(stack) - end + for _, stack in pairs(stash) do + agent.insert(stack) + end - global.this.assign_camouflage(agent, _common) + assign_camouflage(agent, CommonFunctions) - table.insert(agents, agent) - ::continue:: - end + table.insert(agents, agent) + ::continue:: + end - return agents + return agents end local function populate_raid_event(surf) - local claims, group - local status = false - local groups = global.this.events.raid_groups + local claims, group + local status = false + local groups = this.events.raid_groups - for _, p in pairs(game.connected_players) do - groups[p.name] = {} - claims = _claims.get_claims(p.name) - for _, claim in pairs(claims) do - if #claim == 0 then - goto continue - end + for _, p in pairs(game.connected_players) do + groups[p.name] = {} + claims = ClaimsFunctions.get_claims(p.name) + for _, claim in pairs(claims) do + if #claim == 0 then + goto continue + end - status = true - group = { - agents = _create_npc_group(claim, surf), - objects = claim - } - table.insert(groups[p.name], group) + status = true + group = { + agents = _create_npc_group(claim, surf), + objects = claim + } + table.insert(groups[p.name], group) - ::continue:: - end - end + ::continue:: + end + end - return status + return status end local function raid_event(surf) - local raid_groups = global.this.events.raid_groups - if global.this.events.raid_init then - if surf.daytime > 0.01 and surf.daytime <= 0.1 then - for name, groups in pairs(raid_groups) do - for i = #groups, 1, -1 do - local group = groups[i] - local agents = group.agents - for j = #agents, 1, -1 do - local agent = agents[j] - if agent.valid then - agent.destroy() - end + local raid_groups = this.events.raid_groups + if this.events.raid_init then + if surf.daytime > 0.01 and surf.daytime <= 0.1 then + for name, groups in pairs(raid_groups) do + for i = #groups, 1, -1 do + local group = groups[i] + local agents = group.agents + for j = #agents, 1, -1 do + local agent = agents[j] + if agent.valid then + agent.destroy() + end - table.remove(agents, j) - end + table.remove(agents, j) + end - if #agents == 0 then - table.remove(group, i) - end + if #agents == 0 then + table.remove(group, i) + end + end + + if #groups == 0 then + raid_groups[name] = nil + end end - if #groups == 0 then - raid_groups[name] = nil + this.events.raid_init = false + end + else + if surf.daytime < 0.4 or surf.daytime > 0.6 then + return + end + + if populate_raid_event(surf) then + this.events.raid_init = true + end + end + + if game.tick % 4 ~= 0 then + return + end + + for name, groups in pairs(raid_groups) do + local exists = false + for _, p in pairs(game.connected_players) do + if p.name == name then + exists = true + break end - end + end - global.this.events.raid_init = false - end - else - if surf.daytime < 0.4 or surf.daytime > 0.6 then - return - end + if not exists then + raid_groups[name] = nil + goto continue + end - if populate_raid_event(surf) then - global.this.events.raid_init = true - end - end + for _, group in pairs(groups) do + AIFunctions.do_job(surf, AIFunctions.command.attack_objects, group) + end - if game.tick % 4 ~= 0 then - return - end - - for name, groups in pairs(raid_groups) do - local exists = false - for _, p in pairs(game.connected_players) do - if p.name == name then - exists = true - break - end - end - - if not exists then - raid_groups[name] = nil - goto continue - end - - for _, group in pairs(groups) do - _ai.do_job(surf, _ai.command.attack_objects, group) - end - - ::continue:: - end + ::continue:: + end end local function cause_event(s) - merchant_event(s) - raid_event(s) + merchant_event(s) + raid_event(s) end local function kill_player(p) - p.character.die() + p.character.die() end local function on_tick() - local s = global.this.surface - if not s then - log("on_tick: surface empty!") - return - end + local s = this.surface + if not s then + log('on_tick: surface empty!') + return + end - local surf = global.this.surface - if game.tick % 4 == 0 then - _ai.do_job(surf, _ai.command.seek_and_destroy_player) - end + local surf = this.surface + if game.tick % 4 == 0 then + AIFunctions.do_job(surf, AIFunctions.command.seek_and_destroy_player) + end - _layers.do_job(surf) - cause_event(s) + LayersFunctions.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) % 100 == 0 then + AfkFunctions.on_inactive_players(90, kill_player) + end - if (game.tick + 1) % 60 == 0 then - _timers.do_job() - end + if (game.tick + 1) % 60 == 0 then + TimersFunctions.do_job() + end end local function make_ore_patch(e) - if _common.rand_range(1, 30) ~= 1 then - return - end + if CommonFunctions.rand_range(1, 30) ~= 1 then + return + end - local surf = e.surface - local point = e.area.left_top - _map.draw_entity_circle(point, "stone", surf, 6, true, 1000000) - _map.draw_entity_circle(point, "coal", surf, 12, true, 1000000) - _map.draw_entity_circle(point, "copper-ore", surf, 18, true, 1000000) - _map.draw_entity_circle(point, "iron-ore", surf, 24, true, 1000000) - _map.draw_noise_tile_circle(point, "water", surf, 4) + local surf = e.surface + local point = e.area.left_top + MapFuntions.draw_entity_circle(point, 'stone', surf, 6, true, 1000000) + MapFuntions.draw_entity_circle(point, 'coal', surf, 12, true, 1000000) + MapFuntions.draw_entity_circle(point, 'copper-ore', surf, 18, true, 1000000) + MapFuntions.draw_entity_circle(point, 'iron-ore', surf, 24, true, 1000000) + MapFuntions.draw_noise_tile_circle(point, 'water', surf, 4) end local function on_chunk_generated(e) - if e.surface.name ~= "arena" then - return - end + if e.surface.name ~= 'arena' then + return + end - make_ore_patch(e) - _layers.push_chunk(e.position) + make_ore_patch(e) + LayersFunctions.push_chunk(e.position) end local function mined_wreckage(e) - if e.entity.name ~= "mineable-wreckage" then - return - end + if e.entity.name ~= 'crash-site-spaceship-wreck-medium-1' then + return + end - local candidates = {} - local chance = _common.rand_range(0, 1000) - for name, attrs in pairs(global.this._config.wreck_loot) do - local prob = attrs.rare * 100 - if prob < chance then - local cand = { - name = name, - count = _common.rand_range(attrs.count[1], attrs.count[2]), - } - table.insert(candidates, cand) - end - end + local candidates = {} + local chance = CommonFunctions.rand_range(0, 1000) + for name, attrs in pairs(MapConfig.wreck_loot) do + local prob = attrs.rare * 100 + if prob < chance then + local cand = { + name = name, + count = CommonFunctions.rand_range(attrs.count[1], attrs.count[2]) + } + table.insert(candidates, cand) + end + end - local count = #candidates - if count == 0 then - return - end + local count = #candidates + if count == 0 then + return + end - local cand = candidates[_common.rand_range(1, count)] - e.buffer.insert(cand) + local cand = candidates[CommonFunctions.rand_range(1, count)] + e.buffer.insert(cand) end local function on_player_mined_entity(e) - local ent = e.entity - if not ent.valid then - return - end + local ent = e.entity + if not ent.valid then + return + end - mined_wreckage(e) - _claims.on_player_mined_entity(ent) + mined_wreckage(e) + ClaimsFunctions.on_player_mined_entity(ent) end local function on_player_died(e) - local index = e.player_index - if not index then - return -- banned/kicked somewhere else - end + local index = e.player_index + if not index then + return -- banned/kicked somewhere else + end - local p = game.players[index] - _claims.on_player_died(p) - game.merge_forces(p.name, "neutral") + local p = game.players[index] + ClaimsFunctions.on_player_died(p) + game.merge_forces(p.name, 'neutral') end local function on_player_respawned(e) - local p = game.players[e.player_index] - init_player(p) + local p = game.players[e.player_index] + init_player(p) end local function on_player_dropped_item(e) - if not global.this.last_friend then - global.this.last_friend = {} - end + if not this.last_friend then + this.last_friend = {} + end - local p = game.players[e.player_index] - local ent = e.entity - if ent.stack.name == "raw-fish" then - local ent_list = p.surface.find_entities_filtered({ - name = "character", - position = ent.position, - radius = 2, - }) - if not ent_list then - return - end + local p = game.players[e.player_index] + local ent = e.entity + if ent.stack.name == 'raw-fish' then + local ent_list = + p.surface.find_entities_filtered( + { + name = 'character', + position = ent.position, + radius = 2 + } + ) + if not ent_list then + return + end - local peer = nil - for _, char in pairs(ent_list) do - if char.player and char.player.name ~= p.name then - peer = char.player - break - end - end + local peer = nil + for _, char in pairs(ent_list) do + if char.player and char.player.name ~= p.name then + peer = char.player + break + end + end - if peer == nil then - return - end + if peer == nil then + return + end - if p.force.get_cease_fire(peer.name) then - p.print(string.format("You're in the NAP with %s already", peer.name)) - return - end + if p.force.get_cease_fire(peer.name) then + 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("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 + if 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('The NAP was formed with %s', peer.name)) + peer.print(string.format('The NAP was formed with %s', p.name)) + this.last_friend[p.name] = '' + this.last_friend[peer.name] = '' + return + end - global.this.last_friend[p.name] = peer.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", - position = ent.position, - radius = 2, - }) - if not ent_list then - return - end + this.last_friend[p.name] = peer.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', + position = ent.position, + radius = 2 + } + ) + if not ent_list then + return + end - local peer = nil - for _, char in pairs(ent_list) do - if char.player and char.player.name ~= p.name then - peer = char.player - break - end - end + local peer = nil + for _, char in pairs(ent_list) do + if char.player and char.player.name ~= p.name then + peer = char.player + break + end + end - if peer == nil then - return - end + if peer == nil then + return + end - if not p.force.get_cease_fire(peer.name) then - p.print(string.format("You don't have the NAP with %s", p.name)) - return - end + if not p.force.get_cease_fire(peer.name) then + p.print(string.format("You don't have the NAP with %s", p.name)) + return + end - p.force.set_cease_fire(peer.name, false) - peer.force.set_cease_fire(p.name, false) + p.force.set_cease_fire(peer.name, false) + peer.force.set_cease_fire(p.name, false) - global.this.last_friend[p.name] = "" - global.this.last_friend[peer.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 + this.last_friend[p.name] = '' + this.last_friend[peer.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 local function on_chunk_charted(e) - local f_perks = global.this.perks[e.force.name] + local f_perks = this.perks[e.force.name] - if not f_perks then - return - end + if not f_perks then + return + end - if not f_perks.minimap then - e.force.clear_chart() - end + if not f_perks.minimap then + e.force.clear_chart() + end end local function on_entity_damaged(e) - local ent = e.entity + local ent = e.entity - if ent.force.name == "merchant" then - if not ent.force.get_friend(e.force) then - return - end + if ent.force.name == 'merchant' then + if not ent.force.get_friend(e.force) then + return + end - ent.force.set_friend(e.force, false) - e.force.set_friend(ent.force, false) - end + 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_particle(blood) - end - end + if ent.name == 'character' then + local hp = 1.0 - ent.get_health_ratio() + local particles = 45 * hp + local coeff = CommonFunctions.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 = { + (CommonFunctions.rand_range(-20, 20) / 100.0) + coeff, + (CommonFunctions.rand_range(-20, 20) / 100.0) + coeff + }, + frame_speed = 0.01, + vertical_speed = 0.02, + height = 0.01 + } + ent.surface.create_particle(blood) + end + end end local function merchant_death(e) - local ent = e.entity - if ent.force.name ~= "merchant" then - return false - end + local ent = e.entity + if ent.force.name ~= 'merchant' then + return false + end - if ent.name ~= "character" and ent.name ~= "market" then - return false - end + if ent.name ~= 'character' and ent.name ~= 'market' then + return false + end - local s = ent.surface - local explosion = { - name = "massive-explosion", - position = ent.position - } - s.create_entity(explosion) - _remove_merchant_bp(s) + local s = ent.surface + local explosion = { + name = 'massive-explosion', + position = ent.position + } + s.create_entity(explosion) + _remove_merchant_bp(s) - global.this.events.merchant.alive = false - global.this.events.merchant.moving = false - global.this.events.merchant.spawn_tick = game.tick + 1000 - game.merge_forces("merchant", "neutral") + this.events.merchant.alive = false + this.events.merchant.moving = false + this.events.merchant.spawn_tick = game.tick + 1000 + game.merge_forces('merchant', 'neutral') - s.print(">> Merchant died") - for _, player in pairs(game.players) do - redraw_gui(player) - end + s.print('>> Merchant died') + for _, player in pairs(game.players) do + redraw_gui(player) + end - return true + return true end local function hostile_death(e) - local ent = e.entity - local loot = e.loot - if ent.name ~= "character" then - return false - end + local ent = e.entity + local loot = e.loot + if ent.name ~= 'character' then + return false + end - if ent.player then - loot.insert({name = "coin", count = 70}) - else - loot.insert({name = "coin", count = 10}) - end + if ent.player then + loot.insert({name = 'coin', count = 70}) + else + loot.insert({name = 'coin', count = 10}) + end - return true + return true end local function character_death(e) - local ent = e.entity - if ent.name ~= "character" then - return false - end + 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) + 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 - end + if not e.entity.valid then + return + end - if merchant_death(e) then - return - end + if merchant_death(e) then + return + end - hostile_death(e) - character_death(e) - _claims.on_entity_died(e.entity) + hostile_death(e) + character_death(e) + ClaimsFunctions.on_entity_died(e.entity) end - local function merchant_exploit_check(ent) - if ent.type ~= "electric-pole" then - return - end + if ent.type ~= 'electric-pole' then + return + end - local refs = _bp.get_references("merchant") - if not refs or #refs <= 0 then - return - end + local refs = Blueprints.get_references('merchant') + if not refs or #refs <= 0 then + return + end - local bp_ent = _bp.reference_get_entities(refs[1])[1] - local surf = bp_ent.surface + local bp_ent = Blueprints.reference_get_entities(refs[1])[1] + local surf = bp_ent.surface - local query = { - type = "electric-pole", - position = bp_ent.position, - radius = 18 - } - local ents = surf.find_entities_filtered(query) - for _, s_ent in pairs(ents) do - if s_ent.valid and s_ent.force.name ~= "merchant" then - s_ent.die() - end - end + local query = { + type = 'electric-pole', + position = bp_ent.position, + radius = 18 + } + local ents = surf.find_entities_filtered(query) + for _, s_ent in pairs(ents) do + if s_ent.valid and s_ent.force.name ~= 'merchant' then + s_ent.die() + end + end end local function on_built_entity(e) - local ent = e.created_entity - if not ent or not ent.valid then - return - end + local ent = e.created_entity + if not ent or not ent.valid then + return + end - _claims.on_built_entity(ent) - merchant_exploit_check(ent) + ClaimsFunctions.on_built_entity(ent) + merchant_exploit_check(ent) end local function on_market_item_purchased(e) - local p = game.players[e.player_index] - local m = e.market - local o = m.get_market_items()[e.offer_index].offer - local perks = global.this.perks[p.name] + local p = game.players[e.player_index] + local m = e.market + local o = m.get_market_items()[e.offer_index].offer + local perks = this.perks[p.name] - if o.effect_description == "Construct a GPS receiver" then - perks.minimap = true - p.minimap_enabled = true - end + if o.effect_description == 'Construct a GPS receiver' then + perks.minimap = true + p.minimap_enabled = true + end end local function stringify_color(color) - local r, g, b = color.r, color.g, color.b - if r <= 1 then - r = math.floor(r * 255) - end + local r, g, b = color.r, color.g, color.b + if r <= 1 then + r = math.floor(r * 255) + end - if g <= 1 then - g = math.floor(g * 255) - end + if g <= 1 then + g = math.floor(g * 255) + end - if b <= 1 then - b = math.floor(b * 255) - end + if b <= 1 then + b = math.floor(b * 255) + end - return string.format("%d,%d,%d", r, g, b) + return string.format('%d,%d,%d', r, g, b) end local function create_console_message(p, message) - local prefix_fmt = "[color=%s]%s:[/color]" - local msg_fmt = "[color=%s]%s[/color]" - local color = stringify_color(p.chat_color) - local prefix = string.format(prefix_fmt, color, p.name) - local p_msg = string.format(msg_fmt, color, message) + local prefix_fmt = '[color=%s]%s:[/color]' + local msg_fmt = '[color=%s]%s[/color]' + local color = stringify_color(p.chat_color) + local prefix = string.format(prefix_fmt, color, p.name) + local p_msg = string.format(msg_fmt, color, message) - if global.this.perks[p.name].chat_global then - msg_fmt = "[color=red]global:[/color] %s %s" - else - msg_fmt = "[color=green]nap:[/color] %s %s" - end + if this.perks[p.name].chat_global then + msg_fmt = '[color=red]global:[/color] %s %s' + else + msg_fmt = '[color=green]nap:[/color] %s %s' + end - return string.format(msg_fmt, prefix, p_msg) + return string.format(msg_fmt, prefix, p_msg) end local function filter_out_gps(message) - local msg = string.gsub(message, '%[gps=%-?%d+%,?%s*%-?%d+%]', '[gps]') - return msg + local msg = string.gsub(message, '%[gps=%-?%d+%,?%s*%-?%d+%]', '[gps]') + return msg end local function on_console_chat(e) - local pid = e.player_index + local pid = e.player_index - if not pid then - return - end + if not pid then + return + end - local p = game.players[pid] - local msg = create_console_message(p, e.message) - if global.this.perks[p.name].chat_global then - for _, peer in pairs(game.players) do - if peer.name ~= p.name then - local perks = global.this.perks[peer.name] - if perks.minimap then - peer.print(msg) - else - peer.print(filter_out_gps(msg)) - end - end - end - else - for _, f in pairs(game.forces) do - if p.force.get_cease_fire(f) then - local peer = f.players[1] + local p = game.players[pid] + local msg = create_console_message(p, e.message) + if this.perks[p.name].chat_global then + for _, peer in pairs(game.players) do if peer.name ~= p.name then - local perks = global.this.perks[peer.name] - if perks.minimap then - peer.print(msg) - else - peer.print(filter_out_gps(msg)) - end + local perks = this.perks[peer.name] + if perks.minimap then + peer.print(msg) + else + peer.print(filter_out_gps(msg)) + end end - end - end - end + end + else + for _, f in pairs(game.forces) do + if p.force.get_cease_fire(f) then + local peer = f.players[1] + if peer.name ~= p.name then + local perks = this.perks[peer.name] + if perks.minimap then + peer.print(msg) + else + peer.print(filter_out_gps(msg)) + end + end + end + end + end end local function on_research_finished(e) - local r = e.research - if not r.valid then - return - end + local r = e.research + if not r.valid then + return + end - local reward = { - name = "coin", - count = math.ceil(r.research_unit_count * 3) - } - local f = r.force - for _, player in pairs(f.players) do - if player.can_insert(reward) then - player.insert(reward) - end - end + local reward = { + name = 'coin', + count = math.ceil(r.research_unit_count * 3) + } + local f = r.force + for _, player in pairs(f.players) do + if player.can_insert(reward) then + player.insert(reward) + end + end end local function move_to_orbit(player) - local char = player.character - player.character = nil - char.destroy() + local char = player.character + player.character = nil + char.destroy() - game.merge_forces(player.name, "neutral") - player.spectator = true - redraw_gui(player) + game.merge_forces(player.name, 'neutral') + player.spectator = true + redraw_gui(player) - local orbit_perms = game.permissions.get_group("orbit") - orbit_perms.add_player(player) + local orbit_perms = game.permissions.get_group('orbit') + orbit_perms.add_player(player) end local function on_rocket_launched(e) - local surf = global.this.surface - local pid = e.player_index - surf.print(">> The rocket was launched") - if pid == nil then - surf.print(">> Nobody escaped by it") - else - local player = game.players[pid] - surf.print(string.format(">> The %s was able to escape", player.name)) - move_to_orbit(player) - end + local surf = this.surface + local pid = e.player_index + surf.print('>> The rocket was launched') + if pid == nil then + surf.print('>> Nobody escaped by it') + else + local player = game.players[pid] + surf.print(string.format('>> The %s was able to escape', player.name)) + move_to_orbit(player) + end end -_evt.on_init(init_game) -_evt.add(defines.events.on_built_entity, on_built_entity) -_evt.add(defines.events.on_robot_built_entity, on_built_entity) -_evt.add(defines.events.on_research_finished, on_research_finished) -_evt.add(defines.events.on_player_joined_game, on_player_joined_game) -_evt.add(defines.events.on_chunk_generated, on_chunk_generated) -_evt.add(defines.events.on_player_mined_entity, on_player_mined_entity) -_evt.add(defines.events.on_player_died, on_player_died) -_evt.add(defines.events.on_player_kicked, on_player_died) -_evt.add(defines.events.on_player_banned, on_player_died) -_evt.add(defines.events.on_player_respawned, on_player_respawned) -_evt.add(defines.events.on_player_dropped_item, on_player_dropped_item) -_evt.add(defines.events.on_pre_player_left_game, on_player_died) -_evt.add(defines.events.on_entity_damaged, on_entity_damaged) -_evt.add(defines.events.on_entity_died, on_entity_died) -_evt.add(defines.events.on_market_item_purchased, on_market_item_purchased) -_evt.add(defines.events.on_chunk_charted, on_chunk_charted) -_evt.add(defines.events.on_console_chat, on_console_chat) -_evt.add(defines.events.on_gui_click, on_gui_click) -_evt.add(defines.events.on_tick, on_tick) -_evt.add(defines.events.on_tick, on_tick_reset) -_evt.add(defines.events.on_rocket_launched, on_rocket_launched) +Public.explode_ship = explode_ship -_global.register_init({}, - function(tbl) - tbl.this = global.this - end, - function(tbl) - global.this = tbl.this -end) +Event.on_init(init_game) +Event.add(defines.events.on_built_entity, on_built_entity) +Event.add(defines.events.on_robot_built_entity, on_built_entity) +Event.add(defines.events.on_research_finished, on_research_finished) +Event.add(defines.events.on_player_joined_game, on_player_joined_game) +Event.add(defines.events.on_chunk_generated, on_chunk_generated) +Event.add(defines.events.on_player_mined_entity, on_player_mined_entity) +Event.add(defines.events.on_player_died, on_player_died) +Event.add(defines.events.on_player_kicked, on_player_died) +Event.add(defines.events.on_player_banned, on_player_died) +Event.add(defines.events.on_player_respawned, on_player_respawned) +Event.add(defines.events.on_player_dropped_item, on_player_dropped_item) +Event.add(defines.events.on_pre_player_left_game, on_player_died) +Event.add(defines.events.on_entity_damaged, on_entity_damaged) +Event.add(defines.events.on_entity_died, on_entity_died) +Event.add(defines.events.on_market_item_purchased, on_market_item_purchased) +Event.add(defines.events.on_chunk_charted, on_chunk_charted) +Event.add(defines.events.on_console_chat, on_console_chat) +Event.add(defines.events.on_gui_click, on_gui_click) +Event.add(defines.events.on_tick, on_tick) +Event.add(defines.events.on_tick, on_tick_reset) +Event.add(defines.events.on_rocket_launched, on_rocket_launched) + +return Public diff --git a/maps/planet_prison/bp/player_ship.lua b/maps/planet_prison/bp/player_ship.lua index 74edc804..376bbb79 100644 --- a/maps/planet_prison/bp/player_ship.lua +++ b/maps/planet_prison/bp/player_ship.lua @@ -1 +1 @@ -return '{"blueprint":{"icons":[{"signal":{"type":"item","name":"crash-site-lab-repaired"},"index":1},{"signal":{"type":"item","name":"crash-site-generator"},"index":2}],"entities":[{"entity_number":1,"name":"crash-site-generator","position":{"x":3,"y":-3.5}},{"entity_number":2,"name":"crash-site-lab-repaired","position":{"x":-2,"y":-1}},{"entity_number":3,"name":"crash-site-chest-1","position":{"x":4,"y":-1}},{"entity_number":4,"name":"crash-site-chest-2","position":{"x":-3,"y":3}},{"entity_number":5,"name":"crash-site-assembling-machine-1-repaired","position":{"x":-2,"y":-5}}],"item":"blueprint","version":73019621376}}' +return '{"blueprint":{"icons":[{"signal":{"type":"item","name":"crash-site-lab-repaired"},"index":1}],"entities":[{"entity_number":2,"name":"crash-site-chest-1","position":{"x":4,"y":-1}},{"entity_number":3,"name":"crash-site-chest-2","position":{"x":-3,"y":3}}],"item":"blueprint","version":73019621376}}' diff --git a/maps/planet_prison/config.lua b/maps/planet_prison/config.lua index a05fed6f..8fab7191 100644 --- a/maps/planet_prison/config.lua +++ b/maps/planet_prison/config.lua @@ -1,795 +1,783 @@ local public = {} public.player_ship_loot = { - { - name = "piercing-rounds-magazine", - count = 35, - }, - { - name = "grenade", - count = 2, - }, - { - name = "submachine-gun", - count = 1, - }, - { - name = "light-armor", - count = 1, - }, - { - name = "iron-plate", - count = 30, - }, - { - name = "copper-plate", - count = 10, - }, - { - name = "raw-fish", - count = 2, - }, - { - name = "small-lamp", - count = 1 - } + { + name = 'piercing-rounds-magazine', + count = 35 + }, + { + name = 'grenade', + count = 2 + }, + { + name = 'submachine-gun', + count = 1 + }, + { + name = 'light-armor', + count = 1 + }, + { + name = 'iron-plate', + count = 30 + }, + { + name = 'copper-plate', + count = 10 + }, + { + name = 'raw-fish', + count = 2 + }, + { + name = 'small-lamp', + count = 1 + } } public.permission_orbit = { - defines.input_action.activate_copy, - defines.input_action.activate_cut, - defines.input_action.activate_paste, - defines.input_action.add_permission_group, - defines.input_action.add_train_station, - defines.input_action.admin_action, - defines.input_action.alt_select_area, - defines.input_action.alt_select_blueprint_entities, - defines.input_action.alternative_copy, - defines.input_action.begin_mining, - defines.input_action.begin_mining_terrain, - defines.input_action.build_item, - defines.input_action.build_rail, - defines.input_action.build_terrain, - defines.input_action.cancel_craft, - defines.input_action.cancel_deconstruct, - defines.input_action.cancel_new_blueprint, - defines.input_action.cancel_research, - defines.input_action.cancel_upgrade, - defines.input_action.change_active_item_group_for_crafting, - defines.input_action.change_active_item_group_for_filters, - defines.input_action.change_active_quick_bar, - defines.input_action.change_arithmetic_combinator_parameters, - defines.input_action.change_blueprint_book_record_label, - defines.input_action.change_decider_combinator_parameters, - defines.input_action.change_item_label, - defines.input_action.change_multiplayer_config, - defines.input_action.change_picking_state, - defines.input_action.change_programmable_speaker_alert_parameters, - defines.input_action.change_programmable_speaker_circuit_parameters, - defines.input_action.change_programmable_speaker_parameters, - defines.input_action.change_riding_state, - defines.input_action.change_shooting_state, - defines.input_action.change_single_blueprint_record_label, - defines.input_action.change_train_stop_station, - defines.input_action.change_train_wait_condition, - defines.input_action.change_train_wait_condition_data, - defines.input_action.clear_cursor, - defines.input_action.clear_selected_blueprint, - defines.input_action.clear_selected_deconstruction_item, - defines.input_action.clear_selected_upgrade_item, - defines.input_action.connect_rolling_stock, - defines.input_action.copy_entity_settings, - defines.input_action.craft, - defines.input_action.create_blueprint_like, - defines.input_action.cursor_split, - defines.input_action.cursor_transfer, - defines.input_action.custom_input, - defines.input_action.cycle_blueprint_book_backwards, - defines.input_action.cycle_blueprint_book_forwards, - defines.input_action.deconstruct, - defines.input_action.delete_blueprint_library, - defines.input_action.delete_blueprint_record, - defines.input_action.delete_custom_tag, - defines.input_action.delete_permission_group, - defines.input_action.destroy_opened_item, - defines.input_action.disconnect_rolling_stock, - defines.input_action.drag_train_schedule, - defines.input_action.drag_train_wait_condition, - defines.input_action.drop_blueprint_record, - defines.input_action.drop_item, - defines.input_action.drop_to_blueprint_book, - defines.input_action.edit_custom_tag, - defines.input_action.edit_permission_group, - defines.input_action.export_blueprint, - defines.input_action.fast_entity_split, - defines.input_action.fast_entity_transfer, - defines.input_action.go_to_train_station, - defines.input_action.grab_blueprint_record, - defines.input_action.import_blueprint, - defines.input_action.import_blueprint_string, - defines.input_action.import_permissions_string, - defines.input_action.inventory_split, - defines.input_action.inventory_transfer, - defines.input_action.launch_rocket, - defines.input_action.lua_shortcut, - defines.input_action.map_editor_action, - defines.input_action.market_offer, - defines.input_action.mod_settings_changed, - defines.input_action.open_achievements_gui, - defines.input_action.open_blueprint_library_gui, - defines.input_action.open_blueprint_record, - defines.input_action.open_bonus_gui, - defines.input_action.open_character_gui, - defines.input_action.open_equipment, - defines.input_action.open_gui, - defines.input_action.open_item, - defines.input_action.open_kills_gui, - defines.input_action.open_logistic_gui, - defines.input_action.open_mod_item, - defines.input_action.open_production_gui, - defines.input_action.open_technology_gui, - defines.input_action.open_train_gui, - defines.input_action.open_train_station_gui, - defines.input_action.open_trains_gui, - defines.input_action.open_tutorials_gui, - defines.input_action.paste_entity_settings, - defines.input_action.place_equipment, - defines.input_action.quick_bar_pick_slot, - defines.input_action.quick_bar_set_selected_page, - defines.input_action.quick_bar_set_slot, - defines.input_action.remove_cables, - defines.input_action.remove_train_station, - defines.input_action.reset_assembling_machine, - defines.input_action.rotate_entity, - defines.input_action.select_area, - defines.input_action.select_blueprint_entities, - defines.input_action.select_entity_slot, - defines.input_action.select_item, - defines.input_action.select_mapper_slot, - defines.input_action.select_next_valid_gun, - defines.input_action.select_tile_slot, - defines.input_action.set_auto_launch_rocket, - defines.input_action.set_autosort_inventory, - defines.input_action.set_behavior_mode, - defines.input_action.set_car_weapons_control, - defines.input_action.set_circuit_condition, - defines.input_action.set_circuit_mode_of_operation, - defines.input_action.set_deconstruction_item_tile_selection_mode, - defines.input_action.set_deconstruction_item_trees_and_rocks_only, - defines.input_action.set_entity_color, - defines.input_action.set_entity_energy_property, - defines.input_action.set_filter, - defines.input_action.set_heat_interface_mode, - defines.input_action.set_heat_interface_temperature, - defines.input_action.set_infinity_container_filter_item, - defines.input_action.set_infinity_container_remove_unfiltered_items, - defines.input_action.set_infinity_pipe_filter, - defines.input_action.set_inserter_max_stack_size, - defines.input_action.set_inventory_bar, - defines.input_action.set_logistic_filter_item, - defines.input_action.set_logistic_filter_signal, - defines.input_action.set_logistic_trash_filter_item, - defines.input_action.set_request_from_buffers, - defines.input_action.set_research_finished_stops_game, - defines.input_action.set_signal, - defines.input_action.set_single_blueprint_record_icon, - defines.input_action.set_splitter_priority, - defines.input_action.set_train_stopped, - defines.input_action.setup_assembling_machine, - defines.input_action.setup_blueprint, - defines.input_action.setup_single_blueprint_record, - defines.input_action.smart_pipette, - defines.input_action.stack_split, - defines.input_action.stack_transfer, - defines.input_action.start_repair, - defines.input_action.start_research, - defines.input_action.stop_building_by_moving, - defines.input_action.switch_connect_to_logistic_network, - defines.input_action.switch_constant_combinator_state, - defines.input_action.switch_inserter_filter_mode_state, - defines.input_action.switch_power_switch_state, - defines.input_action.switch_to_rename_stop_gui, - defines.input_action.take_equipment, - defines.input_action.toggle_deconstruction_item_entity_filter_mode, - defines.input_action.toggle_deconstruction_item_tile_filter_mode, - defines.input_action.toggle_driving, - defines.input_action.toggle_enable_vehicle_logistics_while_moving, - defines.input_action.toggle_equipment_movement_bonus, - defines.input_action.toggle_map_editor, - defines.input_action.toggle_personal_roboport, - defines.input_action.toggle_show_entity_info, - defines.input_action.translate_string, - defines.input_action.undo, - defines.input_action.upgrade, - defines.input_action.upgrade_opened_blueprint, - defines.input_action.use_artillery_remote, - defines.input_action.use_item, - defines.input_action.wire_dragging, + defines.input_action.activate_copy, + defines.input_action.activate_cut, + defines.input_action.activate_paste, + defines.input_action.add_permission_group, + defines.input_action.add_train_station, + defines.input_action.admin_action, + defines.input_action.alt_select_area, + defines.input_action.alt_select_blueprint_entities, + defines.input_action.alternative_copy, + defines.input_action.begin_mining, + defines.input_action.begin_mining_terrain, + defines.input_action.build_item, + defines.input_action.build_rail, + defines.input_action.build_terrain, + defines.input_action.cancel_craft, + defines.input_action.cancel_deconstruct, + defines.input_action.cancel_new_blueprint, + defines.input_action.cancel_research, + defines.input_action.cancel_upgrade, + defines.input_action.change_active_item_group_for_crafting, + defines.input_action.change_active_item_group_for_filters, + defines.input_action.change_active_quick_bar, + defines.input_action.change_arithmetic_combinator_parameters, + defines.input_action.change_blueprint_book_record_label, + defines.input_action.change_decider_combinator_parameters, + defines.input_action.change_item_label, + defines.input_action.change_multiplayer_config, + defines.input_action.change_picking_state, + defines.input_action.change_programmable_speaker_alert_parameters, + defines.input_action.change_programmable_speaker_circuit_parameters, + defines.input_action.change_programmable_speaker_parameters, + defines.input_action.change_riding_state, + defines.input_action.change_shooting_state, + defines.input_action.change_single_blueprint_record_label, + defines.input_action.change_train_stop_station, + defines.input_action.change_train_wait_condition, + defines.input_action.change_train_wait_condition_data, + defines.input_action.clear_cursor, + defines.input_action.clear_selected_blueprint, + defines.input_action.clear_selected_deconstruction_item, + defines.input_action.clear_selected_upgrade_item, + defines.input_action.connect_rolling_stock, + defines.input_action.copy_entity_settings, + defines.input_action.craft, + defines.input_action.create_blueprint_like, + defines.input_action.cursor_split, + defines.input_action.cursor_transfer, + defines.input_action.custom_input, + defines.input_action.cycle_blueprint_book_backwards, + defines.input_action.cycle_blueprint_book_forwards, + defines.input_action.deconstruct, + defines.input_action.delete_blueprint_library, + defines.input_action.delete_blueprint_record, + defines.input_action.delete_custom_tag, + defines.input_action.delete_permission_group, + defines.input_action.destroy_opened_item, + defines.input_action.disconnect_rolling_stock, + defines.input_action.drag_train_schedule, + defines.input_action.drag_train_wait_condition, + defines.input_action.drop_blueprint_record, + defines.input_action.drop_item, + defines.input_action.drop_to_blueprint_book, + defines.input_action.edit_custom_tag, + defines.input_action.edit_permission_group, + defines.input_action.export_blueprint, + defines.input_action.fast_entity_split, + defines.input_action.fast_entity_transfer, + defines.input_action.go_to_train_station, + defines.input_action.grab_blueprint_record, + defines.input_action.import_blueprint, + defines.input_action.import_blueprint_string, + defines.input_action.import_permissions_string, + defines.input_action.inventory_split, + defines.input_action.inventory_transfer, + defines.input_action.launch_rocket, + defines.input_action.lua_shortcut, + defines.input_action.map_editor_action, + defines.input_action.market_offer, + defines.input_action.mod_settings_changed, + defines.input_action.open_achievements_gui, + defines.input_action.open_blueprint_library_gui, + defines.input_action.open_blueprint_record, + defines.input_action.open_bonus_gui, + defines.input_action.open_character_gui, + defines.input_action.open_equipment, + defines.input_action.open_gui, + defines.input_action.open_item, + defines.input_action.open_kills_gui, + defines.input_action.open_logistic_gui, + defines.input_action.open_mod_item, + defines.input_action.open_production_gui, + defines.input_action.open_technology_gui, + defines.input_action.open_train_gui, + defines.input_action.open_train_station_gui, + defines.input_action.open_trains_gui, + defines.input_action.open_tutorials_gui, + defines.input_action.paste_entity_settings, + defines.input_action.place_equipment, + defines.input_action.quick_bar_pick_slot, + defines.input_action.quick_bar_set_selected_page, + defines.input_action.quick_bar_set_slot, + defines.input_action.remove_cables, + defines.input_action.remove_train_station, + defines.input_action.reset_assembling_machine, + defines.input_action.rotate_entity, + defines.input_action.select_area, + defines.input_action.select_blueprint_entities, + defines.input_action.select_entity_slot, + defines.input_action.select_item, + defines.input_action.select_mapper_slot, + defines.input_action.select_next_valid_gun, + defines.input_action.select_tile_slot, + defines.input_action.set_auto_launch_rocket, + defines.input_action.set_autosort_inventory, + defines.input_action.set_behavior_mode, + defines.input_action.set_car_weapons_control, + defines.input_action.set_circuit_condition, + defines.input_action.set_circuit_mode_of_operation, + defines.input_action.set_deconstruction_item_tile_selection_mode, + defines.input_action.set_deconstruction_item_trees_and_rocks_only, + defines.input_action.set_entity_color, + defines.input_action.set_entity_energy_property, + defines.input_action.set_filter, + defines.input_action.set_heat_interface_mode, + defines.input_action.set_heat_interface_temperature, + defines.input_action.set_infinity_container_filter_item, + defines.input_action.set_infinity_container_remove_unfiltered_items, + defines.input_action.set_infinity_pipe_filter, + defines.input_action.set_inserter_max_stack_size, + defines.input_action.set_inventory_bar, + defines.input_action.set_logistic_filter_item, + defines.input_action.set_logistic_filter_signal, + defines.input_action.set_logistic_trash_filter_item, + defines.input_action.set_request_from_buffers, + defines.input_action.set_research_finished_stops_game, + defines.input_action.set_signal, + defines.input_action.set_single_blueprint_record_icon, + defines.input_action.set_splitter_priority, + defines.input_action.set_train_stopped, + defines.input_action.setup_assembling_machine, + defines.input_action.setup_blueprint, + defines.input_action.setup_single_blueprint_record, + defines.input_action.smart_pipette, + defines.input_action.stack_split, + defines.input_action.stack_transfer, + defines.input_action.start_repair, + defines.input_action.start_research, + defines.input_action.stop_building_by_moving, + defines.input_action.switch_connect_to_logistic_network, + defines.input_action.switch_constant_combinator_state, + defines.input_action.switch_inserter_filter_mode_state, + defines.input_action.switch_power_switch_state, + defines.input_action.switch_to_rename_stop_gui, + defines.input_action.take_equipment, + defines.input_action.toggle_deconstruction_item_entity_filter_mode, + defines.input_action.toggle_deconstruction_item_tile_filter_mode, + defines.input_action.toggle_driving, + defines.input_action.toggle_enable_vehicle_logistics_while_moving, + defines.input_action.toggle_equipment_movement_bonus, + defines.input_action.toggle_map_editor, + defines.input_action.toggle_personal_roboport, + defines.input_action.toggle_show_entity_info, + defines.input_action.translate_string, + defines.input_action.undo, + defines.input_action.upgrade, + defines.input_action.upgrade_opened_blueprint, + defines.input_action.use_artillery_remote, + defines.input_action.use_item, + defines.input_action.wire_dragging } public.self_explode = 60 * 60 * 10 -- time until main shipwreck explosion public.claim_markers = { - "gun-turret", - "laser-turret", - "stone-wall", + 'gun-turret', + 'laser-turret', + 'stone-wall' } public.claim_max_distance = 15 public.base_costs = { - ["gun-turret"] = 1, - ["laser-turret"] = 5, - ["stone-wall"] = 0.1, + ['gun-turret'] = 1, + ['laser-turret'] = 5, + ['stone-wall'] = 0.1 } public.raid_costs = { - { - cost = 1, - chance = 300, - gear = { - { - weap = "shotgun", - ammo = "shotgun-shell", - armor = "light-armor" - }, - { - weap = "pistol", - ammo = "firearm-magazine", - armor = "light-armor" - } - } - }, - { - cost = 15, - chance = 150, - gear = { - { - weap = "shotgun", - ammo = "shotgun-shell", - armor = "light-armor" - }, - { - weap = "pistol", - ammo = "firearm-magazine", - armor = "light-armor" - } - } - }, - { - cost = 30, - chance = 100, - gear = { - { - weap = "shotgun", - ammo = "shotgun-shell", - armor = "light-armor" - }, - { - weap = "pistol", - ammo = "firearm-magazine", - armor = "light-armor" - } - } - }, - { - cost = 40, - chance = 100, - gear = { - { - weap = "shotgun", - ammo = "shotgun-shell", - armor = "heavy-armor" - }, - { - weap = "pistol", - ammo = "firearm-magazine", - armor = "heavy-armor" - } - } - }, - { - cost = 70, - chance = 100, - gear = { - { - weap = "shotgun", - ammo = "piercing-shotgun-shell", - armor = "heavy-armor" - }, - { - weap = "pistol", - ammo = "piercing-rounds-magazine", - armor = "heavy-armor" - } - } - }, + { + cost = 1, + chance = 300, + gear = { + { + weap = 'shotgun', + ammo = 'shotgun-shell', + armor = 'light-armor' + }, + { + weap = 'pistol', + ammo = 'firearm-magazine', + armor = 'light-armor' + } + } + }, + { + cost = 15, + chance = 150, + gear = { + { + weap = 'shotgun', + ammo = 'shotgun-shell', + armor = 'light-armor' + }, + { + weap = 'pistol', + ammo = 'firearm-magazine', + armor = 'light-armor' + } + } + }, + { + cost = 30, + chance = 100, + gear = { + { + weap = 'shotgun', + ammo = 'shotgun-shell', + armor = 'light-armor' + }, + { + weap = 'pistol', + ammo = 'firearm-magazine', + armor = 'light-armor' + } + } + }, + { + cost = 40, + chance = 100, + gear = { + { + weap = 'shotgun', + ammo = 'shotgun-shell', + armor = 'heavy-armor' + }, + { + weap = 'pistol', + ammo = 'firearm-magazine', + armor = 'heavy-armor' + } + } + }, + { + cost = 70, + chance = 100, + gear = { + { + weap = 'shotgun', + ammo = 'piercing-shotgun-shell', + armor = 'heavy-armor' + }, + { + weap = 'pistol', + ammo = 'piercing-rounds-magazine', + armor = 'heavy-armor' + } + } + } } public.wreck_loot = { - ["iron-plate"] = { - rare = 0.1, - count = { 20, 40 }, - }, - ["copper-plate"] = { - rare = 0.1, - count = { 10, 30 }, - }, - ["empty-barrel"] = { - rare = 0.4, - count = { 1, 1}, - }, - ["copper-cable"] = { - rare = 0.5, - count = { 5, 20 }, - }, - ["electronic-circuit"] = { - rare = 0.6, - count = { 5, 20 }, - }, - ["firearm-magazine"] = { - rare = 0.4, - count = { 1, 2 }, - }, - ["steel-plate"] = { - rare = 0.8, - count = { 1, 5 }, - }, - ["explosives"] = { - rare = 0.85, - count = { 1, 5 }, - }, - ["advanced-circuit"] = { - rare = 0.9, - count = { 1, 5 }, - }, - ["processing-unit"] = { - rare = 0.95, - count = { 1, 2 }, - }, - ["electric-engine-unit"] = { - rare = 0.95, - count = { 1, 1 }, - }, - ["battery"] = { - rare = 0.95, - count = { 1, 2 }, - }, - ["piercing-rounds-magazine"] = { - rare = 0.99, - count = { 1, 2 }, - }, + ['iron-plate'] = { + rare = 0.1, + count = {20, 40} + }, + ['copper-plate'] = { + rare = 0.1, + count = {10, 30} + }, + ['empty-barrel'] = { + rare = 0.4, + count = {1, 1} + }, + ['copper-cable'] = { + rare = 0.5, + count = {5, 20} + }, + ['electronic-circuit'] = { + rare = 0.6, + count = {5, 20} + }, + ['firearm-magazine'] = { + rare = 0.4, + count = {1, 2} + }, + ['steel-plate'] = { + rare = 0.8, + count = {1, 5} + }, + ['explosives'] = { + rare = 0.85, + count = {1, 5} + }, + ['advanced-circuit'] = { + rare = 0.9, + count = {1, 5} + }, + ['processing-unit'] = { + rare = 0.95, + count = {1, 2} + }, + ['electric-engine-unit'] = { + rare = 0.95, + count = {1, 1} + }, + ['battery'] = { + rare = 0.95, + count = {1, 2} + }, + ['piercing-rounds-magazine'] = { + rare = 0.99, + count = {1, 2} + } } public.technologies = { - ["military"] = true, - ["artillery"] = false, - ["artillery-shell-range-1"] = false, - ["artillery-shell-speed-1"] = false, - ["automation-3"] = false, - ["battery-equipment"] = false, - ["battery-mk2-equipment"] = false, - ["belt-immunity-equipment"] = false, - ["distractor"] = false, - ["destroyer"] = false, - ["discharge-defense-equipment"] = false, - ["energy-shield-equipment"] = false, - ["energy-shield-mk2-equipment"] = false, - ["exoskeleton-equipment"] = false, - ["explosive-rocketry"] = false, - ["flamethrower"] = false, - ["fusion-reactor-equipment"] = false, - ["kovarex-enrichment-process"] = false, - ["land-mine"] = false, - ["logistics-3"] = false, - ["military-4"] = false, - ["modular-armor"] = false, - ["night-vision-equipment"] = false, - ["nuclear-fuel-reprocessing"] = false, - ["nuclear-power"] = false, - ["personal-laser-defense-equipment"] = false, - ["personal-roboport-equipment"] = false, - ["personal-roboport-mk2-equipment"] = false, - ["power-armor"] = false, - ["power-armor-mk2"] = false, - ["refined-flammables-1"] = false, - ["refined-flammables-2"] = false, - ["refined-flammables-3"] = false, - ["refined-flammables-4"] = false, - ["refined-flammables-5"] = false, - ["refined-flammables-6"] = false, - ["refined-flammables-7"] = false, - ["rocketry"] = false, - ["solar-panel-equipment"] = false, - ["stack-inserter"] = false, - ["stronger-explosives-2"] = false, - ["stronger-explosives-3"] = false, - ["stronger-explosives-4"] = false, - ["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-shooting-speed-2"] = false, - ["laser-shooting-speed-3"] = false, - ["laser-shooting-speed-4"] = false, - ["laser-shooting-speed-5"] = false, - ["laser-shooting-speed-6"] = false, - ["laser-shooting-speed-7"] = false, - ["follower-robot-count-2"] = false, - ["follower-robot-count-3"] = false, - ["follower-robot-count-4"] = false, - ["follower-robot-count-5"] = false, - ["follower-robot-count-6"] = false, - ["follower-robot-count-7"] = false, - ["tank"] = false, - ["uranium-ammo"] = false, - ["uranium-processing"] = false, - ["atomic-bomb"] = false, + ['military'] = true, + ['artillery'] = false, + ['artillery-shell-range-1'] = false, + ['artillery-shell-speed-1'] = false, + ['automation-3'] = false, + ['battery-equipment'] = false, + ['battery-mk2-equipment'] = false, + ['belt-immunity-equipment'] = false, + ['distractor'] = false, + ['destroyer'] = false, + ['discharge-defense-equipment'] = false, + ['energy-shield-equipment'] = false, + ['energy-shield-mk2-equipment'] = false, + ['exoskeleton-equipment'] = false, + ['explosive-rocketry'] = false, + ['flamethrower'] = false, + ['fusion-reactor-equipment'] = false, + ['kovarex-enrichment-process'] = false, + ['land-mine'] = false, + ['logistics-3'] = false, + ['military-4'] = false, + ['modular-armor'] = false, + ['night-vision-equipment'] = false, + ['nuclear-fuel-reprocessing'] = false, + ['nuclear-power'] = false, + ['personal-laser-defense-equipment'] = false, + ['personal-roboport-equipment'] = false, + ['personal-roboport-mk2-equipment'] = false, + ['power-armor'] = false, + ['power-armor-mk2'] = false, + ['refined-flammables-1'] = false, + ['refined-flammables-2'] = false, + ['refined-flammables-3'] = false, + ['refined-flammables-4'] = false, + ['refined-flammables-5'] = false, + ['refined-flammables-6'] = false, + ['refined-flammables-7'] = false, + ['rocketry'] = false, + ['solar-panel-equipment'] = false, + ['stack-inserter'] = false, + ['stronger-explosives-2'] = false, + ['stronger-explosives-3'] = false, + ['stronger-explosives-4'] = false, + ['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-shooting-speed-2'] = false, + ['laser-shooting-speed-3'] = false, + ['laser-shooting-speed-4'] = false, + ['laser-shooting-speed-5'] = false, + ['laser-shooting-speed-6'] = false, + ['laser-shooting-speed-7'] = false, + ['follower-robot-count-2'] = false, + ['follower-robot-count-3'] = false, + ['follower-robot-count-4'] = false, + ['follower-robot-count-5'] = false, + ['follower-robot-count-6'] = false, + ['follower-robot-count-7'] = false, + ['tank'] = false, + ['uranium-ammo'] = false, + ['uranium-processing'] = false, + ['atomic-bomb'] = false } public.merchant_offer = { - { - price = { - { - type = "item", - name = "iron-plate", - amount = 10 - }, - { - type = "item", - name = "advanced-circuit", - amount = 2 - }, - { - type = "item", - name = "battery", - amount = 2 - }, - { - type = "item", - name = "small-lamp", - amount = 2 - }, - { - type = "item", - name = "copper-cable", - amount = 5 - }, - { - type = "item", - name = "steel-plate", - amount = 1 - }, - }, - offer = { - type = "nothing", - effect_description = "Construct a GPS receiver" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 100 - } - }, - offer = { - type = "give-item", - item = "heavy-armor" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 500 - } - }, - offer = { - type = "give-item", - item = "modular-armor" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 12000 - } - }, - offer = { - type = "give-item", - item = "power-armor" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 100 - } - }, - offer = { - type = "give-item", - item = "night-vision-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 70 - } - }, - offer = { - type = "give-item", - item = "battery-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 700 - } - }, - offer = { - type = "give-item", - item = "battery-mk2-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 1200 - } - }, - offer = { - type = "give-item", - item = "exoskeleton-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 2500 - } - }, - offer = { - type = "give-item", - item = "fusion-reactor-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 200 - } - }, - offer = { - type = "give-item", - item = "personal-roboport-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 400 - } - }, - offer = { - type = "give-item", - item = "personal-roboport-mk2-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 90 - } - }, - offer = { - type = "give-item", - item = "solar-panel-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 300 - } - }, - offer = { - type = "give-item", - item = "energy-shield-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 2000 - } - }, - offer = { - type = "give-item", - item = "energy-shield-mk2-equipment" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 50 - } - }, - offer = { - type = "give-item", - 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 = { - { - type = "item", - name = "coin", - amount = 1000 - } - }, - offer = { - type = "unlock-recipe", - recipe = "flamethrower" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 50 - } - }, - offer = { - type = "give-item", - item = "defender-capsule" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 200 - } - }, - offer = { - type = "give-item", - item = "distractor-capsule" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 250 - } - }, - offer = { - type = "give-item", - item = "destroyer-capsule" - } - }, - { - price = { - { - type = "item", - name = "coin", - amount = 70 - } - }, - offer = { - type = "unlock-recipe", - recipe = "filter-inserter" - }, - }, - { - price = { - { - type = "item", - name = "coin", - amount = 100 - } - }, - offer = { - type = "unlock-recipe", - recipe = "stack-inserter" - }, - }, - { - price = { - { - type = "item", - name = "coin", - amount = 170 - } - }, - offer = { - type = "unlock-recipe", - recipe = "stack-filter-inserter" - }, - }, - { - price = { - { - type = "item", - name = "coin", - amount = 65000 - } - }, - offer = { - type = "give-item", - item = "computer" - } - }, + { + price = { + { + type = 'item', + name = 'iron-plate', + amount = 10 + }, + { + type = 'item', + name = 'advanced-circuit', + amount = 2 + }, + { + type = 'item', + name = 'battery', + amount = 2 + }, + { + type = 'item', + name = 'small-lamp', + amount = 2 + }, + { + type = 'item', + name = 'copper-cable', + amount = 5 + }, + { + type = 'item', + name = 'steel-plate', + amount = 1 + } + }, + offer = { + type = 'nothing', + effect_description = 'Construct a GPS receiver' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 100 + } + }, + offer = { + type = 'give-item', + item = 'heavy-armor' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 500 + } + }, + offer = { + type = 'give-item', + item = 'modular-armor' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 12000 + } + }, + offer = { + type = 'give-item', + item = 'power-armor' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 100 + } + }, + offer = { + type = 'give-item', + item = 'night-vision-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 70 + } + }, + offer = { + type = 'give-item', + item = 'battery-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 700 + } + }, + offer = { + type = 'give-item', + item = 'battery-mk2-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 1200 + } + }, + offer = { + type = 'give-item', + item = 'exoskeleton-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 2500 + } + }, + offer = { + type = 'give-item', + item = 'fusion-reactor-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 200 + } + }, + offer = { + type = 'give-item', + item = 'personal-roboport-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 400 + } + }, + offer = { + type = 'give-item', + item = 'personal-roboport-mk2-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 90 + } + }, + offer = { + type = 'give-item', + item = 'solar-panel-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 300 + } + }, + offer = { + type = 'give-item', + item = 'energy-shield-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 2000 + } + }, + offer = { + type = 'give-item', + item = 'energy-shield-mk2-equipment' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 50 + } + }, + offer = { + type = 'give-item', + 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 = { + { + type = 'item', + name = 'coin', + amount = 1000 + } + }, + offer = { + type = 'unlock-recipe', + recipe = 'flamethrower' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 50 + } + }, + offer = { + type = 'give-item', + item = 'defender-capsule' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 200 + } + }, + offer = { + type = 'give-item', + item = 'distractor-capsule' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 250 + } + }, + offer = { + type = 'give-item', + item = 'destroyer-capsule' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 70 + } + }, + offer = { + type = 'unlock-recipe', + recipe = 'filter-inserter' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 100 + } + }, + offer = { + type = 'unlock-recipe', + recipe = 'stack-inserter' + } + }, + { + price = { + { + type = 'item', + name = 'coin', + amount = 170 + } + }, + offer = { + type = 'unlock-recipe', + recipe = 'stack-filter-inserter' + } + } } -public.manual = [[ +public.manual = + [[ [font=heading-1]Planet Prison (1.1.0) - 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. diff --git a/maps/planet_prison/mod/ai.lua b/maps/planet_prison/mod/ai.lua index 7158863a..bae21112 100644 --- a/maps/planet_prison/mod/ai.lua +++ b/maps/planet_prison/mod/ai.lua @@ -1,187 +1,182 @@ local public = {} -local _common = require(".common") +local _common = require('.common') public.command = { - --[[ + --[[ @param args nil --]] - noop = 0, - - --[[ + noop = 0, + --[[ @param args nil --]] - seek_and_destroy_player = 1, - - --[[ + seek_and_destroy_player = 1, + --[[ @param args = { agents, // All movable agents positions, // Table of positions to attack } --]] - attack_objects = 2 + attack_objects = 2 } --[[ 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") - local dest_x = _common.get_axis(dest, "x") - local dest_y = _common.get_axis(dest, "y") + local src_x = _common.get_axis(src, 'x') + local src_y = _common.get_axis(src, 'y') + local dest_x = _common.get_axis(dest, 'x') + local dest_y = _common.get_axis(dest, 'y') - local step = { - x = nil, - y = nil - } + local step = { + x = nil, + y = nil + } - local precision = _common.rand_range(1, 10) - if dest_x - precision > src_x then - step.x = 1 - elseif dest_x < src_x - precision then - step.x = -1 - else - step.x = 0 - end + local precision = _common.rand_range(1, 10) + if dest_x - precision > src_x then + step.x = 1 + elseif dest_x < src_x - precision then + step.x = -1 + else + step.x = 0 + end - if dest_y - precision > src_y then - step.y = 1 - elseif dest_y < src_y - precision then - step.y = -1 - else - step.y = 0 - end + if dest_y - precision > src_y then + step.y = 1 + elseif dest_y < src_y - precision then + step.y = -1 + else + step.y = 0 + end - return _common.direction_lookup[step.x][step.y] + return _common.direction_lookup[step.x][step.y] end local function _move_to(ent, trgt, min_distance) - local state = { - walking = false, - } + local state = { + walking = false + } - local distance = _common.get_distance(trgt.position, ent.position) - if min_distance < distance then - local dir = _get_direction(ent.position, trgt.position) - if dir then - state = { - walking = true, - direction = dir - } - end - end + local distance = _common.get_distance(trgt.position, ent.position) + if min_distance < distance then + local dir = _get_direction(ent.position, trgt.position) + if dir then + state = { + walking = true, + direction = dir + } + end + end - ent.walking_state = state - return state.walking + ent.walking_state = state + return state.walking end local function _shoot_at(ent, trgt) - ent.shooting_state = { - state = defines.shooting.shooting_enemies, - position = trgt.position - } + ent.shooting_state = { + state = defines.shooting.shooting_enemies, + position = trgt.position + } end local function _shoot_stop(ent) - ent.shooting_state = { - state = defines.shooting.not_shooting, - position = {0, 0} - } + ent.shooting_state = { + state = defines.shooting.not_shooting, + position = {0, 0} + } end local function _do_job_seek_and_destroy_player(surf) - for _, player in pairs(game.players) do - if player.character == nil then - goto continue - end + for _, player in pairs(game.players) do + if player.character == nil then + goto continue + end - local search_info = { - name = "character", - position = player.character.position, - radius = 20, - force = "enemy", - } + local search_info = { + name = 'character', + position = player.character.position, + radius = 20, + force = 'enemy' + } - local ents = surf.find_entities_filtered(search_info) - if not ents or #ents == 0 then - goto continue - end + local ents = surf.find_entities_filtered(search_info) + if not ents or #ents == 0 then + goto continue + end - for _, e in pairs(ents) do - if not _move_to(e, player.character, _common.rand_range(5, 10)) then - _shoot_at(e, player.character) - else - _shoot_stop(e) - end - end + for _, e in pairs(ents) do + if not _move_to(e, player.character, _common.rand_range(5, 10)) then + _shoot_at(e, player.character) + else + _shoot_stop(e) + end + end - ::continue:: - end + ::continue:: + end end local function _do_job_attack_objects(surf, args) - local agents = args.agents - if #agents == 0 then - return - end + local agents = args.agents + if #agents == 0 then + return + end - local objects = args.objects - local target, closest, agent, query - for i = #agents, 1, -1 do - agent = agents[i] - if not agent.valid then - table.remove(agents, i) - goto continue - end - - if game.tick % i ~= 0 then - goto continue - end - - query = { - position = agent.position, - radius = 15, - type = { - "projectile", - "beam" - }, - force = { - "enemy", - "player", - "neutral", - }, - invert = true - } - closest = surf.find_entities_filtered(query) - if #closest ~= 0 then - target = _common.get_closest_neighbour(agent.position, closest) - else - if #objects == 0 then - _shoot_stop(agent) + local objects = args.objects + local target, closest, agent, query + for i = #agents, 1, -1 do + agent = agents[i] + if not agent.valid then + table.remove(agents, i) goto continue - end + end - target = _common.get_closest_neighbour(agent.position, objects) - end + if game.tick % i ~= 0 then + goto continue + end - if target == nil or not target.valid then - goto continue - end + query = { + position = agent.position, + radius = 15, + type = { + 'projectile', + 'beam' + }, + force = { + 'enemy', + 'player', + 'neutral' + }, + invert = true + } + closest = surf.find_entities_filtered(query) + if #closest ~= 0 then + target = _common.get_closest_neighbour(agent.position, closest) + else + if #objects == 0 then + _shoot_stop(agent) + goto continue + end - if not _move_to(agent, target, _common.rand_range(5, 15)) then - _shoot_at(agent, target) - else - _shoot_stop(agent) - end + target = _common.get_closest_neighbour(agent.position, objects) + end - ::continue:: - end + if target == nil or not target.valid then + goto continue + end + + if not _move_to(agent, target, _common.rand_range(5, 15)) then + _shoot_at(agent, target) + else + _shoot_stop(agent) + end + + ::continue:: + end end --[[ @@ -190,15 +185,15 @@ do_job - Perform non-stateful operation on all enemy "character" entities. @param command - Command to perform on all non-player controllable characters. --]] public.do_job = function(surf, command, args) - if args == nil then - args = {} - end + if args == nil then + args = {} + end - if command == public.command.seek_and_destroy_player then - _do_job_seek_and_destroy_player(surf) - elseif command == public.command.attack_objects then - _do_job_attack_objects(surf, args) - end + if command == public.command.seek_and_destroy_player then + _do_job_seek_and_destroy_player(surf) + elseif command == public.command.attack_objects then + _do_job_attack_objects(surf, args) + end end return public diff --git a/maps/planet_prison/mod/bp.lua b/maps/planet_prison/mod/bp.lua index 8c7d498d..0b384d49 100644 --- a/maps/planet_prison/mod/bp.lua +++ b/maps/planet_prison/mod/bp.lua @@ -1,12 +1,19 @@ local public = {} -local _common = require(".common") +local _common = require('.common') +local Global = require 'utils.global' +local Token = require 'utils.token' + +local this = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) public.init = function() - if global.this == nil then - global.this = {} - end - - global.this._bps = {} + this._bps = {} end --[[ @@ -15,12 +22,12 @@ push_blueprint - Pushes blueprint into a list. @param bp - Blueprint in JSON format. --]] public.push_blueprint = function(name, bp) - local entry = { - bp = game.json_to_table(bp).blueprint, - hook = nil, - refs = {} - } - global.this._bps[name] = entry + local entry = { + bp = game.json_to_table(bp).blueprint, + hook = nil, + refs = {} + } + this._bps[name] = entry end --[[ @@ -29,17 +36,19 @@ set_blueprint_hook - Set callback to a blueprint. @param hook - Callback that will be called after blueprint is placed. --]] public.set_blueprint_hook = function(name, hook) - if name == nil then - log("bp.set_blueprint_hook: name is nil") - return - end + if name == nil then + log('bp.set_blueprint_hook: name is nil') + return + end - if global.this._bps[name] == nil then - log("bp.set_blueprint_hook: unrecognized blueprint") - return - end + if this._bps[name] == nil then + log('bp.set_blueprint_hook: unrecognized blueprint') + return + end - global.this._bps[name].hook = hook + local token = Token.register(hook) + + this._bps[name].hook = token end --[[ @@ -47,18 +56,18 @@ get_references - Get all references of the blueprint on the map. @param name - Blueprint handle. --]] public.get_references = function(name) - if name == nil then - log("bp.get_references: name is nil") - return {} - end + if name == nil then + log('bp.get_references: name is nil') + return {} + end - local object = global.this._bps[name] - if object == nil then - log("bp.get_references: unrecognized blueprint") - return {} - end + local object = this._bps[name] + if object == nil then + log('bp.get_references: unrecognized blueprint') + return {} + end - return object.refs + return object.refs end --[[ @@ -66,18 +75,18 @@ get_references - Gets opaque object representing bp references. @param name - Blueprint handle. --]] public.get_references = function(name) - if name == nil then - log("bp.get_references: name is nil") - return - end + if name == nil then + log('bp.get_references: name is nil') + return + end - local object = global.this._bps[name] - if object == nil then - log("bp.get_references: unrecognized blueprint") - return - end + local object = this._bps[name] + if object == nil then + log('bp.get_references: unrecognized blueprint') + return + end - return object.refs + return object.refs end --[[ @@ -85,7 +94,7 @@ reference_get_bounding_box - Return bounding box from the reference. @param reference - Valid reference object fetched from get_references. --]] public.reference_get_bounding_box = function(reference) - return reference.bb + return reference.bb end --[[ @@ -93,7 +102,7 @@ reference_get_entities - Return references to entities. @param reference - Valid reference object fetched from get_references. --]] public.reference_get_entities = function(reference) - return reference.entities + return reference.entities end --[[ @@ -101,10 +110,9 @@ reference_get_timestamp - Return timestamp of a reference @param reference - Valid reference object fetched from get_references. --]] public.reference_get_timestamp = function(reference) - return reference.timestamp + return reference.timestamp end - --[[ unlink_references_filtered - Unlinks all references of blueprint on the map if they meet the query rules. @@ -115,32 +123,32 @@ unlinked. @return An array of unlinked references. --]] public.unlink_references_filtered = function(name, query) - if name == nil then - log("bp.get_references: name is nil") - return - end + if name == nil then + log('bp.get_references: name is nil') + return + end - local object = global.this._bps[name] - if object == nil then - log("bp.get_references: unrecognized blueprint") - return - end + local object = this._bps[name] + if object == nil then + log('bp.get_references: unrecognized blueprint') + return + end - local refs = {} - for i = #object.refs, 1, -1 do - local ref = object.refs[i] - if query and query.timestamp then - if ref.timestamp > query.timestamp then - goto continue - end - end + local refs = {} + for i = #object.refs, 1, -1 do + local ref = object.refs[i] + if query and query.timestamp then + if ref.timestamp > query.timestamp then + goto continue + end + end - table.insert(refs, ref) - table.remove(object.refs, i) - ::continue:: - end + table.insert(refs, ref) + table.remove(object.refs, i) + ::continue:: + end - return refs + return refs end --[[ @@ -153,42 +161,42 @@ meet the query rules. removed. --]] public.destroy_references_filtered = function(surf, name, query) - if name == nil then - log("bp.get_references: name is nil") - return - end + if name == nil then + log('bp.get_references: name is nil') + return + end - local object = global.this._bps[name] - if object == nil then - log("bp.get_references: unrecognized blueprint") - return - end + local object = this._bps[name] + if object == nil then + log('bp.get_references: unrecognized blueprint') + return + end - for i = 1, #object.refs do - local ref = object.refs[i] - if query and query.timestamp then - if ref.timestamp > query.timestamp then - goto continue - end - end + for i = 1, #object.refs do + local ref = object.refs[i] + if query and query.timestamp then + if ref.timestamp > query.timestamp then + goto continue + end + end - for _, ent in pairs(ref.entities) do - if ent.valid then - ent.destroy() - end - end + for _, ent in pairs(ref.entities) do + if ent.valid then + ent.destroy() + end + end - local tiles = {} - for _, tile in pairs(ref.tiles) do - tile.name = "concrete" - table.insert(tiles, tile) - end + local tiles = {} + for _, tile in pairs(ref.tiles) do + tile.name = 'concrete' + table.insert(tiles, tile) + end - surf.set_tiles(tiles) + surf.set_tiles(tiles) - table.remove(object.refs, i) - ::continue:: - end + table.remove(object.refs, i) + ::continue:: + end end --[[ @@ -197,28 +205,28 @@ destroy_references - Destroys all references of blueprint on the map @param name - Blueprint handle. --]] public.destroy_references = function(surf, name) - public.destroy_references_filtered(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 _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 + 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 + tile.name = 'concrete' + table.insert(tiles, tile) + ::continue:: + end - surf.set_tiles(tiles) + surf.set_tiles(tiles) end --[[ @@ -227,64 +235,64 @@ destroy_reference - Destroys reference of a blueprint at given surface. @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 + for _, meta in pairs(this._bps) do + for i = 1, #meta.refs do + local ref = meta.refs[i] + if reference.id == ref.id then + _bp_destroy_reference(surf, ref) + table.remove(meta.refs, i) + return + end + end + end end local function _build_tiles(surf, point, tiles) - local _tiles = {} + local _tiles = {} - local get_axis = _common.get_axis - for _, tile in pairs(tiles) do - local _tile = { - name = tile.name, - position = { - x = get_axis(tile.position, "x") + get_axis(point, "x"), - y = get_axis(tile.position, "y") + get_axis(point, "y") - } - } - table.insert(_tiles, _tile) - end + local get_axis = _common.get_axis + for _, tile in pairs(tiles) do + local _tile = { + name = tile.name, + position = { + x = get_axis(tile.position, 'x') + get_axis(point, 'x'), + y = get_axis(tile.position, 'y') + get_axis(point, 'y') + } + } + table.insert(_tiles, _tile) + end - surf.set_tiles(_tiles) - return _tiles + surf.set_tiles(_tiles) + return _tiles end - local function _build_entities(surf, point, entities, hook, args) - local _entities = {} + local _entities = {} - local get_axis = _common.get_axis - for _, ent in pairs(entities) do - local ent_info = { - position = { - x = get_axis(ent.position, "x") + get_axis(point, "x"), - y = get_axis(ent.position, "y") + get_axis(point, "y") - }, - name = ent.name, - } - local e = surf.create_entity(ent_info) - if not e or not e.valid then - goto continue - end + local get_axis = _common.get_axis + for _, ent in pairs(entities) do + local ent_info = { + position = { + x = get_axis(ent.position, 'x') + get_axis(point, 'x'), + y = get_axis(ent.position, 'y') + get_axis(point, 'y') + }, + name = ent.name + } + local e = surf.create_entity(ent_info) + if not e or not e.valid then + goto continue + end - if hook then - hook(e, args) - end + if hook then + local token = Token.get(hook) + token(e, args) + end - table.insert(_entities, e) - ::continue:: - end + table.insert(_entities, e) + ::continue:: + end - return _entities + return _entities end --[[ @@ -295,79 +303,79 @@ build - Place blueprint at given point. @param args - If hook was set, this will be argument passed. --]] public.build = function(surf, name, point, args) - if surf == nil then - log("bp.build: surf is nil") - return - end + if surf == nil then + log('bp.build: surf is nil') + return + end - if name == nil then - log("bp.build: name is nil") - return - end + if name == nil then + log('bp.build: name is nil') + return + end - local object = global.this._bps[name] - if object == nil then - log("bp.set_blueprint_hook: unrecognized blueprint") - return - end + local object = this._bps[name] + if object == nil then + log('bp.set_blueprint_hook: unrecognized blueprint') + return + end - local instance = { - entities = {}, - tiles = {}, - bb = nil, - timestamp = game.tick, - } - local bbs = {} - local tiles = object.bp.tiles - if tiles and #tiles > 0 then - instance.tiles = _build_tiles(surf, point, tiles) - local bb = _common.create_bounding_box_by_points(instance.tiles) - table.insert(bbs, bb) + local instance = { + entities = {}, + tiles = {}, + bb = nil, + timestamp = game.tick + } + local bbs = {} + local tiles = object.bp.tiles + if tiles and #tiles > 0 then + instance.tiles = _build_tiles(surf, point, tiles) + local bb = _common.create_bounding_box_by_points(instance.tiles) + table.insert(bbs, bb) - local query = { - name = "character", - area = bb, - invert = true, - } - for _, ent in pairs(surf.find_entities_filtered(query)) do - if ent.valid then - ent.destroy() - end - end - end - - local entities = object.bp.entities - if entities and #entities > 0 then - instance.entities = _build_entities(surf, point, entities, object.hook, args) - local bb = _common.create_bounding_box_by_points(instance.entities) - table.insert(bbs, bb) - - local query = { - name = "character", - area = bb, - invert = true, - } - for _, ent_found in pairs(surf.find_entities_filtered(query)) do - if not ent_found.valid then - goto continue - end - - for _, ent_spawned in pairs(instance.entities) do - if ent_found == ent_spawned then - goto continue + local query = { + name = 'character', + area = bb, + invert = true + } + for _, ent in pairs(surf.find_entities_filtered(query)) do + if ent.valid then + ent.destroy() end - end + end + end - ent_found.die() - ::continue:: - end - end + local entities = object.bp.entities + if entities and #entities > 0 then + instance.entities = _build_entities(surf, point, entities, object.hook, args) + local bb = _common.create_bounding_box_by_points(instance.entities) + table.insert(bbs, bb) - instance.bb = _common.merge_bounding_boxes(bbs) - instance.id = game.tick - table.insert(object.refs, instance) + local query = { + name = 'character', + area = bb, + invert = true + } + for _, ent_found in pairs(surf.find_entities_filtered(query)) do + if not ent_found.valid then + goto continue + end - return instance + for _, ent_spawned in pairs(instance.entities) do + if ent_found == ent_spawned then + goto continue + end + end + + ent_found.die() + ::continue:: + end + end + + instance.bb = _common.merge_bounding_boxes(bbs) + instance.id = game.tick + table.insert(object.refs, instance) + + return instance end return public diff --git a/maps/planet_prison/mod/claims.lua b/maps/planet_prison/mod/claims.lua index a66ece17..f252a8b1 100644 --- a/maps/planet_prison/mod/claims.lua +++ b/maps/planet_prison/mod/claims.lua @@ -1,5 +1,15 @@ local public = {} -local common = require(".common") +local common = require('.common') +local Global = require 'utils.global' + +local this = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) --[[ init - Initialize claim system. @@ -7,89 +17,85 @@ init - Initialize claim system. @param max_distance - Maximal distance allowed between markers --]] public.init = function(names, max_distance) - if global.this == nil then - global.this = {} - end + if type(names) ~= 'table' then + names = {names} + end - if type(names) ~= "table" then - names = { names } - end - - global.this._claims_info = {} - global.this._claims_visible_to = {} - global.this._claim_markers = names - global.this._claim_max_dist = max_distance + this._claims_info = {} + this._claims_visible_to = {} + this._claim_markers = names + this._claim_max_dist = max_distance end local function claim_new_claim(ent, deps) - local comm = deps.common - local point = { - { - x = comm.get_axis(ent.position, "x"), - y = comm.get_axis(ent.position, "y"), - } - } + local comm = deps.common + local point = { + { + x = comm.get_axis(ent.position, 'x'), + y = comm.get_axis(ent.position, 'y') + } + } - local claims = global.this._claims_info - if claims[ent.force.name] == nil then - claims[ent.force.name] = {} - claims[ent.force.name].polygons = {} - claims[ent.force.name].claims = {} - claims[ent.force.name].collections = {} - end + local claims = this._claims_info + if claims[ent.force.name] == nil then + claims[ent.force.name] = {} + claims[ent.force.name].polygons = {} + claims[ent.force.name].claims = {} + claims[ent.force.name].collections = {} + end - table.insert(claims[ent.force.name].collections, point) + table.insert(claims[ent.force.name].collections, point) end local function claim_on_build_entity(ent, deps) - local max_dist = global.this._claim_max_dist - local force = ent.force.name - local comm = deps.common - local data = global.this._claims_info[force] + local max_dist = this._claim_max_dist + local force = ent.force.name + local comm = deps.common + local data = this._claims_info[force] - if data == nil then - claim_new_claim(ent, deps) - return - end + if data == nil then + claim_new_claim(ent, deps) + return + end - local in_range = false - local collections = data.collections - for i = 1, #collections do - local points = collections[i] + local in_range = false + local collections = data.collections + for i = 1, #collections do + local points = collections[i] - for _, point in pairs(points) do - point = point - local dist = comm.get_distance(point, ent.position) - if max_dist < dist then - goto continue - end + for _, point in pairs(points) do + point = point + local dist = comm.get_distance(point, ent.position) + if max_dist < dist then + goto continue + end - in_range = true - point = { - x = comm.get_axis(ent.position, "x"), - y = comm.get_axis(ent.position, "y"), - } - table.insert(points, point) - data.claims[i] = comm.get_convex_hull(points) + in_range = true + point = { + x = comm.get_axis(ent.position, 'x'), + y = comm.get_axis(ent.position, 'y') + } + table.insert(points, point) + data.claims[i] = comm.get_convex_hull(points) - break - ::continue:: - end - end + break + ::continue:: + end + end - if not in_range then - claim_new_claim(ent, deps) - end + if not in_range then + claim_new_claim(ent, deps) + end end local function claims_in_markers(name) - for _, marker in pairs(global.this._claim_markers) do - if name == marker then - return true - end - end + for _, marker in pairs(this._claim_markers) do + if name == marker then + return true + end + end - return false + return false end --[[ @@ -97,47 +103,47 @@ on_build_entity - Event processing function. @param ent - Entity --]] public.on_built_entity = function(ent) - if not claims_in_markers(ent.name) then - return - end + if not claims_in_markers(ent.name) then + return + end - local deps = { - common = common, - } - claim_on_build_entity(ent, deps) + local deps = { + common = common + } + claim_on_build_entity(ent, deps) end local function claim_on_entity_died(ent, deps) - local comm = deps.common - local force = ent.force.name - local data = global.this._claims_info[force] - if data == nil then - return - end + local comm = deps.common + local force = ent.force.name + local data = this._claims_info[force] + if data == nil then + return + end - for i = 1, #data.collections do - local points = data.collections[i] + for i = 1, #data.collections do + local points = data.collections[i] - for j = 1, #points do - local point = points[j] - if comm.positions_equal(point, ent.position) then - table.remove(points, j) + for j = 1, #points do + local point = points[j] + if comm.positions_equal(point, ent.position) then + table.remove(points, j) - data.claims[i] = comm.get_convex_hull(points) + data.claims[i] = comm.get_convex_hull(points) + break + end + end + + if #points == 0 then + table.remove(data.claims, i) + table.remove(data.collections, i) break - end - end + end + end - if #points == 0 then - table.remove(data.claims, i) - table.remove(data.collections, i) - break - end - end - - if #data.claims == 0 then - global.this._claims_info[force] = nil - end + if #data.claims == 0 then + this._claims_info[force] = nil + end end --[[ @@ -145,14 +151,14 @@ on_entity_died - Event processing function. @param ent - Entity --]] public.on_entity_died = function(ent) - if not claims_in_markers(ent.name) then - return - end + if not claims_in_markers(ent.name) then + return + end - local deps = { - common = common, - } - claim_on_entity_died(ent, deps) + local deps = { + common = common + } + claim_on_entity_died(ent, deps) end --[[ @@ -160,7 +166,7 @@ on_player_mined_entity - Event processing function. @param ent - Entity --]] public.on_player_mined_entity = function(ent) - public.on_entity_died(ent) + public.on_entity_died(ent) end --[[ @@ -168,7 +174,7 @@ on_player_died - Event processing function @param player - Player --]] public.on_player_died = function(player) - global.this._claims_info[player.name] = nil + this._claims_info[player.name] = nil end --[[ @@ -176,33 +182,33 @@ get_claims - Get all claims data points for given force. @param f_name - Force name. --]] public.get_claims = function(f_name) - if global.this._claims_info[f_name] == nil then - return {} - end + if this._claims_info[f_name] == nil then + return {} + end - return global.this._claims_info[f_name].claims + return this._claims_info[f_name].claims end local function claims_update_visiblity() - if #global.this._claims_visible_to == 0 then - for _, info in pairs(global.this._claims_info) do - for _, id in pairs(info.polygons) do - if rendering.is_valid(id) then - rendering.set_visible(id, false) + if #this._claims_visible_to == 0 then + for _, info in pairs(this._claims_info) do + for _, id in pairs(info.polygons) do + if rendering.is_valid(id) then + rendering.set_visible(id, false) + end end - end - end - return - end + end + return + end - for _, info in pairs(global.this._claims_info) do - for _, id in pairs(info.polygons) do - if rendering.is_valid(id) then - rendering.set_visible(id, true) - rendering.set_players(id, global.this._claims_visible_to) - end - end - end + for _, info in pairs(this._claims_info) do + for _, id in pairs(info.polygons) do + if rendering.is_valid(id) then + rendering.set_visible(id, true) + rendering.set_players(id, this._claims_visible_to) + end + end + end end --[[ @@ -210,14 +216,14 @@ set_visibility_to - Specifies who can see the claims and redraws. @param name - Name of a player. --]] public.set_visibility_to = function(name) - for _, p in pairs(global.this._claims_visible_to) do - if p == name then - return - end - end + for _, p in pairs(this._claims_visible_to) do + if p == name then + return + end + end - table.insert(global.this._claims_visible_to, name) - claims_update_visiblity() + table.insert(this._claims_visible_to, name) + claims_update_visiblity() end --[[ @@ -225,14 +231,14 @@ remove_visibility_from - Remove the claim visibility from the player. @param name - Name of a player. --]] public.remove_visibility_from = function(name) - for i = 1, #global.this._claims_visible_to do - local p = global.this._claims_visible_to[i] - if p == name then - table.remove(global.this._claims_visible_to, i) - claims_update_visiblity() - break - end - end + for i = 1, #this._claims_visible_to do + local p = this._claims_visible_to[i] + if p == name then + table.remove(this._claims_visible_to, i) + claims_update_visiblity() + break + end + end end return public diff --git a/maps/planet_prison/mod/common.lua b/maps/planet_prison/mod/common.lua index 3f13719e..1f1eb75f 100644 --- a/maps/planet_prison/mod/common.lua +++ b/maps/planet_prison/mod/common.lua @@ -1,8 +1,18 @@ local public = {} +local Global = require 'utils.global' + +local this = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) public.init = function() - if global.this == nil then - global.this = {} + if this == nil then + this = {} end end @@ -12,11 +22,11 @@ rand_range - Return random integer within the range. @param stop - Stop range. --]] public.rand_range = function(start, stop) - if not global.this.rng then - global.this.rng = game.create_random_generator() + if not this.rng then + this.rng = game.create_random_generator() end - return global.this.rng(start, stop) + return this.rng(start, stop) end --[[ diff --git a/maps/planet_prison/mod/layers.lua b/maps/planet_prison/mod/layers.lua index ca6e9935..741eb401 100644 --- a/maps/planet_prison/mod/layers.lua +++ b/maps/planet_prison/mod/layers.lua @@ -1,24 +1,31 @@ local public = {} -local _common = require(".common") -local _simplex = require(".simplex_noise") +local _common = require('.common') +local _simplex = require('.simplex_noise') +local Token = require 'utils.token' +local Global = require 'utils.global' + +local this = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) 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() + this._grid = {} + this._exclusions = {} + this._layers = {} + this._collision_mask = {} + _simplex.init() end --[[ push_chunk - Pushes chunk position into a grid for later processing. @param chunk - ChunkPosition --]] public.push_chunk = function(chunk) - table.insert(global.this._grid, chunk) + table.insert(this._grid, chunk) end --[[ @@ -26,7 +33,7 @@ add_excluding_bounding_box - Pushes bounding box into exclusion list. @param bb - BoundindBox. --]] public.push_excluding_bounding_box = function(bb) - table.insert(global.this._exclusions, bb) + table.insert(this._exclusions, bb) end --[[ @@ -34,13 +41,13 @@ remove_ecluding_bounding_box - Removes bounding box from exclusion list. @param bb - BoundingBox to get rid of. --]] public.remove_excluding_bounding_box = function(bb) - for i = 1, #global.this._exclusions do - local box = global.this._exclusions[i] - if box == bb then - table.remove(global.this._exclusions, i) - break - end - end + for i = 1, #this._exclusions do + local box = this._exclusions[i] + if box == bb then + table.remove(this._exclusions, i) + break + end + end end --[[ @@ -52,18 +59,18 @@ add_noise_layer - Add noise layer that will be applied onto the grid. @param elevation - Layer visibility [0f - 1f) --]] public.add_noise_layer = function(type, name, objects, elevation, resolution) - local layer = { - type = type, - name = name, - objects = objects, - elevation = elevation, - resolution = resolution, - cache = {}, - hook = nil, - deps = nil, - } + local layer = { + type = type, + name = name, + objects = objects, + elevation = elevation, + resolution = resolution, + cache = {}, + hook = nil, + deps = nil + } - table.insert(global.this._layers, layer) + table.insert(this._layers, layer) end --[[ @@ -72,12 +79,12 @@ add_noise_layer_hook - Execute callback on created object. @param hook - Callback that will be called with an object argument. --]] public.add_noise_layer_hook = function(name, hook) - for _, layer in pairs(global.this._layers) do - if layer.name == name then - layer.hook = hook - break - end - end + for _, layer in pairs(this._layers) do + if layer.name == name then + layer.hook = hook + break + end + end end --[[ @@ -86,12 +93,12 @@ 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 + for _, layer in pairs(this._layers) do + if layer.name == name then + layer.deps = deps + break + end + end end --[[ @@ -99,74 +106,89 @@ set_collision_mask - Set which tiles should be ignored. @param mask - Table of collision masks. --]] public.set_collision_mask = function(mask) - global.this._collision_mask = mask + this._collision_mask = mask end local function _do_job_tile(surf, layer) - surf.set_tiles(layer.cache) + surf.set_tiles(layer.cache) 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 + local hook = layer.hook + if not hook then + return + end + local func = Token.get(hook) + if not func then + return + end + func = Token.get(func) + local deps = layer.deps + if not deps then + return + end + local func2 = Token.get(deps) + if not func2 then + return + end + 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 + goto continue + end + end + + local ent = surf.create_entity(object) + if not ent or not ent.valid then goto continue - end - end + end - local ent = surf.create_entity(object) - if not ent or not ent.valid then - goto continue - end + if hook then + func(ent, func2) + end - if hook then - hook(ent, deps) - end - - ::continue:: - end + ::continue:: + end end local function _do_job(surf, x, y) - local point = { - x = x, - y = y, - } + local point = { + x = x, + y = y + } - for _, exclusion in pairs(global.this._exclusions) do - if _common.point_in_bounding_box(point, exclusion) then - return - end - end + for _, exclusion in pairs(this._exclusions) do + if _common.point_in_bounding_box(point, exclusion) then + return + end + end - for _, layer in pairs(global.this._layers) do - local ret = _simplex.get(point, layer.resolution) - if ret >= layer.elevation then - local tile = surf.get_tile(point) - for _, mask in pairs(global.this._collision_mask) do - if tile.collides_with(mask) then - goto continue + for _, layer in pairs(this._layers) do + local ret = _simplex.get(point, layer.resolution) + if ret >= layer.elevation then + local tile = surf.get_tile(point) + for _, mask in pairs(this._collision_mask) do + if tile.collides_with(mask) then + goto continue + end end - end - local object_name = layer.objects[1] - if #layer.objects > 1 then - local index = _common.rand_range(1, #layer.objects) - object_name = layer.objects[index] - end + local object_name = layer.objects[1] + if #layer.objects > 1 then + local index = _common.rand_range(1, #layer.objects) + object_name = layer.objects[index] + end - local object = { - name = object_name, - position = point, - } - table.insert(layer.cache, object) + local object = { + name = object_name, + position = point + } + table.insert(layer.cache, object) - break - ::continue:: - end - end + break + ::continue:: + end + end end --[[ @@ -174,39 +196,39 @@ do_job - Do a single step propagation of a layers. @param surf - LuaSurface, onto which action is taken. --]] public.do_job = function(surf) - if #global.this._grid <= 0 then - return - end + if #this._grid <= 0 then + return + end - local chunk = table.remove(global.this._grid) - local x = _common.get_axis(chunk, "x") - local y = _common.get_axis(chunk, "y") + local chunk = table.remove(this._grid) + local x = _common.get_axis(chunk, 'x') + local y = _common.get_axis(chunk, 'y') - chunk = { - left_top = { - x = x * 32, - y = y * 32 - }, - right_bottom = { - x = (x * 32) + 32, - y = (y * 32) + 32, - } - } + chunk = { + left_top = { + x = x * 32, + y = y * 32 + }, + right_bottom = { + x = (x * 32) + 32, + y = (y * 32) + 32 + } + } - _common.for_bounding_box(surf, chunk, _do_job) + _common.for_bounding_box(surf, chunk, _do_job) - for _, layer in pairs(global.this._layers) do - local cache = layer.cache - if #cache >= 1 then - if layer.type == "LuaTile" then - _do_job_tile(surf, layer) - elseif layer.type == "LuaEntity" then - _do_job_entity(surf, layer) - end + for _, layer in pairs(this._layers) do + local cache = layer.cache + if #cache >= 1 then + if layer.type == 'LuaTile' then + _do_job_tile(surf, layer) + elseif layer.type == 'LuaEntity' then + _do_job_entity(surf, layer) + end - layer.cache = {} - end - end + layer.cache = {} + end + end end return public diff --git a/maps/planet_prison/mod/timers.lua b/maps/planet_prison/mod/timers.lua index 745fdb0a..c4e77bd7 100644 --- a/maps/planet_prison/mod/timers.lua +++ b/maps/planet_prison/mod/timers.lua @@ -1,12 +1,18 @@ local public = {} -local _global = require('utils.global') +local Global = require 'utils.global' +local Token = require 'utils.token' + +local this = {} + +Global.register( + this, + function(tbl) + this = tbl + end +) public.init = function() - if global.this == nil then - global.this = {} - end - - global.this.timers = {} + this.timers = {} end --[[ @@ -15,18 +21,20 @@ set_timer - Sets a timer. @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, - } + local id = game.tick + local token = Token.register(hook) + local token2 = Token.register(left) + local entry = { + left = token2, + hook_finish = token, + hook_update = nil, + deps = nil, + running = false, + last_update = 0 + } - global.this.timers[id] = entry - return id + this.timers[id] = entry + return id end --[[ @@ -36,7 +44,8 @@ timers is updated. @param hook - Hook that will be executed per update. --]] public.set_timer_on_update = function(id, hook) - global.this.timers[id].hook_update = hook + local token = Token.register(hook) + this.timers[id].hook_update = token end --[[ @@ -45,7 +54,8 @@ set_timer_dependency - Adds dependency into user callback. @param deps - Dependency of timer to add. --]] public.set_timer_dependency = function(id, deps) - global.this.timers[id].deps = deps + local token = Token.register(deps) + this.timers[id].deps = token end --[[ @@ -53,8 +63,8 @@ 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 + this.timers[id].running = true + this.timers[id].last_update = game.tick end --[[ @@ -62,37 +72,37 @@ kill_timer - Effectivly kills the timer. @param id - Timer id. --]] public.kill_timer = function(id) - global.this.timers[id] = nil + 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 + for id, entry in pairs(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 + 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 + if entry.hook_update ~= nil then + if not entry.hook_update(entry.left, entry.deps) then + goto premature_finish + end end - end - goto continue - end + goto continue + end - ::premature_finish:: - entry.hook_finish(entry.deps) - global.this.timers[id] = nil + ::premature_finish:: + entry.hook_finish(entry.deps) + this.timers[id] = nil - ::continue:: - end + ::continue:: + end end return public