/*======================================================================*/ ScriptEntry *scriptOf(int actor) { ScriptEntry *scr; if (admin[actor].script != 0) { for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) if (scr->code == admin[actor].script) break; if (!isEndOfArray(scr)) return scr; } return NULL; }
/*======================================================================*/ bool equalParameterArrays(Parameter parameters1[], Parameter parameters2[]) { int i; if ((parameters1 == NULL) != (parameters2 == NULL)) return FALSE; if (parameters1 == NULL) // Because then parameter2 is also NULL return TRUE; for (i = 0; i < lengthOfParameterArray(parameters1); i++) { if (isEndOfArray(¶meters2[i])) return FALSE; if (parameters1[i].instance != parameters2[i].instance) return FALSE; } return isEndOfArray(¶meters2[i]); }
/*======================================================================*/ bool inParameterArray(Parameter theArray[], Aword theCode) { int i; for (i = 0; !isEndOfArray(&theArray[i]) && theArray[i].instance != theCode; i++); return (theArray[i].instance == theCode); }
/*======================================================================*/ bool passesContainerLimits(Aint theContainer, Aint theAddedInstance) { LimitEntry *limit; Aword props; if (!isAContainer(theContainer)) syserr("Checking limits for a non-container."); /* Find the container properties */ props = instances[theContainer].container; if (containers[props].limits != 0) { /* Any limits at all? */ for (limit = (LimitEntry *) pointerTo(containers[props].limits); !isEndOfArray(limit); limit++) if (limit->atr == 1-I_COUNT) { /* TODO This is actually some encoding of the attribute number, right? */ if (countInContainer(theContainer) >= limit->val) { interpret(limit->stms); return(FALSE); } } else { if (sumAttributeInContainer(theContainer, limit->atr) + getInstanceAttribute(theAddedInstance, limit->atr) > limit->val) { interpret(limit->stms); return(FALSE); } } } return(TRUE); }
/* A parameter position with code == 0 means this is a multiple position. * We must loop over this position (and replace it by each present in the * matched list) */ int findMultiplePosition(Parameter parameters[]) { // TODO: this should look at the isAll and isExplicitMultiple flags instead int multiplePosition; for (multiplePosition = 0; !isEndOfArray(¶meters[multiplePosition]); multiplePosition++) if (parameters[multiplePosition].instance == 0) return multiplePosition; return -1; }
/*======================================================================*/ void printParameterArray(Parameter parameters[]) { int i; printf("["); for (i = 0; !isEndOfArray(¶meters[i]); i++) { printf("%d ", (int)parameters[i].instance); } printf("]\n"); }
/*======================================================================*/ void addParameter(Parameter theArrayPosition[], Parameter *theParameter) { if (theArrayPosition == NULL) syserr("Adding to null parameter array"); if (isEndOfArray(&theArrayPosition[0])) clearParameter(&theArrayPosition[0], NULL); copyParameter(&theArrayPosition[0], theParameter); setEndOfArray(&theArrayPosition[1]); }
/*======================================================================*/ void intersectParameterArrays(Parameter one[], Parameter other[]) { int i, last = 0; for (i = 0; !isEndOfArray(&one[i]); i++) if (inParameterArray(other, one[i].instance)) one[last++] = one[i]; setEndOfArray(&one[last]); }
/*======================================================================*/ void copyReferencesToParameterArray(Aint references[], Parameter parameterArray[]) { int i; for (i = 0; !isEndOfArray(&references[i]); i++) { parameterArray[i].instance = references[i]; parameterArray[i].firstWord = EOF; /* Ensure that there is no word that can be used */ } setEndOfArray(¶meterArray[i]); }
/*======================================================================*/ int lengthOfParameterArray(Parameter theArray[]) { int i = 0; if (theArray == NULL) return 0; while (!isEndOfArray(&theArray[i])) i++; return i; }
/*----------------------------------------------------------------------*/ static AttributeEntry *findAttribute(AttributeEntry *attributeTable, int attributeCode) { AttributeEntry *attribute = attributeTable; while (attribute->code != attributeCode) { attribute++; if (isEndOfArray(attribute)) syserr("Attribute not found."); } return attribute; }
/*======================================================================*/ void subtractParameterArrays(Parameter theArray[], Parameter remove[]) { int i; if (remove == NULL) return; for (i = 0; !isEndOfArray(&theArray[i]); i++) if (inParameterArray(remove, theArray[i].instance)) theArray[i].instance = 0; /* Mark empty */ compressParameterArray(theArray); }
/*----------------------------------------------------------------------*/ static void saveSets(AFILE saveFile) { SetInitEntry *initEntry; if (header->setInitTable != 0) for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); !isEndOfArray(initEntry); initEntry++) { Set *attr = (Set *)getInstanceSetAttribute(initEntry->instanceCode, initEntry->attributeCode); fwrite((void *)&attr->size, sizeof(attr->size), 1, saveFile); fwrite((void *)attr->members, sizeof(attr->members[0]), attr->size, saveFile); } }
/*======================================================================*/ void copyParameterArray(Parameter to[], Parameter from[]) { int i; if (to == NULL && from == NULL) return; if (to == NULL) syserr("Copying to null parameter array"); setEndOfArray(&to[0]); for (i = 0; !isEndOfArray(&from[i]); i++) addParameter(&to[i], &from[i]); }
/*----------------------------------------------------------------------*/ static void saveStrings(AFILE saveFile) { StringInitEntry *initEntry; if (header->stringInitTable != 0) for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); !isEndOfArray(initEntry); initEntry++) { char *attr = (char *)getInstanceStringAttribute(initEntry->instanceCode, initEntry->attributeCode); Aint length = strlen(attr) + 1; fwrite((void *)&length, sizeof(length), 1, saveFile); fwrite((void *)attr, 1, length, saveFile); } }
/*----------------------------------------------------------------------*/ static void restoreStrings(AFILE saveFile) { StringInitEntry *initEntry; if (header->stringInitTable != 0) for (initEntry = (StringInitEntry *)pointerTo(header->stringInitTable); !isEndOfArray(initEntry); initEntry++) { Aint length; char *string; fread((void *)&length, sizeof(Aint), 1, saveFile); string = allocate(length+1); fread((void *)string, 1, length, saveFile); setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, (Aptr)string); } }
/*======================================================================*/ bool checksFailed(Aaddr adr, bool execute) { CheckEntry *chk = (CheckEntry *) pointerTo(adr); if (chk->exp == 0) { if (execute == EXECUTE_CHECK_BODY_ON_FAIL) interpret(chk->stms); return TRUE; } else { while (!isEndOfArray(chk)) { if (!evaluate(chk->exp)) { if (execute == EXECUTE_CHECK_BODY_ON_FAIL) interpret(chk->stms); return TRUE; } chk++; } return FALSE; } }
/*----------------------------------------------------------------------*/ static void showAttributes(AttributeEntry *attributes) { AttributeEntry *at; int i; char str[80]; if (attributes == 0) return; i = 1; for (at = attributes; !isEndOfArray(at); at++) { sprintf(str, "$i$t%s[%d] = %d", (char *) pointerTo(at->id), at->code, (int)at->value); #if ISO == 0 fromIso(str, str); #endif output(str); i++; } }
/*----------------------------------------------------------------------*/ static void restoreSets(AFILE saveFile) { SetInitEntry *initEntry; if (header->setInitTable != 0) for (initEntry = (SetInitEntry *)pointerTo(header->setInitTable); !isEndOfArray(initEntry); initEntry++) { Aint setSize; Set *set; int i; fread((void *)&setSize, sizeof(setSize), 1, saveFile); set = newSet(setSize); for (i = 0; i < setSize; i++) { Aword member; fread((void *)&member, sizeof(member), 1, saveFile); addToSet(set, member); } setInstanceAttribute(initEntry->instanceCode, initEntry->attributeCode, (Aptr)set); } }
/*======================================================================*/ Parameter *findEndOfParameterArray(Parameter *parameters) { Parameter *parameter; for (parameter = parameters; !isEndOfArray(parameter); parameter++); return parameter; }