2021-10-13 09:21:53 +01:00
local Memory = require ' maps.pirates.memory '
local Roles = require ' maps.pirates.roles.roles '
local Classes = require ' maps.pirates.roles.classes '
local Crew = require ' maps.pirates.crew '
local Boats = require ' maps.pirates.structures.boats.boats '
local Dock = require ' maps.pirates.surfaces.dock '
local Balance = require ' maps.pirates.balance '
local Common = require ' maps.pirates.common '
local Utils = require ' maps.pirates.utils_local '
local Math = require ' maps.pirates.math '
local inspect = require ' utils.inspect ' . inspect
2022-02-26 23:33:19 +00:00
local SurfacesCommon = require ' maps.pirates.surfaces.common '
2021-10-13 09:21:53 +01:00
local Upgrades = require ' maps.pirates.boat_upgrades '
local Public = { }
2022-03-01 21:59:48 +00:00
--== Warning: If something only costs fuel, then we need to check the player can't buy it whilst they're dead
2021-10-13 09:21:53 +01:00
Public.main_shop_data_1 = {
2022-03-01 21:59:48 +00:00
repair_cannons = {
tooltip = ' Repair the cannons. ' ,
what_you_get_sprite_buttons = { [ ' item/artillery-turret ' ] = false } ,
base_cost = { coins = 800 } ,
} ,
2021-10-13 09:21:53 +01:00
new_boat_cutter = {
2022-02-25 00:01:28 +00:00
tooltip = ' Purchase a cutter. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' utility/spawn_flag ' ] = false } ,
2022-02-24 19:39:03 +00:00
base_cost = { fuel = 3000 } ,
2021-10-13 09:21:53 +01:00
} ,
new_boat_sloop_with_hold = {
2022-02-25 00:01:28 +00:00
tooltip = ' Purchase a sloop (with hold). ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' utility/spawn_flag ' ] = false } ,
2022-02-24 19:39:03 +00:00
base_cost = { fuel = 3500 } ,
2021-10-13 09:21:53 +01:00
} ,
new_boat_cutter_with_hold = {
2022-02-25 00:01:28 +00:00
tooltip = ' Purchase a cutter (with hold). ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' utility/spawn_flag ' ] = false } ,
2022-02-24 19:39:03 +00:00
base_cost = { fuel = 5000 } ,
2021-10-13 09:21:53 +01:00
} ,
2022-02-24 19:39:03 +00:00
-- buy_iron = {
-- tooltip = 'Purchase 250 iron plates for 300 stored fuel.',
-- what_you_get_sprite_buttons = {['item/iron-plate'] = 250},
-- base_cost = {fuel = 300},
-- },
-- buy_copper = {
-- tooltip = 'Purchase 250 copper plates for 300 stored fuel.',
-- what_you_get_sprite_buttons = {['item/copper-plate'] = 250},
-- base_cost = {fuel = 300},
-- },
2021-10-13 09:21:53 +01:00
-- sell_iron = {
2022-02-24 19:39:03 +00:00
-- tooltip = 'Purchase 200 stored fuel for 2000 iron plates.',
2021-10-13 09:21:53 +01:00
-- what_you_get_sprite_buttons = {['item/sulfur'] = 200},
-- base_cost = {iron_plates = 2000},
-- },
-- sell_copper = {
2022-02-24 19:39:03 +00:00
-- tooltip = 'Purchase 100 stored fuel for 2500 copper plates',
2021-10-13 09:21:53 +01:00
-- what_you_get_sprite_buttons = {['item/sulfur'] = 100},
-- base_cost = {copper_plates = 2500},
-- },
2022-02-25 00:01:28 +00:00
-- as as initial pass let's try making the fuel values half of the old gold values...
2021-10-13 09:21:53 +01:00
[ Upgrades.enum . MORE_POWER ] = {
2022-02-25 00:01:28 +00:00
tooltip = ' Upgrade the ship \' s power. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' utility/status_working ' ] = false } ,
2022-03-05 14:09:35 +00:00
base_cost = { coins = 5000 , fuel = 800 } ,
2021-10-13 09:21:53 +01:00
} ,
[ Upgrades.enum . EXTRA_HOLD ] = {
2022-02-25 00:01:28 +00:00
tooltip = ' Upgrade the ship \' s hold. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/steel-chest ' ] = false } ,
2022-03-01 15:57:23 +00:00
base_cost = { coins = 10000 , fuel = 2000 } ,
2021-10-13 09:21:53 +01:00
} ,
[ Upgrades.enum . ROCKETS_FOR_SALE ] = {
2022-02-25 00:01:28 +00:00
tooltip = ' Unlock rockets for sale at covered-up markets. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/rocket-launcher ' ] = false } ,
2022-03-01 15:57:23 +00:00
base_cost = { coins = 10000 , fuel = 2500 } ,
2021-10-13 09:21:53 +01:00
} ,
[ Upgrades.enum . UNLOCK_MERCHANTS ] = {
2022-02-25 00:01:28 +00:00
tooltip = ' Unlock merchant ships. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' entity/market ' ] = false } ,
2022-03-03 00:19:20 +00:00
base_cost = { coins = 10000 , fuel = 2000 } ,
2021-10-13 09:21:53 +01:00
} ,
}
Public.main_shop_data_2 = {
rail_signal = {
2022-02-26 13:55:36 +00:00
tooltip = " 100 signals, used to steer the boat one space in the Crow's Nest View. " ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/rail-signal ' ] = 100 } ,
2022-02-26 13:55:36 +00:00
base_cost = { coins = 600 } ,
2021-10-13 09:21:53 +01:00
} ,
2022-03-02 13:26:17 +00:00
extra_time = {
tooltip = ' Relax at sea for an extra minute. (Increases the next destination \' s loading time.) ' ,
what_you_get_sprite_buttons = { [ ' utility/time_editor_icon ' ] = 60 } ,
base_cost = { coins = 10 } ,
} ,
2021-10-13 09:21:53 +01:00
artillery_shell = {
2022-03-07 09:50:25 +00:00
tooltip = ' 8 cannon shells. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/artillery-shell ' ] = 10 } ,
2022-03-07 09:50:25 +00:00
base_cost = { coins = 800 , fuel = 10 } ,
2021-10-13 09:21:53 +01:00
} ,
artillery_remote = {
2022-02-24 19:39:03 +00:00
tooltip = ' An artillery targeting remote. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/artillery-targeting-remote ' ] = 1 } ,
2022-02-26 13:55:36 +00:00
base_cost = { coins = 12000 , fuel = 2500 } ,
2021-10-13 09:21:53 +01:00
} ,
-- buy_fast_loader = {
2022-02-24 19:39:03 +00:00
-- tooltip = 'A fast loader for 500 stored fuel.',
2021-10-13 09:21:53 +01:00
-- what_you_get_sprite_buttons = {['item/fast-loader'] = 1},
2022-02-24 19:39:03 +00:00
-- base_cost = {fuel = 500},
2021-10-13 09:21:53 +01:00
-- },
uranium_ore = {
2022-02-24 19:39:03 +00:00
tooltip = ' 10 green rocks of unknown origin. ' ,
2021-10-13 09:21:53 +01:00
what_you_get_sprite_buttons = { [ ' item/uranium-238 ' ] = 10 } ,
2022-03-05 19:56:41 +00:00
base_cost = { coins = 1000 , fuel = 100 } ,
2021-10-13 09:21:53 +01:00
} ,
}
function Public . initialise_main_shop ( )
local memory = Memory.get_crew_memory ( )
memory.mainshop_availability_bools = {
uranium_ore = true ,
rail_signal = true ,
artillery_shell = true ,
artillery_remote = true ,
2022-03-02 13:26:17 +00:00
extra_time = true ,
2021-10-13 09:21:53 +01:00
new_boat_sloop_with_hold = false ,
new_boat_cutter_with_hold = false ,
new_boat_cutter = false ,
buy_iron = false ,
2022-03-01 21:59:48 +00:00
repair_cannons = false ,
2021-10-13 09:21:53 +01:00
-- sell_iron = false,
-- buy_fast_loader = true,
-- sell_copper = false,
}
end
2022-02-27 16:42:25 +00:00
function Public . main_shop_try_purchase ( player , purchase_name )
2021-10-13 09:21:53 +01:00
local memory = Memory.get_crew_memory ( )
2022-03-01 21:59:48 +00:00
local destination = Common.current_destination ( )
2021-10-13 09:21:53 +01:00
local shop_data_1 = Public.main_shop_data_1
local shop_data_2 = Public.main_shop_data_2
2022-02-27 16:42:25 +00:00
local trade_data = shop_data_1 [ purchase_name ] or shop_data_2 [ purchase_name ]
2021-10-13 09:21:53 +01:00
if not trade_data then return end
2022-02-24 19:39:03 +00:00
local stored_fuel = memory.stored_fuel
2022-02-27 16:42:25 +00:00
if not stored_fuel then return end
-- local captain_index = memory.playerindex_captain
-- if not (stored_fuel and captain_index) then return end
-- local captain = game.players[captain_index]
if not Common.validate_player_and_character ( player ) then return end
local inv = player.get_inventory ( defines.inventory . character_main )
2022-02-27 18:47:53 +00:00
if not ( inv and inv.valid ) then return end
2021-10-13 09:21:53 +01:00
local multiplier = Balance.main_shop_cost_multiplier ( )
local can_buy = true
-- local rate_limit_ok = not (memory.mainshop_rate_limit_ticker and memory.mainshop_rate_limit_ticker > 0)
local rate_limit_ok = true
2022-02-24 19:39:03 +00:00
local enough_fuel = true
2021-10-13 09:21:53 +01:00
local enough_iron_plates = true
2022-02-24 19:39:03 +00:00
local enough_coins = true
2021-10-13 09:21:53 +01:00
local enough_copper_plates = true
2022-02-24 19:39:03 +00:00
local coins_got = nil
2021-10-13 09:21:53 +01:00
local iron_plates_got = nil
local copper_plates_got = nil
-- local able_to_buy_boats = memory.boat.state == Boats.enum_state.DOCKED --disabled for now
local able_to_buy_boats = false
-- local able_to_buy_boats = (memory.boat.state == Boats.enum_state.DOCKED or memory.boat.state == Boats.enum_state.APPROACHING) --problem with this if you buy whilst approaching, the original one no longer moves
for k , v in pairs ( trade_data.base_cost ) do
2022-02-24 19:39:03 +00:00
if k == ' fuel ' then
enough_fuel = ( stored_fuel >= v * multiplier )
elseif k == ' coins ' then
2022-02-27 16:42:25 +00:00
coins_got = inv.get_item_count ( ' coin ' )
2022-02-24 19:39:03 +00:00
enough_coins = coins_got >= v * multiplier
2021-10-13 09:21:53 +01:00
elseif k == ' iron_plates ' then
2022-02-27 16:42:25 +00:00
iron_plates_got = inv.get_item_count ( ' iron-plate ' )
2021-10-13 09:21:53 +01:00
enough_iron_plates = iron_plates_got >= v * multiplier
elseif k == ' copper_plates ' then
2022-02-27 16:42:25 +00:00
copper_plates_got = inv.get_item_count ( ' copper-plate ' )
2021-10-13 09:21:53 +01:00
enough_copper_plates = copper_plates_got >= v * multiplier
end
end
2022-02-24 19:39:03 +00:00
can_buy = rate_limit_ok and enough_coins and enough_fuel and enough_iron_plates and enough_copper_plates
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
if purchase_name == ' new_boat_sloop_with_hold ' or purchase_name == ' new_boat_cutter_with_hold ' or purchase_name == ' new_boat_cutter ' then can_buy = can_buy and able_to_buy_boats end
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
-- @TODO: prevent people from buying things whilst marooned
2021-10-13 09:21:53 +01:00
if can_buy then
for k , v in pairs ( trade_data.base_cost ) do
2022-02-24 19:39:03 +00:00
if k == ' fuel ' then
memory.stored_fuel = memory.stored_fuel - v * multiplier
elseif k == ' coins ' then
2022-02-27 16:42:25 +00:00
inv.remove { name = " coin " , count = v * multiplier }
2021-10-13 09:21:53 +01:00
elseif k == ' iron_plates ' then
2022-02-27 16:42:25 +00:00
inv.remove { name = " iron-plate " , count = v * multiplier }
2021-10-13 09:21:53 +01:00
elseif k == ' copper_plates ' then
2022-02-27 16:42:25 +00:00
inv.remove { name = " copper-plate " , count = v * multiplier }
2021-10-13 09:21:53 +01:00
end
end
2022-03-04 17:57:58 +00:00
local force = memory.force
2021-10-13 09:21:53 +01:00
if not ( force and force.valid ) then return end
local gotamount
2022-02-27 16:42:25 +00:00
if purchase_name == ' uranium_ore ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/uranium-238 ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' uranium-238 ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is buying green rocks... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' extra_time ' then
2021-10-13 09:21:53 +01:00
local success = Crew.try_add_extra_time_at_sea ( 60 * 60 )
if success then
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' %s is buying extra time at sea... ' , player.name ) )
2021-10-13 09:21:53 +01:00
else
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , string.format ( ' Can \' t buy more time than this. ' , player.name ) )
2021-10-13 09:21:53 +01:00
-- refund:
2022-02-24 19:39:03 +00:00
memory.stored_fuel = memory.stored_fuel + trade_data.base_cost . fuel * multiplier
2021-10-13 09:21:53 +01:00
end
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' rail_signal ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/rail-signal ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' rail-signal ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is buying signals... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' artillery_shell ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/artillery-shell ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' artillery-shell ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is buying cannon shells... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' artillery_remote ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/artillery-targeting-remote ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' artillery-targeting-remote ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is buying an artillery targeting remote... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' new_boat_cutter ' or purchase_name == ' new_boat_cutter_with_hold ' or purchase_name == ' new_boat_sloop_with_hold ' then
2021-10-13 09:21:53 +01:00
Dock.execute_boat_purchase ( )
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' [font=heading-1]%s bought a %s.[/font] ' , player.name , Boats [ Common.current_destination ( ) . static_params.boat_for_sale_type ] . Data.display_name ) )
2021-10-13 09:21:53 +01:00
2022-03-01 21:59:48 +00:00
elseif purchase_name == ' repair_cannons ' then
-- heal all cannons:
local cannons = game.surfaces [ destination.surface_name ] . find_entities_filtered ( { type = ' artillery-turret ' } )
for _ , c in pairs ( cannons ) do
c.health = c.prototype . max_health
end
Common.notify_force_light ( force , string.format ( ' %s repaired the ship \' s cannons. ' , player.name ) )
memory.mainshop_availability_bools [ purchase_name ] = false
2022-02-27 16:42:25 +00:00
elseif purchase_name == Upgrades.enum . MORE_POWER then
2021-10-13 09:21:53 +01:00
Upgrades.execute_upgade ( Upgrades.enum . MORE_POWER )
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' [font=heading-1]%s upgraded the ship \' s power.[/font] ' , player.name ) )
memory.mainshop_availability_bools [ purchase_name ] = false
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == Upgrades.enum . EXTRA_HOLD then
2021-10-13 09:21:53 +01:00
Upgrades.execute_upgade ( Upgrades.enum . EXTRA_HOLD )
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' [font=heading-1]%s upgraded the ship \' s hold.[/font] ' , player.name ) )
memory.mainshop_availability_bools [ purchase_name ] = false
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == Upgrades.enum . UNLOCK_MERCHANTS then
2021-10-13 09:21:53 +01:00
Upgrades.execute_upgade ( Upgrades.enum . UNLOCK_MERCHANTS )
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' [font=heading-1]%s unlocked merchant ships.[/font] ' , player.name ) )
memory.mainshop_availability_bools [ purchase_name ] = false
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == Upgrades.enum . ROCKETS_FOR_SALE then
2021-10-13 09:21:53 +01:00
Upgrades.execute_upgade ( Upgrades.enum . ROCKETS_FOR_SALE )
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' [font=heading-1]%s unlocked the sale of rockets at covered-up markets.[/font] ' , player.name ) )
memory.mainshop_availability_bools [ purchase_name ] = false
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' sell_iron ' then
2022-02-25 00:01:28 +00:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/coal ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' fuel ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is selling iron... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' buy_iron ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/iron-plate ' ]
Common.give_reward_items { { name = ' iron-plate ' , count = gotamount } }
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' %s is buying iron... ' , player.name ) )
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' buy_copper ' then
2021-10-13 09:21:53 +01:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/copper-plate ' ]
Common.give_reward_items { { name = ' copper-plate ' , count = gotamount } }
2022-02-27 16:42:25 +00:00
Common.notify_force_light ( force , string.format ( ' %s is buying copper... ' , player.name ) )
2021-10-13 09:21:53 +01:00
-- elseif name == 'buy_fast_loader' then
-- gotamount = trade_data.what_you_get_sprite_buttons['item/fast-loader']
2022-02-27 16:42:25 +00:00
-- Common.give(player, {{name = 'fast-loader', count = gotamount}})
-- Common.notify_force_light(force,string.format('%s bought a fast loader...', player.name))
2021-10-13 09:21:53 +01:00
2022-02-27 16:42:25 +00:00
elseif purchase_name == ' sell_copper ' then
2022-02-25 00:01:28 +00:00
gotamount = trade_data.what_you_get_sprite_buttons [ ' item/coal ' ]
2022-02-27 16:42:25 +00:00
Common.give ( player , { { name = ' fuel ' , count = gotamount } } )
Common.notify_force_light ( force , string.format ( ' %s is selling copper... ' , player.name ) )
2021-10-13 09:21:53 +01:00
end
-- memory.mainshop_rate_limit_ticker = Common.mainshop_rate_limit_ticks
else
-- play sound?
if rate_limit_ok == false then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Shop rate limit exceeded. ' )
2021-10-13 09:21:53 +01:00
end
2022-02-24 19:39:03 +00:00
if enough_fuel == false then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Not enough stored fuel. ' )
2022-02-24 19:39:03 +00:00
end
if enough_coins == false then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Not enough coins. ' )
2021-10-13 09:21:53 +01:00
end
if enough_iron_plates == false then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Not enough iron plates. ' )
2021-10-13 09:21:53 +01:00
end
if enough_copper_plates == false then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Not enough copper plates. ' )
2021-10-13 09:21:53 +01:00
end
2022-02-27 16:42:25 +00:00
if ( purchase_name == ' new_boat_cutter ' or purchase_name == ' new_boat_sloop_with_hold ' or purchase_name == ' new_boat_cutter_with_hold ' ) and ( not able_to_buy_boats ) then
2022-03-07 12:57:07 +00:00
Common.notify_error ( player , ' Not able to purchase ships right now. ' )
2021-10-13 09:21:53 +01:00
end
end
end
function Public . event_on_market_item_purchased ( event )
2022-02-26 18:25:48 +00:00
local player_index , market , offer_index , trade_count = event.player_index , event.market , event.offer_index , event.count
2021-10-13 09:21:53 +01:00
local player = game.players [ player_index ]
if not ( market and market.valid and offer_index and Common.validate_player ( player ) ) then return end
local crew_id = tonumber ( string.sub ( player.force . name , - 3 , - 1 ) ) or nil
Memory.set_working_id ( crew_id )
local memory = Memory.get_crew_memory ( )
2022-02-26 23:33:19 +00:00
local destination = Common.current_destination ( )
2021-10-13 09:21:53 +01:00
local alloffers = market.get_market_items ( )
local this_offer = alloffers [ offer_index ]
2022-02-26 18:25:48 +00:00
2021-10-13 09:21:53 +01:00
local price = this_offer.price
2022-02-26 18:25:48 +00:00
local offer_type = this_offer.offer . type
local offer_giveitem_name , offer_giveitem_count
if offer_type == ' give-item ' then
offer_giveitem_name = this_offer.offer . item
offer_giveitem_count = this_offer.offer . count
end
2021-10-13 09:21:53 +01:00
local inv = player.get_inventory ( defines.inventory . character_main )
2022-02-26 18:25:48 +00:00
2022-03-05 14:09:35 +00:00
-- check for BARTER vs STATIC vs ONE-oFF
2022-02-26 23:33:19 +00:00
-- One-off becomes unavailable after purchase, such as class purchase
-- Static doesn't decay
-- Barter decays
local decay_type
2022-03-03 00:19:20 +00:00
local dock_bool = destination.type == SurfacesCommon.enum . DOCK
local purchase_bool = ( price and price [ 1 ] and price [ 1 ] . name and ( price [ 1 ] . name == ' coin ' ) )
local simple_efficiency_trade_bool = ( price and price [ 1 ] and price [ 1 ] . name and ( price [ 1 ] . name == ' pistol ' or price [ 1 ] . name == ' burner-mining-drill ' ) )
2022-02-26 18:25:48 +00:00
if offer_type == ' nothing ' then
2022-02-26 23:33:19 +00:00
decay_type = ' one-off '
2022-03-03 00:19:20 +00:00
elseif dock_bool and purchase_bool and ( offer_giveitem_name ) and ( offer_giveitem_name == ' stone ' or offer_giveitem_name == ' iron-ore ' or offer_giveitem_name == ' copper-ore ' or offer_giveitem_name == ' crude-oil-barrel ' ) then
decay_type = ' double_decay '
2022-03-05 19:56:41 +00:00
elseif dock_bool and purchase_bool and ( offer_giveitem_name ) then
decay_type = ' one-off '
2022-03-03 00:19:20 +00:00
elseif simple_efficiency_trade_bool or ( offer_giveitem_name and ( offer_giveitem_name == ' defender-capsule ' or offer_giveitem_name == ' gun-turret ' ) ) then
2022-02-26 23:33:19 +00:00
decay_type = ' static '
else
decay_type = ' decay '
end
2022-03-05 14:09:35 +00:00
-- For everything but static, we want to disallow multi-purchases in this game, so refund any additional purchases:
if decay_type ~= ' static ' and player and trade_count and trade_count > 1 then
inv = player.get_inventory ( defines.inventory . character_main )
if not inv then return end
for _ , p in pairs ( price ) do
inv.insert { name = p.name , count = p.amount * ( trade_count - 1 ) }
end
if offer_type == ' give-item ' then
inv.remove { name = offer_giveitem_name , count = offer_giveitem_count * ( trade_count - 1 ) }
end
end
2022-02-26 23:33:19 +00:00
if decay_type == ' one-off ' then
2021-10-13 09:21:53 +01:00
local force = player.force
2022-02-26 23:33:19 +00:00
if offer_type == ' nothing ' then
2022-03-04 17:57:58 +00:00
local class_for_sale = destination.static_params . class_for_sale
-- if not class_for_sale then return end
local required_class = Classes.class_purchase_requirement [ class_for_sale ]
local ok = true
-- check if they have the required class to buy it
if required_class then
if not ( memory.classes_table and memory.classes_table [ player.index ] and memory.classes_table [ player.index ] == required_class ) then
ok = false
2022-03-07 12:57:07 +00:00
Common.notify_error ( force , string.format ( ' You need to be a %s to buy this. ' , Classes.display_form [ required_class ] ) )
2022-03-04 17:57:58 +00:00
end
2022-02-26 23:33:19 +00:00
end
2022-03-04 17:57:58 +00:00
if ok then
if required_class then
if force and force.valid then
Common.notify_force_light ( force , string.format ( ' %s upgraded their class from %s to %s. ([font=scenario-message-dialog]%s[/font]) ' , player.name , Classes.display_form [ required_class ] , Classes.display_form [ class_for_sale ] , Classes.explanation [ class_for_sale ] ) )
end
else
-- check if they have a role already - renounce it if so
if memory.classes_table and memory.classes_table [ player.index ] then
Classes.try_renounce_class ( player )
end
if force and force.valid then
Common.notify_force_light ( force , string.format ( ' %s bought the class %s. ([font=scenario-message-dialog]%s[/font]) ' , player.name , Classes.display_form [ class_for_sale ] , Classes.explanation [ class_for_sale ] ) )
end
end
memory.classes_table [ player.index ] = class_for_sale
2022-03-05 14:09:35 +00:00
memory.available_classes_pool = Utils.ordered_table_with_single_value_removed ( memory.available_classes_pool , class_for_sale )
2022-02-26 23:33:19 +00:00
2022-03-04 17:57:58 +00:00
if destination.dynamic_data and destination.dynamic_data . market_class_offer_rendering then
rendering.destroy ( destination.dynamic_data . market_class_offer_rendering )
end
market.remove_market_item ( offer_index )
if Classes.class_unlocks [ class_for_sale ] then
for _ , upgrade in pairs ( Classes.class_unlocks [ class_for_sale ] ) do
memory.available_classes_pool [ # memory.available_classes_pool + 1 ] = upgrade
end
end
else
--refund
inv = player.get_inventory ( defines.inventory . character_main )
if not inv then return end
for _ , p in pairs ( price ) do
inv.insert { name = p.name , count = p.amount }
end
2022-02-26 23:33:19 +00:00
end
else
Common.notify_force_light ( player.force , player.name .. ' bought ' .. this_offer.offer . count .. ' ' .. this_offer.offer . item .. ' for ' .. price [ 1 ] . amount .. ' ' .. price [ 1 ] . name .. ' . ' )
2022-03-04 17:57:58 +00:00
market.remove_market_item ( offer_index )
2021-10-13 09:21:53 +01:00
end
else
2022-02-26 23:33:19 +00:00
-- print:
2022-02-26 13:55:36 +00:00
if ( price and price [ 1 ] ) then
2022-02-27 16:42:25 +00:00
if not ( price [ 1 ] . name and price [ 1 ] . name == ' burner-mining-drill ' ) then --this one is too boring to announce
if price [ 2 ] then
local fish = price [ 2 ] . name
if fish == ' raw-fish ' then fish = ' fish ' end
Common.notify_force_light ( player.force , player.name .. ' is trading away ' .. price [ 1 ] . amount .. ' ' .. price [ 1 ] . name .. ' and ' .. fish .. ' for ' .. this_offer.offer . count .. ' ' .. this_offer.offer . item .. ' ... ' )
else
2022-02-28 16:36:46 +00:00
if price [ 1 ] . name == ' coin ' then
Common.notify_force_light ( player.force , player.name .. ' bought ' .. this_offer.offer . count .. ' ' .. this_offer.offer . item .. ' for ' .. price [ 1 ] . amount .. ' ' .. price [ 1 ] . name .. ' ... ' )
elseif this_offer.offer . item == ' coin ' then
2022-03-04 17:57:58 +00:00
local sold_amount = price [ 1 ] . amount
if sold_amount == 1 then sold_amount = ' a ' end
Common.notify_force_light ( player.force , player.name .. ' sold ' .. sold_amount .. ' ' .. price [ 1 ] . name .. ' for ' .. this_offer.offer . count .. ' ' .. this_offer.offer . item .. ' ... ' )
2022-02-28 16:36:46 +00:00
else
Common.notify_force_light ( player.force , player.name .. ' is trading away ' .. price [ 1 ] . amount .. ' ' .. price [ 1 ] . name .. ' for ' .. this_offer.offer . count .. ' ' .. this_offer.offer . item .. ' ... ' )
end
2022-02-27 16:42:25 +00:00
end
2022-02-26 13:55:36 +00:00
end
end
2022-02-26 23:33:19 +00:00
if decay_type == ' static ' then
2021-10-13 09:21:53 +01:00
if not inv then return end
local flying_text_color = { r = 255 , g = 255 , b = 255 }
local text1 = ' [color=1,1,1]+ ' .. this_offer.offer . count .. ' [/color] [item= ' .. alloffers [ offer_index ] . offer.item .. ' ] '
local text2 = ' [color= ' .. flying_text_color.r .. ' , ' .. flying_text_color.g .. ' , ' .. flying_text_color.b .. ' ]( ' .. inv.get_item_count ( alloffers [ offer_index ] . offer.item ) .. ' )[/color] '
Common.flying_text ( player.surface , player.position , text1 .. ' [font=count-font] ' .. text2 .. ' [/font] ' )
else
2022-03-03 00:19:20 +00:00
local decay_param = Balance.barter_decay_parameter ( )
if decay_type == ' double_decay ' then decay_param = Balance.barter_decay_parameter ( ) ^ 2 end
2021-10-13 09:21:53 +01:00
if not inv then return end
local flying_text_color = { r = 255 , g = 255 , b = 255 }
local text1 = ' [color=1,1,1]+ ' .. this_offer.offer . count .. ' [/color] [item= ' .. alloffers [ offer_index ] . offer.item .. ' ] '
local text2 = ' [color= ' .. flying_text_color.r .. ' , ' .. flying_text_color.g .. ' , ' .. flying_text_color.b .. ' ]( ' .. inv.get_item_count ( this_offer.offer . item ) .. ' )[/color] '
Common.flying_text ( player.surface , player.position , text1 .. ' [font=count-font] ' .. text2 .. ' [/font] ' )
2022-02-26 18:25:48 +00:00
--update market trades:
2022-03-03 00:19:20 +00:00
alloffers [ offer_index ] . offer.count = Math.max ( Math.floor ( alloffers [ offer_index ] . offer.count * decay_param ) , 1 )
2021-10-13 09:21:53 +01:00
market.clear_market_items ( )
for _ , offer in pairs ( alloffers ) do
market.add_market_item ( offer )
end
end
end
end
return Public