Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions luarules/gadgets/api_missions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ end

local sounds = VFS.Include('luarules/mission_api/sounds.lua')

local scriptPath
local triggersController, actionsController

local function loadMission()
local function loadMission(scriptPath)
local mission = VFS.Include("singleplayer/" .. scriptPath)
local rawTriggers = mission.Triggers
local rawActions = mission.Actions

GG['MissionAPI'].Triggers = triggersController.ProcessRawTriggers(rawTriggers, rawActions)
GG['MissionAPI'].Actions = actionsController.ProcessRawActions(rawActions)
GG['MissionAPI'].UnitLoadout = mission.UnitLoadout
GG['MissionAPI'].FeatureLoadout = mission.FeatureLoadout

local validateReferences = VFS.Include('luarules/mission_api/validation.lua').ValidateReferences
validateReferences()
Expand All @@ -38,13 +39,14 @@ end

function gadget:Initialize()
-- TODO: Actually pass script path
--scriptPath = 'mission-api-tests/validation_test.lua'
--scriptPath = 'mission-api-tests/test_mission.lua'
--scriptPath = 'mission-api-tests/markers_test.lua'
--scriptPath = 'mission-api-tests/sound_test.lua'
--scriptPath = 'mission-api-tests/issue_orders_test.lua'
--scriptPath = 'mission-api-tests/unit_triggers_test.lua'
scriptPath = 'mission-api-tests/feature_triggers_test.lua'
--local scriptPath = 'mission-api-tests/validation_test.lua'
--local scriptPath = 'mission-api-tests/test_mission.lua'
--local scriptPath = 'mission-api-tests/markers_test.lua'
--local scriptPath = 'mission-api-tests/sound_test.lua'
--local scriptPath = 'mission-api-tests/issue_orders_test.lua'
--local scriptPath = 'mission-api-tests/unit_triggers_test.lua'
--local scriptPath = 'mission-api-tests/feature_triggers_test.lua'
local scriptPath = 'mission-api-tests/loadout_test.lua'

if not scriptPath then
gadgetHandler:RemoveGadget()
Expand All @@ -68,7 +70,26 @@ function gadget:Initialize()
triggersController = VFS.Include('luarules/mission_api/triggers_loader.lua')
actionsController = VFS.Include('luarules/mission_api/actions_loader.lua')

loadMission();
loadMission(scriptPath)
end

function gadget:GamePreload()
if Spring.GetGameRulesParam("loadedGame") == 1 then
Spring.Echo("Mission API: Loading saved game, skipping loadout")
return
end

if GG['MissionAPI'].UnitLoadout then
Spring.Echo("Mission API: Creating unit loadout")
end
if GG['MissionAPI'].FeatureLoadout then
Spring.Echo("Mission API: Creating feature loadout")
end

local tracking = VFS.Include('luarules/mission_api/tracking.lua')
local loadoutModule = VFS.Include('luarules/mission_api/loadout.lua')
loadoutModule.SpawnUnitLoadout(GG['MissionAPI'].UnitLoadout, tracking.TrackUnit)
loadoutModule.SpawnFeatureLoadout(GG['MissionAPI'].FeatureLoadout, tracking.TrackFeature)
end

function gadget:GameFrame(frameNumber)
Expand Down
16 changes: 8 additions & 8 deletions luarules/mission_api/actions.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local loadout = VFS.Include('luarules/mission_api/loadout.lua')
local sounds = VFS.Include('luarules/mission_api/sounds.lua')
local tracking = VFS.Include('luarules/mission_api/tracking.lua')
local trackUnit = tracking.TrackUnit
Expand Down Expand Up @@ -191,14 +192,7 @@ for _, unitDef in pairs(UnitDefs) do
end

local function createFeature(featureDefName, position, featureName, facing)
-- Convert named facing to a heading integer (engine uses 0-65535 headings)
local facingToHeading = { s = 0, n = 32768, e = 16384, w = 49152,
south = 0, north = 32768, east = 16384, west = 49152,
[0] = 0, [1] = 32768, [2] = 16384, [3] = 49152 }
local heading = facing and (facingToHeading[facing] or 0) or 0

local gaiaTeamID = Spring.GetGaiaTeamID()
local featureID = Spring.CreateFeature(featureDefName, position.x, position.y, position.z, heading, gaiaTeamID)
local featureID = loadout.CreateFeature(featureDefName, position, facing)
local unitDefName = corpseToUnitDefName[featureDefName]
if unitDefName then
Spring.SetFeatureResurrect(featureID, unitDefName, facing)
Expand All @@ -219,6 +213,11 @@ local function destroyFeature(featureName)
end
end

local function spawnLoadout(unitLoadout, featureLoadout)
loadout.SpawnUnitLoadout(unitLoadout, trackUnit)
loadout.SpawnFeatureLoadout(featureLoadout, trackFeature)
end

local function spawnExplosion(weaponDefName, position, direction)
direction = direction or { x = 0, y = 0, z = 0 }
local weaponDef = WeaponDefNames[weaponDefName]
Expand Down Expand Up @@ -316,6 +315,7 @@ return {
TransferUnits = transferUnits,
NameUnits = nameUnits,
UnnameUnits = unnameUnits,
SpawnLoadout = spawnLoadout,

-- Features
CreateFeature = createFeature,
Expand Down
41 changes: 6 additions & 35 deletions luarules/mission_api/actions_dispatcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,12 @@ local parameterSchema = actionsSchema.Parameters
local types = GG['MissionAPI'].ActionTypes
local actions = GG['MissionAPI'].Actions

local typeMapping = {
-- TODO: Since the names are all the same, could we ditch this mapping and do it dynamically instead?
[types.EnableTrigger] = actionFunctions.EnableTrigger,
[types.DisableTrigger] = actionFunctions.DisableTrigger,
[types.IssueOrders] = actionFunctions.IssueOrders,
-- [types.AllowCommands] = ,
-- [types.RestrictCommands] = ,
-- [types.AlterBuildlist] = ,
-- [types.EnableBuildOption] = ,
-- [types.DisableBuildOption] = ,
[types.SpawnUnits] = actionFunctions.SpawnUnits,
[types.DespawnUnits] = actionFunctions.DespawnUnits,
[types.TransferUnits] = actionFunctions.TransferUnits,
[types.NameUnits] = actionFunctions.NameUnits,
[types.UnnameUnits] = actionFunctions.UnnameUnits,
[types.CreateFeature] = actionFunctions.CreateFeature,
[types.DestroyFeature] = actionFunctions.DestroyFeature,
[types.SpawnExplosion] = actionFunctions.SpawnExplosion,
-- [types.SpawnWeapons] = ,
-- [types.SpawnEffects] = ,
-- [types.RevealLOS] = ,
-- [types.UnrevealLOS] = ,
-- [types.AlterMapZones] = ,
-- [types.ControlCamera] = ,
-- [types.Pause] = ,
-- [types.Unpause] = ,
[types.PlaySound] = actionFunctions.PlaySound,
[types.SendMessage] = actionFunctions.SendMessage,
[types.AddMarker] = actionFunctions.AddMarker,
[types.EraseMarker] = actionFunctions.EraseMarker,
[types.DrawLines] = actionFunctions.DrawLines,
[types.ClearAllMarkers] = actionFunctions.ClearAllMarkers,
[types.Victory] = actionFunctions.Victory,
[types.Defeat] = actionFunctions.Defeat,
}
local typeMapping = {}
for name, typeId in pairs(types) do
if actionFunctions[name] then
typeMapping[typeId] = actionFunctions[name]
end
end

-- unpack() does not handle optional parameters, as it cannot pass a value as nil
local function unpackActionParameters(actionId, i)
Expand Down
15 changes: 15 additions & 0 deletions luarules/mission_api/actions_schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local actionTypes = {
TransferUnits = 404,
NameUnits = 405,
UnnameUnits = 406,
SpawnLoadout = 407,

-- Features
CreateFeature = 450,
Expand Down Expand Up @@ -234,6 +235,20 @@ local parameters = {
},
},

[actionTypes.SpawnLoadout] = {
[1] = {
name = 'unitLoadout',
required = false,
type = Types.UnitLoadout,
},
[2] = {
name = 'featureLoadout',
required = false,
type = Types.FeatureLoadout,
},
requiresOneOf = { 'unitLoadout', 'featureLoadout' },
},

-- SFX
[actionTypes.SpawnExplosion] = {
[1] = {
Expand Down
69 changes: 69 additions & 0 deletions luarules/mission_api/loadout.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
--- Loadout spawning logic used in :GamePreload() and the SpawnLoadout action.
---

-- Convert named facing to a heading integer (engine uses 0-65535 headings)
local facingToHeading = {
s = 0, n = 32768, e = 16384, w = 49152,
south = 0, north = 32768, east = 16384, west = 49152,
[0] = 0, [1] = 32768, [2] = 16384, [3] = 49152,
}

local gaiaTeamID = Spring.GetGaiaTeamID()

local function spawnUnitLoadout(unitLoadout, trackUnit)
for _, unit in ipairs(unitLoadout or {}) do
local y = Spring.GetGroundHeight(unit.x, unit.z)
local unitID = Spring.CreateUnit(unit.name, unit.x, y, unit.z, unit.facing or 's', unit.team)
if unitID then
if unit.neutral == true or unit.neutral == 'true' then
Spring.SetUnitNeutral(unitID, true)
end
if not table.isNilOrEmpty(unit.orders) then
for _, order in ipairs(unit.orders) do
local cmdID = order[1]
local params = order[2] or {}
local opts = order[3] or {}
-- TODO: update when orders can be on named units
Spring.GiveOrderToUnit(unitID, cmdID, params, opts)
end
else
Spring.GiveOrderToUnit(unitID, CMD.STOP, {}, 0)
end
if unit.unitName and trackUnit then
trackUnit(unit.unitName, unitID)
end
end
end
end

local function createFeature(featureDefName, position, facing)
local heading = facingToHeading[facing or 0]
local featureID = Spring.CreateFeature(featureDefName, position.x, position.y, position.z, heading, gaiaTeamID)

if featureDefName:sub(-5) == '_dead' then
local unitDefName = featureDefName:sub(1, -6)
if UnitDefNames[unitDefName] then
Spring.SetFeatureResurrect(featureID, unitDefName, facing)
end
end

return featureID
end

local function spawnFeatureLoadout(featureLoadout, trackFeature)
for _, feature in ipairs(featureLoadout or {}) do
feature.y = Spring.GetGroundHeight(feature.x, feature.z)
local featureID = createFeature(feature.name, feature, feature.facing)

if featureID and feature.featureName and trackFeature then
trackFeature(feature.featureName, featureID)
end
end
end

return {
SpawnUnitLoadout = spawnUnitLoadout,
SpawnFeatureLoadout = spawnFeatureLoadout,
CreateFeature = createFeature,
}
2 changes: 2 additions & 0 deletions luarules/mission_api/parameter_types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local types = {
AllyTeamIDs = 'AllyTeamIDs',
Orders = 'Orders',
Area = 'Area',
UnitLoadout = 'UnitLoadout',
FeatureLoadout = 'FeatureLoadout',

-- String Validators:
String = 'String',
Expand Down
Loading
Loading