1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-03-17 20:58:13 +02:00
This commit is contained in:
MewMew 2019-12-17 16:45:23 +01:00
parent 99e4336dd9
commit c3e1a682e5
3 changed files with 157 additions and 0 deletions

View File

@ -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

25
modules/towny/main.lua Normal file
View File

@ -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)

29
modules/towny/team.lua Normal file
View File

@ -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