Skip to content

Commit

Permalink
CRUISE: Reduce the scope of variables used in the for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Strangerke committed May 31, 2014
1 parent 7d24e14 commit 1dbcd74
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 221 deletions.
10 changes: 3 additions & 7 deletions engines/cruise/actor.cpp
Expand Up @@ -426,16 +426,13 @@ void explore(int depart, int arrivee) {
}

void chemin0(int depart, int arrivee) {
int i;
//int y=30;

prem = 0;
prem2 = 0;
dist_chemin = 0;
idsol = 0;
solmax = 999;

for (i = 0; i < 20 + 1; i++)
for (int i = 0; i < 20 + 1; i++)
fl[i] = -1;

X = 0, Y = 30;
Expand Down Expand Up @@ -582,10 +579,9 @@ int16 computePathfinding(MovementEntry &moveInfo, int16 x, int16 y, int16 destX,
}

if (!flagCt) {
int i;
int16 *ptr;

for (i = 0; i < NUM_PERSONS; i++) { // 10 = num perso
int i = 0;
for (; i < NUM_PERSONS; i++) { // 10 = num perso
if (!persoTable[i]) {
break;
}
Expand Down
4 changes: 1 addition & 3 deletions engines/cruise/background.cpp
Expand Up @@ -65,12 +65,10 @@ int loadCVT(uint8 **ptr) {
char *localPtr = (char *) * ptr;

if (!strcmp(localPtr, "CVT")) {
int i;
localPtr += 4;

for (i = 0; i < 0x20; i++) {
for (int i = 0; i < 0x20; i++)
cvtPalette[i] = *(localPtr++);
}

*ptr = (uint8 *) localPtr;

Expand Down
30 changes: 9 additions & 21 deletions engines/cruise/cruise_main.cpp
Expand Up @@ -110,7 +110,6 @@ void loadPackedFileToMem(int fileIdx, uint8 *buffer) {
int getNumObjectsByClass(int scriptIdx, int param) {
objDataStruct *ptr2;
int counter;
int i;

if (!overlayTable[scriptIdx].ovlData)
return (0);
Expand All @@ -125,7 +124,7 @@ int getNumObjectsByClass(int scriptIdx, int param) {

counter = 0;

for (i = 0; i < overlayTable[scriptIdx].ovlData->numObj; i++) {
for (int i = 0; i < overlayTable[scriptIdx].ovlData->numObj; i++) {
if (ptr2[i]._class == param) {
counter++;
}
Expand All @@ -135,15 +134,12 @@ int getNumObjectsByClass(int scriptIdx, int param) {
}

void resetFileEntryRange(int start, int count) {
int i;

for (i = 0; i < count; ++i)
for (int i = 0; i < count; ++i)
resetFileEntry(start + i);
}

int getProcParam(int overlayIdx, int param2, const char *name) {
int numSymbGlob;
int i;
exportEntryStruct *arraySymbGlob;
char *exportNamePtr;
char exportName[80];
Expand All @@ -161,7 +157,7 @@ int getProcParam(int overlayIdx, int param2, const char *name) {
if (!exportNamePtr)
return 0;

for (i = 0; i < numSymbGlob; i++) {
for (int i = 0; i < numSymbGlob; i++) {
if (arraySymbGlob[i].var4 == param2) {
Common::strlcpy(exportName, arraySymbGlob[i].offsetToName + exportNamePtr, sizeof(exportName));

Expand All @@ -188,9 +184,7 @@ void changeScriptParamInList(int param1, int param2, scriptInstanceStruct *pScri
}

void initBigVar3() {
int i;

for (i = 0; i < NUM_FILE_ENTRIES; i++) {
for (int i = 0; i < NUM_FILE_ENTRIES; i++) {
if (filesDatabase[i].subData.ptr) {
MemFree(filesDatabase[i].subData.ptr);
}
Expand Down Expand Up @@ -338,13 +332,12 @@ void removeExtention(const char *name, char *buffer) { // not like in original
int lastFileSize;

int loadFileSub1(uint8 **ptr, const char *name, uint8 *ptr2) {
int i;
char buffer[256];
int fileIdx;
int unpackedSize;
uint8 *unpackedBuffer;

for (i = 0; i < 64; i++) {
for (int i = 0; i < 64; i++) {
if (preloadData[i].ptr) {
if (!strcmp(preloadData[i].name, name)) {
error("Unsupported code in loadFIleSub1");
Expand Down Expand Up @@ -440,8 +433,6 @@ uint8 *mainProc14(uint16 overlay, uint16 idx) {
}

void CruiseEngine::initAllData() {
int i;

setupFuncArray();
initOverlayTable();

Expand All @@ -457,15 +448,13 @@ void CruiseEngine::initAllData() {

menuTable[0] = NULL;

for (i = 0; i < 2000; i++) {
for (int i = 0; i < 2000; i++)
globalVars[i] = 0;
}

for (i = 0; i < 8; i++) {
for (int i = 0; i < 8; i++)
backgroundTable[i].name[0] = 0;
}

for (i = 0; i < NUM_FILE_ENTRIES; i++) {
for (int i = 0; i < NUM_FILE_ENTRIES; i++) {
filesDatabase[i].subData.ptr = NULL;
filesDatabase[i].subData.ptrMask = NULL;
}
Expand Down Expand Up @@ -963,12 +952,11 @@ bool findRelation(int objOvl, int objIdx, int x, int y) {
bool found = false;
bool first = true;
int testState = -1;
int j;
int16 objectState;

getSingleObjectParam(objOvl, objIdx, 5, &objectState);

for (j = 1; j < numOfLoadedOverlay; j++) {
for (int j = 1; j < numOfLoadedOverlay; j++) {
if (overlayTable[j].alreadyLoaded) {
int idHeader = overlayTable[j].ovlData->numMsgRelHeader;

Expand Down
5 changes: 2 additions & 3 deletions engines/cruise/ctp.cpp
Expand Up @@ -95,7 +95,6 @@ void getWalkBoxCenter(int n, int16 table[][40]) {
// ax dx bx
void renderCTPWalkBox(int16 *walkboxData, int hotPointX, int hotPointY, int X, int Y, int scale) {
int numPoints;
int i;
int16 *destination;

int startX = X - ((upscaleValue(hotPointX, scale) + 0x8000) >> 16);
Expand All @@ -105,7 +104,7 @@ void renderCTPWalkBox(int16 *walkboxData, int hotPointX, int hotPointY, int X, i

destination = polyBuffer2;

for (i = 0; i < numPoints; i++) {
for (int i = 0; i < numPoints; i++) {
int pointX = *(walkboxData++);
int pointY = *(walkboxData++);

Expand All @@ -119,7 +118,7 @@ void renderCTPWalkBox(int16 *walkboxData, int hotPointX, int hotPointY, int X, i
m_color = 0;
ctpVarUnk = 0;

for (i = 0; i < numPoints; i++) {
for (int i = 0; i < numPoints; i++) {
walkboxTable[i] = i;
}

Expand Down
14 changes: 5 additions & 9 deletions engines/cruise/dataLoader.cpp
Expand Up @@ -170,11 +170,11 @@ int createResFileEntry(int width, int height, int size, int resType) {
return 0; // for compilers that don't support NORETURN

#if 0
int i;
int entryNumber;
int div = 0;

for (i = 0; i < NUM_FILE_ENTRIES; i++) {
int i = 0;
for (; i < NUM_FILE_ENTRIES; i++) {
if (!filesDatabase[i].subData.ptr)
break;
}
Expand Down Expand Up @@ -280,10 +280,9 @@ int loadFileRange(const char *name, int startIdx, int currentEntryIdx, int numId

switch (fileType) {
case type_SET: {
int i;
int numMaxEntriesInSet = getNumMaxEntiresInSet(ptr);

for (i = 0; i < numIdx; i++) {
for (int i = 0; i < numIdx; i++) {
if ((startIdx + i) > numMaxEntriesInSet) {
MemFree(ptr);
return 0; // exit if limit is reached
Expand Down Expand Up @@ -325,12 +324,9 @@ int loadFullBundle(const char *name, int startIdx) {
switch (fileType) {
case type_SET: {
// Sprite set
int i;
int numMaxEntriesInSet;
int numMaxEntriesInSet = getNumMaxEntiresInSet(ptr); // get maximum number of sprites/animations in SET file

numMaxEntriesInSet = getNumMaxEntiresInSet(ptr); // get maximum number of sprites/animations in SET file

for (i = 0; i < numMaxEntriesInSet; i++) {
for (int i = 0; i < numMaxEntriesInSet; i++) {
loadSetEntry(name, ptr, i, startIdx + i);
}

Expand Down
39 changes: 13 additions & 26 deletions engines/cruise/decompiler.cpp
Expand Up @@ -130,13 +130,12 @@ void pushDecomp(char *string, ...) {

void resolveDecompShort(char *buffer) {
ovlData3Struct *data3Ptr = currentScript;
int i;

importScriptStruct *importEntry =
(importScriptStruct *)(data3Ptr->dataPtr +
data3Ptr->offsetToImportData);

for (i = 0; i < data3Ptr->numRelocGlob; i++) {
for (int i = 0; i < data3Ptr->numRelocGlob; i++) {
switch (importEntry->type) {
case 20: // script
case 30:
Expand Down Expand Up @@ -177,13 +176,12 @@ void resolveDecompShort(char *buffer) {

void resolveDecompChar(char *buffer) {
ovlData3Struct *data3Ptr = currentScript;
int i;

importScriptStruct *importEntry =
(importScriptStruct *)(data3Ptr->dataPtr +
data3Ptr->offsetToImportData);

for (i = 0; i < data3Ptr->numRelocGlob; i++) {
for (int i = 0; i < data3Ptr->numRelocGlob; i++) {
switch (importEntry->type) {
default: {
if (importEntry->offset ==
Expand Down Expand Up @@ -315,9 +313,7 @@ void resolveVarName(char *ovlIdxString, int varType, char *varIdxString,
}

if (!strcmp(ovlIdxString, "0")) {
int i;

for (i = 0; i < currentDecompOvl->numSymbGlob; i++) {
for (int i = 0; i < currentDecompOvl->numSymbGlob; i++) {
if (varIdx == currentDecompOvl->arraySymbGlob[i].idx) {
if (((currentDecompOvl->arraySymbGlob[i].var4 & 0xF0) == 0) && varType != 0x20) { // var
strcpy(outputName,
Expand Down Expand Up @@ -733,7 +729,6 @@ int decompFunction() {
unsigned long int numArg = atoi(popDecomp());
char *ovlStr;
char *idxStr;
int i;
char functionName[100];

idxStr = popDecomp();
Expand All @@ -743,7 +738,7 @@ int decompFunction() {

sprintf(tempbuffer, "_startASync(%s", functionName);

for (i = 0; i < numArg; i++) {
for (int i = 0; i < numArg; i++) {
strcatuint8(tempbuffer, ",");
strcatuint8(tempbuffer, popDecomp());
}
Expand Down Expand Up @@ -1094,7 +1089,6 @@ int decompFunction() {
unsigned long int numArg = atoi(popDecomp());
char *ovlStr;
char *idxStr;
int i;
char functionName[256];

idxStr = popDecomp();
Expand All @@ -1104,7 +1098,7 @@ int decompFunction() {

sprintf(tempbuffer, "%s(", functionName);

for (i = 0; i < numArg; i++) {
for (int i = 0; i < numArg; i++) {
if (i)
strcatuint8(tempbuffer, ",");
strcatuint8(tempbuffer, popDecomp());
Expand Down Expand Up @@ -1174,14 +1168,13 @@ int decompFunction() {
unsigned long int numArg = atoi(popDecomp());
char *ovlStr;
char *idxStr;
int i;

idxStr = popDecomp();
ovlStr = popDecomp();

sprintf(tempbuffer, "_op_6F(%s,%s", idxStr, ovlStr);

for (i = 0; i < numArg; i++) {
for (int i = 0; i < numArg; i++) {
strcatuint8(tempbuffer, ",");
strcatuint8(tempbuffer, popDecomp());
}
Expand Down Expand Up @@ -1286,9 +1279,7 @@ int decompBreak() {
}

void generateIndentation() {
int i, j;

for (i = 0; i < positionInDecompileLineTable; i++) {
for (int i = 0; i < positionInDecompileLineTable; i++) {
if (decompileLineTable[i].type != 0) {
char *gotoStatement;
int destLine;
Expand All @@ -1302,7 +1293,7 @@ void generateIndentation() {
destLine = atoi(gotoStatement);
destLineIdx = -1;

for (j = 0; j < positionInDecompileLineTable; j++) {
for (int j = 0; j < positionInDecompileLineTable; j++) {
if (decompileLineTable[j].lineOffset == destLine) {
destLineIdx = j;
break;
Expand All @@ -1312,7 +1303,7 @@ void generateIndentation() {
assert(destLineIdx != -1);

if (destLineIdx > i) {
for (j = i + 1; j < destLineIdx; j++) {
for (int j = i + 1; j < destLineIdx; j++) {
decompileLineTable[j].indent++;
}

Expand All @@ -1328,7 +1319,6 @@ void generateIndentation() {
void dumpScript(uint8 *ovlName, ovlDataStruct *ovlData, int idx) {
uint8 opcodeType;
char buffer[256];
int i;

char temp[256];
char scriptName[256];
Expand Down Expand Up @@ -1367,9 +1357,8 @@ void dumpScript(uint8 *ovlName, ovlDataStruct *ovlData, int idx) {

decompileStackPosition = 0;

for (i = 0; i < 64; i++) {
for (int i = 0; i < 64; i++)
decompOpcodeTypeTable[i] = NULL;
}

decompOpcodeTypeTable[1] = decompLoadVar;
decompOpcodeTypeTable[2] = decompSaveVar;
Expand Down Expand Up @@ -1412,9 +1401,7 @@ void dumpScript(uint8 *ovlName, ovlDataStruct *ovlData, int idx) {

generateIndentation();

for (i = 0; i < positionInDecompileLineTable; i++) {
int j;

for (int i = 0; i < positionInDecompileLineTable; i++) {
if (decompileLineTable[i].pendingElse) {
fprintf(fHandle, "%05d:\t",
decompileLineTable[i].lineOffset);
Expand All @@ -1423,9 +1410,9 @@ void dumpScript(uint8 *ovlName, ovlDataStruct *ovlData, int idx) {
}

fprintf(fHandle, "%05d:\t", decompileLineTable[i].lineOffset);
for (j = 0; j < decompileLineTable[i].indent; j++) {
for (int j = 0; j < decompileLineTable[i].indent; j++)
fprintf(fHandle, "\t");
}

fprintf(fHandle, "%s", decompileLineTable[i].line);
fprintf(fHandle, "\n");
}
Expand Down

0 comments on commit 1dbcd74

Please sign in to comment.