Continue porting overworld to scripting

This commit is contained in:
Marianne Gagnon 2015-05-24 19:15:50 -04:00
parent 4c02204eff
commit 1ada542fa5
3 changed files with 41 additions and 20 deletions

View File

@ -177,7 +177,17 @@ namespace Scripting
void ScriptEngine::runFunction(std::string function_name) void ScriptEngine::runFunction(std::string function_name)
{ {
std::function<void(asIScriptContext*)> callback; std::function<void(asIScriptContext*)> callback;
runFunction(function_name, callback); std::function<void(asIScriptContext*)> get_return_value;
runFunction(function_name, callback, get_return_value);
}
//-----------------------------------------------------------------------------
void ScriptEngine::runFunction(std::string function_name,
std::function<void(asIScriptContext*)> callback)
{
std::function<void(asIScriptContext*)> get_return_value;
runFunction(function_name, callback, get_return_value);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -185,7 +195,9 @@ namespace Scripting
/** runs the specified script /** runs the specified script
* \param string scriptName = name of script to run * \param string scriptName = name of script to run
*/ */
void ScriptEngine::runFunction(std::string function_name, std::function<void(asIScriptContext*)> callback) void ScriptEngine::runFunction(std::string function_name,
std::function<void(asIScriptContext*)> callback,
std::function<void(asIScriptContext*)> get_return_value)
{ {
int r; //int for error checking int r; //int for error checking
@ -199,11 +211,11 @@ namespace Scripting
if (cached_script == m_loaded_files.end()) if (cached_script == m_loaded_files.end())
{ {
// Compile the script code // Compile the script code
Log::debug("Scripting", "Compiling script '%s' (was not in cache)", script_filename.c_str()); Log::info("Scripting", "Checking for script file '%s'", script_filename.c_str());
r = compileScript(m_engine, script_filename); r = compileScript(m_engine, script_filename);
if (r < 0) if (r < 0)
{ {
Log::debug("Scripting", "Script '%s' is not available", script_filename.c_str()); Log::info("Scripting", "Script '%s' is not available", script_filename.c_str());
m_loaded_files[script_filename] = false; m_loaded_files[script_filename] = false;
m_functions_cache[function_name] = NULL; // remember that this script is unavailable m_functions_cache[function_name] = NULL; // remember that this script is unavailable
return; return;
@ -306,6 +318,9 @@ namespace Scripting
// Retrieve the return value from the context here (for scripts that return values) // Retrieve the return value from the context here (for scripts that return values)
// <type> returnValue = ctx->getReturnType(); for example // <type> returnValue = ctx->getReturnType(); for example
//float returnValue = ctx->GetReturnFloat(); //float returnValue = ctx->GetReturnFloat();
if (get_return_value)
get_return_value(ctx);
} }
// We must release the contexts when no longer using them // We must release the contexts when no longer using them

View File

@ -53,7 +53,11 @@ namespace Scripting
~ScriptEngine(); ~ScriptEngine();
void runFunction(std::string function_name); void runFunction(std::string function_name);
void runFunction(std::string function_name, std::function<void(asIScriptContext*)> callback); void runFunction(std::string function_name,
std::function<void(asIScriptContext*)> callback);
void runFunction(std::string function_name,
std::function<void(asIScriptContext*)> callback,
std::function<void(asIScriptContext*)> get_return_value);
void evalScript(std::string script_fragment); void evalScript(std::string script_fragment);
void cleanupCache(); void cleanupCache();

View File

@ -1116,6 +1116,8 @@ bool Track::loadMainTrack(const XMLNode &root)
// some static meshes are conditional // some static meshes are conditional
std::string condition; std::string condition;
n->get("if", &condition); n->get("if", &condition);
// TODO: convert "if" and "ifnot" to scripting.
if (condition == "splatting") if (condition == "splatting")
{ {
if (!irr_driver->supportsSplatting()) continue; if (!irr_driver->supportsSplatting()) continue;
@ -1193,15 +1195,15 @@ bool Track::loadMainTrack(const XMLNode &root)
if (!shown) continue; if (!shown) continue;
} }
else if (condition == "allchallenges")
{
// allow ONE unsolved challenge : the last one
if (getNumOfCompletedChallenges() < m_challenges.size() - 1)
continue;
}
else if (condition.size() > 0) else if (condition.size() > 0)
{ {
Log::error("track", "Unknown condition <%s>\n", condition.c_str()); unsigned char result = -1;
Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
std::function<void(asIScriptContext*)> null_callback;
script_engine->runFunction("bool " + condition + "()", null_callback,
[&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); });
if (result == 0)
continue;
} }
std::string neg_condition; std::string neg_condition;
@ -1210,18 +1212,18 @@ bool Track::loadMainTrack(const XMLNode &root)
{ {
if (irr_driver->supportsSplatting()) continue; if (irr_driver->supportsSplatting()) continue;
} }
else if (neg_condition == "allchallenges")
{
// allow ONE unsolved challenge : the last one
if (getNumOfCompletedChallenges() >= m_challenges.size() - 1)
continue;
}
else if (neg_condition.size() > 0) else if (neg_condition.size() > 0)
{ {
Log::error("track", "Unknown condition <%s>\n", unsigned char result = -1;
neg_condition.c_str()); Scripting::ScriptEngine* script_engine = World::getWorld()->getScriptEngine();
std::function<void(asIScriptContext*)> null_callback;
script_engine->runFunction("bool " + neg_condition + "()", null_callback,
[&](asIScriptContext* ctx) { result = ctx->GetReturnByte(); });
if (result != 0)
continue;
} }
bool tangent = false; bool tangent = false;
n->get("tangents", &tangent); n->get("tangents", &tangent);