1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

added train saviour mod

This commit is contained in:
grilledham 2018-08-13 21:01:13 +01:00
parent 4e78a77ae7
commit b970b2f07f
2 changed files with 83 additions and 3 deletions

View File

@ -12,6 +12,7 @@ require 'corpse_util'
--require 'infinite_storage_chest'
require 'fish_market'
require 'reactor_meltdown'
require 'train_saviour'
require 'map_layout'
require 'bot'
require 'player_colors'
@ -26,7 +27,6 @@ require 'paint'
require 'score'
require 'popup'
local Event = require 'utils.event'
local function player_created(event)
@ -200,7 +200,6 @@ local function format_time(ticks)
return table.concat(result, ' ')
end
global.cheated_items = {}
global.cheated_items_by_timestamp = {}
Event.add(
@ -225,7 +224,7 @@ Event.add(
local name = stack.name
local user_item_record = data[name] or {count = 0}
local count = user_item_record.count
local time = user_item_record["time"] or format_time(game.tick)
local time = user_item_record['time'] or format_time(game.tick)
data[name] = {count = stack.count + count, time = time}
end
)

81
train_saviour.lua Normal file
View File

@ -0,0 +1,81 @@
local Event = require 'utils.event'
local Market_items = require 'resources.market_items'
local Global = require 'utils.global'
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)
table.insert(Market_items, {price = {{'coin', 100}}, offer = {type = 'give-item', item = saviour_token_name}})
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.players[player_index]
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
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 {
player.name,
' has been saved from a train death. Their ',
saviour_token_name,
' survival item has be used up.'
}
)
end
Event.add(defines.events.on_pre_player_died, on_pre_death)