2020-08-10 23:15:29 +02:00
|
|
|
require 'modules.check_fullness'
|
|
|
|
|
2020-07-30 17:47:50 +02:00
|
|
|
local Event = require 'utils.event'
|
|
|
|
local Functions = require 'maps.mountain_fortress_v3.ic.functions'
|
|
|
|
local IC = require 'maps.mountain_fortress_v3.ic.table'
|
2020-08-21 13:56:01 +02:00
|
|
|
local Minimap = require 'maps.mountain_fortress_v3.ic.minimap'
|
2020-07-30 17:47:50 +02:00
|
|
|
local Public = {}
|
|
|
|
|
|
|
|
Public.reset = IC.reset
|
|
|
|
Public.get_table = IC.get
|
|
|
|
|
|
|
|
local function on_entity_died(event)
|
|
|
|
local entity = event.entity
|
2020-08-09 20:31:50 +02:00
|
|
|
if not entity or not entity.valid then
|
2020-07-30 17:47:50 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-02-15 23:34:24 +02:00
|
|
|
local valid_types = IC.get_types()
|
2020-08-10 23:15:29 +02:00
|
|
|
|
2021-02-15 23:34:24 +02:00
|
|
|
if (valid_types[entity.type] or valid_types[entity.name]) then
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.kill_car(entity)
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_player_mined_entity(event)
|
|
|
|
local entity = event.entity
|
2020-08-09 20:31:50 +02:00
|
|
|
if not entity or not entity.valid then
|
2020-07-30 17:47:50 +02:00
|
|
|
return
|
|
|
|
end
|
2020-08-05 21:36:20 +02:00
|
|
|
|
2021-02-15 23:34:24 +02:00
|
|
|
local valid_types = IC.get_types()
|
2020-08-05 21:36:20 +02:00
|
|
|
|
2021-02-15 23:34:24 +02:00
|
|
|
if (valid_types[entity.type] or valid_types[entity.name]) then
|
2021-01-12 22:53:19 +02:00
|
|
|
Minimap.kill_minimap(game.players[event.player_index])
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.save_car(event)
|
2020-08-09 20:31:50 +02:00
|
|
|
end
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_robot_mined_entity(event)
|
|
|
|
local entity = event.entity
|
2020-08-05 21:36:20 +02:00
|
|
|
|
2020-07-30 17:47:50 +02:00
|
|
|
if not entity and not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
2020-08-05 21:36:20 +02:00
|
|
|
|
2021-02-15 23:34:24 +02:00
|
|
|
local valid_types = IC.get_types()
|
|
|
|
|
|
|
|
if (valid_types[entity.type] or valid_types[entity.name]) then
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.kill_car(entity)
|
2020-08-05 21:36:20 +02:00
|
|
|
end
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_built_entity(event)
|
2020-08-09 20:31:50 +02:00
|
|
|
local ce = event.created_entity
|
|
|
|
|
|
|
|
if not ce or not ce.valid then
|
|
|
|
return
|
|
|
|
end
|
2021-02-15 23:34:24 +02:00
|
|
|
|
|
|
|
local valid_types = IC.get_types()
|
|
|
|
|
|
|
|
if (valid_types[ce.type] or valid_types[ce.name]) ~= true then
|
2020-08-09 20:31:50 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = game.get_player(event.player_index)
|
|
|
|
if not player or not player.valid then
|
2020-08-05 21:36:20 +02:00
|
|
|
return
|
|
|
|
end
|
2020-07-30 17:47:50 +02:00
|
|
|
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.create_car(event)
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_player_driving_changed_state(event)
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.use_door_with_entity(player, event.entity)
|
|
|
|
Functions.validate_owner(player, event.entity)
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_tick()
|
|
|
|
local tick = game.tick
|
|
|
|
|
2021-05-23 02:02:11 +02:00
|
|
|
if tick % 20 == 1 then
|
2021-05-16 14:42:15 +02:00
|
|
|
Functions.item_transfer()
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
|
2020-08-21 13:56:01 +02:00
|
|
|
if tick % 240 == 0 then
|
|
|
|
Minimap.update_minimap()
|
|
|
|
end
|
|
|
|
|
|
|
|
if tick % 400 == 0 then
|
2021-05-16 14:42:15 +02:00
|
|
|
Functions.remove_invalid_cars()
|
2020-07-30 17:47:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-27 13:27:34 +02:00
|
|
|
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
|
2021-09-17 22:26:13 +02:00
|
|
|
local cars = IC.get('cars')
|
|
|
|
if not cars[entity.unit_number] then
|
2020-08-27 13:27:34 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Minimap.kill_minimap(game.players[event.player_index])
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_gui_opened(event)
|
|
|
|
local entity = event.entity
|
|
|
|
if not entity or not entity.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not entity.unit_number then
|
|
|
|
return
|
|
|
|
end
|
2021-09-17 22:26:13 +02:00
|
|
|
local cars = IC.get('cars')
|
|
|
|
local car = cars[entity.unit_number]
|
2020-08-27 13:27:34 +02:00
|
|
|
if not car then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-05-18 21:11:28 +02:00
|
|
|
local surface_index = car.surface
|
|
|
|
local surface = game.surfaces[surface_index]
|
|
|
|
if not surface or not surface.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-08-27 13:27:34 +02:00
|
|
|
Minimap.minimap(
|
|
|
|
game.players[event.player_index],
|
2021-05-18 21:11:28 +02:00
|
|
|
surface,
|
2020-08-27 13:27:34 +02:00
|
|
|
{
|
|
|
|
car.area.left_top.x + (car.area.right_bottom.x - car.area.left_top.x) * 0.5,
|
|
|
|
car.area.left_top.y + (car.area.right_bottom.y - car.area.left_top.y) * 0.5
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-08-21 13:56:01 +02:00
|
|
|
local function on_gui_click(event)
|
|
|
|
local element = event.element
|
|
|
|
if not element or not element.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local player = game.get_player(event.player_index)
|
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if event.element.name == 'minimap_button' then
|
|
|
|
Minimap.minimap(player, false)
|
|
|
|
elseif event.element.name == 'minimap_frame' or event.element.name == 'minimap_toggle_frame' then
|
|
|
|
Minimap.toggle_minimap(event)
|
|
|
|
elseif event.element.name == 'switch_auto_map' then
|
|
|
|
Minimap.toggle_auto(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function trigger_on_player_kicked_from_surface(data)
|
|
|
|
local player = data.player
|
|
|
|
local target = data.target
|
2021-09-17 22:26:13 +02:00
|
|
|
Functions.kick_player_from_surface(player, target)
|
2020-08-21 13:56:01 +02:00
|
|
|
end
|
|
|
|
|
2020-07-30 17:47:50 +02:00
|
|
|
local function on_init()
|
|
|
|
Public.reset()
|
|
|
|
end
|
|
|
|
|
2021-01-11 16:58:12 +02:00
|
|
|
local function on_gui_switch_state_changed(event)
|
|
|
|
local element = event.element
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
if not (player and player.valid) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not element.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if element.name == 'ic_auto_switch' then
|
|
|
|
Minimap.toggle_auto(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-21 13:56:01 +02:00
|
|
|
local changed_surface = Minimap.changed_surface
|
|
|
|
|
2020-07-30 17:47:50 +02:00
|
|
|
Event.on_init(on_init)
|
|
|
|
Event.add(defines.events.on_tick, on_tick)
|
2020-08-27 13:27:34 +02:00
|
|
|
Event.add(defines.events.on_gui_opened, on_gui_opened)
|
|
|
|
Event.add(defines.events.on_gui_closed, on_gui_closed)
|
2020-07-30 17:47:50 +02:00
|
|
|
Event.add(defines.events.on_player_driving_changed_state, on_player_driving_changed_state)
|
|
|
|
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_player_mined_entity, on_player_mined_entity)
|
|
|
|
Event.add(defines.events.on_robot_mined_entity, on_robot_mined_entity)
|
2020-08-21 13:56:01 +02:00
|
|
|
Event.add(defines.events.on_gui_click, on_gui_click)
|
|
|
|
Event.add(defines.events.on_player_changed_surface, changed_surface)
|
|
|
|
Event.add(IC.events.on_player_kicked_from_surface, trigger_on_player_kicked_from_surface)
|
2021-01-11 16:58:12 +02:00
|
|
|
Event.add(defines.events.on_gui_switch_state_changed, on_gui_switch_state_changed)
|
|
|
|
|
2020-07-30 17:47:50 +02:00
|
|
|
return Public
|