1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-24 03:47:58 +02:00

349 lines
12 KiB
Lua
Raw Normal View History

2019-12-17 16:45:23 +01:00
local Public = {}
local Table = require "modules.towny.table"
2019-12-17 16:45:23 +01:00
2019-12-22 17:16:26 +01:00
local outlander_color = {150, 150, 150}
local outlander_chat_color = {170, 170, 170}
local item_drop_radius = 1.65
local function can_force_accept_member(force)
local townytable = Table.get_table()
local town_centers = townytable.town_centers
local size_of_town_centers = townytable.size_of_town_centers
local member_limit = 0
if size_of_town_centers <= 1 then return true end
for _, town in pairs(town_centers) do
member_limit = member_limit + #town.market.force.connected_players
end
member_limit = math.floor(member_limit / size_of_town_centers) + 4
if #force.connected_players >= member_limit then
game.print({"modules_towny.message_too_many_players", force.name, member_limit}, {255, 255, 0})
return
end
return true
end
2019-12-18 12:25:51 +01:00
function Public.set_player_color(player)
local townytable = Table.get_table()
2019-12-18 12:25:51 +01:00
if player.force.index == 1 then
2019-12-22 17:16:26 +01:00
player.color = outlander_color
player.chat_color = outlander_chat_color
2019-12-18 12:25:51 +01:00
return
end
local town_center = townytable.town_centers[player.force.name]
if not town_center then return end
2019-12-18 12:25:51 +01:00
player.color = town_center.color
player.chat_color= town_center.color
2019-12-18 12:25:51 +01:00
end
function Public.set_town_color(event)
local townytable = Table.get_table()
2019-12-18 12:25:51 +01:00
if event.command ~= "color" then return end
local player = game.players[event.player_index]
2019-12-18 12:25:51 +01:00
if player.force.index == 1 then
2019-12-22 17:16:26 +01:00
player.color = outlander_color
player.chat_color = outlander_chat_color
2019-12-18 12:25:51 +01:00
return
end
local town_center = townytable.town_centers[player.name]
2019-12-18 12:25:51 +01:00
if not town_center then
Public.set_player_color(player)
return
end
2019-12-18 12:25:51 +01:00
town_center.color = {player.color.r, player.color.g, player.color.b}
rendering.set_color(town_center.town_caption, town_center.color)
for _, p in pairs(player.force.players) do
2019-12-18 19:08:09 +01:00
Public.set_player_color(p)
2019-12-18 12:25:51 +01:00
end
end
2019-12-19 17:11:13 +01:00
function Public.set_all_player_colors()
for _, p in pairs(game.connected_players) do
Public.set_player_color(p)
end
end
2019-12-17 22:40:16 +01:00
function Public.add_player_to_town(player, town_center)
player.force = town_center.market.force
game.permissions.get_group("Default").add_player(player)
player.tag = ""
Public.set_player_color(player)
2019-12-17 22:40:16 +01:00
end
2019-12-22 17:16:26 +01:00
function Public.give_outlander_items(player)
2019-12-17 22:40:16 +01:00
player.insert({name = "stone-furnace", count = 1})
player.insert({name = "raw-fish", count = 3})
player.insert({name = "coal", count = 3})
2019-12-17 22:40:16 +01:00
end
2019-12-22 17:16:26 +01:00
function Public.set_player_to_outlander(player)
2019-12-17 22:40:16 +01:00
player.force = game.forces.player
2019-12-22 17:16:26 +01:00
game.permissions.get_group("outlander").add_player(player)
player.tag = "[Outlander]"
2019-12-18 12:25:51 +01:00
Public.set_player_color(player)
2019-12-17 22:40:16 +01:00
end
2019-12-22 17:16:26 +01:00
local function ally_outlander(player, target)
local townytable = Table.get_table()
2019-12-17 22:40:16 +01:00
local requesting_force = player.force
local target_force = target.force
if requesting_force.index ~= 1 and target_force.index ~= 1 then return end
if requesting_force.index == 1 and target_force.index == 1 then return true end
2019-12-17 22:40:16 +01:00
if requesting_force.index == 1 then
townytable.requests[player.index] = target_force.name
2019-12-18 19:08:09 +01:00
local target_player = false
if target.type == "character" then
2019-12-18 19:08:09 +01:00
target_player = target.player
else
target_player = game.players[target_force.name]
end
2019-12-18 19:08:09 +01:00
if target_player then
if townytable.requests[target_player.index] then
if townytable.requests[target_player.index] == player.name then
if townytable.town_centers[target_force.name] then
if not can_force_accept_member(target_force) then return true end
game.print({"modules_towny.message_settled", player.name, target_force.name}, {255, 255, 0})
Public.add_player_to_town(player, townytable.town_centers[target_force.name])
2019-12-18 19:08:09 +01:00
return true
end
end
end
end
game.print({"modules_towny.message_settling", player.name, target_force.name}, {255, 255, 0})
return true
2019-12-17 22:40:16 +01:00
end
if target_force.index == 1 then
if target.type ~= "character" then return true end
2019-12-17 22:40:16 +01:00
local target_player = target.player
if not target_player then return true end
townytable.requests[player.index] = target_player.name
if townytable.requests[target_player.index] then
if townytable.requests[target_player.index] == player.force.name then
if not can_force_accept_member(player.force) then return true end
2019-12-23 18:09:01 +01:00
if player.force.name == player.name then
game.print({"modules_towny.message_accepted_own", player.name, target_player.name}, {255, 255, 0})
2019-12-23 18:09:01 +01:00
else
game.print({"modules_towny.message_accepted_own", player.name, target_player.name, player.force.name}, {255, 255, 0})
2019-12-23 18:09:01 +01:00
end
Public.add_player_to_town(target_player, townytable.town_centers[player.force.name])
return true
end
end
if player.force.name == player.name then
game.print({"modules_towny.message_invite_own", player.name, target_player.name}, {255, 255, 0})
else
game.print({"modules_towny.message_invite", player.name, target_player.name, player.force.name}, {255, 255, 0})
end
return true
2019-12-17 22:40:16 +01:00
end
end
local function ally_neighbour_towns(player, target)
local requesting_force = player.force
local target_force = target.force
2019-12-17 23:21:22 +01:00
if target_force.get_friend(requesting_force) and requesting_force.get_friend(target_force) then return end
2019-12-17 22:40:16 +01:00
requesting_force.set_friend(target_force, true)
game.print({"modules_towny.message_friend", requesting_force.name, target_force.name}, {255, 255, 0})
2019-12-17 23:21:22 +01:00
if target_force.get_friend(requesting_force) then
game.print({"modules_towny.message_alliance", requesting_force.name, target_force.name}, {255, 255, 0})
2019-12-17 22:40:16 +01:00
end
end
function Public.ally_town(player, item)
local position = item.position
local surface = player.surface
local area = {{position.x - item_drop_radius, position.y - item_drop_radius}, {position.x + item_drop_radius, position.y + item_drop_radius}}
2019-12-18 19:08:09 +01:00
local requesting_force = player.force
local target = false
for _, e in pairs(surface.find_entities_filtered({type = {"character", "market"}, area = area})) do
if e.force.name ~= requesting_force.name then
target = e
break
end
end
if not target then return end
2019-12-18 19:08:09 +01:00
if target.force.index == 2 or target.force.index == 3 then return end
if ally_outlander(player, target) then return end
ally_neighbour_towns(player, target)
end
2019-12-17 22:40:16 +01:00
function Public.declare_war(player, item)
local townytable = Table.get_table()
2019-12-17 22:40:16 +01:00
local position = item.position
local surface = player.surface
local area = {{position.x - item_drop_radius, position.y - item_drop_radius}, {position.x + item_drop_radius, position.y + item_drop_radius}}
2019-12-17 22:40:16 +01:00
local requesting_force = player.force
local target = surface.find_entities_filtered({type = {"character", "market"}, area = area})[1]
if not target then return end
local target_force = target.force
2019-12-18 19:08:09 +01:00
if target_force.index <= 3 then return end
2019-12-17 22:40:16 +01:00
if requesting_force.name == target_force.name then
if player.name ~= target.force.name then
2019-12-22 17:16:26 +01:00
Public.set_player_to_outlander(player)
game.print({"modules_towny.message_abandon", player.name, target_force.name}, {255, 255, 0})
townytable.requests[player.index] = nil
end
2019-12-17 22:40:16 +01:00
if player.name == target.force.name then
if target.type ~= "character" then return end
local target_player = target.player
if not target_player then return end
if target_player.index == player.index then return end
2019-12-22 17:16:26 +01:00
Public.set_player_to_outlander(target_player)
game.print({"modules_towny.message_banish", player.name, target_player.name}, {255, 255, 0})
townytable.requests[player.index] = nil
2019-12-17 22:40:16 +01:00
end
return
end
if requesting_force.index == 1 then return end
2019-12-17 22:40:16 +01:00
requesting_force.set_friend(target_force, false)
target_force.set_friend(requesting_force, false)
game.print({"modules_towny.message_war", player.name, target_force.name, requesting_force.name}, {255, 255, 0})
2019-12-19 17:11:13 +01:00
end
2019-12-20 21:13:50 +01:00
local radius = 96
2019-12-19 17:11:13 +01:00
function Public.reveal_entity_to_all(entity)
local chart_area = {{entity.position.x - radius, entity.position.y - radius}, {entity.position.x + radius, entity.position.y + radius}}
local surface = entity.surface
for _, force in pairs(game.forces) do
force.chart(surface, chart_area)
end
end
local function delete_chart_tag_for_all_forces(market)
local forces = game.forces
local position = market.position
2019-12-19 17:11:13 +01:00
local surface = market.surface
for _, force in pairs(forces) do
2019-12-19 17:11:13 +01:00
local tags = force.find_chart_tags(surface, {{position.x - 0.1, position.y - 0.1}, {position.x + 0.1, position.y + 0.1}})
local tag = tags[1]
if tag then
if tag.icon.name == "stone-furnace" then
tag.destroy()
end
end
end
end
2019-12-24 12:43:47 +01:00
function Public.add_chart_tag(force, market)
2019-12-19 17:11:13 +01:00
local position = market.position
local tags = force.find_chart_tags(market.surface, {{position.x - 0.1, position.y - 0.1}, {position.x + 0.1, position.y + 0.1}})
if tags[1] then return end
force.add_chart_tag(market.surface, {icon = {type = 'item', name = 'stone-furnace'}, position = position, text = market.force.name .. "'s Town"})
2019-12-19 17:11:13 +01:00
end
function Public.update_town_chart_tags()
local townytable = Table.get_table()
local town_centers = townytable.town_centers
2019-12-19 17:11:13 +01:00
local forces = game.forces
for _, town_center in pairs(town_centers) do
local market = town_center.market
for _, force in pairs(forces) do
if force.is_chunk_visible(market.surface, town_center.chunk_position) then
2019-12-24 12:43:47 +01:00
Public.add_chart_tag(force, market)
2019-12-19 17:11:13 +01:00
end
end
end
2019-12-17 22:40:16 +01:00
end
2019-12-17 16:45:23 +01:00
function Public.add_new_force(force_name)
2019-12-20 23:10:03 +01:00
local force = game.create_force(force_name)
force.research_queue_enabled = true
force.share_chart = true
2019-12-20 23:10:03 +01:00
force.technologies["atomic-bomb"].enabled = false
force.technologies["explosive-rocketry"].enabled = false
force.technologies["rocketry"].enabled = false
force.technologies["artillery-shell-range-1"].enabled = false
2019-12-20 23:10:03 +01:00
force.technologies["artillery-shell-speed-1"].enabled = false
force.set_ammo_damage_modifier("landmine", -0.6)
force.set_ammo_damage_modifier("artillery-shell", -0.75)
force.manual_mining_speed_modifier = 1
2019-12-17 16:45:23 +01:00
end
function Public.kill_force(force_name)
local townytable = Table.get_table()
2019-12-17 16:45:23 +01:00
local force = game.forces[force_name]
local market = townytable.town_centers[force_name].market
2019-12-17 16:45:23 +01:00
local surface = market.surface
2019-12-17 20:07:00 +01:00
surface.create_entity({name = "big-artillery-explosion", position = market.position})
2019-12-17 18:06:56 +01:00
for _, player in pairs(force.players) do
if player.character then
player.character.die()
else
townytable.requests[player.index] = "kill-character"
end
2019-12-17 18:06:56 +01:00
player.force = game.forces.player
end
2019-12-18 00:46:21 +01:00
for _, e in pairs(surface.find_entities_filtered({force = force_name})) do
2019-12-19 17:11:13 +01:00
if e.valid then
if e.type == "wall" or e.type == "gate" then
e.die()
2019-12-18 00:46:21 +01:00
end
end
2019-12-17 23:21:22 +01:00
end
2019-12-17 16:45:23 +01:00
2019-12-17 18:06:56 +01:00
game.merge_forces(force_name, "neutral")
townytable.town_centers[force_name] = nil
townytable.size_of_town_centers = townytable.size_of_town_centers - 1
2019-12-19 17:11:13 +01:00
delete_chart_tag_for_all_forces(market)
Public.reveal_entity_to_all(market)
game.print({"modules_towny.message_destroyed", force_name, "[gps=" .. math.floor(market.position.x) .. "," .. math.floor(market.position.y) .. "]"}, {255, 255, 0})
2019-12-17 16:45:23 +01:00
end
2019-12-20 14:46:20 +01:00
local player_force_disabled_recipes = {"lab", "automation-science-pack", "stone-brick"}
local player_force_enabled_recipes = {"submachine-gun", "assembling-machine-1", "small-lamp", "shotgun", "shotgun-shell", "underground-belt", "splitter", "steel-plate", "car", "cargo-wagon", "constant-combinator", "engine-unit", "green-wire", "locomotive", "rail", "train-stop", "arithmetic-combinator", "decider-combinator"}
2019-12-20 14:46:20 +01:00
function Public.setup_player_force()
2019-12-22 17:16:26 +01:00
local p = game.permissions.create_group("outlander")
2019-12-20 14:46:20 +01:00
for action_name, _ in pairs(defines.input_action) do
p.set_allows_action(defines.input_action[action_name], true)
end
local defs = {
defines.input_action.start_research,
}
for _, d in pairs(defs) do p.set_allows_action(d, false) end
2019-12-20 14:46:20 +01:00
local force = game.forces.player
local recipes = force.recipes
2019-12-20 14:46:20 +01:00
for k, recipe_name in pairs(player_force_disabled_recipes) do
recipes[recipe_name].enabled = false
end
for k, recipe_name in pairs(player_force_enabled_recipes) do
recipes[recipe_name].enabled = true
end
2019-12-20 21:13:50 +01:00
force.set_ammo_damage_modifier("landmine", -0.6)
2019-12-20 23:10:03 +01:00
force.set_ammo_damage_modifier("artillery-shell", -0.75)
2019-12-20 14:46:20 +01:00
end
return Public