Started migration to TypeScript

This commit is contained in:
Pax1601
2023-01-08 10:05:10 +01:00
parent 036f70db34
commit ca6b66eb33
55 changed files with 1429 additions and 662 deletions

View File

@@ -1,39 +1,81 @@
Olympus = {}
Olympus.unitCounter = 1
Olympus.payloadRegistry = {}
function Olympus.notify(message, displayFor)
trigger.action.outText(message, displayFor)
end
function Olympus.move(unitName, lat, lng, altitude, speed, category, targetName)
Olympus.notify("Olympus.move " .. unitName .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. speed .. "m/s " .. category .. " target " .. targetName, 10)
local unit = Unit.getByName(unitName)
if unit ~= nil then
-- Gets a unit class reference from a given ObjectID (the ID used by Olympus for unit referencing)
function Olympus.getUnitByID(ID)
for name, table in pairs(mist.DBs.unitsByName) do
local unit = Unit.getByName(name)
if unit and unit:getObjectID() == ID then
return unit
end
end
return nil
end
function Olympus.getCountryIDByCoalition(coalition)
local countryID = 0
if coalition == 'red' then
countryID = country.id.RUSSIA
else
countryID = country.id.USA
end
return countryID
end
function Olympus.getCoalitionByCoalitionID(coalitionID)
local coalition = "neutral"
if coalitionID == 1 then
coalition = "red"
elseif coalitionID == 2 then
coalition = "blue"
end
return coalition
end
-- Builds a valid task depending on the provided options
function Olympus.buildTask(options)
local task = nil
-- Engage specific target by ID. Checks if target exists.
if options['id'] == 'EngageUnit' and options['targetID'] ~= nil then
local target = Olympus.getUnitByID(options['targetID'])
if target and target:isExist() then
task = {
id = 'EngageUnit',
params = {
unitId = options['targetID'],
}
}
end
end
return task
end
-- Move a unit. Since most tasks in DCS are Enroute tasks, this function is the main way to control the unit AI
function Olympus.move(ID, lat, lng, altitude, speed, category, taskOptions)
Olympus.notify("Olympus.move " .. ID .. " (" .. lat .. ", " .. lng ..") " .. altitude .. "m " .. speed .. "m/s " .. category, 2)
local unit = Olympus.getUnitByID(ID)
if unit then
if category == "Aircraft" then
local startPoint = mist.getLeadPos(unit:getGroup())
local endPoint = coord.LLtoLO(lat, lng, 0)
local task = nil
if targetName ~= "" then
targetID = Unit.getByName(targetName):getID()
task = {
id = 'EngageUnit',
params = {
unitId = targetID,
}
}
end
local path = {
[1] = mist.fixedWing.buildWP(startPoint, flyOverPoint, speed, altitude, 'BARO'),
[2] = mist.fixedWing.buildWP(endPoint, turningPoint, speed, altitude, 'BARO')
}
local path = {}
path[#path + 1] = mist.fixedWing.buildWP(startPoint, flyOverPoint, speed, altitude, 'BARO')
if task ~= nil then
path[#path].task = task
end
path[#path + 1] = mist.fixedWing.buildWP(endPoint, turningPoint, speed, altitude, 'BARO')
if task ~= nil then
path[#path].task = task
-- If a task exists assign it to the controller
local task = Olympus.buildTask(taskOptions)
if task then
path[1].task = task
path[2].task = task
end
-- Assign the mission task to the controller
local missionTask = {
id = 'Mission',
params = {
@@ -47,7 +89,7 @@ function Olympus.move(unitName, lat, lng, altitude, speed, category, targetName)
if groupCon then
groupCon:setTask(missionTask)
end
Olympus.notify("Olympus.move executed succesfully on a air unit", 10)
Olympus.notify("Olympus.move executed successfully on a Aircraft", 2)
elseif category == "GroundUnit" then
vars =
{
@@ -59,17 +101,18 @@ function Olympus.move(unitName, lat, lng, altitude, speed, category, targetName)
disableRoads = true
}
mist.groupToRandomPoint(vars)
Olympus.notify("Olympus.move executed succesfully on a ground unit", 10)
Olympus.notify("Olympus.move executed succesfully on a ground unit", 2)
else
Olympus.notify("Olympus.move not implemented yet for " .. category, 10)
Olympus.notify("Olympus.move not implemented yet for " .. category, 2)
end
else
Olympus.notify("Error in Olympus.move " .. unitName, 10)
Olympus.notify("Error in Olympus.move " .. unitName, 2)
end
end
-- Creates a simple smoke on the ground
function Olympus.smoke(color, lat, lng)
Olympus.notify("Olympus.smoke " .. color .. " (" .. lat .. ", " .. lng ..")", 10)
Olympus.notify("Olympus.smoke " .. color .. " (" .. lat .. ", " .. lng ..")", 2)
local colorEnum = nil
if color == "green" then
colorEnum = trigger.smokeColor.Green
@@ -85,14 +128,15 @@ function Olympus.smoke(color, lat, lng)
trigger.action.smoke(mist.utils.makeVec3GL(coord.LLtoLO(lat, lng, 0)), colorEnum)
end
function Olympus.spawnGround(coalition, type, lat, lng, ID)
Olympus.notify("Olympus.spawnGround " .. coalition .. " " .. type .. " (" .. lat .. ", " .. lng ..")", 10)
-- Spawns a single ground unit
function Olympus.spawnGroundUnit(coalition, unitType, lat, lng)
Olympus.notify("Olympus.spawnGroundUnit " .. coalition .. " " .. unitType .. " (" .. lat .. ", " .. lng ..")", 2)
local spawnLocation = mist.utils.makeVec3GL(coord.LLtoLO(lat, lng, 0))
local unitTable =
{
[1] =
{
["type"] = type,
["type"] = unitType,
["x"] = spawnLocation.x,
["y"] = spawnLocation.z,
["playerCanDrive"] = true,
@@ -100,13 +144,8 @@ function Olympus.spawnGround(coalition, type, lat, lng, ID)
},
}
local countryID = nil
if coalition == 'red' then
countryID = country.id.RUSSIA
else
countryID = country.id.USA
end
local countryID = Olympus.getCountryIDByCoalition(coalition)
local vars =
{
units = unitTable,
@@ -116,17 +155,31 @@ function Olympus.spawnGround(coalition, type, lat, lng, ID)
}
mist.dynAdd(vars)
Olympus.unitCounter = Olympus.unitCounter + 1
Olympus.notify("Olympus.spawnGround completed succesfully", 10)
Olympus.notify("Olympus.spawnGround completed succesfully", 2)
end
function Olympus.spawnAir(coalition, unitType, lat, lng, payloadName)
local alt = 5000
Olympus.notify("Olympus.spawnAir " .. coalition .. " " .. unitType .. " (" .. lat .. ", " .. lng ..") " .. payloadName, 10)
-- Spawns a single aircraft. Spawn options are:
-- payloadName: a string, one of the names defined in unitPayloads.lua. Must be compatible with the unitType
-- airbaseName: a string, if present the aircraft will spawn on the ground of the selected airbase
-- payload: a table, if present the unit will receive this specific payload. Overrides payloadName
function Olympus.spawnAircraft(coalition, unitType, lat, lng, spawnOptions)
local payloadName = spawnOptions["payloadName"]
local airbaseName = spawnOptions["airbaseName"]
local payload = spawnOptions["payload"]
Olympus.notify("Olympus.spawnAircraft " .. coalition .. " " .. unitType .. " (" .. lat .. ", " .. lng ..")", 2)
local spawnLocation = mist.utils.makeVec3GL(coord.LLtoLO(lat, lng, 0))
local payload = {}
if Olympus.unitPayloads[unitType][payloadName] ~= nil then
payload = Olympus.unitPayloads[unitType][payloadName]
if payload == nil then
if payloadName and payloadName ~= "" and Olympus.unitPayloads[unitType][payloadName] ~= nil then
payload = Olympus.unitPayloads[unitType][payloadName]
else
payload = {}
end
end
local countryID = Olympus.getCountryIDByCoalition(coalition)
local unitTable =
{
[1] =
@@ -134,12 +187,10 @@ function Olympus.spawnAir(coalition, unitType, lat, lng, payloadName)
["type"] = unitType,
["x"] = spawnLocation.x,
["y"] = spawnLocation.z,
["alt"] = alt,
["skill"] = "Excellent",
["payload"] =
{
["pylons"] = payload,
["fuel"] = 4900,
["flare"] = 60,
["ammo_type"] = 1,
["chaff"] = 60,
@@ -151,31 +202,97 @@ function Olympus.spawnAir(coalition, unitType, lat, lng, payloadName)
[1] = 1,
[2] = 1,
[3] = 1,
["name"] = "Enfield11",
["name"] = "Olympus" .. Olympus.unitCounter,
},
["name"] = "Olympus-" .. Olympus.unitCounter
},
}
local countryID = nil
if coalition == 'red' then
countryID = country.id.RUSSIA
else
countryID = country.id.USA
-- If a airbase is provided the first waypoint is set as a From runway takeoff.
local route = {}
if airbaseName and airbaseName ~= "" then
local airbase = Airbase.getByName(airbaseName)
if airbase then
local airbaseID = airbase:getID()
route =
{
["points"] =
{
[1] =
{
["action"] = "From Runway",
["task"] =
{
["id"] = "ComboTask",
["params"] = {["tasks"] = {},},
},
["type"] = "TakeOff",
["ETA"] = 0,
["ETA_locked"] = true,
["x"] = spawnLocation.x,
["y"] = spawnLocation.z,
["formation_template"] = "",
["airdromeId"] = airbaseID,
["speed_locked"] = true,
},
},
}
end
end
local vars =
{
units = unitTable,
country = countryID,
category = 'airplane',
task = "CAP",
tasks = {},
name = "Olympus-" .. Olympus.unitCounter,
route = route,
task = 'CAP',
}
mist.dynAdd(vars)
-- Save the payload to be reused in case the unit is cloned. TODO: save by ID not by name (it works but I like consistency)
Olympus.payloadRegistry[vars.name] = payload
Olympus.unitCounter = Olympus.unitCounter + 1
Olympus.notify("Olympus.spawnAir completed succesfully", 10)
Olympus.notify("Olympus.spawnAir completed successfully", 2)
end
Olympus.notify("OlympusCommand script loaded correctly", 10)
-- Clones a unit by ID. Will clone the unit with the same original payload as the source unit. TODO: only works on Olympus unit not ME units.
function Olympus.clone(ID)
Olympus.notify("Olympus.clone " .. ID, 2)
local unit = Olympus.getUnitByID(ID)
if unit then
local coalition = Olympus.getCoalitionByCoalitionID(unit:getCoalition())
local lat, lng, alt = coord.LOtoLL(unit:getPoint())
-- TODO: only works on Aircraft
local spawnOptions = {
payload = Olympus.payloadRegistry[unitName]
}
Olympus.spawnAircraft(coalition, unit:getTypeName(), lat + 0.001, lng + 0.001, spawnOptions)
end
Olympus.notify("Olympus.clone completed successfully", 2)
end
function Olympus.follow(leaderID, ID)
Olympus.notify("Olympus.follow " .. ID .. " " .. leaderID, 2)
local leader = Olympus.getUnitByID(leaderID)
local unit = Olympus.getUnitByID(ID)
local followTask = {
id = 'Follow',
params = {
groupId = leader:getGroup():getID(),
pos = {x = 0 , y = 0, z = 20} ,
lastWptIndexFlag = false,
lastWptIndex = 1
}
}
Olympus.notify("Olympus.follow group ID" .. unit:getGroup():getID(), 2)
unit:getGroup():getController():pushTask(followTask)
Olympus.notify("Olympus.follow completed successfully", 2)
end
Olympus.notify("OlympusCommand script loaded successfully", 2)

View File

@@ -11,8 +11,6 @@ function Olympus.setMissionData(arg, time)
local bullseyeVec3 = coalition.getMainRefPoint(0)
local bullseyeLatitude, bullseyeLongitude, bullseyeAltitude = coord.LOtoLL(bullseyeVec3)
local bullseye = {}
bullseye["x"] = bullseyeVec3.x
bullseye["y"] = bullseyeVec3.z
bullseye["lat"] = bullseyeLatitude
bullseye["lng"] = bullseyeLongitude
@@ -41,9 +39,26 @@ function Olympus.setMissionData(arg, time)
end
end
-- Airbases data
local base = world.getAirbases()
local basesData = {}
for i = 1, #base do
local info = {}
local latitude, longitude, altitude = coord.LOtoLL(Airbase.getPoint(base[i]))
info["callsign"] = Airbase.getCallsign(base[i])
info["coalition"] = Airbase.getCoalition(base[i])
info["lat"] = latitude
info["lng"] = longitude
if Airbase.getUnit(base[i]) then
info["unitId"] = Airbase.getUnit(base[i]):getID()
end
basesData[i] = info
end
-- Assemble missionData table
missionData["bullseye"] = bullseye
missionData["unitsData"] = unitsData
missionData["airbases"] = basesData
local command = "Olympus.missionData = " .. Olympus.serializeTable(missionData) .. "\n" .. "Olympus.OlympusDLL.setMissionData()"
net.dostring_in("export", command)
@@ -55,8 +70,6 @@ function Olympus.serializeTable(val, name, skipnewlines, depth)
depth = depth or 0
local tmp = string.rep(" ", depth)
if name then
if type(name) == "number" then
tmp = tmp .. "[" .. name .. "]" .. " = "
@@ -67,11 +80,9 @@ function Olympus.serializeTable(val, name, skipnewlines, depth)
if type(val) == "table" then
tmp = tmp .. "{" .. (not skipnewlines and "\n" or "")
for k, v in pairs(val) do
tmp = tmp .. Olympus.serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "")
end
tmp = tmp .. string.rep(" ", depth) .. "}"
elseif type(val) == "number" then
tmp = tmp .. tostring(val)

8
scripts/server.py Normal file
View File

@@ -0,0 +1,8 @@
try:
# Python 2
from SimpleHTTPServer import test, SimpleHTTPRequestHandler
except ImportError:
# Python 3
from http.server import test, SimpleHTTPRequestHandler
test(SimpleHTTPRequestHandler)

44
scripts/server.spec Normal file
View File

@@ -0,0 +1,44 @@
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['server.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='server',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True,
disable_windowed_traceback=False,
argv_emulation=False,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
)