From 4c6edd5e58e5b61b601d39a4aec271e1ec2d4747 Mon Sep 17 00:00:00 2001 From: Randy Heit Date: Sat, 26 Jul 2014 22:58:47 -0500 Subject: [PATCH] Add script array support to ZDoom --- src/p_acs.cpp | 421 +++++++++++++++++++++++++++++++++++++------------- src/p_acs.h | 79 +++++++++- 2 files changed, 389 insertions(+), 111 deletions(-) diff --git a/src/p_acs.cpp b/src/p_acs.cpp index 87e65cac3..baf573b99 100644 --- a/src/p_acs.cpp +++ b/src/p_acs.cpp @@ -122,10 +122,11 @@ FRandom pr_acs ("ACS"); struct CallReturn { - CallReturn(int pc, ScriptFunction *func, FBehavior *module, SDWORD *locals, bool discard, unsigned int runaway) + CallReturn(int pc, ScriptFunction *func, FBehavior *module, SDWORD *locals, ACSLocalArrays *arrays, bool discard, unsigned int runaway) : ReturnFunction(func), ReturnModule(module), ReturnLocals(locals), + ReturnArrays(arrays), ReturnAddress(pc), bDiscardResult(discard), EntryInstrCount(runaway) @@ -134,6 +135,7 @@ struct CallReturn ScriptFunction *ReturnFunction; FBehavior *ReturnModule; SDWORD *ReturnLocals; + ACSLocalArrays *ReturnArrays; int ReturnAddress; int bDiscardResult; unsigned int EntryInstrCount; @@ -1673,6 +1675,26 @@ void FBehavior::SerializeVarSet (FArchive &arc, SDWORD *vars, int max) } } +static int ParseLocalArrayChunk(void *chunk, ACSLocalArrays *arrays, int offset) +{ + unsigned count = (LittleShort(((unsigned *)chunk)[1]) - 2) / 4; + int *sizes = (int *)((BYTE *)chunk + 10); + arrays->Count = count; + if (count > 0) + { + ACSLocalArrayInfo *info = new ACSLocalArrayInfo[count]; + arrays->Info = info; + for (unsigned i = 0; i < count; ++i) + { + info[i].Size = LittleLong(sizes[i]); + info[i].Offset = offset; + offset += info[i].Size; + } + } + // Return the new local variable size, with space for the arrays + return offset; +} + FBehavior::FBehavior (int lumpnum, FileReader * fr, int len) { BYTE *object; @@ -1822,12 +1844,45 @@ FBehavior::FBehavior (int lumpnum, FileReader * fr, int len) { DWORD *chunk; - Functions = FindChunk (MAKE_ID('F','U','N','C')); - if (Functions != NULL) + // Load functions + BYTE *funcs; + Functions = NULL; + funcs = FindChunk (MAKE_ID('F','U','N','C')); + if (funcs != NULL) { - NumFunctions = LittleLong(((DWORD *)Functions)[1]) / 8; - Functions += 8; + NumFunctions = LittleLong(((DWORD *)funcs)[1]) / 8; + funcs += 8; FunctionProfileData = new ACSProfileInfo[NumFunctions]; + Functions = new ScriptFunction[NumFunctions]; + for (i = 0; i < NumFunctions; ++i) + { + ScriptFunctionInFile *funcf = &((ScriptFunctionInFile *)funcs)[i]; + ScriptFunction *funcm = &Functions[i]; + funcm->ArgCount = funcf->ArgCount; + funcm->HasReturnValue = funcf->HasReturnValue; + funcm->ImportNum = funcf->ImportNum; + funcm->LocalCount = funcf->LocalCount; + funcm->Address = funcf->Address; + } + } + + // Load local arrays for functions + if (NumFunctions > 0) + { + for (chunk = (DWORD *)FindChunk(MAKE_ID('F','A','R','Y')); chunk != NULL; chunk = (DWORD *)NextChunk((BYTE *)chunk)) + { + int size = LittleLong(chunk[1]); + if (size >= 6) + { + unsigned int func_num = LittleShort(((WORD *)chunk)[4]); + if (func_num < (unsigned int)NumFunctions) + { + ScriptFunction *func = &Functions[func_num]; + // Unlike scripts, functions do not include their arg count in their local count. + func->LocalCount = ParseLocalArrayChunk(chunk, &func->LocalArrays, func->LocalCount + func->ArgCount) - func->ArgCount; + } + } + } } // Load JUMP points @@ -2135,6 +2190,11 @@ FBehavior::~FBehavior () delete[] ArrayStore; ArrayStore = NULL; } + if (Functions != NULL) + { + delete[] Functions; + Functions = NULL; + } if (FunctionProfileData != NULL) { delete[] FunctionProfileData; @@ -2302,6 +2362,21 @@ void FBehavior::LoadScriptsDirectory () } } + // Load script array sizes. (One chunk per script that uses arrays.) + for (scripts.b = FindChunk(MAKE_ID('S','A','R','Y')); scripts.dw != NULL; scripts.b = NextChunk(scripts.b)) + { + int size = LittleLong(scripts.dw[1]); + if (size >= 6) + { + int script_num = LittleShort(scripts.w[4]); + ScriptPtr *ptr = const_cast(FindScript(script_num)); + if (ptr != NULL) + { + ptr->VarCount = ParseLocalArrayChunk(scripts.b, &ptr->LocalArrays, ptr->VarCount); + } + } + } + // Load script names (if any) scripts.b = FindChunk(MAKE_ID('S','N','A','M')); if (scripts.dw != NULL) @@ -5460,14 +5535,50 @@ inline int getshort (int *&pc) return res; } +static bool CharArrayParms(int &capacity, int &offset, int &a, int *Stack, int &sp, bool ranged) +{ + if (ranged) + { + capacity = STACK(1); + offset = STACK(2); + if (capacity < 1 || offset < 0) + { + sp -= 4; + return false; + } + sp -= 2; + } + else + { + capacity = INT_MAX; + offset = 0; + } + a = STACK(1); + offset += STACK(2); + sp -= 2; + return true; +} + int DLevelScript::RunScript () { DACSThinker *controller = DACSThinker::ActiveThinker; SDWORD *locals = localvars; + ACSLocalArrays noarrays; + ACSLocalArrays *localarrays = &noarrays; ScriptFunction *activeFunction = NULL; FRemapTable *translation = 0; int resultValue = 1; + if (InModuleScriptNumber >= 0) + { + ScriptPtr *ptr = activeBehavior->GetScriptPtr(InModuleScriptNumber); + assert(ptr != NULL); + if (ptr != NULL) + { + localarrays = &ptr->LocalArrays; + } + } + // Hexen truncates all special arguments to bytes (only when using an old MAPINFO and old ACS format const int specialargmask = ((level.flags2 & LEVEL2_HEXENHACK) && activeBehavior->GetFormat() == ACS_Old) ? 255 : ~0; @@ -5832,9 +5943,10 @@ int DLevelScript::RunScript () } sp += i; ::new(&Stack[sp]) CallReturn(activeBehavior->PC2Ofs(pc), activeFunction, - activeBehavior, mylocals, pcd == PCD_CALLDISCARD, runaway); + activeBehavior, mylocals, localarrays, pcd == PCD_CALLDISCARD, runaway); sp += (sizeof(CallReturn) + sizeof(int) - 1) / sizeof(int); pc = module->Ofs2PC (func->Address); + localarrays = &func->LocalArrays; activeFunction = func; activeBehavior = module; fmt = module->GetFormat(); @@ -5868,6 +5980,7 @@ int DLevelScript::RunScript () activeBehavior = ret->ReturnModule; fmt = activeBehavior->GetFormat(); locals = ret->ReturnLocals; + localarrays = ret->ReturnArrays; if (!ret->bDiscardResult) { Stack[sp++] = value; @@ -5966,6 +6079,11 @@ int DLevelScript::RunScript () sp--; break; + case PCD_ASSIGNSCRIPTARRAY: + localarrays->Set(locals, NEXTBYTE, STACK(2), STACK(1)); + sp -= 2; + break; + case PCD_ASSIGNMAPARRAY: activeBehavior->SetArrayVal (*(activeBehavior->MapVars[NEXTBYTE]), STACK(2), STACK(1)); sp -= 2; @@ -5997,6 +6115,10 @@ int DLevelScript::RunScript () PushToStack (ACS_GlobalVars[NEXTBYTE]); break; + case PCD_PUSHSCRIPTARRAY: + STACK(1) = localarrays->Get(locals, NEXTBYTE, STACK(1)); + break; + case PCD_PUSHMAPARRAY: STACK(1) = activeBehavior->GetArrayVal (*(activeBehavior->MapVars[NEXTBYTE]), STACK(1)); break; @@ -6029,6 +6151,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_ADDSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) + STACK(1)); + sp -= 2; + } + break; + case PCD_ADDMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6074,6 +6204,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_SUBSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) - STACK(1)); + sp -= 2; + } + break; + case PCD_SUBMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6119,6 +6257,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_MULSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) * STACK(1)); + sp -= 2; + } + break; + case PCD_MULMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6192,6 +6338,19 @@ int DLevelScript::RunScript () } break; + case PCD_DIVSCRIPTARRAY: + if (STACK(1) == 0) + { + state = SCRIPT_DivideBy0; + } + else + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) / STACK(1)); + sp -= 2; + } + break; + case PCD_DIVMAPARRAY: if (STACK(1) == 0) { @@ -6280,6 +6439,19 @@ int DLevelScript::RunScript () } break; + case PCD_MODSCRIPTARRAY: + if (STACK(1) == 0) + { + state = SCRIPT_ModulusBy0; + } + else + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) % STACK(1)); + sp -= 2; + } + break; + case PCD_MODMAPARRAY: if (STACK(1) == 0) { @@ -6341,6 +6513,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_ANDSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) & STACK(1)); + sp -= 2; + } + break; + case PCD_ANDMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6386,6 +6566,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_EORSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) ^ STACK(1)); + sp -= 2; + } + break; + case PCD_EORMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6431,6 +6619,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_ORSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) | STACK(1)); + sp -= 2; + } + break; + case PCD_ORMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6477,6 +6673,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_LSSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) << STACK(1)); + sp -= 2; + } + break; + case PCD_LSMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6522,6 +6726,14 @@ int DLevelScript::RunScript () sp--; break; + case PCD_RSSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(2); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) >> STACK(1)); + sp -= 2; + } + break; + case PCD_RSMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6564,6 +6776,14 @@ int DLevelScript::RunScript () ++ACS_GlobalVars[NEXTBYTE]; break; + case PCD_INCSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(1); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) + 1); + sp--; + } + break; + case PCD_INCMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -6605,6 +6825,14 @@ int DLevelScript::RunScript () --ACS_GlobalVars[NEXTBYTE]; break; + case PCD_DECSCRIPTARRAY: + { + int a = NEXTBYTE, i = STACK(1); + localarrays->Set(locals, a, i, localarrays->Get(locals, a, i) - 1); + sp--; + } + break; + case PCD_DECMAPARRAY: { int a = *(activeBehavior->MapVars[NEXTBYTE]); @@ -7031,37 +7259,35 @@ scriptwait: } break; + // Print script character array + case PCD_PRINTSCRIPTCHARARRAY: + case PCD_PRINTSCRIPTCHRANGE: + { + int capacity, offset, a, c; + if (CharArrayParms(capacity, offset, a, Stack, sp, pcd == PCD_PRINTSCRIPTCHRANGE)) + { + while (capacity-- && (c = localarrays->Get(locals, a, offset)) != '\0') + { + work += (char)c; + offset++; + } + } + } + break; + // [JB] Print map character array case PCD_PRINTMAPCHARARRAY: case PCD_PRINTMAPCHRANGE: { - int capacity, offset; - - if (pcd == PCD_PRINTMAPCHRANGE) + int capacity, offset, a, c; + if (CharArrayParms(capacity, offset, a, Stack, sp, pcd == PCD_PRINTMAPCHRANGE)) { - capacity = STACK(1); - offset = STACK(2); - if (capacity < 1 || offset < 0) + while (capacity-- && (c = activeBehavior->GetArrayVal (a, offset)) != '\0') { - sp -= 4; - break; + work += (char)c; + offset++; } - sp -= 2; } - else - { - capacity = 0x7FFFFFFF; - offset = 0; - } - - int a = *(activeBehavior->MapVars[STACK(1)]); - offset += STACK(2); - int c; - while(capacity-- && (c = activeBehavior->GetArrayVal (a, offset)) != '\0') { - work += (char)c; - offset++; - } - sp-= 2; } break; @@ -7069,32 +7295,15 @@ scriptwait: case PCD_PRINTWORLDCHARARRAY: case PCD_PRINTWORLDCHRANGE: { - int capacity, offset; - if (pcd == PCD_PRINTWORLDCHRANGE) + int capacity, offset, a, c; + if (CharArrayParms(capacity, offset, a, Stack, sp, pcd == PCD_PRINTWORLDCHRANGE)) { - capacity = STACK(1); - offset = STACK(2); - if (capacity < 1 || offset < 0) + while (capacity-- && (c = ACS_WorldArrays[a][offset]) != '\0') { - sp -= 4; - break; + work += (char)c; + offset++; } - sp -= 2; } - else - { - capacity = 0x7FFFFFFF; - offset = 0; - } - - int a = STACK(1); - offset += STACK(2); - int c; - while(capacity-- && (c = ACS_WorldArrays[a][offset]) != '\0') { - work += (char)c; - offset++; - } - sp-= 2; } break; @@ -7102,32 +7311,15 @@ scriptwait: case PCD_PRINTGLOBALCHARARRAY: case PCD_PRINTGLOBALCHRANGE: { - int capacity, offset; - if (pcd == PCD_PRINTGLOBALCHRANGE) + int capacity, offset, a, c; + if (CharArrayParms(capacity, offset, a, Stack, sp, pcd == PCD_PRINTGLOBALCHRANGE)) { - capacity = STACK(1); - offset = STACK(2); - if (capacity < 1 || offset < 0) + while (capacity-- && (c = ACS_GlobalArrays[a][offset]) != '\0') { - sp -= 4; - break; + work += (char)c; + offset++; } - sp -= 2; } - else - { - capacity = 0x7FFFFFFF; - offset = 0; - } - - int a = STACK(1); - offset += STACK(2); - int c; - while(capacity-- && (c = ACS_GlobalArrays[a][offset]) != '\0') { - work += (char)c; - offset++; - } - sp-= 2; } break; @@ -8674,6 +8866,7 @@ scriptwait: } break; + case PCD_STRCPYTOSCRIPTCHRANGE: case PCD_STRCPYTOMAPCHRANGE: case PCD_STRCPYTOWORLDCHRANGE: case PCD_STRCPYTOGLOBALCHRANGE: @@ -8704,7 +8897,7 @@ scriptwait: break; } - for (int i = 0;i < STACK(1); i++) + for (int i = 0; i < STACK(1); i++) { if (! (*(lookup++))) { @@ -8715,43 +8908,55 @@ scriptwait: switch (pcd) { - case PCD_STRCPYTOMAPCHRANGE: - { - int a = STACK(5); - if (a < NUM_MAPVARS && a > 0 && - activeBehavior->MapVars[a]) - { - Stack[sp-6] = activeBehavior->CopyStringToArray(*(activeBehavior->MapVars[a]), index, capacity, lookup); - } - } - break; - case PCD_STRCPYTOWORLDCHRANGE: - { - int a = STACK(5); + case PCD_STRCPYTOSCRIPTCHRANGE: + { + int a = STACK(5); - while (capacity-- > 0) - { - ACS_WorldArrays[a][index++] = *lookup; - if (! (*(lookup++))) goto STRCPYTORANGECOMPLETE; // complete with terminating 0 - } - - Stack[sp-6] = !(*lookup); // true/success if only terminating 0 was not copied - } - break; - case PCD_STRCPYTOGLOBALCHRANGE: + while (capacity-- > 0) { - int a = STACK(5); - - while (capacity-- > 0) - { - ACS_GlobalArrays[a][index++] = *lookup; - if (! (*(lookup++))) goto STRCPYTORANGECOMPLETE; // complete with terminating 0 - } - - Stack[sp-6] = !(*lookup); // true/success if only terminating 0 was not copied + localarrays->Set(locals, a, index++, *lookup); + if (! (*(lookup++))) goto STRCPYTORANGECOMPLETE; // complete with terminating 0 } - break; - + + Stack[sp-6] = !(*lookup); // true/success if only terminating 0 was not copied + } + break; + case PCD_STRCPYTOMAPCHRANGE: + { + int a = STACK(5); + if (a < NUM_MAPVARS && a > 0 && + activeBehavior->MapVars[a]) + { + Stack[sp-6] = activeBehavior->CopyStringToArray(*(activeBehavior->MapVars[a]), index, capacity, lookup); + } + } + break; + case PCD_STRCPYTOWORLDCHRANGE: + { + int a = STACK(5); + + while (capacity-- > 0) + { + ACS_WorldArrays[a][index++] = *lookup; + if (! (*(lookup++))) goto STRCPYTORANGECOMPLETE; // complete with terminating 0 + } + + Stack[sp-6] = !(*lookup); // true/success if only terminating 0 was not copied + } + break; + case PCD_STRCPYTOGLOBALCHRANGE: + { + int a = STACK(5); + + while (capacity-- > 0) + { + ACS_GlobalArrays[a][index++] = *lookup; + if (! (*(lookup++))) goto STRCPYTORANGECOMPLETE; // complete with terminating 0 + } + + Stack[sp-6] = !(*lookup); // true/success if only terminating 0 was not copied + } + break; } sp -= 5; } diff --git a/src/p_acs.h b/src/p_acs.h index ec41ab886..02544e367 100644 --- a/src/p_acs.h +++ b/src/p_acs.h @@ -144,6 +144,51 @@ struct ProfileCollector int Index; }; +struct ACSLocalArrayInfo +{ + unsigned int Size; + int Offset; +}; + +struct ACSLocalArrays +{ + unsigned int Count; + ACSLocalArrayInfo *Info; + + ACSLocalArrays() + { + Count = 0; + Info = NULL; + } + ~ACSLocalArrays() + { + if (Info != NULL) + { + delete[] Info; + Info = NULL; + } + } + + // Bounds-checking Set and Get for local arrays + void Set(int *locals, int arraynum, int arrayentry, int value) + { + if ((unsigned int)arraynum < Count && + (unsigned int)arrayentry < Info[arraynum].Size) + { + locals[Info[arraynum].Offset + arrayentry] = value; + } + } + int Get(int *locals, int arraynum, int arrayentry) + { + if ((unsigned int)arraynum < Count && + (unsigned int)arrayentry < Info[arraynum].Size) + { + return locals[Info[arraynum].Offset + arrayentry]; + } + return 0; + } +}; + // The in-memory version struct ScriptPtr { @@ -153,6 +198,7 @@ struct ScriptPtr BYTE ArgCount; WORD VarCount; WORD Flags; + ACSLocalArrays LocalArrays; ACSProfileInfo ProfileData; }; @@ -189,7 +235,7 @@ struct ScriptFlagsPtr WORD Flags; }; -struct ScriptFunction +struct ScriptFunctionInFile { BYTE ArgCount; BYTE LocalCount; @@ -198,6 +244,16 @@ struct ScriptFunction DWORD Address; }; +struct ScriptFunction +{ + BYTE ArgCount; + BYTE HasReturnValue; + BYTE ImportNum; + int LocalCount; + DWORD Address; + ACSLocalArrays LocalArrays; +}; + // Script types enum { @@ -285,7 +341,7 @@ private: BYTE *Chunks; ScriptPtr *Scripts; int NumScripts; - BYTE *Functions; + ScriptFunction *Functions; ACSProfileInfo *FunctionProfileData; int NumFunctions; ArrayInfo *ArrayStore; @@ -694,8 +750,25 @@ public: PCD_SCRIPTWAITNAMED, PCD_TRANSLATIONRANGE3, PCD_GOTOSTACK, + PCD_ASSIGNSCRIPTARRAY, + PCD_PUSHSCRIPTARRAY, + PCD_ADDSCRIPTARRAY, + PCD_SUBSCRIPTARRAY, + PCD_MULSCRIPTARRAY, + PCD_DIVSCRIPTARRAY, +/*370*/ PCD_MODSCRIPTARRAY, + PCD_INCSCRIPTARRAY, + PCD_DECSCRIPTARRAY, + PCD_ANDSCRIPTARRAY, + PCD_EORSCRIPTARRAY, + PCD_ORSCRIPTARRAY, + PCD_LSSCRIPTARRAY, + PCD_RSSCRIPTARRAY, + PCD_PRINTSCRIPTCHARARRAY, + PCD_PRINTSCRIPTCHRANGE, +/*380*/ PCD_STRCPYTOSCRIPTCHRANGE, -/*363*/ PCODE_COMMAND_COUNT +/*381*/ PCODE_COMMAND_COUNT }; // Some constants used by ACS scripts