1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-26 03:52:22 +02:00

198 lines
5.3 KiB
Lua
Raw Normal View History

2020-05-17 12:23:55 +02:00
local Event = require 'utils.event'
local Functions = require 'maps.mountain_fortress_v3.icw.functions'
2020-06-03 20:09:00 +02:00
local ICW = require 'maps.mountain_fortress_v3.icw.table'
2020-05-17 12:23:55 +02:00
local Public = {}
2020-06-03 20:09:00 +02:00
Public.reset = ICW.reset
Public.get_table = ICW.get
2020-05-17 12:23:55 +02:00
local function on_entity_died(event)
local entity = event.entity
if not entity and not entity.valid then
return
end
2020-07-08 22:57:43 +02:00
local wagon_types = ICW.get('wagon_types')
if not wagon_types[entity.type] then
2020-05-17 12:23:55 +02:00
return
end
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
Functions.subtract_wagon_entity_count(icw, entity)
Functions.kill_wagon(icw, entity)
end
local function on_player_mined_entity(event)
local entity = event.entity
if not entity and not entity.valid then
return
end
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-08-10 23:15:29 +02:00
if not icw.allow_player_to_build_trains then
return
end
2020-05-17 12:23:55 +02:00
Functions.subtract_wagon_entity_count(icw, entity)
Functions.kill_wagon(icw, entity)
end
local function on_robot_mined_entity(event)
local entity = event.entity
if not entity and not entity.valid then
return
end
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-08-10 23:15:29 +02:00
if not icw.allow_player_to_build_trains then
return
end
2020-05-17 12:23:55 +02:00
Functions.subtract_wagon_entity_count(icw, entity)
Functions.kill_wagon(icw, entity)
end
local function on_built_entity(event)
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-08-10 23:15:29 +02:00
if not icw.allow_player_to_build_trains then
return
end
2020-05-17 12:23:55 +02:00
local created_entity = event.created_entity
Functions.create_wagon(icw, created_entity)
Functions.add_wagon_entity_count(icw, created_entity)
end
local function on_robot_built_entity(event)
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-08-10 23:15:29 +02:00
if not icw.allow_player_to_build_trains then
return
end
2020-05-17 12:23:55 +02:00
local created_entity = event.created_entity
Functions.create_wagon(icw, created_entity)
Functions.add_wagon_entity_count(icw, created_entity)
end
local function on_player_driving_changed_state(event)
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
local player = game.players[event.player_index]
2020-07-24 17:33:28 +02:00
Functions.use_cargo_wagon_door_with_entity(icw, player, event.entity)
2020-05-17 12:23:55 +02:00
end
2020-07-06 15:45:09 +02:00
local function on_player_changed_surface(event)
local player = game.players[event.player_index]
Functions.kill_minimap(player)
end
2020-05-17 12:23:55 +02:00
--[[
local function on_player_created(event)
local player = game.players[event.player_index]
player.insert({name = "cargo-wagon", count = 5})
player.insert({name = "artillery-wagon", count = 5})
player.insert({name = "fluid-wagon", count = 5})
player.insert({name = "locomotive", count = 5})
player.insert({name = "rail", count = 100})
end
]]
local function on_gui_closed(event)
local entity = event.entity
if not entity then
return
end
if not entity.valid then
return
end
if not entity.unit_number then
return
end
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
if not icw.wagons[entity.unit_number] then
return
end
Functions.kill_minimap(game.players[event.player_index])
end
local function on_gui_opened(event)
local entity = event.entity
if not entity then
return
end
if not entity.valid then
return
end
if not entity.unit_number then
return
end
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
local wagon = icw.wagons[entity.unit_number]
if not wagon then
return
end
2020-05-21 23:08:23 +02:00
2020-05-17 12:23:55 +02:00
Functions.draw_minimap(
icw,
game.players[event.player_index],
wagon.surface,
{
wagon.area.left_top.x + (wagon.area.right_bottom.x - wagon.area.left_top.x) * 0.5,
wagon.area.left_top.y + (wagon.area.right_bottom.y - wagon.area.left_top.y) * 0.5
}
)
end
local function on_player_died(event)
Functions.kill_minimap(game.players[event.player_index])
end
2020-06-03 20:09:00 +02:00
local function on_train_created()
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
Functions.request_reconstruction(icw)
end
local function on_gui_click(event)
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
Functions.toggle_minimap(icw, event)
end
local function on_tick()
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
local tick = game.tick
2020-07-06 15:45:09 +02:00
2020-05-17 12:23:55 +02:00
if tick % 60 == 0 then
Functions.item_transfer(icw)
end
if tick % 240 == 0 then
Functions.update_minimap(icw)
end
if not icw.rebuild_tick then
return
end
if icw.rebuild_tick ~= tick then
return
end
Functions.reconstruct_all_trains(icw)
icw.rebuild_tick = nil
end
local function on_init()
Public.reset()
end
function Public.register_wagon(wagon_entity)
2020-06-03 20:09:00 +02:00
local icw = ICW.get()
2020-05-17 12:23:55 +02:00
return Functions.create_wagon(icw, wagon_entity)
end
Event.on_init(on_init)
Event.add(defines.events.on_tick, on_tick)
Event.add(defines.events.on_player_driving_changed_state, on_player_driving_changed_state)
2020-07-06 15:45:09 +02:00
Event.add(defines.events.on_player_changed_surface, on_player_changed_surface)
2020-05-17 12:23:55 +02:00
Event.add(defines.events.on_entity_died, on_entity_died)
Event.add(defines.events.on_built_entity, on_built_entity)
Event.add(defines.events.on_train_created, on_train_created)
Event.add(defines.events.on_robot_built_entity, on_robot_built_entity)
Event.add(defines.events.on_player_died, on_player_died)
--Event.add(defines.events.on_player_created, on_player_created)
Event.add(defines.events.on_gui_click, on_gui_click)
Event.add(defines.events.on_gui_closed, on_gui_closed)
Event.add(defines.events.on_gui_opened, on_gui_opened)
Event.add(defines.events.on_player_mined_entity, on_player_mined_entity)
Event.add(defines.events.on_robot_mined_entity, on_robot_mined_entity)
return Public