aboutsummaryrefslogtreecommitdiff
path: root/engines/sludge/objtypes.cpp
blob: 58572b0f92d16c0fb9c93f8f01a64e0c506a5813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
#include "allfiles.h"
#include "objtypes.h"
#include "variable.h"
#include "newfatal.h"
#include "moreio.h"
#include "fileset.h"
#include "CommonCode/version.h"

namespace Sludge {

objectType *allObjectTypes = NULL;
extern char *outputDir;

#define DEBUG_COMBINATIONS  0

bool initObjectTypes() {
	return true;
}

objectType *findObjectType(int i) {
	objectType *huntType = allObjectTypes;

	while (huntType) {
		if (huntType -> objectNum == i) return huntType;
		huntType = huntType -> next;
	}

	return loadObjectType(i);
}

objectType *loadObjectType(int i) {
#if ALLOW_FILE
	int a, nameNum;
	objectType *newType = new objectType;

	if (checkNew(newType)) {
		if (openObjectSlice(i)) {
			nameNum = get2bytes(bigDataFile);
			newType -> r = (byte) fgetc(bigDataFile);
			newType -> g = (byte) fgetc(bigDataFile);
			newType -> b = (byte) fgetc(bigDataFile);
			newType -> speechGap = fgetc(bigDataFile);
			newType -> walkSpeed = fgetc(bigDataFile);
			newType -> wrapSpeech = get4bytes(bigDataFile);
			newType -> spinSpeed = get2bytes(bigDataFile);

			if (gameVersion >= VERSION(1, 6)) {
				// aaLoad
				fgetc(bigDataFile);
				getFloat(bigDataFile);
				getFloat(bigDataFile);
			}

			if (gameVersion >= VERSION(1, 4)) {
				newType -> flags = get2bytes(bigDataFile);
			} else {
				newType -> flags = 0;
			}

			newType -> numCom = get2bytes(bigDataFile);
			newType -> allCombis = (newType -> numCom) ? new combination[newType -> numCom] : NULL;

#if DEBUG_COMBINATIONS
			FILE *callEventLog = fopen("callEventLog.txt", "at");
			if (callEventLog) {
				fprintf(callEventLog, "Object type %d has %d combinations... ", i, newType -> numCom);
			}
#endif

			for (a = 0; a < newType -> numCom; a ++) {
				newType -> allCombis[a].withObj = get2bytes(bigDataFile);
				newType -> allCombis[a].funcNum = get2bytes(bigDataFile);
#if DEBUG_COMBINATIONS
				if (callEventLog) {
					fprintf(callEventLog, "%d(%d) ", newType -> allCombis[a].withObj, newType -> allCombis[a].funcNum);
				}
#endif
			}
#if DEBUG_COMBINATIONS
			if (callEventLog) {
				fprintf(callEventLog, "\n");
				fclose(callEventLog);
			}
#endif
			finishAccess();
			newType -> screenName = getNumberedString(nameNum);
			newType -> objectNum = i;
			newType -> next = allObjectTypes;
			allObjectTypes = newType;
			return newType;
		}
	}
#endif
	return NULL;
}

#if ALLOW_FILE
objectType *loadObjectRef(FILE *fp) {
	objectType *r = loadObjectType(get2bytes(fp));
	delete r -> screenName;
	r -> screenName = readString(fp);
	return r;
}

void saveObjectRef(objectType *r, FILE *fp) {
	put2bytes(r -> objectNum, fp);
	writeString(r -> screenName, fp);
}
#endif

int getCombinationFunction(int withThis, int thisObject) {
	int i, num = 0;
	objectType *obj = findObjectType(thisObject);

#if DEBUG_COMBINATIONS
	FILE *callEventLog = fopen("callEventLog.txt", "at");
	if (callEventLog) {
		fprintf(callEventLog, "Combining %d and %d - ", thisObject, withThis);
	}
#endif

	for (i = 0; i < obj -> numCom; i ++) {
		if (obj -> allCombis[i].withObj == withThis) {
			num = obj -> allCombis[i].funcNum;
			break;
		}
	}

#if DEBUG_COMBINATIONS
	if (callEventLog) {
		fprintf(callEventLog, "got function number %d\n", num);
		fclose(callEventLog);
	}
#endif

	return num;
}

void removeObjectType(objectType *oT) {
	objectType * * huntRegion = & allObjectTypes;

	while (* huntRegion) {
		if ((* huntRegion) == oT) {
//			FILE * debuggy2 = fopen ("debug.txt", "at");
//			fprintf (debuggy2, "DELETING OBJECT TYPE: %p %s\n", oT, oT -> screenName);
//			fclose (debuggy2);

			* huntRegion = oT -> next;
			delete oT -> allCombis;
			delete oT -> screenName;
			delete oT;
			return;
		} else {
			huntRegion = & ((* huntRegion) -> next);
		}
	}
	fatal("Can't delete object type: bad pointer");
}

} // End of namespace Sludge