-
Notifications
You must be signed in to change notification settings - Fork 36
/
profile.cpp
268 lines (216 loc) · 5.16 KB
/
profile.cpp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "nx.h"
#include "profile.h"
#include "profile.fdh"
#define PF_WEAPONS_OFFS 0x38
#define PF_CURWEAPON_OFFS 0x24
#define PF_INVENTORY_OFFS 0xD8
#define PF_TELEPORTER_OFFS 0x158
#define PF_FLAGS_OFFS 0x218
#define MAX_WPN_SLOTS 8
#define MAX_TELE_SLOTS 8
// load savefile #num into the given Profile structure.
bool profile_load(const char *pfname, Profile *file)
{
int i, curweaponslot;
FILE *fp;
stat("Loading profile from %s...", pfname);
memset(file, 0, sizeof(Profile));
fp = fileopen(pfname, "rb");
if (!fp)
{
staterr("profile_load: unable to open '%s'", pfname);
return 1;
}
if (!fverifystring(fp, "Do041220"))
{
staterr("profile_load: invalid savegame format: '%s'", pfname);
fclose(fp);
return 1;
}
file->stage = fgetl(fp);
file->songno = fgetl(fp);
file->px = fgetl(fp);
file->py = fgetl(fp);
file->pdir = CVTDir(fgetl(fp));
file->maxhp = fgeti(fp);
file->num_whimstars = fgeti(fp);
file->hp = fgeti(fp);
fgeti(fp); // unknown value
curweaponslot = fgetl(fp); // current weapon (slot, not number, converted below)
fgetl(fp); // unknown value
file->equipmask = fgetl(fp); // equipped items
// load weapons
fseek(fp, PF_WEAPONS_OFFS, SEEK_SET);
for(i=0;i<MAX_WPN_SLOTS;i++)
{
int type = fgetl(fp);
if (!type) break;
if (type < 0 || type >= WPN_COUNT) {
staterr("profile_load: invalid weapon type %d", type);
break;
}
int level = fgetl(fp);
int xp = fgetl(fp);
int maxammo = fgetl(fp);
int ammo = fgetl(fp);
file->weapons[type].hasWeapon = true;
file->weapons[type].level = (level - 1);
file->weapons[type].xp = xp;
file->weapons[type].ammo = ammo;
file->weapons[type].maxammo = maxammo;
if (i == curweaponslot)
{
file->curWeapon = type;
}
}
// load inventory
file->ninventory = 0;
fseek(fp, PF_INVENTORY_OFFS, SEEK_SET);
for(i=0;i<MAX_INVENTORY;i++)
{
int item = fgetl(fp);
if (!item) break;
file->inventory[file->ninventory++] = item;
}
// load teleporter slots
file->num_teleslots = 0;
fseek(fp, PF_TELEPORTER_OFFS, SEEK_SET);
for(i=0;i<NUM_TELEPORTER_SLOTS;i++)
{
int slotno = fgetl(fp);
int scriptno = fgetl(fp);
if (slotno == 0) break;
file->teleslots[file->num_teleslots].slotno = slotno;
file->teleslots[file->num_teleslots].scriptno = scriptno;
file->num_teleslots++;
}
// load flags
fseek(fp, PF_FLAGS_OFFS, SEEK_SET);
if (!fverifystring(fp, "FLAG"))
{
staterr("profile_load: missing 'FLAG' marker");
fclose(fp);
return 1;
}
fresetboolean();
for(i=0;i<NUM_GAMEFLAGS;i++)
{
file->flags[i] = fbooleanread(fp);
}
fclose(fp);
return 0;
}
bool profile_save(const char *pfname, Profile *file)
{
FILE *fp;
int i;
//stat("Writing saved game to %s...", pfname);
fp = fileopen(pfname, "wb");
if (!fp)
{
staterr("profile_save: unable to open %s", pfname);
return 1;
}
fputstringnonull("Do041220", fp);
fputl(file->stage, fp);
fputl(file->songno, fp);
fputl(file->px, fp);
fputl(file->py, fp);
fputl((file->pdir == RIGHT) ? 2:0, fp);
fputi(file->maxhp, fp);
fputi(file->num_whimstars, fp);
fputi(file->hp, fp);
fseek(fp, 0x2C, SEEK_SET);
fputi(file->equipmask, fp);
// save weapons
fseek(fp, PF_WEAPONS_OFFS, SEEK_SET);
int slotno = 0, curweaponslot = 0;
for(i=0;i<WPN_COUNT;i++)
{
if (file->weapons[i].hasWeapon)
{
fputl(i, fp);
fputl(file->weapons[i].level + 1, fp);
fputl(file->weapons[i].xp, fp);
fputl(file->weapons[i].maxammo, fp);
fputl(file->weapons[i].ammo, fp);
if (i == file->curWeapon)
curweaponslot = slotno;
slotno++;
if (slotno >= MAX_WPN_SLOTS) break;
}
}
if (slotno < MAX_WPN_SLOTS)
fputl(0, fp); // 0-type weapon: terminator
// go back and save slot no of current weapon
fseek(fp, PF_CURWEAPON_OFFS, SEEK_SET);
fputl(curweaponslot, fp);
// save inventory
fseek(fp, PF_INVENTORY_OFFS, SEEK_SET);
for(i=0;i<file->ninventory;i++)
{
fputl(file->inventory[i], fp);
}
fputl(0, fp);
// write teleporter slots
fseek(fp, PF_TELEPORTER_OFFS, SEEK_SET);
for(i=0;i<MAX_TELE_SLOTS;i++)
{
if (i < file->num_teleslots)
{
fputl(file->teleslots[i].slotno, fp);
fputl(file->teleslots[i].scriptno, fp);
}
else
{
fputl(0, fp);
fputl(0, fp);
}
}
// write flags
fseek(fp, PF_FLAGS_OFFS, SEEK_SET);
fputstringnonull("FLAG", fp);
fresetboolean();
for(i=0;i<NUM_GAMEFLAGS;i++)
{
fbooleanwrite(file->flags[i], fp);
}
fbooleanflush(fp);
fclose(fp);
return 0;
}
/*
void c------------------------------() {}
*/
// returns the filename for a save file given it's number
const char *GetProfileName(int num)
{
#ifndef __HAIKU__
if (num == 0)
return "profile.dat";
else
return stprintf("profile%d.dat", num+1);
#else
char path[PATH_MAX];
char *haikuPath = getHaikuSettingsPath();
strcpy(path, haikuPath);
if (num == 0) {
strcat(path, "profile.dat");
} else {
strcat(path, "profile%d.dat");
}
free(haikuPath);
return stprintf(path, num+1);
#endif
}
// returns whether the given save file slot exists
bool ProfileExists(int num)
{
return file_exists(GetProfileName(num));
}
bool AnyProfileExists()
{
for(int i=0;i<MAX_SAVE_SLOTS;i++)
if (ProfileExists(i)) return true;
return false;
}