From c3e1a682e5a768dc7b98e66389d00877787cd7f0 Mon Sep 17 00:00:00 2001 From: MewMew Date: Tue, 17 Dec 2019 16:45:23 +0100 Subject: [PATCH] wip --- modules/towny/found_town_center.lua | 103 ++++++++++++++++++++++++++++ modules/towny/main.lua | 25 +++++++ modules/towny/team.lua | 29 ++++++++ 3 files changed, 157 insertions(+) create mode 100644 modules/towny/found_town_center.lua create mode 100644 modules/towny/main.lua create mode 100644 modules/towny/team.lua diff --git a/modules/towny/found_town_center.lua b/modules/towny/found_town_center.lua new file mode 100644 index 00000000..c0765fae --- /dev/null +++ b/modules/towny/found_town_center.lua @@ -0,0 +1,103 @@ +local Team = require "modules.towny.team" + +local table_insert = table.insert + +local min_distance_to_spawn = 1 +local square_min_distance_to_spawn = min_distance_to_spawn ^ 2 +local town_radius = 32 + +local town_wall_vectors = {} +for x = -32, 32, 1 do + table_insert(town_wall_vectors, {x, 32}) + table_insert(town_wall_vectors, {x, -32}) +end +for y = -31, 31, 1 do + table_insert(town_wall_vectors, {32, y}) + table_insert(town_wall_vectors, {-32, y}) +end + +local function draw_town_spawn(player_name) + local market = global.towny.town_centers[player_name] + local position = market.position + local surface = market.surface + + for _, vector in pairs(town_wall_vectors) do + local p = {position.x + vector[1], position.x + vector[2]} + if surface.can_place_entity({name = "stone-wall", position = p, force = player_name}) then + surface.create_entity({name = "stone-wall", position = p, force = player_name}) + end + end +end + +local function is_valid_location(surface, entity) + if global.towny.size_of_town_centers > 48 then + surface.create_entity({ + name = "flying-text", + position = entity.position, + text = "Too many town centers on the map!", + color = {r=0.77, g=0.0, b=0.0} + }) + return + end + + if entity.position.x ^ 2 + entity.position.y ^ 2 < square_min_distance_to_spawn then + surface.create_entity({ + name = "flying-text", + position = entity.position, + text = "Town location is too close to spawn!", + color = {r=0.77, g=0.0, b=0.0} + }) + return + end + + local area = {{entity.position.x - town_radius, entity.position.y - town_radius}, {entity.position.x + town_radius, entity.position.y + town_radius}} + local count = 0 + for _, e in pairs(surface.find_entities_filtered({area = area})) do + if e.force.index ~= 3 then + if e.name == "market" then + surface.create_entity({ + name = "flying-text", + position = entity.position, + text = "Town location is too close to another town center!", + color = {r=0.77, g=0.0, b=0.0} + }) + return + end + count = count + 1 + end + end + + if count <= 4 then return true end +end + +local function found_town_center(event) + local entity = event.created_entity + if entity.name ~= "stone-furnace" then return end + + local player = game.players[event.player_index] + local player_name = tostring(player.name) + + if game.forces[player_name] then return end + + local surface = entity.surface + + if not is_valid_location(surface, entity) then + player.insert({name = "stone-furnace", count = 1}) + entity.destroy() + return + end + + 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.size_of_town_centers = global.towny.size_of_town_centers + 1 + + entity.destroy() + + draw_town_spawn(player_name) + + player.force = game.forces[player_name] + game.print(player.name .. " has founded a new town!", {255, 255, 0}) +end + +return found_town_center \ No newline at end of file diff --git a/modules/towny/main.lua b/modules/towny/main.lua new file mode 100644 index 00000000..a48670ef --- /dev/null +++ b/modules/towny/main.lua @@ -0,0 +1,25 @@ +local found_town_center = require "modules.towny.found_town_center" + +local function on_player_joined_game(event) + local player = game.players[event.player_index] + if player.online_time == 0 then + player.insert({name = "pistol", count = 1}) + player.insert({name = "firearm-magazine", count = 16}) + player.insert({name = "stone-furnace", count = 1}) + end +end + +local function on_built_entity(event) + found_town_center(event) +end + +local function on_init() + global.towny = {} + global.towny.town_centers = {} + global.towny.size_of_town_centers = 0 +end + +local Event = require 'utils.event' +Event.on_init(on_init) +Event.add(defines.events.on_player_joined_game, on_player_joined_game) +Event.add(defines.events.on_built_entity, on_built_entity) \ No newline at end of file diff --git a/modules/towny/team.lua b/modules/towny/team.lua new file mode 100644 index 00000000..c9f00792 --- /dev/null +++ b/modules/towny/team.lua @@ -0,0 +1,29 @@ +local Public = {} + +function Public.set_homeless_player(player) + if player.character then player.character.die() end + player.force = game.forces.player +end + +function Public.add_new_force(force_name) + game.create_force(force_name) + + game.forces.player.set_cease_fire(force_name, true) + game.forces[force_name].set_cease_fire('player', true) +end + +function Public.kill_force(force_name) + local force = game.forces[force_name] + local market = global.towny.town_centers[force_name] + local surface = market.surface + + for _, player in pairs(force.players) do Public.set_homeless_player(player) end + + for _, e in pairs(surface.find_entities_filtered({force = force_name})) do e.active = false end + + merge_forces(force_name, "neutral") + + game.print(force_name .. "'s town has fallen!", {255, 255, 0}) +end + +return Public \ No newline at end of file