Skip to content

Commit

Permalink
qtscript: make sure both eventObjectSeen and
Browse files Browse the repository at this point in the history
eventGroupSeen are triggered for objects that
have both OBJECT and GROUP labels on them.
  • Loading branch information
haoNoQ committed Nov 23, 2014
1 parent 5389125 commit e10467c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/qtscript.cpp
Expand Up @@ -1234,21 +1234,21 @@ bool triggerEventSeen(BASE_OBJECT *psViewer, BASE_OBJECT *psSeen)
for (int i = 0; i < scripts.size() && psSeen && psViewer; ++i)
{
QScriptEngine *engine = scripts.at(i);
int seen = seenLabelCheck(engine, psSeen, psViewer);
if (seen == SCRIPT_OBJECT_SEEN)
std::pair<bool, int> callbacks = seenLabelCheck(engine, psSeen, psViewer);
if (callbacks.first)
{
QScriptValueList args;
args += convMax(psViewer, engine);
args += convMax(psSeen, engine);
callFunction(engine, "eventObjectSeen", args);
}
else if (seen > 0)
if (callbacks.second)
{
QScriptValueList args;
args += convMax(psViewer, engine);
args += QScriptValue(seen); // group id
args += QScriptValue(callbacks.second); // group id
callFunction(engine, "eventGroupSeen", args);
} // else do nothing
}
}
return true;
}
Expand Down
21 changes: 14 additions & 7 deletions src/qtscriptfuncs.cpp
Expand Up @@ -328,27 +328,34 @@ void showLabel(const QString &key)
}
}

// Returns zero for no action, SCRIPT_OBJECT_SEEN for object seen callback, positive for group callback
int seenLabelCheck(QScriptEngine *engine, BASE_OBJECT *seen, BASE_OBJECT *viewer)
// The bool return value is true when an object callback needs to be called.
// The int return value holds group id when a group callback needs to be called, 0 otherwise.
std::pair<bool, int> seenLabelCheck(QScriptEngine *engine, BASE_OBJECT *seen, BASE_OBJECT *viewer)
{
GROUPMAP *psMap = groups.value(engine);
int groupId = psMap->value(seen);
bool foundObj = false, foundGroup = false;
for (LABELMAP::iterator i = labels.begin(); i != labels.end(); i++)
{
labeltype &l = (*i);
if (l.id == seen->id && l.triggered == 0)
if (l.id == seen->id && l.triggered == 0
&& (l.subscriber == ALL_PLAYERS || l.subscriber == viewer->player))
{
l.triggered = viewer->id; // record who made the discovery
return SCRIPT_OBJECT_SEEN;
foundObj = true;
}
else if (l.type == SCRIPT_GROUP && l.id == groupId && l.triggered == 0
&& (l.subscriber == ALL_PLAYERS || l.subscriber == viewer->player))
{
l.triggered = viewer->id; // record who made the discovery
return groupId;
foundGroup = true;
}
}
return 0;
if (foundObj || foundGroup)
{
updateLabelModel();
}
return std::make_pair(foundObj, foundGroup ? groupId : 0);
}

bool areaLabelCheck(DROID *psDroid)
Expand All @@ -361,7 +368,7 @@ bool areaLabelCheck(DROID *psDroid)
labeltype &l = i.value();
if (l.triggered == 0 && (l.subscriber == ALL_PLAYERS || l.subscriber == psDroid->player)
&& ((l.type == SCRIPT_AREA && l.p1.x < x && l.p1.y < y && l.p2.x > x && l.p2.y > y)
|| (l.type == SCRIPT_RADIUS && iHypot(l.p1 - removeZ(psDroid->pos)) < l.p2.x)))
|| (l.type == SCRIPT_RADIUS && iHypot(l.p1 - removeZ(psDroid->pos)) < l.p2.x)))
{
// We're inside an untriggered area
activated = true;
Expand Down
4 changes: 1 addition & 3 deletions src/qtscriptfuncs.h
Expand Up @@ -37,8 +37,6 @@ enum SCRIPT_TYPE
SCRIPT_COUNT
};

#define SCRIPT_OBJECT_SEEN -1

#include <QtScript/QScriptEngine>

// ----------------------------------------------
Expand Down Expand Up @@ -79,7 +77,7 @@ QStandardItemModel *createLabelModel();
void showLabel(const QString &key);

/// Check if this object marked for a seen trigger once it comes into vision
int seenLabelCheck(QScriptEngine *engine, BASE_OBJECT *seen, BASE_OBJECT *viewer);
std::pair<bool, int> seenLabelCheck(QScriptEngine *engine, BASE_OBJECT *seen, BASE_OBJECT *viewer);

/// Assert for scripts that give useful backtraces and other info.
#define SCRIPT_ASSERT(context, expr, ...) \
Expand Down

0 comments on commit e10467c

Please sign in to comment.