Fix timers. Initial work on game object representation.

master
Per Inge Mathisen 2011-01-26 22:13:47 +01:00
parent c1f1c3e31d
commit 5244ea8c7a
4 changed files with 64 additions and 12 deletions

View File

@ -6,7 +6,6 @@ function tick()
function eventGameInit()
{
var p = scavengerPlayer();
console("hey, i'm ze console! scav is " + p);
debug("TEST scav is " + p);
setGlobalTimer(tick, 100);
setGlobalTimer("tick", 100);
}

View File

@ -84,6 +84,7 @@
#include "keybind.h"
#include "wrappers.h"
#include "random.h"
#include "qtscript.h"
#include "warzoneconfig.h"
@ -218,6 +219,7 @@ GAMECODE gameLoop(void)
{
eventProcessTriggers(realTime/SCR_TICKRATE);
}
updateScripts();
}
/* Run the in game interface and see if it grabbed any mouse clicks */

View File

@ -38,7 +38,7 @@
struct timerNode
{
QString function; // maybe we should save this as a QScriptValue instead? might be faster
QString function;
QScriptEngine *engine;
QString baseobj;
int frameTime;
@ -56,9 +56,9 @@ QList<timerNode> timers;
/// Scripting engine (what others call the scripting context, but QtScript's nomenclature is different).
QList<QScriptEngine *> scripts;
static bool callFunction(QScriptEngine *engine, QString funcName, QScriptValueList args)
static bool callFunction(QScriptEngine *engine, QString function, QScriptValueList args)
{
QScriptValue value = engine->globalObject().property(funcName);
QScriptValue value = engine->globalObject().property(function);
if (value.isValid() && value.isFunction())
{
QScriptValue result = value.call(QScriptValue(), args);
@ -67,18 +67,23 @@ static bool callFunction(QScriptEngine *engine, QString funcName, QScriptValueLi
// TODO, get filename to output here somehow
int line = engine->uncaughtExceptionLineNumber();
debug(LOG_ERROR, "Uncaught exception calling event %s at line %d: %s",
funcName.toAscii().constData(), line, result.toString().toAscii().constData());
value.toString().toAscii().constData(), line, result.toString().toAscii().constData());
return false;
}
}
else
{
debug(LOG_ERROR, "Invalid function type for \"%s\"", function.toAscii().constData());
return false;
}
return true;
}
static QScriptValue js_removeTimer(QScriptContext *context, QScriptEngine *engine)
{
QString function = context->argument(0).toString();
int i;
for (i = 0; i < timers.size(); ++i)
int i, size = timers.size();
for (i = 0; i < size; ++i)
{
timerNode node = timers.at(i);
if (node.function == function)
@ -87,10 +92,11 @@ static QScriptValue js_removeTimer(QScriptContext *context, QScriptEngine *engin
break;
}
}
if (i == timers.size())
if (i == size)
{
// Friendly warning
debug(LOG_ERROR, "Did not find timer %s to remove", function.toAscii().constData());
QString warnName = function.left(15) + "...";
debug(LOG_ERROR, "Did not find timer %s to remove", warnName.toAscii().constData());
}
return QScriptValue();
}
@ -101,7 +107,8 @@ static QScriptValue js_setGlobalTimer(QScriptContext *context, QScriptEngine *en
QScriptValue function = context->argument(0);
QScriptValue ms = context->argument(1);
int player = engine->globalObject().property("me").toInt32();
timerNode node(engine, function.toString(), player, ms.toInt32() + gameTime);
QString funcName = function.toString();
timerNode node(engine, funcName, player, ms.toInt32() + gameTime);
timers.push_back(node);
return QScriptValue();
}

View File

@ -31,6 +31,49 @@
// All script functions should be prefixed with "js_" then followed by same name as in script.
static QScriptValue convStructure(STRUCTURE *psStruct, QScriptEngine *engine)
{
QScriptValue value = engine->newObject();
value.setProperty("id", psStruct->id, QScriptValue::ReadOnly);
return value;
}
static QScriptValue js_enumStruct(QScriptContext *context, QScriptEngine *engine)
{
QScriptValue statsName = context->argument(0);
QScriptValue targetPlayer = context->argument(1);
QScriptValue lookingPlayer = context->argument(2);
QList<STRUCTURE *> matches;
int player = targetPlayer.toInt32();
int looking = -1;
QString stat = statsName.toString();
if (!lookingPlayer.isUndefined()) // third arg optional
{
looking = lookingPlayer.toInt32();
}
ASSERT_OR_RETURN(QScriptValue(), player < MAX_PLAYERS && player >= 0, "Target player index out of range: %d", player);
ASSERT_OR_RETURN(QScriptValue(), looking < MAX_PLAYERS && looking >= -1, "Looking player index out of range: %d", looking);
for (STRUCTURE *psStruct = apsStructLists[player]; psStruct; psStruct = psStruct->psNext)
{
if ((looking == -1 || psStruct->visible[looking]) && stat.compare(psStruct->pStructureType->pName) == 0)
{
matches.push_back(psStruct);
}
}
if (matches.size() == 0)
{
return QScriptValue();
}
QScriptValue result = engine->newArray(matches.size());
for (int i = 0; i < matches.size(); i++)
{
STRUCTURE *psStruct = matches.at(i);
result.setProperty(i, convStructure(psStruct, engine));
}
return result;
}
// is this really useful?
static QScriptValue js_debug(QScriptContext *context, QScriptEngine *engine)
{
@ -104,7 +147,7 @@ static QScriptValue js_getDerrick(QScriptContext *context, QScriptEngine *engine
psObj = psTile->psObject;
}
}
return QScriptEngine::toScriptValue(psObj);
return convStructure(psObj, engine);
}
#endif
@ -117,5 +160,6 @@ bool registerFunctions(QScriptEngine *engine)
engine->globalObject().setProperty("debug", engine->newFunction(js_debug));
engine->globalObject().setProperty("console", engine->newFunction(js_console));
engine->globalObject().setProperty("scavengerPlayer", engine->newFunction(js_scavengerPlayer));
engine->globalObject().setProperty("enumStruct", engine->newFunction(js_enumStruct));
return true;
}