Skip to content

Commit

Permalink
Mtn: fix bugs with productions and mystical chest
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerkiz committed Nov 24, 2024
1 parent 2e46c16 commit 3ae9f83
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 58 deletions.
2 changes: 1 addition & 1 deletion maps/mountain_fortress_v3/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Commands.new('mtn_set_queue_speed', 'Usable only for admins - sets the queue spe
end
)

Commands.new('mtn_complete_quests', 'Usable only for admins - sets the queue speed of this map!')
Commands.new('mtn_complete_quests', 'Usable only for admins - completes all the quests!')
:require_admin()
:require_validation()
:add_parameter('no_grace', true, 'boolean')
Expand Down
77 changes: 55 additions & 22 deletions maps/mountain_fortress_v3/locomotive/market.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ local function add_space(frame)
add_style(frame.add { type = 'line', direction = 'horizontal' }, space)
end

local function add_mystical_chest(surface)
local this = Public.get()

if this.mystical_chest_enabled then
if this.mystical_chest and this.mystical_chest.valid then
this.mystical_chest.destroy()
end

local req_chest = surface.find_entities_filtered { name = 'requester-chest', position = { x = this.market.position.x, y = this.market.position.y + 2 } }
if req_chest and #req_chest > 0 then
req_chest[#req_chest].destroy()
end

this.mystical_chest = surface.create_entity { name = 'requester-chest', position = { x = this.market.position.x, y = this.market.position.y + 2 }, force = 'neutral' }
this.mystical_chest.minable = false
this.mystical_chest.destructible = false
if not this.mystical_chest_price then
Public.add_mystical_chest()
end
rendering.draw_text {
text = 'Mystical chest',
surface = surface,
target = this.mystical_chest,
scale = 1.2,
color = { r = 0.98, g = 0.66, b = 0.22 },
alignment = 'center'
}
end
end

local function get_player_data(player, remove)
local storage_data = Public.get('player_market_settings')
if not storage_data then
Expand Down Expand Up @@ -1668,29 +1698,7 @@ local function create_market(data, rebuild)
)
end

if this.mystical_chest_enabled then
if this.mystical_chest and this.mystical_chest.entity then
this.mystical_chest.entity.destroy()
this.mystical_chest.entity = nil
end

this.mystical_chest = {
entity = surface.create_entity { name = 'requester-chest', position = { x = center_position.x, y = center_position.y + 2 }, force = 'neutral' }
}
this.mystical_chest.entity.minable = false
this.mystical_chest.entity.destructible = false
if not this.mystical_chest.price then
Public.add_mystical_chest()
end
rendering.draw_text {
text = 'Mystical chest',
surface = surface,
target = this.mystical_chest.entity,
scale = 1.2,
color = { r = 0.98, g = 0.66, b = 0.22 },
alignment = 'center'
}
end

Public.wintery(this.market, 5.5)

Expand Down Expand Up @@ -1753,6 +1761,30 @@ local function place_market()
end
end

local function place_chest()
local locomotive = Public.get('locomotive')
if not locomotive then
return
end

if not locomotive.valid then
return
end

local icw_table = ICW.get_table()
local unit_surface = locomotive.unit_number
if not icw_table.wagons[unit_surface] then
return
end

local surface = game.surfaces[icw_table.wagons[unit_surface].surface.index]
local mystical_chest = Public.get('mystical_chest')

if not mystical_chest or not mystical_chest.valid or mystical_chest.surface.index ~= surface.index then
add_mystical_chest(surface)
end
end

function Public.close_gui_player(frame)
if not frame then
return
Expand Down Expand Up @@ -1788,6 +1820,7 @@ local function tick()

if ticker % 30 == 0 then
place_market()
place_chest()
end
end

Expand Down
40 changes: 22 additions & 18 deletions maps/mountain_fortress_v3/mystical_chest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ local function get_random_weighted(weighted_table, weight_index)
end
end

local function init_price_check(locomotive, mystical_chest)
local function init_price_check(locomotive)
local mystical_chest = Public.get('mystical_chest')
if not (mystical_chest and mystical_chest.valid) then
return
end

local roll = 48 + abs(locomotive.position.y) * 1.75
roll = roll * random(25, 1337) * 0.01

Expand All @@ -141,7 +146,7 @@ local function init_price_check(locomotive, mystical_chest)
insert(price, { min = v, value = { comparator = "=", name = k, quality = "normal" } })
end

mystical_chest.price = price
Public.set('mystical_chest_price', price)
end

local function roll_item_stacks(remaining_budget, max_slots, blacklist)
Expand Down Expand Up @@ -358,14 +363,11 @@ local function container_opened(event)
end

local mystical_chest = Public.get('mystical_chest')
if not mystical_chest then
return
end
if not (mystical_chest.entity and mystical_chest.entity.valid) then
if not (mystical_chest and mystical_chest.valid) then
return
end

if entity.unit_number ~= mystical_chest.entity.unit_number then
if entity.unit_number ~= mystical_chest.unit_number then
return
end

Expand Down Expand Up @@ -502,30 +504,32 @@ function Public.add_mystical_chest(player)
return
end

local mystical_chest_price = Public.get('mystical_chest_price')
local mystical_chest = Public.get('mystical_chest')
if not (mystical_chest.entity and mystical_chest.entity.valid) then
if not (mystical_chest and mystical_chest.valid) then
return
end

if not mystical_chest.price then
init_price_check(locomotive, mystical_chest)
if not mystical_chest_price then
init_price_check(locomotive)
mystical_chest_price = Public.get('mystical_chest_price')
end

local entity = mystical_chest.entity
local entity = mystical_chest

local inventory = entity.get_inventory(defines.inventory.chest)

for key, item_stack in pairs(mystical_chest.price) do
for key, item_stack in pairs(mystical_chest_price) do
local stack = { name = item_stack.value.name, count = item_stack.min }
local count_removed = inventory.remove(stack)
mystical_chest.price[key].min = mystical_chest.price[key].min - count_removed
if mystical_chest.price[key].min <= 0 then
table.remove(mystical_chest.price, key)
mystical_chest_price[key].min = mystical_chest_price[key].min - count_removed
if mystical_chest_price[key].min <= 0 then
table.remove(mystical_chest_price, key)
end
end

if #mystical_chest.price == 0 then
init_price_check(locomotive, mystical_chest)
if #mystical_chest_price == 0 then
init_price_check(locomotive)
if player and player.valid then
mystical_chest_reward(player)
local mystical_chest_completed = Public.get('mystical_chest_completed')
Expand All @@ -538,7 +542,7 @@ function Public.add_mystical_chest(player)
entity.get_requester_point().remove_section(1)
entity.get_requester_point().add_section()

for slot, item_stack in pairs(mystical_chest.price) do
for slot, item_stack in pairs(mystical_chest_price) do
entity.get_requester_point().get_section(1).set_slot(slot, item_stack)
end
end
Expand Down
44 changes: 27 additions & 17 deletions maps/mountain_fortress_v3/stateful/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -472,25 +472,48 @@ end

local function get_item_produced_count(_, item_name)
local force = game.forces.player
local statistics = Public.get('statistics')

local loco_surface = Public.get('loco_surface')

local production = force.get_item_production_statistics('nauvis').input_counts[item_name]
if not production then
return false
production = 0
end

if not loco_surface or not loco_surface.valid then
return production
end

if not statistics.surfaces_produced then
statistics.surfaces_produced = {}
end

if not statistics.surfaces_produced[loco_surface.name] then
statistics.surfaces_produced[loco_surface.name] = {}
end

local loco_production = force.get_item_production_statistics(loco_surface.name).input_counts[item_name]
if not loco_production then
return production
loco_production = 0
end
if not statistics.surfaces_produced[loco_surface.name][item_name] then
statistics.surfaces_produced[loco_surface.name][item_name] = loco_production
else
statistics.surfaces_produced[loco_surface.name][item_name] = statistics.surfaces_produced[loco_surface.name][item_name] + (loco_production - statistics.surfaces_produced[loco_surface.name][item_name])
end

local item_count = 0

for _, data in pairs(statistics.surfaces_produced) do
for name, count in pairs(data) do
if name == item_name then
item_count = item_count + count
end
end
end

return production + loco_production
return production + item_count
end

local function get_entity_mined_count(_, item_name)
Expand All @@ -503,19 +526,6 @@ local function get_entity_mined_count(_, item_name)
end
end

local loco_surface = Public.get('loco_surface')

if not loco_surface or not loco_surface.valid then
return count
end

for name, entity_count in pairs(force.get_entity_build_count_statistics(loco_surface.name).output_counts) do
if name:find(item_name) then
count = count + entity_count
end
end


return count
end

Expand Down Expand Up @@ -940,7 +950,7 @@ end
local function get_random_research_recipe()
-- scale(10, 20)
local research_level_list = {
'energy-weapons-damage-7',
'laser-weapons-damage-7',
'stronger-explosives-7',
'mining-productivity-4',
'worker-robots-speed-6',
Expand Down
4 changes: 4 additions & 0 deletions maps/mountain_fortress_v3/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ function Public.reset_main_table()
this.charts = {
tags = {}
}
this.statistics = {
surfaces_produced = {}
}
this.death_mode = false
this.collapse_started = false
this.locomotive_position = nil
Expand Down Expand Up @@ -305,6 +308,7 @@ function Public.reset_main_table()
construction_bot_cost = random(150, 350),
chest_cost = random(400, 600)
}
this.mystical_chest_price = nil
this.collapse_grace = true
this.corpse_removal_disabled = true
this.locomotive_biter = nil
Expand Down

0 comments on commit 3ae9f83

Please sign in to comment.