From 59b9967e9cb24dcc631589d6a5b0bbbefedb8692 Mon Sep 17 00:00:00 2001 From: MewMew Date: Tue, 17 Dec 2019 20:07:00 +0100 Subject: [PATCH] custom health, build restrictions --- modules/towny/connected_building.lua | 52 ++++++++++++++++++++++++++-- modules/towny/main.lua | 19 +++++++++- modules/towny/team.lua | 4 ++- modules/towny/town_center.lua | 50 +++++++++++++++++++++++--- 4 files changed, 116 insertions(+), 9 deletions(-) diff --git a/modules/towny/connected_building.lua b/modules/towny/connected_building.lua index a09f6b4b..3477723f 100644 --- a/modules/towny/connected_building.lua +++ b/modules/towny/connected_building.lua @@ -1,6 +1,53 @@ local Public = {} -local connection_radius = 5 +local connection_radius = 4 + +local entity_type_whitelist = { + ["accumulator"] = true, + ["ammo-turret"] = true, + ["arithmetic-combinator"] = true, + ["artillery-turret"] = true, + ["assembling-machine"] = true, + ["boiler"] = true, + ["constant-combinator"] = true, + ["container"] = true, + ["curved-rail"] = true, + ["decider-combinator"] = true, + ["electric-turret"] = true, + ["fluid-turret"] = true, + ["furnace"] = true, + ["gate"] = true, + ["generator"] = true, + ["heat-pipe"] = true, + ["infinity-container"] = true, + ["heat-interface"] = true, + ["infinity-pipe"] = true, + ["inserter"] = true, + ["lamp"] = true, + ["loader"] = true, + ["logistic-container"] = true, + ["market"] = true, + ["mining-drill"] = true, + ["offshore-pump"] = true, + ["pipe"] = true, + ["pipe-to-ground"] = true, + ["programmable-speaker"] = true, + ["pump"] = true, + ["radar"] = true, + ["rail-chain-signal"] = true, + ["rail-signal"] = true, + ["reactor"] = true, + ["roboport"] = true, + ["rocket-silo"] = true, + ["solar-panel"] = true, + ["splitter"] = true, + ["storage-tank"] = true, + ["straight-rail"] = true, + ["train-stop"] = true, + ["transport-belt"] = true, + ["underground-belt"] = true, + ["wall"] = true, +} local function is_entity_isolated(surface, entity) local position_x = entity.position.x @@ -9,7 +56,7 @@ local function is_entity_isolated(surface, entity) local count = 0 for _, e in pairs(surface.find_entities_filtered({area = area, force = entity.force.name})) do - if game.entity_prototypes[e.name] and game.recipe_prototypes[e.name] or e.name == "market" then + if entity_type_whitelist[e.type] then count = count + 1 if count > 1 then return end end @@ -36,6 +83,7 @@ end function Public.prevent_isolation(event) local entity = event.created_entity if not entity.valid then return end + if entity.type == "entity-ghost" then return end local surface = event.created_entity.surface if is_entity_isolated(surface, entity) then diff --git a/modules/towny/main.lua b/modules/towny/main.lua index 781aaa4d..3657ed6e 100644 --- a/modules/towny/main.lua +++ b/modules/towny/main.lua @@ -35,6 +35,20 @@ local function on_entity_died(event) end end +local function on_entity_damaged(event) + local entity = event.entity + if entity.name == "market" then + Town_center.set_market_health(entity, event.final_damage_amount) + end +end + +local function on_player_repaired_entity(event) + local entity = event.entity + if entity.name == "market" then + Town_center.set_market_health(entity, -3) + end +end + local function on_init() global.towny = {} global.towny.town_centers = {} @@ -48,4 +62,7 @@ Event.add(defines.events.on_player_joined_game, on_player_joined_game) Event.add(defines.events.on_player_respawned, on_player_respawned) Event.add(defines.events.on_robot_built_entity, on_robot_built_entity) Event.add(defines.events.on_built_entity, on_built_entity) -Event.add(defines.events.on_entity_died, on_entity_died) \ No newline at end of file +Event.add(defines.events.on_entity_died, on_entity_died) +Event.add(defines.events.on_entity_damaged, on_entity_damaged) +Event.add(defines.events.on_player_repaired_entity, on_player_repaired_entity) + diff --git a/modules/towny/team.lua b/modules/towny/team.lua index 220dc135..5ecb8452 100644 --- a/modules/towny/team.lua +++ b/modules/towny/team.lua @@ -9,9 +9,11 @@ end function Public.kill_force(force_name) local force = game.forces[force_name] - local market = global.towny.town_centers[force_name] + local market = global.towny.town_centers[force_name].market local surface = market.surface + surface.create_entity({name = "big-artillery-explosion", position = market.position}) + for _, player in pairs(force.players) do if player.character then player.character.die() end player.force = game.forces.player diff --git a/modules/towny/town_center.lua b/modules/towny/town_center.lua index 8f15474d..ccb1b3e0 100644 --- a/modules/towny/town_center.lua +++ b/modules/towny/town_center.lua @@ -69,7 +69,7 @@ local starter_supplies = { } local function draw_town_spawn(player_name) - local market = global.towny.town_centers[player_name] + local market = global.towny.town_centers[player_name].market local position = market.position local surface = market.surface @@ -113,15 +113,17 @@ local function draw_town_spawn(player_name) for i = 1, 4, 1 do for _, vector in pairs(resource_vectors[i]) do - local p = {position.x + vector[1], position.y + vector[2]} - surface.create_entity({name = ores[i], position = p, amount = 1500}) + local p = {position.x + vector[1], position.y + vector[2]} + if not surface.get_tile(p).collides_with("resource-layer") then + surface.create_entity({name = ores[i], position = p, amount = 1500}) + end end end for _, item_stack in pairs(starter_supplies) do local m1 = -8 + math_random(0, 16) local m2 = -8 + math_random(0, 16) - local p = {position.x + m1, position.x + m2} + local p = {position.x + m1, position.y + m2} p = surface.find_non_colliding_position("wooden-chest", p, 32, 1) if p then local e = surface.create_entity({name = "wooden-chest", position = p, force = player_name}) @@ -182,6 +184,15 @@ local function is_valid_location(surface, entity) return true end +function Public.set_market_health(entity, final_damage_amount) + local town_center = global.towny.town_centers[entity.force.name] + town_center.health = town_center.health - final_damage_amount + local m = town_center.health / town_center.max_health + entity.health = 150 * m + rendering.set_text(town_center.health_text, "Health: " .. town_center.health) + +end + function Public.found(event) local entity = event.created_entity if entity.name ~= "stone-furnace" then return end @@ -201,7 +212,36 @@ function Public.found(event) Team.add_new_force(player_name) - global.towny.town_centers[player_name] = surface.create_entity({name = "market", position = entity.position, force = player_name}) + global.towny.town_centers[player_name] = {} + local town_center = global.towny.town_centers[player_name] + town_center.market = surface.create_entity({name = "market", position = entity.position, force = player_name}) + town_center.max_health = 5000 + town_center.health = town_center.max_health + + town_center.health_text = rendering.draw_text{ + text = "HP: " .. town_center.health .. " / " .. town_center.max_health, + surface = surface, + target = town_center.market, + target_offset = {0, -2.5}, + color = {200, 200, 200}, + scale = 1.00, + font = "default-game", + alignment = "center", + scale_with_zoom = false + } + + town_center.town_caption = rendering.draw_text{ + text = player.name .. "'s town", + surface = surface, + target = town_center.market, + target_offset = {0, -3.25}, + color = player.chat_color, + scale = 1.30, + font = "default-game", + alignment = "center", + scale_with_zoom = false + } + global.towny.size_of_town_centers = global.towny.size_of_town_centers + 1 entity.destroy()