Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/components/CCmpAIManager.cpp
Show First 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | bool Initialise() | ||||
LOGERROR("Failed to create AI player: %s: can't find constructor '%s'", path.string8(), constructor); | LOGERROR("Failed to create AI player: %s: can't find constructor '%s'", path.string8(), constructor); | ||||
return false; | return false; | ||||
} | } | ||||
m_ScriptInterface->GetProperty(metadata, "useShared", m_UseSharedComponent); | m_ScriptInterface->GetProperty(metadata, "useShared", m_UseSharedComponent); | ||||
// Set up the data to pass as the constructor argument | // Set up the data to pass as the constructor argument | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
m_ScriptInterface->CreateObject( | ScriptInterface::CreateObject( | ||||
cx, | |||||
&settings, | &settings, | ||||
"player", m_Player, | "player", m_Player, | ||||
"difficulty", m_Difficulty, | "difficulty", m_Difficulty, | ||||
"behavior", m_Behavior); | "behavior", m_Behavior); | ||||
if (!m_UseSharedComponent) | if (!m_UseSharedComponent) | ||||
{ | { | ||||
ENSURE(m_Worker.m_HasLoadedEntityTemplates); | ENSURE(m_Worker.m_HasLoadedEntityTemplates); | ||||
▲ Show 20 Lines • Show All 280 Lines • ▼ Show 20 Lines | if (!m_ScriptInterface->GetProperty(AIModule, "SharedScript", &ctor) | ||||
|| ctor.isUndefined()) | || ctor.isUndefined()) | ||||
{ | { | ||||
LOGERROR("Failed to create shared AI component: %s: can't find constructor '%s'", path.string8(), "SharedScript"); | LOGERROR("Failed to create shared AI component: %s: can't find constructor '%s'", path.string8(), "SharedScript"); | ||||
return false; | return false; | ||||
} | } | ||||
// Set up the data to pass as the constructor argument | // Set up the data to pass as the constructor argument | ||||
JS::RootedValue playersID(cx); | JS::RootedValue playersID(cx); | ||||
m_ScriptInterface->CreateObject(&playersID); | ScriptInterface::CreateObject(cx, &playersID); | ||||
for (size_t i = 0; i < m_Players.size(); ++i) | for (size_t i = 0; i < m_Players.size(); ++i) | ||||
{ | { | ||||
JS::RootedValue val(cx); | JS::RootedValue val(cx); | ||||
m_ScriptInterface->ToJSVal(cx, &val, m_Players[i]->m_Player); | m_ScriptInterface->ToJSVal(cx, &val, m_Players[i]->m_Player); | ||||
m_ScriptInterface->SetPropertyInt(playersID, i, val, true); | m_ScriptInterface->SetPropertyInt(playersID, i, val, true); | ||||
} | } | ||||
ENSURE(m_HasLoadedEntityTemplates); | ENSURE(m_HasLoadedEntityTemplates); | ||||
JS::RootedValue settings(cx); | JS::RootedValue settings(cx); | ||||
m_ScriptInterface->CreateObject( | ScriptInterface::CreateObject( | ||||
cx, | |||||
&settings, | &settings, | ||||
"players", playersID, | "players", playersID, | ||||
"templates", m_EntityTemplates); | "templates", m_EntityTemplates); | ||||
JS::AutoValueVector argv(cx); | JS::AutoValueVector argv(cx); | ||||
argv.append(settings); | argv.append(settings); | ||||
m_ScriptInterface->CallConstructor(ctor, argv, &m_SharedAIObj); | m_ScriptInterface->CallConstructor(ctor, argv, &m_SharedAIObj); | ||||
▲ Show 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | public: | ||||
void LoadEntityTemplates(const std::vector<std::pair<std::string, const CParamNode*> >& templates) | void LoadEntityTemplates(const std::vector<std::pair<std::string, const CParamNode*> >& templates) | ||||
{ | { | ||||
JSContext* cx = m_ScriptInterface->GetContext(); | JSContext* cx = m_ScriptInterface->GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
m_HasLoadedEntityTemplates = true; | m_HasLoadedEntityTemplates = true; | ||||
m_ScriptInterface->CreateObject(&m_EntityTemplates); | ScriptInterface::CreateObject(cx, &m_EntityTemplates); | ||||
JS::RootedValue val(cx); | JS::RootedValue val(cx); | ||||
for (size_t i = 0; i < templates.size(); ++i) | for (size_t i = 0; i < templates.size(); ++i) | ||||
{ | { | ||||
templates[i].second->ToJSVal(cx, false, &val); | templates[i].second->ToJSVal(cx, false, &val); | ||||
m_ScriptInterface->SetProperty(m_EntityTemplates, templates[i].first.c_str(), val, true); | m_ScriptInterface->SetProperty(m_EntityTemplates, templates[i].first.c_str(), val, true); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 536 Lines • ▼ Show 20 Lines | void LoadPathfinderClasses(JS::HandleValue state) | ||||
if (!cmpPathfinder) | if (!cmpPathfinder) | ||||
return; | return; | ||||
const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); | const ScriptInterface& scriptInterface = GetSimContext().GetScriptInterface(); | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue classesVal(cx); | JS::RootedValue classesVal(cx); | ||||
scriptInterface.CreateObject(&classesVal); | ScriptInterface::CreateObject(cx, &classesVal); | ||||
std::map<std::string, pass_class_t> classes; | std::map<std::string, pass_class_t> classes; | ||||
cmpPathfinder->GetPassabilityClasses(classes); | cmpPathfinder->GetPassabilityClasses(classes); | ||||
for (std::map<std::string, pass_class_t>::iterator it = classes.begin(); it != classes.end(); ++it) | for (std::map<std::string, pass_class_t>::iterator it = classes.begin(); it != classes.end(); ++it) | ||||
scriptInterface.SetProperty(classesVal, it->first.c_str(), it->second, true); | scriptInterface.SetProperty(classesVal, it->first.c_str(), it->second, true); | ||||
scriptInterface.SetProperty(state, "passabilityClasses", classesVal, true); | scriptInterface.SetProperty(state, "passabilityClasses", classesVal, true); | ||||
} | } | ||||
CAIWorker m_Worker; | CAIWorker m_Worker; | ||||
}; | }; | ||||
REGISTER_COMPONENT_TYPE(AIManager) | REGISTER_COMPONENT_TYPE(AIManager) |
Wildfire Games · Phabricator