Files
factorio-scenario-ExpCluster/Addons/Guis/warp-system.lua
2018-01-06 19:28:11 +00:00

216 lines
8.8 KiB
Lua

--[[
Explosive Gaming
This file can be used with permission but this and the credit below must remain in the file.
Contact a member of management on our discord to seek permission to use our code.
Any changes that you may make to the code are yours but that does not make the script yours.
Discord: https://discord.gg/r6dC2uK
]]
--Please Only Edit Below This Line-----------------------------------------------------------
local warp_tiles = {
{-3,-2},{-3,-1},{-3,0},{-3,1},{-3,2},{3,-2},{3,-1},{3,0},{3,1},{3,2},
{-2,-3},{-1,-3},{0,-3},{1,-3},{2,-3},{-2,3},{-1,3},{0,3},{1,3},{2,3}
}
local warp_entities = {
{"small-lamp",-3,-2},{"small-lamp",-3,2},{"small-lamp",3,-2},{"small-lamp",3,2},
{"small-lamp",-2,-3},{"small-lamp",2,-3},{"small-lamp",-2,3},{"small-lamp",2,3},
{"small-electric-pole",-3,-3},{"small-electric-pole",3,3},{"small-electric-pole",-3,3},{"small-electric-pole",3,-3}
}
local warp_radius = 4
local spawn_warp_scale = 5
local warp_tile = 'lab-dark-1'
local warp_partern = 'lab-dark-2'
local warp_limit = 60
local warp_item = 'discharge-defense-equipment'
local global_offset = {x=0,y=0}
local function _warps(reset)
global.addons = not reset and global.addons or {}
global.addons.warps = not reset and global.addons.warps or {_n=0,warps={},cooldowns={}}
return global.addons.warps
end
local function remove_warp_point(name)
local warp = _warps().warps[name]
if not warp then return end
local surface = warp.surface
local offset = warp.position
local tiles = {}
local tiles = {}
for x = -warp_radius-2, warp_radius+2 do
for y = -warp_radius-2, warp_radius+2 do
if x^2+y^2 < (warp_radius+1)^2 then
table.insert(tiles,{name=warp.old_tile,position={x+offset.x,y+offset.y}})
local entities = surface.find_entities_filtered{area={{x+offset.x-1,y+offset.y-1},{x+offset.x,y+offset.y}}}
for _,entity in pairs(entities) do if entity.name ~= 'player' then entity.destroy() end end
end
end
end
surface.set_tiles(tiles)
if warp.tag.valid then warp.tag.destroy() end
_warps().warps[name] = nil
_warps()._n = _warps()._n-1
Gui.left.update('warp-list')
end
local function make_warp_point(position,surface,name)
local warp = _warps().warps[name]
if warp then return end; warp = nil
local offset = {x=math.floor(position.x),y=math.floor(position.y)}
local old_tile = surface.get_tile(offset).name
local base_tiles = {}
local tiles = {}
-- this makes a base plate to make the warp point
for x = -warp_radius-2, warp_radius+2 do
for y = -warp_radius-2, warp_radius+2 do
if x^2+y^2 < warp_radius^2 then
table.insert(base_tiles,{name=warp_tile,position={x+offset.x,y+offset.y}})
end
end
end
surface.set_tiles(base_tiles)
-- this adds the patern and entities
for _,position in pairs(warp_tiles) do
table.insert(tiles,{name=warp_partern,position={position[1]+offset.x+global_offset.x,position[2]+offset.y+global_offset.y}})
end
surface.set_tiles(tiles)
for _,entity in pairs(warp_entities) do
local entity = surface.create_entity{name=entity[1],position={entity[2]+offset.x+global_offset.x,entity[3]+offset.y+global_offset.y},force='neutral'}
entity.destructible = false; entity.health = 0; entity.minable = false; entity.rotatable = false
end
local tag = player.force.add_chart_tag(warp.surface,{
position=warp.position,
text='Warp: '..name,
icon={type='item',name=warp_item}
})
_warps().warps[name] = {tag=tag,surface=surface,position=tag.position,old_tile=old_tile}
Gui.left.update('warp-list')
end
commands.add_command('make-warp', 'Make a warp point at your location', {'name',true}, function(event,args)
if not game.player then return end
local position = game.player.position
local name = args.name
if _warps().warps[name] then player_return({'warp-system.name-used'},defines.text_color.med) return commands.error end
make_warp_point(position,game.player.surface,name)
end)
local remove_warp = Gui.inputs.add{
type='button',
name='remove-warp-point',
caption='utility/remove',
tooltip={'warp-system.remove-tooltip'}
}:on_event('click',function(event)
local name = event.element.parent.name
remove_warp_point(name)
end)
local go_to_warp Gui.inputs.add{
type='button',
name='go-to-warp-point',
caption='utility/export_slot',
tooltip={'warp-system.go-to-tooltip'}
}:on_event('click',function(event)
local player = Game.get_player(event)
local cooldown = _warps().cooldowns[event.player_index] or 0
local warp = _warps().warps[element.parent.name]
if cooldown > 0 then player_return({'warp-system.cooldown',cooldown},nil,event) end
if player.vehicle then player.vehicle.set_driver() end
if player.vehicle then player.vehicle.set_passenger() end
if player.vehicle then return end
player.teleport(warp.surface.find_non_colliding_position('player',warp.position,32,1),warp.surface)
if not Ranking.get_rank(player):allowed('always-warp') then
event.element.parent.parent.parent.style.visible = false
_warps().cooldown[event.player_index] = warp_limit
end
end)
Gui.left.add{
name='warp-list',
caption='item/'..warp_item,
tooltip={'warp-system.tooltip'},
draw=function(frame)
local player = Game.get_player(frame.player_index)
frame.caption={'warp-system.name'}
local flow = frame.add{
type='flow',
direction='vertical'
}
flow.style.maximal_height = 200
for name,warp in pairs(_warps().warp) do
if not warp.tag or not warp.tag.valid then
player.force.add_chart_tag(warp.surface,{
position=warp.position,
text='Warp: '..name,
icon={type='item',name=warp_item}
})
end
flow.add{
type='label',
caption=name,
style='caption_label'
}
local _flow = flow.add{
type='flow',
name=name
}
local btn = go_to_warp:draw(_flow)
btn.style.height = 20
btn.style.width = 20
if Ranking.get_rank(player):allowed('edit-warp-list') then
local btn = remove_warp:draw(_flow)
btn.style.height = 20
btn.style.width = 20
end
end
local cooldown = _warps().cooldowns[event.player_index] or 0
if cooldown > 0 then frame.style.visible = false return end
if _warps()._n == 0 then frame.style.visible = false return end
if Ranking.get_rank(player):allowed('always-warp') then return
elseif player.surface.get_tile(player.position).name == warp_tile
or player.surface.get_tile(player.position).name == warp_partern
and player.surface.name == 'nauvis'
then return
elseif player.position.x^2+player.position.y^2 < (warp_radius*spawn_warp_scale)^2 then return
else frame.style.visible = false end
end,
can_open=function(player)
local cooldown = _warps().cooldowns[event.player_index] or 0
if cooldown > 0 then return {'warp-system.cooldown',cooldown} end
if _warps()._n == 0 then return {'warp-system.none'} end
if Ranking.get_rank(player):allowed('always-warp') then return true
elseif player.surface.get_tile(player.position).name == warp_tile
or player.surface.get_tile(player.position).name == warp_partern
and player.surface.name == 'nauvis'
then return true
elseif player.position.x^2+player.position.y^2 < (warp_radius*spawn_warp_scale)^2 then return true
else return {'warp-system.not-on-warp'} end
end,
open_on_join=true
}
Event.register(defines.events.on_tick,function(event)
if not (event.tick % 60 == 0) then return end
for index,time in pairs(_warps().cooldown) do
if time > 0 then
_warps().cooldown[index] = time-1
if _warps().cooldown[index] == 0 then player_return({'warp-point.cooldown-zero'},defines.text_color.low,index) end
end
end
end)
Event.register(defines.events.on_player_created, function(event)
if event.player_index == 1 then
local player = Game.get_player(event)
player.force.chart(player.surface, {{player.position.x - 20, player.position.y - 20}, {player.position.x + 20, player.position.y + 20}})
local tag = player.force.add_chart_tag(player.surface,{
position={0,0},
text='Warp: Spawn',
icon={type='item',name=warp_item}
})
_warps().warps['Spawn'] = {tag=tag,surface=player.surface,position={0,0}}
end
end)