Skip to content

Commit

Permalink
ACS|Hexen: Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jan 27, 2014
1 parent 75e7122 commit 0be5024
Showing 1 changed file with 56 additions and 40 deletions.
96 changes: 56 additions & 40 deletions doomsday/plugins/hexen/src/acscript.cpp
Expand Up @@ -188,25 +188,28 @@ class Interpreter
*/
void startOpenScripts()
{
DENG_ASSERT(!IS_CLIENT);

// Each is allotted 1 second for initialization.
for(int i = 0; i < _scriptCount; ++i)
{
BytecodeScriptInfo &info = _scriptInfo[i];

if(info.flags & BytecodeScriptInfo::Open)
{
newACScript(info, TICSPERSEC);
info.state = Running;
ACScript *script = newACScript(info, 0/*no args*/, TICSPERSEC);
DENG_ASSERT(script != 0);
DENG_UNUSED(script);
}
}
}

/**
* Start/resume the specified script.
*
* @return Pointer to the @em newly-started script thinker; otherwise @c 0.
* @return @c true iff a script was newly started (or deferred).
*/
ACScript *startScript(int scriptNumber, uint map, byte *args, mobj_t *activator = 0,
bool startScript(int scriptNumber, uint map, byte *args, mobj_t *activator = 0,
Line *line = 0, int side = 0)
{
DENG_ASSERT(!IS_CLIENT);
Expand All @@ -215,44 +218,32 @@ class Interpreter
{
// Script is not for the current map.
// Add it to the store to be started when that map is next entered.
newDeferredTask(map - 1, scriptNumber, args);
return 0;
}

if(!hasScriptEntrypoint(scriptNumber))
{
// Script not found.
App_Log(DE2_SCR_WARNING, "ACS: Unknown script #%i", scriptNumber);
return 0;
return newDeferredTask(map - 1, scriptNumber, args);
}

BytecodeScriptInfo &info = scriptInfo(scriptNumber);

if(info.state == Suspended)
if(BytecodeScriptInfo *info = scriptInfoPtr(scriptNumber))
{
// Resume a suspended script.
info.state = Running;
return 0;
}

if(info.state != Inactive)
{
// Script is already executing.
return 0;
// Resume a suspended script?
if(info->state == Suspended)
{
info->state = Running;
return true;
}
else if(ACScript *script = newACScript(*info, args))
{
script->activator = activator;
script->line = line;
script->side = side;
return true;
}
}

ACScript *script = newACScript(info);

script->activator = activator;
script->line = line;
script->side = side;
for(int i = 0; i < info.argCount; ++i)
else
{
script->vars[i] = args[i];
// Script not found.
App_Log(DE2_SCR_WARNING, "ACS: Unknown script #%i", scriptNumber);
}
info.state = Running;

return script;
return false; // Perhaps its already executing?
}

/**
Expand Down Expand Up @@ -323,16 +314,30 @@ class Interpreter
while(i < _deferredTasksSize)
{
DeferredTask *task = &_deferredTasks[i];
int scriptNumber = task->scriptNumber;

if(task->map != map)
{
i++;
continue;
}

ACScript *script = startScript(task->scriptNumber, map, task->args);
DENG_ASSERT(script != 0);
script->delayCount = TICSPERSEC;
if(BytecodeScriptInfo *info = scriptInfoPtr(scriptNumber))
{
// Resume a suspended script?
if(info->state == Suspended)
{
info->state = Running;
}
else
{
newACScript(*info, task->args, TICSPERSEC);
}
}
else
{
App_Log(DE2_SCR_WARNING, "ACS: Unknown script #%i", scriptNumber);
}

_deferredTasksSize -= 1;
if(i == _deferredTasksSize)
Expand Down Expand Up @@ -596,8 +601,11 @@ class Interpreter
return -1;
}

ACScript *newACScript(BytecodeScriptInfo &info, int delayCount = 0)
ACScript *newACScript(BytecodeScriptInfo &info, byte const *args, int delayCount = 0)
{
// Is the script is already executing?
if(info.state != Inactive) return 0;

ACScript *script = (ACScript *) Z_Calloc(sizeof(*script), PU_MAP, 0);
script->thinker.function = (thinkfunc_t) ACScript_Thinker;

Expand All @@ -606,8 +614,16 @@ class Interpreter
script->pcodePtr = info.pcodePtr;
script->delayCount = delayCount;

for(int i = 0; i < info.argCount; ++i)
{
DENG_ASSERT(args != 0);
script->vars[i] = args[i];
}

Thinker_Add(&script->thinker);

info.state = Running;

return script;
}

Expand Down Expand Up @@ -1684,7 +1700,7 @@ void P_ACScriptRunDeferredTasks(uint map/*Uri const *mapUri*/)
dd_bool P_StartACScript(int scriptNumber, uint map, byte *args, mobj_t *activator,
Line *line, int side)
{
return interp.startScript(scriptNumber, map, args, activator, line, side) != 0;
return interp.startScript(scriptNumber, map, args, activator, line, side);
}

dd_bool P_TerminateACScript(int scriptNumber, uint /*map*/)
Expand Down

0 comments on commit 0be5024

Please sign in to comment.