diff --git a/modules/towny/main.lua b/modules/towny/main.lua index 3657ed6e..6f274bed 100644 --- a/modules/towny/main.lua +++ b/modules/towny/main.lua @@ -1,22 +1,34 @@ local Town_center = require "modules.towny.town_center" local Team = require "modules.towny.team" local Connected_building = require "modules.towny.connected_building" +--local Market = require "modules.towny.market" local function on_player_joined_game(event) local player = game.players[event.player_index] - if player.force.index == 1 then - player.print("Towny is enabled on this server!", {255, 255, 0}) - player.print("Place a stone furnace, to found a new town center. Or join a town by visiting another player's center.", {255, 255, 0}) + if player.force.index ~= 1 then return end + + Team.set_player_to_homeless(player) + player.print("Towny is enabled on this server!", {255, 255, 0}) + player.print("Place a stone furnace, to found a new town center. Or join a town by visiting another player's center.", {255, 255, 0}) + + if player.online_time == 0 then + Team.give_homeless_items(player) + return end - if player.online_time == 0 then player.insert({name = "stone-furnace", count = 1}) end + if player.character then + if player.character.valid then + player.character.die() + end + end end local function on_player_respawned(event) local player = game.players[event.player_index] if player.force.index ~= 1 then return end - player.insert({name = "stone-furnace", count = 1}) + Team.set_player_to_homeless(player) + Team.give_homeless_items(player) end local function on_built_entity(event) @@ -49,11 +61,56 @@ local function on_player_repaired_entity(event) end end +local function on_player_dropped_item(event) + local player = game.players[event.player_index] + local entity = event.entity + if entity.stack.name == "raw-fish" then + Team.ally_town(player, entity) + return + end + if entity.stack.name == "coal" then + Team.declare_war(player, entity) + return + end +end + local function on_init() global.towny = {} + global.towny.homeless_requests = {} global.towny.town_centers = {} global.towny.size_of_town_centers = 0 game.difficulty_settings.technology_price_multiplier = 0.5 + + local p = game.permissions.create_group("Homeless") + for action_name, _ in pairs(defines.input_action) do + p.set_allows_action(defines.input_action[action_name], false) + end + local defs = { + defines.input_action.craft, + defines.input_action.build_item, + defines.input_action.cursor_split, + defines.input_action.cursor_transfer, + defines.input_action.clean_cursor_stack, + defines.input_action.drop_item, + defines.input_action.begin_mining, + defines.input_action.change_picking_state, + defines.input_action.edit_permission_group, + defines.input_action.gui_click, + defines.input_action.gui_confirmed, + defines.input_action.gui_elem_changed, + defines.input_action.gui_location_changed, + defines.input_action.gui_selected_tab_changed, + defines.input_action.gui_selection_state_changed, + defines.input_action.gui_switch_state_changed, + defines.input_action.gui_text_changed, + defines.input_action.gui_value_changed, + defines.input_action.open_character_gui, + defines.input_action.open_kills_gui, + defines.input_action.start_walking, + defines.input_action.toggle_show_entity_info, + defines.input_action.write_to_console, + } + for _, d in pairs(defs) do p.set_allows_action(d, true) end end local Event = require 'utils.event' @@ -65,4 +122,4 @@ Event.add(defines.events.on_built_entity, on_built_entity) 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) - +Event.add(defines.events.on_player_dropped_item, on_player_dropped_item) \ No newline at end of file diff --git a/modules/towny/market.lua b/modules/towny/market.lua new file mode 100644 index 00000000..d43d1339 --- /dev/null +++ b/modules/towny/market.lua @@ -0,0 +1,4 @@ +local Public = {} + + +return Public \ No newline at end of file diff --git a/modules/towny/team.lua b/modules/towny/team.lua index 5ecb8452..adaa5d00 100644 --- a/modules/towny/team.lua +++ b/modules/towny/team.lua @@ -1,10 +1,109 @@ local Public = {} +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 = "" + player.color = town_center.color + player.chat_color = town_center.color +end + +function Public.give_homeless_items(player) + player.insert({name = "stone-furnace", count = 1}) + player.insert({name = "raw-fish", count = 3}) +end + +function Public.set_player_to_homeless(player) + player.force = game.forces.player + game.permissions.get_group("Homeless").add_player(player) + player.tag = "[Homeless]" + player.color = {150, 150, 150} + player.chat_color = {150, 150, 150} +end + +function Public.ally_town(player, item) + local position = item.position + local surface = player.surface + local area = {{position.x - 2, position.y - 2}, {position.x + 2, position.y + 2}} + + 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 + local target_force = target.force + + if requesting_force.index == 1 then + global.towny.homeless_requests[player.index] = target_force.name + game.print(">> " .. player.name .. " wants to settle in " .. target_force.name .. " Town!", {255, 255, 0}) + return + end + + if target_force.index == 1 then + if target.type ~= "character" then return end + local target_player = target.player + if not target_player then return end + if not global.towny.homeless_requests[target_player.index] then return end + game.print(">> " .. player.name .. " has accepted " .. target_player.name .. " into their Town!", {255, 255, 0}) + Public.add_player_to_town(target_player, global.towny.town_centers[player.name]) + return + end + + requesting_force.set_friend(target_force, true) + game.print(">> Town " .. requesting_force.name .. " has set " .. target_force.name .. " as their friend!", {255, 255, 0}) + + if requesting_force.get_friend(target_force) then + game.print(">> The towns " .. requesting_force.name .. " and " .. target_force.name .. " have formed an alliance!", {255, 255, 0}) + end +end + +function Public.declare_war(player, item) + local position = item.position + local surface = player.surface + local area = {{position.x - 2, position.y - 2}, {position.x + 2, position.y + 2}} + + 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 + + if requesting_force.name == target_force.name then + if player.name ~= target.force.name then + Public.set_player_to_homeless(player) + game.print(">> " .. player.name .. " has abandoned " .. target_force.name .. "'s Town!", {255, 255, 0}) + end + 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 + Public.set_player_to_homeless(target_player) + game.print(">> " .. player.name .. " has banished " .. target_player.name .. " from their Town!", {255, 255, 0}) + end + return + end + + if target_force.index == 1 then return end + + requesting_force.set_friend(target_force, false) + target_force.set_friend(requesting_force, false) + game.print(">> Town " .. requesting_force.name .. " has set " .. target_force.name .. " as their foe!", {255, 255, 0}) +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) + + game.forces[force_name].research_queue_enabled = true end function Public.kill_force(force_name) diff --git a/modules/towny/town_center.lua b/modules/towny/town_center.lua index ccb1b3e0..925c6520 100644 --- a/modules/towny/town_center.lua +++ b/modules/towny/town_center.lua @@ -8,6 +8,29 @@ local min_distance_to_spawn = 1 local square_min_distance_to_spawn = min_distance_to_spawn ^ 2 local town_radius = 32 +local colors = {} +local c1 = 250 +local c2 = 125 +local c3 = -25 +for v = c1, c2, c3 do + table.insert(colors, {0, 0, v}) +end +for v = c1, c2, c3 do + table.insert(colors, {0, v, 0}) +end +for v = c1, c2, c3 do + table.insert(colors, {v, 0, 0}) +end +for v = c1, c2, c3 do + table.insert(colors, {0, v, v}) +end +for v = c1, c2, c3 do + table.insert(colors, {v, v, 0}) +end +for v = c1, c2, c3 do + table.insert(colors, {v, 0, v}) +end + local town_wall_vectors = {} for x = town_radius * -1, town_radius, 1 do table_insert(town_wall_vectors, {x, town_radius}) @@ -189,7 +212,7 @@ function Public.set_market_health(entity, final_damage_amount) 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) + rendering.set_text(town_center.health_text, "HP: " .. town_center.health .. " / " .. town_center.max_health) end @@ -217,6 +240,7 @@ function Public.found(event) 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.color = colors[math_random(1, #colors)] town_center.health_text = rendering.draw_text{ text = "HP: " .. town_center.health .. " / " .. town_center.max_health, @@ -235,7 +259,7 @@ function Public.found(event) surface = surface, target = town_center.market, target_offset = {0, -3.25}, - color = player.chat_color, + color = town_center.color, scale = 1.30, font = "default-game", alignment = "center", @@ -248,9 +272,11 @@ function Public.found(event) draw_town_spawn(player_name) - player.force = game.forces[player_name] - game.print(">> " .. player.name .. " has founded a new town!", {255, 255, 0}) + Team.add_player_to_town(player, town_center) + player.force.set_spawn_position({x = town_center.market.position.x, y = town_center.market.position.y + 4}, surface) + + game.print(">> " .. player.name .. " has founded a new town!", {255, 255, 0}) return true end