For Spring 0.83+, I have expanded the API to allow full two-way*** interaction between native (C{++}, Java, Python, ...) AI's and Lua gadgets and widgets. The tools at your disposal are:
Code: Select all
-- Lua callins (one new, one old, UNSYNCED LUA ONLY)
-- (RecvSkirmishAIMessage was formerly known as AICallIn)
gadget:RecvSkirmishAIMessage(number aiTeam, string aiMessage) --> string
widget:RecvSkirmishAIMessage(number aiTeam, string aiMessage) --> string
-- Lua callouts (new, UNSYNCED LUA ONLY)
Spring.SendSkirmishAIMessage(number aiTeam, string aiMessage) -->
boolean, {[1] = string, [2] = string, ...}
// legacy C++ callins (new)
void IGlobalAI::RecvLuaMessage(const char* inData, const char** outData);
// legacy C++ callouts (one new, one old)
const char* IAICallback::CallLuaRules(const char* inData, int inSize, int* outSize);
const char* IAICallback::CallLuaUI(const char* inData, int inSize, int* outSize);
Code: Select all
-- some LuaUI widget
function widget:RecvSkirmishAIMessage(aiTeam, aiMessage)
func = loadstring(aiMessage)
if (func ~= nil) then
func()
end
return ""
end
-- some LuaRules gadget
if (gadgetHandler:IsSyncedCode()) then
function gadget:Explosion(weaponDefID, px, py, pz, ownerID)
SendToUnsynced("Explosion", px, py, pz)
end
else
local function HandleExplosion(...)
local x = arg[3]
local y = arg[4]
local z = arg[5]
local aiTeam = 1
local aiMessage = "t = {" ..
"msgID=\"Explosion\"" .. ", " ..
"x=" .. x .. ", " ..
"y=" .. y .. ", " ..
"z=" .. z .. ", " ..
"}"
Spring.SendSkirmishAIMessage(aiTeam, aiMessage)
end
local eventMap = {Explosion = HandleExplosion}
function gadget:RecvFromSynced(...)
local eventName = arg[2]
if (eventMap[eventName] ~= nil) then
eventMap[eventName](...)
end
end
end
// AI code
void AI::UnitDestroyed(int unitID, int attackerID) {
static char buffer[1024];
const float3& unitPos = callback->GetUnitPosition(unitID);
const char* luaString = "Spring.MarkerAddPoint(%f, %f, %f, \"AI: unit %d was here\")";
snprintf(buffer, 1024, luaString, pos.x, pos.y, pos.z, unitID);
callback->CallLuaUI(buffer, -1, NULL);
}
void AI::RecvLuaMessage(const char* inData, const char** outData) {
const LuaTable& table = LuaParser::Parse(inData);
if (table.GetValue<std::string>("msgID") == "Explosion") {
const float x = table.GetValue<float>("x");
const float y = table.GetValue<float>("y");
const float z = table.GetValue<float>("z");
intelHandler->Explosion(x, y, z);
}
}
*** WARNING: http://springrts.com/phpbb/viewtopic.ph ... 92#p546792