2020-05-23 21:18:18 +02:00
|
|
|
local Collapse = require 'modules.collapse'
|
|
|
|
local Balance = require 'maps.mountain_fortress_v3.balance'
|
2021-05-25 22:19:20 +02:00
|
|
|
local RPG = require 'modules.rpg.main'
|
2020-06-03 20:09:00 +02:00
|
|
|
local WPT = require 'maps.mountain_fortress_v3.table'
|
2020-06-07 13:33:24 +02:00
|
|
|
local Alert = require 'utils.alert'
|
2020-05-23 21:18:18 +02:00
|
|
|
local Event = require 'utils.event'
|
2020-06-05 23:53:58 +02:00
|
|
|
local Task = require 'utils.task'
|
|
|
|
local Token = require 'utils.token'
|
2021-11-07 21:40:37 +02:00
|
|
|
local Color = require 'utils.color_presets'
|
2020-05-23 21:18:18 +02:00
|
|
|
|
|
|
|
local raise_event = script.raise_event
|
|
|
|
local floor = math.floor
|
2020-12-05 19:09:09 +02:00
|
|
|
local abs = math.abs
|
2020-08-14 17:16:04 +02:00
|
|
|
local random = math.random
|
2021-05-23 02:02:11 +02:00
|
|
|
local sub = string.sub
|
2020-05-23 21:18:18 +02:00
|
|
|
local sqrt = math.sqrt
|
2022-04-07 00:17:41 +02:00
|
|
|
local zone_settings = WPT.zone_settings
|
2020-11-17 13:45:27 +02:00
|
|
|
|
2021-11-23 21:28:23 +02:00
|
|
|
local clear_breach_text_and_render = function()
|
|
|
|
local beam1 = WPT.get('zone1_beam1')
|
|
|
|
if beam1 and beam1.valid then
|
|
|
|
beam1.destroy()
|
|
|
|
end
|
|
|
|
local beam2 = WPT.get('zone1_beam2')
|
|
|
|
if beam2 and beam2.valid then
|
|
|
|
beam2.destroy()
|
|
|
|
end
|
|
|
|
local zone1_text1 = WPT.get('zone1_text1')
|
|
|
|
if zone1_text1 then
|
|
|
|
rendering.set_text(zone1_text1, 'Collapse has started!')
|
|
|
|
end
|
|
|
|
local zone1_text2 = WPT.get('zone1_text2')
|
|
|
|
if zone1_text2 then
|
|
|
|
rendering.set_text(zone1_text2, 'Collapse has started!')
|
|
|
|
end
|
|
|
|
local zone1_text3 = WPT.get('zone1_text3')
|
|
|
|
if zone1_text3 then
|
|
|
|
rendering.set_text(zone1_text3, 'Collapse has started!')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 23:53:58 +02:00
|
|
|
local collapse_message =
|
|
|
|
Token.register(
|
|
|
|
function(data)
|
|
|
|
local pos = data.position
|
2020-09-04 22:04:28 +02:00
|
|
|
local message = ({'breached_wall.collapse_start'})
|
2020-06-05 23:53:58 +02:00
|
|
|
local collapse_position = {
|
|
|
|
position = pos
|
|
|
|
}
|
|
|
|
Alert.alert_all_players_location(collapse_position, message)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2020-08-14 17:16:04 +02:00
|
|
|
local spidertron_unlocked =
|
|
|
|
Token.register(
|
|
|
|
function()
|
2020-09-04 22:04:28 +02:00
|
|
|
local message = ({'breached_wall.spidertron_unlocked'})
|
2020-08-14 17:16:04 +02:00
|
|
|
Alert.alert_all_players(30, message, nil, 'achievement/tech-maniac', 0.1)
|
|
|
|
end
|
|
|
|
)
|
2021-05-15 16:01:07 +02:00
|
|
|
|
2020-06-05 23:53:58 +02:00
|
|
|
local first_player_to_zone =
|
|
|
|
Token.register(
|
|
|
|
function(data)
|
|
|
|
local player = data.player
|
2020-09-17 09:14:07 +02:00
|
|
|
if not player or not player.valid then
|
|
|
|
return
|
|
|
|
end
|
2020-06-05 23:53:58 +02:00
|
|
|
local breached_wall = data.breached_wall
|
2020-10-30 23:05:05 +02:00
|
|
|
local message = ({'breached_wall.first_to_reach', player.name, breached_wall})
|
2020-06-05 23:53:58 +02:00
|
|
|
Alert.alert_all_players(10, message)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
local artillery_warning =
|
|
|
|
Token.register(
|
|
|
|
function()
|
2020-09-04 22:04:28 +02:00
|
|
|
local message = ({'breached_wall.artillery_warning'})
|
2020-06-05 23:53:58 +02:00
|
|
|
Alert.alert_all_players(10, message)
|
|
|
|
end
|
|
|
|
)
|
2020-06-03 20:09:00 +02:00
|
|
|
|
2020-11-17 16:03:05 +02:00
|
|
|
local spidertron_too_far =
|
|
|
|
Token.register(
|
|
|
|
function(data)
|
|
|
|
local player = data.player
|
|
|
|
local message = ({'breached_wall.cheating_through', player.name})
|
|
|
|
Alert.alert_all_players(30, message)
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
2021-11-07 21:40:37 +02:00
|
|
|
local check_distance_between_player_and_locomotive = function(player)
|
|
|
|
local surface = player.surface
|
|
|
|
local position = player.position
|
|
|
|
local locomotive = WPT.get('locomotive')
|
|
|
|
if not locomotive or not locomotive.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local gap_between_locomotive = WPT.get('gap_between_locomotive')
|
|
|
|
|
|
|
|
if not gap_between_locomotive.highest_pos then
|
|
|
|
gap_between_locomotive.highest_pos = locomotive.position
|
|
|
|
end
|
|
|
|
|
|
|
|
gap_between_locomotive.highest_pos = locomotive.position
|
|
|
|
gap_between_locomotive = WPT.get('gap_between_locomotive')
|
|
|
|
|
|
|
|
local c_y = position.y
|
|
|
|
local t_y = gap_between_locomotive.highest_pos.y
|
|
|
|
|
|
|
|
if c_y - t_y <= gap_between_locomotive.neg_gap then
|
|
|
|
player.teleport({position.x, locomotive.position.y + gap_between_locomotive.neg_gap}, surface)
|
|
|
|
player.print(({'breached_wall.hinder'}), Color.warning)
|
|
|
|
if player.character then
|
|
|
|
player.character.health = player.character.health - 5
|
|
|
|
player.character.surface.create_entity({name = 'water-splash', position = position})
|
|
|
|
if player.character.health <= 0 then
|
|
|
|
player.character.die('enemy')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-23 23:10:45 +02:00
|
|
|
local compare_player_pos = function(player)
|
|
|
|
local p = player.position
|
|
|
|
local index = player.index
|
2022-04-07 00:17:41 +02:00
|
|
|
local adjusted_zones = WPT.get('adjusted_zones')
|
2022-04-07 16:11:18 +02:00
|
|
|
if not adjusted_zones.size then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local zone = floor((abs(p.y / zone_settings.zone_depth)) % adjusted_zones.size) + 1
|
2022-04-07 00:17:41 +02:00
|
|
|
|
|
|
|
if adjusted_zones.scrap[zone] then
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.set_value_to_player(index, 'scrap_zone', true)
|
2020-12-05 19:09:09 +02:00
|
|
|
else
|
2021-05-25 22:19:20 +02:00
|
|
|
local has_scrap = RPG.get_value_from_player(index, 'scrap_zone')
|
2020-11-23 23:10:45 +02:00
|
|
|
if has_scrap then
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.set_value_to_player(index, 'scrap_zone', false)
|
2020-11-23 23:10:45 +02:00
|
|
|
end
|
2020-12-05 19:09:09 +02:00
|
|
|
end
|
|
|
|
|
2022-04-07 00:17:41 +02:00
|
|
|
if adjusted_zones.forest[zone] then
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.set_value_to_player(index, 'forest_zone', true)
|
2020-12-05 19:09:09 +02:00
|
|
|
else
|
2021-05-25 22:19:20 +02:00
|
|
|
local is_in_forest = RPG.get_value_from_player(index, 'forest_zone')
|
2020-12-05 19:09:09 +02:00
|
|
|
if is_in_forest then
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.set_value_to_player(index, 'forest_zone', false)
|
2020-11-23 23:10:45 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-17 16:03:05 +02:00
|
|
|
local compare_player_and_train = function(player, entity)
|
2020-12-04 02:09:52 +02:00
|
|
|
if not player.driving then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if not (entity and entity.valid) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-11-17 16:03:05 +02:00
|
|
|
local position = player.position
|
|
|
|
local locomotive = WPT.get('locomotive')
|
|
|
|
if not locomotive or not locomotive.valid then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local gap_between_zones = WPT.get('gap_between_zones')
|
2020-11-18 16:24:22 +02:00
|
|
|
gap_between_zones.highest_pos = locomotive.position
|
|
|
|
gap_between_zones = WPT.get('gap_between_zones')
|
|
|
|
|
|
|
|
local c_y = position.y
|
|
|
|
local t_y = gap_between_zones.highest_pos.y
|
2022-01-16 21:37:27 +02:00
|
|
|
local spidertron_warning_position = gap_between_zones.neg_gap + 50
|
|
|
|
|
|
|
|
if c_y - t_y <= spidertron_warning_position then
|
|
|
|
local surface = player.surface
|
|
|
|
surface.create_entity(
|
2022-04-05 19:27:46 +02:00
|
|
|
{
|
|
|
|
name = 'flying-text',
|
|
|
|
position = position,
|
2022-04-07 00:17:41 +02:00
|
|
|
text = 'Warning! You are too far away from the main locomotive!',
|
2022-04-05 19:27:46 +02:00
|
|
|
color = {r = 0.9, g = 0.0, b = 0.0}
|
|
|
|
}
|
|
|
|
)
|
2022-01-16 21:37:27 +02:00
|
|
|
end
|
2020-11-17 16:03:05 +02:00
|
|
|
|
|
|
|
if c_y - t_y <= gap_between_zones.neg_gap then
|
|
|
|
if entity.health then
|
|
|
|
entity.health = entity.health - 500
|
|
|
|
if entity.health <= 0 then
|
|
|
|
entity.die('enemy')
|
|
|
|
Task.set_timeout_in_ticks(30, spidertron_too_far, {player = player})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-23 21:18:18 +02:00
|
|
|
local function distance(player)
|
2020-11-17 13:45:27 +02:00
|
|
|
local index = player.index
|
2021-05-25 22:19:20 +02:00
|
|
|
local bonus = RPG.get_value_from_player(index, 'bonus')
|
|
|
|
local rpg_extra = RPG.get('rpg_extra')
|
2020-05-23 21:18:18 +02:00
|
|
|
local breached_wall = WPT.get('breached_wall')
|
2020-07-06 15:45:09 +02:00
|
|
|
local bonus_xp_on_join = WPT.get('bonus_xp_on_join')
|
2020-07-28 11:24:16 +02:00
|
|
|
local enable_arties = WPT.get('enable_arties')
|
2020-06-03 20:09:00 +02:00
|
|
|
|
2020-11-23 23:10:45 +02:00
|
|
|
local p = player.position
|
|
|
|
|
2021-05-23 02:02:11 +02:00
|
|
|
local validate_spider = WPT.get('validate_spider')
|
|
|
|
if validate_spider[index] then
|
|
|
|
local e = validate_spider[index]
|
2020-11-17 16:03:05 +02:00
|
|
|
if not (e and e.valid) then
|
2021-05-23 02:02:11 +02:00
|
|
|
validate_spider[index] = nil
|
2020-11-17 16:03:05 +02:00
|
|
|
end
|
2021-05-23 02:02:11 +02:00
|
|
|
compare_player_and_train(player, validate_spider[index])
|
2020-11-17 16:03:05 +02:00
|
|
|
end
|
|
|
|
|
2020-11-23 23:10:45 +02:00
|
|
|
compare_player_pos(player)
|
|
|
|
|
2022-04-07 00:17:41 +02:00
|
|
|
local distance_to_center = floor(sqrt(p.y ^ 2))
|
2020-05-23 21:18:18 +02:00
|
|
|
local location = distance_to_center
|
2022-04-07 00:17:41 +02:00
|
|
|
if location < zone_settings.zone_depth * bonus - 10 then
|
2020-05-23 21:18:18 +02:00
|
|
|
return
|
|
|
|
end
|
2020-06-03 20:09:00 +02:00
|
|
|
|
2022-04-07 00:17:41 +02:00
|
|
|
local max = zone_settings.zone_depth * bonus
|
|
|
|
local breach_max = zone_settings.zone_depth * breached_wall
|
2020-06-03 20:09:00 +02:00
|
|
|
local breach_max_times = location >= breach_max
|
|
|
|
local max_times = location >= max
|
|
|
|
if max_times then
|
|
|
|
if breach_max_times then
|
2021-05-23 02:02:11 +02:00
|
|
|
local placed_trains_in_zone = WPT.get('placed_trains_in_zone')
|
|
|
|
local biters = WPT.get('biters')
|
2020-05-23 21:18:18 +02:00
|
|
|
rpg_extra.breached_walls = rpg_extra.breached_walls + 1
|
2020-07-06 15:45:09 +02:00
|
|
|
rpg_extra.reward_new_players = bonus_xp_on_join * rpg_extra.breached_walls
|
2021-05-23 02:02:11 +02:00
|
|
|
WPT.set('breached_wall', breached_wall + 1)
|
|
|
|
biters.amount = 0
|
|
|
|
placed_trains_in_zone.randomized = false
|
2020-05-23 21:18:18 +02:00
|
|
|
raise_event(Balance.events.breached_wall, {})
|
2021-02-15 23:34:24 +02:00
|
|
|
if WPT.get('breached_wall') == WPT.get('spidertron_unlocked_at_zone') then
|
2020-08-14 17:16:04 +02:00
|
|
|
local main_market_items = WPT.get('main_market_items')
|
|
|
|
if not main_market_items['spidertron'] then
|
|
|
|
local rng = random(70000, 120000)
|
|
|
|
main_market_items['spidertron'] = {
|
|
|
|
stack = 1,
|
|
|
|
value = 'coin',
|
|
|
|
price = rng,
|
2022-04-07 00:17:41 +02:00
|
|
|
tooltip = 'BiterStunner 9000',
|
2020-08-14 17:16:04 +02:00
|
|
|
upgrade = false,
|
|
|
|
static = true
|
|
|
|
}
|
|
|
|
Task.set_timeout_in_ticks(150, spidertron_unlocked)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 23:53:58 +02:00
|
|
|
local data = {
|
|
|
|
player = player,
|
|
|
|
breached_wall = breached_wall
|
|
|
|
}
|
|
|
|
Task.set_timeout_in_ticks(360, first_player_to_zone, data)
|
2020-06-03 20:09:00 +02:00
|
|
|
if breached_wall == 5 then
|
2020-07-28 11:24:16 +02:00
|
|
|
if enable_arties == 6 then
|
|
|
|
Task.set_timeout_in_ticks(360, artillery_warning)
|
|
|
|
end
|
2020-06-03 20:09:00 +02:00
|
|
|
end
|
|
|
|
end
|
2020-11-17 16:03:05 +02:00
|
|
|
|
2020-06-03 20:09:00 +02:00
|
|
|
if not Collapse.start_now() then
|
2021-11-23 21:28:23 +02:00
|
|
|
clear_breach_text_and_render()
|
2020-06-03 20:09:00 +02:00
|
|
|
Collapse.start_now(true)
|
2020-06-05 23:53:58 +02:00
|
|
|
local data = {
|
|
|
|
position = Collapse.get_position()
|
|
|
|
}
|
|
|
|
Task.set_timeout_in_ticks(550, collapse_message, data)
|
2020-05-23 21:18:18 +02:00
|
|
|
end
|
2020-11-17 16:03:05 +02:00
|
|
|
|
2022-04-08 16:11:06 +02:00
|
|
|
if Collapse.start_now() then
|
|
|
|
clear_breach_text_and_render()
|
|
|
|
end
|
|
|
|
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.set_value_to_player(index, 'bonus', bonus + 1)
|
2020-11-17 13:45:27 +02:00
|
|
|
|
2021-05-25 22:19:20 +02:00
|
|
|
RPG.gain_xp(player, bonus_xp_on_join * bonus)
|
2020-05-23 21:18:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_player_changed_position(event)
|
2020-09-17 09:14:07 +02:00
|
|
|
local player = game.get_player(event.player_index)
|
2021-05-23 02:02:11 +02:00
|
|
|
local surface_name = player.surface.name
|
2020-05-23 21:18:18 +02:00
|
|
|
local map_name = 'mountain_fortress_v3'
|
2021-11-07 21:40:37 +02:00
|
|
|
|
|
|
|
if sub(surface_name, 0, #map_name) ~= map_name then
|
2021-05-23 02:02:11 +02:00
|
|
|
return
|
|
|
|
end
|
2020-05-23 21:18:18 +02:00
|
|
|
|
2021-11-07 21:40:37 +02:00
|
|
|
check_distance_between_player_and_locomotive(player)
|
|
|
|
|
|
|
|
if random(1, 3) ~= 1 then
|
2020-05-23 21:18:18 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
distance(player)
|
|
|
|
end
|
2020-11-17 16:03:05 +02:00
|
|
|
local function on_player_driving_changed_state(event)
|
|
|
|
local player = game.players[event.player_index]
|
|
|
|
if not (player and player.valid) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local entity = event.entity
|
|
|
|
if not (entity and entity.valid) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local s = WPT.get('validate_spider')
|
|
|
|
if entity.name == 'spidertron' then
|
|
|
|
if not s[player.index] then
|
|
|
|
s[player.index] = entity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if s[player.index] then
|
|
|
|
s[player.index] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-23 21:18:18 +02:00
|
|
|
Event.add(defines.events.on_player_changed_position, on_player_changed_position)
|
2020-11-17 16:03:05 +02:00
|
|
|
Event.add(defines.events.on_player_driving_changed_state, on_player_driving_changed_state)
|