1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/features/train_saviour.lua

100 lines
2.7 KiB
Lua
Raw Normal View History

2018-08-13 22:01:13 +02:00
local Event = require 'utils.event'
local Market_items = require 'resources.market_items'
local Global = require 'utils.global'
2018-09-11 01:08:22 +02:00
local Donators = require 'resources.donators'
2018-11-11 09:20:07 +02:00
local UserGroups = require 'features.user_groups'
2018-09-23 00:25:13 +02:00
local Game = require 'utils.game'
2018-09-11 01:08:22 +02:00
local train_perk_flag = Donators.donator_perk_flags.train
2018-08-13 22:01:13 +02:00
local saviour_token_name = 'small-plane' -- item name for what saves players
local saviour_timeout = 180 -- number of ticks players are train immune after getting hit (roughly)
2018-09-11 01:08:22 +02:00
table.insert(
Market_items, 3,
{price = {{Market_items.market_item, 100}}, offer = {type = 'nothing', effect_description = 'Train Immunity (+1 ' .. saviour_token_name .. ')\nEach ' .. saviour_token_name .. ' in your inventory will save you\nfrom being killed by a train once\n\nPrice: 100 '..Market_items.market_item..'s'}, item = saviour_token_name}
2018-09-11 01:08:22 +02:00
)
2018-08-13 22:01:13 +02:00
local remove_stack = {name = saviour_token_name, count = 1}
local saved_players = {}
Global.register(
saved_players,
function(tbl)
saved_players = tbl
end
)
local train_names = {
['locomotive'] = true,
['cargo-wagon'] = true,
['fluid-wagon'] = true,
['artillery-wagon'] = true
}
local function save_player(player)
player.character.health = 1
local pos = player.surface.find_non_colliding_position('player', player.position, 100, 2)
if not pos then
return
end
player.teleport(pos, player.surface)
end
local function on_pre_death(event)
local cause = event.cause
if not cause or not cause.valid then
return
end
if not train_names[cause.name] then
return
end
local player_index = event.player_index
local player = Game.get_player_by_index(player_index)
2018-08-13 22:01:13 +02:00
if not player or not player.valid then
return
end
local tick = saved_players[player_index]
local game_tick = game.tick
if tick and game_tick - tick <= saviour_timeout then
save_player(player)
return
end
2018-09-11 01:08:22 +02:00
local player_name = player.name
if UserGroups.player_has_donator_perk(player_name, train_perk_flag) then
2018-09-11 01:08:22 +02:00
saved_players[player_index] = game_tick
save_player(player)
game.print(player_name .. ' has been saved from a train death as a perk of donating to the server.')
return
end
2018-08-13 22:01:13 +02:00
local saviour_tokens = player.get_item_count(saviour_token_name)
if saviour_tokens < 1 then
return
end
player.remove_item(remove_stack)
saved_players[player_index] = game_tick
save_player(player)
game.print(
table.concat {
2018-09-11 01:08:22 +02:00
player_name,
2018-08-13 22:01:13 +02:00
' has been saved from a train death. Their ',
saviour_token_name,
2018-11-01 02:59:46 +02:00
' survival item has been consumed.'
2018-08-13 22:01:13 +02:00
}
)
end
Event.add(defines.events.on_pre_player_died, on_pre_death)