Jump to content

Accessability of methods/functions of "Engine"


Fluxkompensator
 Share

Recommended Posts

Hello,

I wrote a new component in c++ and now I want to access to this methods in _petrabot.js:

Engine.QueryInterface(SYSTEM_ENTITY, IID_MyComponent).

In binaries\data\mods\public\simulation\helpers\InitGame.js it works fine.

In _petrabot.js you can access to this method:

Engine.IncludeModule("common-api")

but not to this one:

Engine.QueryInterface(SYSTEM_ENTITY, IID_MyComponent):

" QueryInterface is not a function"

(btw.: SYSTEM_ENTITY and IID_MyComponent are also not defined in _petrabot.js, I have replaced these with the corresponding integer value)

It seems to be Engine is defined, but not QueryInterface-function, SYSTEM_ENTITY and IID_MyComponent!?

What I am doing wrong?

Is there a document where I can see, which functions of "Engine" are accessible in which javascript-files or a general documentation of "Engine"?

Maybe I just need a Link.

Thank you for your time and attention

Link to comment
Share on other sites

I stumbled on the same question before. In short, not each part of the JS scripts can access each C++ method, they have to be reflected into the JS runtime beforehand.

In detail: See /source/simulation2/components/CCmpAIManager.cpp - the CAIWorker is the C++ "driver" of a single bot afaik. At construction time, it initiates the m_ScriptInterface:

m_ScriptInterface(new ScriptInterface("Engine", "AI", g_ScriptRuntime))

that's the "Engine" object you see in JS. Following in the constructor you see the functions being registered:

m_ScriptInterface->RegisterFunction<void, int, CScriptValRooted, CAIWorker::PostCommand>("PostCommand");
m_ScriptInterface->RegisterFunction<void, std::wstring, CAIWorker::IncludeModule>("IncludeModule");
m_ScriptInterface->RegisterFunction<void, CAIWorker::DumpHeap>("DumpHeap");
m_ScriptInterface->RegisterFunction<void, CAIWorker::ForceGC>("ForceGC");
m_ScriptInterface->RegisterFunction<void, std::wstring, std::vector<u32>, u32, u32, u32, CAIWorker::DumpImage>("DumpImage");

So these are the functions of the Engine you can invoke from the AI. Note this list includes "IncludeModule" but not "QueryInterface" so a script cannot break out its sandbox directly. As a side notice, PostCommand is used heavily in the common-api scripts.

Additionally, in /binaries/data/mods/public/simulation/components/ you'll find files named AIInterface.js and AIProxy.js; these are for passing the simulation game/state to the AI.

Link to comment
Share on other sites

One can run Object.keys(Engine).forEach(k => print(k + "\n")) within a bot and get the list of available commands:

ProfileStartProfileStopPostCommandIncludeModuleDumpHeapForceGCDumpImage

More is available via global, Object.keys(global).forEach(k => print(k + "\n")) :

API3
clone
deepcopy
DoesModificationApply
Engine
error
g_pluralTranslations
g_pluralTranslationsWithContext
g_translations
g_translationsWithContext
GetIdentityClasses
GetTechModifiedProperty
GetVisibleIdentityClasses
global
HANNIBAL
HANNIBAL_DEBUG
isNonEmptyString
log
markForTranslation
markForTranslationWithContext
MatchesClassList
PlayerID
print
sprintf
translate
translateMessageObject
translateObjectKeys
translatePlural
translatePluralWithContext
translateWithContext
Vector2D
Vector2Dprototype
Vector3D
Vector3Dprototype
vsprintf
warn

Same thing (Engine) in the GUI compartment looks different:

AddPlaylistItem
AssignNetworkPlayer
AtlasIsAvailable
BuildDirEntList
CameraFollow
CameraFollowFPS
CameraGetX
CameraGetZ
CameraMoveTo
ClearAllPlayerReady
ClearPlaylist
ConfigDB_CreateValue
ConfigDB_GetValue
ConfigDB_Reload
ConfigDB_SetFile
ConfigDB_WriteFile
ConnectXmppClient
Console_GetVisibleEnabled
Console_SetVisibleEnabled
Crash
DebugWarn
DeleteSavedGame
DisconnectNetworkGame
DisconnectXmppClient
DisplayErrorDialog
DumpSimState
DumpTerrainMipmap
EnableTimeWarpRecording
EncryptPassword
EndGame
Exit
FileExists
ForceGC
FormatDecimalNumberIntoString
FormatMillisecondsIntoDateString
GameView_GetConstrainCameraEnabled
GameView_GetCullingEnabled
GameView_GetLockCullCameraEnabled
GameView_SetConstrainCameraEnabled
GameView_SetCullingEnabled
GameView_SetLockCullCameraEnabled
GetAIs
GetAllLocales
GetAvailableMods
GetBoardList
GetBuildTimestamp
GetCurrentLocale
GetDictionariesForLocale
GetDictionaryLocale
GetEngineInfo
GetFPS
GetFallbackToAvailableDictLocale
GetFileMTime
GetFileSize
GetFollowedEntity
GetGUIObjectByName
GetGameList
GetLocaleBaseName
GetLocaleCountry
GetLocaleLanguage
GetLocaleScript
GetMapSettings
GetMatchID
GetPlayerID
GetPlayerList
GetProfile
GetProfilerState
GetSavedGames
GetSimRate
GetSupportedLocaleBaseNames
GetSupportedLocaleDisplayNames
GetTerrainAtScreenPoint
GetUserReportStatus
GuiInterfaceCall
HasXmppClient
HotkeyIsPressed
IsAtlasRunning
IsPaused
IsRankedGame
IsUserReportEnabled
LoadMapSettings
LobbyBan
LobbyGetNick
LobbyGetPlayerPresence
LobbyGetPlayerRole
LobbyGetRoomSubject
LobbyGuiPollMessage
LobbyKick
LobbySendMessage
LobbySetNick
LobbySetPlayerPresence
MusicPlaying
OpenURL
PickEntitiesAtPoint
PickFriendlyEntitiesInRect
PickFriendlyEntitiesOnScreen
PickSimilarFriendlyEntities
PlayAmbientSound
PlayMusic
PlayUISound
PollNetworkClient
PopGuiPage
PopGuiPageCB
PostNetworkCommand
ProfileStart
ProfileStop
PushGuiPage
QuickLoad
QuickSave
ReadFile
ReadFileLines
ReevaluateCurrentLocaleAndReload
Renderer_GetDisplayFrustumEnabled
Renderer_GetGenTangentsEnabled
Renderer_GetParticlesEnabled
Renderer_GetPostprocEnabled
Renderer_GetPreferGLSLEnabled
Renderer_GetRenderPath
Renderer_GetShadowPCFEnabled
Renderer_GetShadowsEnabled
Renderer_GetShowSkyEnabled
Renderer_GetSilhouettesEnabled
Renderer_GetSmoothLOSEnabled
Renderer_GetWaterFancyEffectsEnabled
Renderer_GetWaterRealDepthEnabled
Renderer_GetWaterReflectionEnabled
Renderer_GetWaterRefractionEnabled
Renderer_GetWaterShadowsEnabled
Renderer_GetWaterUglyEnabled
Renderer_SetDisplayFrustumEnabled
Renderer_SetGenTangentsEnabled
Renderer_SetParticlesEnabled
Renderer_SetPostprocEnabled
Renderer_SetPreferGLSLEnabled
Renderer_SetRenderPath
Renderer_SetShadowPCFEnabled
Renderer_SetShadowsEnabled
Renderer_SetShowSkyEnabled
Renderer_SetSilhouettesEnabled
Renderer_SetSmoothLOSEnabled
Renderer_SetWaterFancyEffectsEnabled
Renderer_SetWaterRealDepthEnabled
Renderer_SetWaterReflectionEnabled
Renderer_SetWaterRefractionEnabled
Renderer_SetWaterShadowsEnabled
Renderer_SetWaterUglyEnabled
RestartEngine
RestartInAtlas
RewindTimeWarp
SaveGame
SaveGamePrefix
SaveLocale
SendChangeStateGame
SendGameReport
SendGetBoardList
SendGetGameList
SendGetProfile
SendGetRatingList
SendNetworkChat
SendNetworkReady
SendRegisterGame
SendUnregisterGame
SetActionGain
SetAmbientGain
SetBoundingBoxDebugOverlay(true)
SetCameraData
SetCameraTarget
SetCursor
SetMasterGain
SetMods
SetMusicGain
SetNetworkGameAttributes
SetNetworkPlayerStatus
SetPaused
SetPlayerID
SetRankedGame
SetSimRate
SetTurnLength
SetUIGain
SetUserReportEnabled
StartGame
StartMusic
StartNetworkGame
StartNetworkHost
StartNetworkJoin
StartPlaylist
StartRegisterXmppClient
StartSavedGame
StartXTimer
StartXmppClient
StopMusic
StopXTimer
StopXmppClient
SubmitUserReport
SwitchGuiPage
Translate
TranslateArray
TranslateLines
TranslatePlural
TranslatePluralWithContext
TranslateWithContext
UseLongStrings
ValidateLocale

What I miss the most is interactively firing methods a bot has during run time, like you do with the GUI console(F9). Once a bot runs, nothing can interfere. Access to the camera would also help with testing. Another trick would be to chat commands to the bot, but that doesn't work either.

Link to comment
Share on other sites

Thanks a lot for the quick reply!

My intention was to advance the AI with several selflearning features.

One of these features should be to remember past skirmish statistics. Even after shutdown the system.

To do that, I need to write files to the HDD/SSD.

As far as i know there is no option to write/edit files for the AI right now. (in general for JS)

So I wrote a C++-Component, which can do it. -> But AI can't use the component via "QueryInterface".

Do you know an option for the AI to store informations or writing/editing files?

Is there any function or something else I can use? (Maybe the vfs.?)

Or maybe there is another way to do this?

  • Like 1
Link to comment
Share on other sites

> Do you know an option for the AI to store informations or writing/editing files?

There is none. Except another debugging tool reads and writes from stdout.

> to remember past skirmish statistics. Even after shutdown the system.

0AD is deterministic. Every game is replicable on every computer. That means bots in a multi player game need *same* code, data and thus behavior. If one bots learned something, he needs to share it with all other, too. Otherwise you'll get out of sync errors very quickly.

> Or maybe there is another way to do this?

Sure, spend the bots a XMLHttpRequest object like browsers do to sync over the net. Some projects link to curl to embed it. Then the bots can make sure having same knowledge.

Or, very advanced, let players train their private ally bots, then the game has to sync the full bot code across all connected players.

In both cases I'm ready to work on the server part, syncing data or code. Just the c++ part is way over my head, especially to make it work async with callbacks.

Edited by agentx
Link to comment
Share on other sites

> Do you know an option for the AI to store informations or writing/editing files?

There is none. Except another debugging tool reads and writes from stdout.

> to remember past skirmish statistics. Even after shutdown the system.

0AD is deterministic. Every game is replicable on every computer. That means bots in a multi player game need *same* code, data and thus behavior. If one bots learned something, he needs to share it with all other, too. Otherwise you'll get out of sync errors very quickly.

> Or maybe there is another way to do this?

Sure, spend the bots a XMLHttpRequest object like browsers do to sync over the net. Some projects link to curl to embed it. Then the bots can make sure having same knowledge.

Or, very advanced, let players train their private ally bots, then the game has to sync the full bot code across all connected players.

In both cases I'm ready to work on the server part, syncing data or code. Just the c++ part is way over my head, especially to make it work async with callbacks.

Do you plan to do such a thing ? *.*

Maybe you could use the anonymous feedback to send ai data =)

Link to comment
Share on other sites

I'm not sure if I have understood correctly:
The AI I want to write, should be for single player games only.
(I'm afraid hive mind, multi player sync and thinks like that, are a way over my head.)
Just Player VS. "Learning_AI". No multi player, just local skirmish. (later maybe one player and many AI's)

The data/file where the bot should write his experience is local.
With "The feature to remember past skirmish statistics" I mean the experience of the local bot. (no hive mind)
That also means if you use the "Learning_AI" the first time, they will not have any experiences.

Do you think there is still a problem with synchronization?


@ stanislas69:
> Do you plan to do such a thing ? *.*

My idea is to write an AI, which has the ability to learn and acts more dynamic.
But my programmer skill's (C, C++ and Java) are basic and in JS I'm just at the beginning.
The goal is a prototype of the AI. (Not finished and far away from "ready to releasing".)

Edited by Fluxkompensator
  • Like 1
Link to comment
Share on other sites

The idea is that the AI remember the games they played.

(The decisions they make and the result of these.)

In the next game(s), the AI will make decisions based on their previous experiences.

I hope this will creates a kind of "evolvement".

To give the AI the ability to remember, I want to write the experiences/decisions into a file.

Then the AI can load/read them when a new game (skirmish) starts and save/write them in the end of a game.

For example:

The AI remeber the last game:

The AI only build towers and no army and losed the game.

In this game they will try another way (less towers, more army or less towers more workers....)

Link to comment
Share on other sites

Thats very uncomfortable:

Today I readed my posts again and see that I make a translation mistake:

In my description I often using "they" instead of "the AI".

(Thats why you asking about how to sharing the knowledge of the AI's. -> Btw. thats a nice idea^^. )

Im very sorry about this :(

Actually I want to speak about only one AI(singual) and not about many AI's(plural).

I will give this AI the ability to remember previous games.

In this way the AI can make decisions based on his experience.

The bot will learn in every game, and changing his strategy if necessary.(The AI will also including some random decisions too)

The experience of the AI, I want to write into a simple file on the computer of the user.

Because of this I'm looking for a way to let the AI writing files.

The Current state is, that I have found a dirty way to do this. (I added funcitons in "/source/simulation2/components/CCmpAIManager.cpp")

I'm not happy with this solution, so if anybody have an another idea for me, pls let me know. (My problem: AI can not access "Engine.QueryInterface()")

@ fabio: Thanks for the links.

I hope all of you have a nice evening and night today. :)

Edited by Fluxkompensator
  • Like 1
Link to comment
Share on other sites

> Actually I want to speak about only one AI(singual) and not about many AI's(plural).

Actually my error, I should have made clear that an AI might share knowledge only with its own type. Anyway, if you put sync issues aside, what's left it dealing with with either file system or a web service.

Happy New Year°

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...