Skip to content

Commit

Permalink
Add lua rendering to debug window
Browse files Browse the repository at this point in the history
Refactor DebugShipSpawn to fill the ctrl+F12 spawner tool, merge with PerfInfo window
Add extension points to the PerfInfo window for lua to render in
Expose a few more ImGui functions to lua
Remove unnecessary INIT->init redirection function in pigui handlers
  • Loading branch information
sturnclaw committed Aug 2, 2020
1 parent 7be4098 commit 61f4981
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 89 deletions.
144 changes: 111 additions & 33 deletions data/modules/DebugShipSpawn/DebugShipSpawn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ local Game = require('Game')
local Space = require('Space')
local Ship = require('Ship')
local ShipDef = require('ShipDef')
local Timer = require('Timer')
local Equipment = require('Equipment')
local Vector2, Color
do
local _obj_0 = _G
Vector2, Color = _obj_0.Vector2, _obj_0.Color
end
local ui = require('pigui.pigui')
local Vector2
Vector2 = _G.Vector2
local ui = require('pigui')
local debug_ui = require('pigui.views.debug')
local ship_defs = { }
local update_ship_def_table
update_ship_def_table = function()
Expand Down Expand Up @@ -45,14 +44,26 @@ draw_ship_info = function(self)
ui.text("Ship Class:")
ui.nextColumn()
ui.text(self.shipClass)
return ui.nextColumn()
ui.nextColumn()
return ui.columns(1, '')
end
local ai_opt_selected = 1
local ai_options = {
"FlyTo",
"Kamikaze",
"Kill"
}
local missile_selected = 0
local missile_names = {
"Guided Missile",
"Unguided Missile",
"Smart Missile"
}
local missile_types = {
"missile_guided",
"missile_unguided",
"missile_smart"
}
local draw_ai_info
draw_ai_info = function()
for i, opt in ipairs(ai_options) do
Expand All @@ -61,7 +72,55 @@ draw_ai_info = function()
end
end
end
local spawn_distance = 10
local spawn_distance = 5.0
local spawn_ship_free
spawn_ship_free = function(ship_name, ai_option, equipment)
local new_ship
do
local _with_0 = Space.SpawnShipNear(ship_name, Game.player, spawn_distance, spawn_distance)
_with_0:SetLabel(Ship.MakeRandomLabel())
for _index_0 = 1, #equipment do
local equip = equipment[_index_0]
_with_0:AddEquip(equip)
end
_with_0:UpdateEquipStats()
new_ship = _with_0
end
return new_ship["AI" .. tostring(ai_option)](new_ship, Game.player)
end
local spawn_ship_docked
spawn_ship_docked = function(ship_name, ai_option, equipment)
local new_ship
do
local _with_0 = Space.SpawnShipDocked(ship_name, Game.player:GetNavTarget())
_with_0:SetLabel(Ship.MakeRandomLabel())
for _index_0 = 1, #equipment do
local equip = equipment[_index_0]
_with_0:AddEquip(equip)
end
_with_0:UpdateEquipStats()
new_ship = _with_0
end
return new_ship["AI" .. tostring(ai_option)](new_ship, Game.player)
end
local do_spawn_missile
do_spawn_missile = function(type)
if Game.player:IsDocked() then
return nil
end
local new_missile
do
local _with_0 = Game.player:SpawnMissile(type)
_with_0:AIKamikaze(Game.player:GetCombatTarget())
new_missile = _with_0
end
return Timer:CallEvery(2, function()
if new_missile:exists() then
new_missile:Arm()
end
return true
end)
end
local ship_spawn_debug_window
ship_spawn_debug_window = function()
ui.child('ship_list', Vector2(150, 0), draw_ship_types)
Expand All @@ -70,40 +129,59 @@ ship_spawn_debug_window = function()
if ship_name then
ship = ShipDef[ship_name]
end
local ship_equip = {
Equipment.laser.pulsecannon_dual_1mw,
Equipment.misc.laser_cooling_booster,
Equipment.misc.atmospheric_shielding
}
ui.sameLine()
if ship then
return ui.group(function()
ui.child('ship_info', Vector2(-150, -ui.getFrameHeightWithSpacing()), function()
return draw_ship_info(ship)
local spawner_group_height = ui.getFrameHeight() * 2 + ui.getFrameHeightWithSpacing()
ui.child('ship_info', Vector2(0, -spawner_group_height), function()
draw_ship_info(ship)
ui.spacing()
ui.separator()
ui.spacing()
return draw_ai_info()
end)
ui.sameLine()
ui.child('ai_info', Vector2(150, -ui.getFrameHeightWithSpacing()), draw_ai_info)
if ui.button("Spawn", Vector2(0, 0)) then
local new_ship = Space.SpawnShipNear(ship_name, Game.player, spawn_distance, spawn_distance)
new_ship:AddEquip(Equipment.laser.pulsecannon_dual_1mw)
new_ship:AddEquip(Equipment.misc.laser_cooling_booster)
new_ship:AddEquip(Equipment.misc.atmospheric_shielding)
new_ship:SetLabel(Ship.MakeRandomLabel())
local ai_method_name = "AI" .. tostring(ai_options[ai_opt_selected])
new_ship[ai_method_name](new_ship, Game.player)
if ui.button("Spawn Ship", Vector2(0, 0)) then
spawn_ship_free(ship_name, ai_options[ai_opt_selected], ship_equip)
end
local nav_target = Game.player:GetNavTarget()
if nav_target and nav_target:isa("SpaceStation") then
ui.sameLine()
if ui.button("Spawn Docked", Vector2(0, 0)) then
spawn_ship_docked(ship_name, ai_options[ai_opt_selected], ship_equip)
end
end
ui.sameLine()
if Game.player:GetCombatTarget() then
ui.sameLine()
if ui.button("Spawn Missile", Vector2(0, 0)) then
do_spawn_missile(missile_types[missile_selected + 1])
end
end
ui.nextItemWidth(-1.0)
local _
_, missile_selected = ui.combo("##missile_type", missile_selected, missile_names)
ui.text("Spawn Distance:")
ui.sameLine()
spawn_distance = ui.sliderFloat("#spawn_distance", spawn_distance, 0.5, 50, "%.1fkm")
ui.nextItemWidth(-1.0)
spawn_distance = ui.sliderFloat("##spawn_distance", spawn_distance, 0.5, 20, "%.1fkm")
end)
end
end
local displayDebugWindow = false
return ui.registerModule('game', function()
if ui.isKeyReleased(ui.keys.f11) and ui.ctrlHeld() then
displayDebugWindow = not displayDebugWindow
return debug_ui.registerTab("Ship Spawner", function()
if not (Game.player and Game.CurrentView() == "world") then
return nil
end
if displayDebugWindow and Game.CurrentView() == "world" then
return ui.withStyleColors({
["WindowBg"] = Color(15, 15, 16, 240)
}, function()
return ui.window("Ship Spawn Debug", { }, ship_spawn_debug_window)
end)
if ui.beginTabItem("Ship Spawner") then
ship_spawn_debug_window()
ui.endTabItem()
if ui.isKeyReleased(string.byte('r')) and ui.ctrlHeld() then
package.reimport('.DebugShipSpawn')
end
end
if ui.isKeyReleased(ui.keys.f12) and ui.ctrlHeld() then
return nil
end
end)
140 changes: 106 additions & 34 deletions data/modules/DebugShipSpawn/DebugShipSpawn.moon
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ Game = require 'Game'
Space = require 'Space'
Ship = require 'Ship'
ShipDef = require 'ShipDef'
Timer = require 'Timer'
Equipment = require 'Equipment'

import Vector2, Color from _G
ui = require 'pigui.pigui'
import Vector2 from _G
ui = require 'pigui'
debug_ui = require 'pigui.views.debug'

ship_defs = {}

Expand Down Expand Up @@ -42,53 +44,123 @@ draw_ship_info = =>
ui.text "Ship Class:"
ui.nextColumn!
ui.text @shipClass
ui.nextColumn!
ui.nextColumn!

ui.columns 1, ''

ai_opt_selected = 1
ai_options = {
"FlyTo", "Kamikaze", "Kill"
}

-- ui.combo is zero-based
missile_selected = 0
missile_names = {
"Guided Missile", "Unguided Missile", "Smart Missile"
}
missile_types = {
"missile_guided",
"missile_unguided",
"missile_smart",
}

draw_ai_info = ->
for i, opt in ipairs ai_options
if ui.selectable opt, ai_opt_selected == i
ai_opt_selected = i

spawn_distance = 10
spawn_distance = 5.0 -- km

spawn_ship_free = (ship_name, ai_option, equipment) ->
new_ship = with Space.SpawnShipNear ship_name, Game.player, spawn_distance, spawn_distance
\SetLabel Ship.MakeRandomLabel!
\AddEquip equip for equip in *equipment
\UpdateEquipStats!

-- Invoke the specified AI method on the new ship.
new_ship["AI#{ai_option}"] new_ship, Game.player

spawn_ship_docked = (ship_name, ai_option, equipment) ->
new_ship = with Space.SpawnShipDocked ship_name, Game.player\GetNavTarget!
\SetLabel Ship.MakeRandomLabel!
\AddEquip equip for equip in *equipment
\UpdateEquipStats!

-- Invoke the specified AI method on the new ship.
new_ship["AI#{ai_option}"] new_ship, Game.player

-- Spawn a missile attacking the player's current combat target
do_spawn_missile = (type) ->
if Game.player\IsDocked!
return nil

new_missile = with Game.player\SpawnMissile type
\AIKamikaze Game.player\GetCombatTarget!

Timer\CallEvery 2, ->
if new_missile\exists!
new_missile\Arm!

return true

ship_spawn_debug_window = ->
ui.child 'ship_list', Vector2(150, 0), draw_ship_types

ship_name = ship_defs[selected_ship_type]
ship = ShipDef[ship_name] if ship_name

ship_equip = {
Equipment.laser.pulsecannon_dual_1mw
Equipment.misc.laser_cooling_booster
Equipment.misc.atmospheric_shielding
}

ui.sameLine!
if ship then ui.group ->
ui.child 'ship_info', Vector2(-150, -ui.getFrameHeightWithSpacing!), ->
draw_ship_info ship

ui.sameLine!
ui.child 'ai_info', Vector2(150, -ui.getFrameHeightWithSpacing!), draw_ai_info

if ui.button "Spawn", Vector2(0, 0)
new_ship = Space.SpawnShipNear(ship_name, Game.player, spawn_distance, spawn_distance)
new_ship\AddEquip Equipment.laser.pulsecannon_dual_1mw
new_ship\AddEquip Equipment.misc.laser_cooling_booster
new_ship\AddEquip Equipment.misc.atmospheric_shielding
new_ship\SetLabel Ship.MakeRandomLabel !
ai_method_name = "AI#{ai_options[ai_opt_selected]}"
new_ship[ai_method_name] new_ship, Game.player

ui.sameLine!

ui.text "Spawn Distance:"
ui.sameLine!
spawn_distance = ui.sliderFloat("#spawn_distance", spawn_distance, 0.5, 50, "%.1fkm")

displayDebugWindow = false
ui.registerModule 'game', ->
if ui.isKeyReleased(ui.keys.f11) and ui.ctrlHeld!
displayDebugWindow = not displayDebugWindow

if displayDebugWindow and Game.CurrentView() == "world"
ui.withStyleColors { "WindowBg": Color(15, 15, 16, 240) }, ->
ui.window "Ship Spawn Debug", {}, ship_spawn_debug_window
if ship then ui.group ->
spawner_group_height = ui.getFrameHeight! * 2 + ui.getFrameHeightWithSpacing!
ui.child 'ship_info', Vector2(0, -spawner_group_height), ->
draw_ship_info ship

ui.spacing!
ui.separator!
ui.spacing!

draw_ai_info!

if ui.button "Spawn Ship", Vector2(0, 0)
spawn_ship_free ship_name, ai_options[ai_opt_selected], ship_equip

nav_target = Game.player\GetNavTarget!
if nav_target and nav_target\isa "SpaceStation"
ui.sameLine!
if ui.button "Spawn Docked", Vector2(0, 0)
spawn_ship_docked ship_name, ai_options[ai_opt_selected], ship_equip

if Game.player\GetCombatTarget!
ui.sameLine!
if ui.button "Spawn Missile", Vector2(0, 0)
do_spawn_missile missile_types[missile_selected + 1]

ui.nextItemWidth -1.0
_, missile_selected = ui.combo "##missile_type", missile_selected, missile_names

ui.text "Spawn Distance:"
ui.nextItemWidth -1.0
spawn_distance = ui.sliderFloat("##spawn_distance", spawn_distance, 0.5, 20, "%.1fkm")

debug_ui.registerTab "Ship Spawner", ->
unless Game.player and Game.CurrentView() == "world" -- when not in a game, Game.player will return nil
return nil

if ui.beginTabItem "Ship Spawner"
ship_spawn_debug_window!

ui.endTabItem!

if ui.isKeyReleased(string.byte 'r') and ui.ctrlHeld!
package.reimport '.DebugShipSpawn'

if ui.isKeyReleased(ui.keys.f12) and ui.ctrlHeld!
-- TODO: port Pi.cpp ship spawning behavior here
-- do_spawn_ship()
nil
Loading

0 comments on commit 61f4981

Please sign in to comment.