diff --git a/modules/towny/main.lua b/modules/towny/main.lua index 11489888..b32607eb 100644 --- a/modules/towny/main.lua +++ b/modules/towny/main.lua @@ -1,9 +1,10 @@ -local Town_center = require "modules.towny.town_center" -local Team = require "modules.towny.team" -local Connected_building = require "modules.towny.connected_building" local Combat_balance = require "modules.towny.combat_balance" +local Connected_building = require "modules.towny.connected_building" +local Market = require "modules.towny.market" +local Team = require "modules.towny.team" +local Town_center = require "modules.towny.town_center" require "modules.global_chat_toggle" ---local Market = require "modules.towny.market" + local function on_player_joined_game(event) local player = game.players[event.player_index] @@ -67,7 +68,7 @@ end local function on_player_repaired_entity(event) local entity = event.entity if entity.name == "market" then - Town_center.set_market_health(entity, -3) + Town_center.set_market_health(entity, -4) end end @@ -88,6 +89,15 @@ local function on_console_command(event) Team.set_town_color(event) end +local function on_market_item_purchased(event) + Market.offer_purchased(event) + Market.refresh_offers(event) +end + +local function on_gui_opened(event) + Market.refresh_offers(event) +end + local function on_init() global.towny = {} global.towny.requests = {} @@ -139,4 +149,6 @@ 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) -Event.add(defines.events.on_player_used_capsule, on_player_used_capsule) \ No newline at end of file +Event.add(defines.events.on_player_used_capsule, on_player_used_capsule) +Event.add(defines.events.on_market_item_purchased, on_market_item_purchased) +Event.add(defines.events.on_gui_opened, on_gui_opened) \ No newline at end of file diff --git a/modules/towny/market.lua b/modules/towny/market.lua index d43d1339..b0094400 100644 --- a/modules/towny/market.lua +++ b/modules/towny/market.lua @@ -1,4 +1,66 @@ +local Town_center = require "modules.towny.town_center" local Public = {} +local upgrade_functions = { + --Upgrade Town Center Health + [1] = function(town_center) + town_center.health = town_center.health + town_center.max_health + town_center.max_health = town_center.max_health * 2 + Town_center.set_market_health(town_center.market, 0) + end, +} + +local function clear_offers(market) + for i = 1, 256, 1 do + local a = market.remove_market_item(1) + if a == false then return end + end +end + +local function set_offers(town_center) + local market = town_center.market + local market_items = { + {price = {{"coin", town_center.max_health * 0.1}}, offer = {type = 'nothing', effect_description = "Upgrade Town Center Health"}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'wood', count = 50}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'iron-ore', count = 50}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'copper-ore', count = 50}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'stone', count = 50}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'coal', count = 50}}, + {price = {{"coin", 10}}, offer = {type = 'give-item', item = 'uranium-ore', count = 50}}, + {price = {{'wood', 12}}, offer = {type = 'give-item', item = "coin"}}, + {price = {{'iron-ore', 12}}, offer = {type = 'give-item', item = "coin"}}, + {price = {{'copper-ore', 12}}, offer = {type = 'give-item', item = "coin"}}, + {price = {{'stone', 12}}, offer = {type = 'give-item', item = "coin"}}, + {price = {{'coal', 12}}, offer = {type = 'give-item', item = "coin"}}, + {price = {{'uranium-ore', 10}}, offer = {type = 'give-item', item = "coin"}}, + } + for _, item in pairs(market_items) do + market.add_market_item(item) + end +end + +function Public.refresh_offers(event) + local market = event.entity or event.market + if not market then return end + if not market.valid then return end + if market.name ~= "market" then return end + local town_center = global.towny.town_centers[market.force.name] + if not town_center then return end + clear_offers(market) + set_offers(town_center) +end + +function Public.offer_purchased(event) + local offer_index = event.offer_index + if not upgrade_functions[offer_index] then return end + + local market = event.market + + local town_center = global.towny.town_centers[market.force.name] + if not town_center then return end + + upgrade_functions[offer_index](town_center) +end + +return Public -return Public \ No newline at end of file diff --git a/modules/towny/town_center.lua b/modules/towny/town_center.lua index de9d5137..286d313a 100644 --- a/modules/towny/town_center.lua +++ b/modules/towny/town_center.lua @@ -122,7 +122,7 @@ local function draw_town_spawn(player_name) for _, e in pairs(surface.find_entities_filtered({area = area, force = "neutral"})) do if not clear_blacklist_types[e.type] then - --e.destroy() + e.destroy() end end @@ -183,18 +183,19 @@ local function draw_town_spawn(player_name) local vector_indexes = {1,2,3,4} table.shuffle_table(vector_indexes) + local tree = "tree-0" .. math_random(1, 9) for _, vector in pairs(additional_resource_vectors[vector_indexes[1]]) do if math_random(1, 6) == 1 then local p = {position.x + vector[1], position.y + vector[2]} - p = surface.find_non_colliding_position("tree-01", p, 32, 1) + p = surface.find_non_colliding_position(tree, p, 32, 1) if p then - surface.create_entity({name = "tree-01", position = p}) + surface.create_entity({name = tree, position = p}) end end end local area = {{position.x - town_radius * 1.5, position.y - town_radius * 1.5}, {position.x + town_radius * 1.5, position.y + town_radius * 1.5}} - if surface.count_tiles_filtered({name = {"water", "deepwater"}, area = area}) == 0 then + if surface.count_tiles_filtered({name = {"water", "deepwater"}, area = area}) < 8 then for _, vector in pairs(additional_resource_vectors[vector_indexes[2]]) do local p = {position.x + vector[1], position.y + vector[2]} if surface.get_tile(p).name ~= "out-of-map" then @@ -301,8 +302,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, "HP: " .. town_center.health .. " / " .. town_center.max_health) - + rendering.set_text(town_center.health_text, "HP: " .. town_center.health .. " / " .. town_center.max_health) end function Public.found(event) @@ -342,7 +342,7 @@ function Public.found(event) 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.max_health = 1000 town_center.health = town_center.max_health town_center.color = colors[math_random(1, #colors)]