int startNewFunctionNum (unsigned int funcNum, unsigned int numParamsExpected, loadedFunction * calledBy, variableStack * & vStack, bool returnSommet) { loadedFunction * newFunc = new loadedFunction; checkNew (newFunc); newFunc -> originalNumber = funcNum; loadFunctionCode (newFunc); if (newFunc -> numArgs != (int)numParamsExpected) return fatal ("Wrong number of parameters!"); if (newFunc -> numArgs > newFunc -> numLocals) return fatal ("More arguments than local variable space!"); // Now, lets copy the parameters from the calling function's stack... while (numParamsExpected) { numParamsExpected --; if (vStack == NULL) return fatal ("Corrupted file! The stack's empty and there were still parameters expected"); copyVariable (vStack -> thisVar, newFunc -> localVars[numParamsExpected]); trimStack (vStack); } newFunc -> cancelMe = false; newFunc -> timeLeft = 0; newFunc -> returnSomething = returnSommet; newFunc -> calledBy = calledBy; newFunc -> stack = NULL; newFunc -> freezerLevel = 0; newFunc -> runThisLine = 0; newFunc -> isSpeech = 0; initVarNew (newFunc -> reg); restartFunction (newFunc); return 1; }
LoadedFunction *loadFunction(Common::SeekableReadStream *stream) { int a; // Reserve memory... LoadedFunction *buildFunc = new LoadedFunction; if (!checkNew(buildFunc)) return NULL; // See what it was called by and load if we need to... buildFunc->originalNumber = stream->readUint16BE(); buildFunc->calledBy = NULL; if (stream->readByte()) { buildFunc->calledBy = loadFunction(stream); if (!buildFunc->calledBy) return NULL; } buildFunc->timeLeft = stream->readUint32LE(); buildFunc->runThisLine = stream->readUint16BE(); buildFunc->freezerLevel = 0; buildFunc->cancelMe = stream->readByte(); buildFunc->returnSomething = stream->readByte(); buildFunc->isSpeech = stream->readByte(); loadVariable(&(buildFunc->reg), stream); loadFunctionCode(buildFunc); buildFunc->stack = loadStack(stream, NULL); for (a = 0; a < buildFunc->numLocals; a++) { loadVariable(&(buildFunc->localVars[a]), stream); } return buildFunc; }