Include and link physfs properly.
[tuxanci.git] / src / client / saveLoad.c
blob899ef25ba60a02fae2b7c0c1eea629a5892afaaf
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
6 #include "textFile.h"
7 #include "homeDirector.h"
9 #include "arena.h"
10 #include "arenaFile.h"
11 #include "tux.h"
12 #include "shot.h"
13 #include "item.h"
14 #include "modules.h"
16 #include "saveLoad.h"
18 #include "choiceArena.h"
19 #include "world.h"
21 static void action_saveTux(space_t *space, tux_t *tux, textFile_t *textFile)
23 char str[STR_PROTO_SIZE];
25 snprintf(str, STR_PROTO_SIZE,
26 "TUX %d %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d",
27 tux->id, tux->x, tux->y, tux->status, tux->position,
28 tux->control, tux->frame, tux->score, tux->name, tux->gun,
29 tux->bonus, tux->shot[GUN_SIMPLE], tux->shot[GUN_DUAL_SIMPLE],
30 tux->shot[GUN_SCATTER], tux->shot[GUN_TOMMY],
31 tux->shot[GUN_LASSER], tux->shot[GUN_MINE],
32 tux->shot[GUN_BOMBBALL], tux->bonus_time, tux->pickup_time);
34 list_add(textFile->text, strdup(str));
37 static void action_saveShot(space_t *space, shot_t *shot, textFile_t *textFile)
39 char str[STR_PROTO_SIZE];
41 snprintf(str, STR_PROTO_SIZE, "SHOT %d %d %d %d %d %d %d %d %d",
42 shot->id, shot->x, shot->y, shot->px, shot->py,
43 shot->position, shot->gun, shot->author_id,
44 shot->isCanKillAuthor);
46 list_add(textFile->text, strdup(str));
49 static void action_saveItem(space_t *space, item_t *item, textFile_t *textFile)
51 char str[STR_PROTO_SIZE];
53 snprintf(str, STR_PROTO_SIZE, "ITEM %d %d %d %d %d %d %d",
54 item->id, item->type, item->x, item->y,
55 item->count, item->frame, item->author_id);
57 list_add(textFile->text, strdup(str));
60 static void saveContextArenaToTextFile(textFile_t *textFile, arena_t *arena)
62 char str[STR_PROTO_SIZE];
64 sprintf(str, "ARENA %s %d %d",
65 arena_file_get_net_name(choice_arena_get()),
66 arena->countRound, arena->max_countRound);
68 list_add(textFile->text, strdup(str));
70 space_action(arena->spaceTux, action_saveTux, textFile);
71 space_action(arena->spaceShot, action_saveShot, textFile);
72 space_action(arena->spaceItem, action_saveItem, textFile);
75 void save_arena(char *filename, arena_t *arena)
77 textFile_t *textFile;
78 char path[STR_PATH_SIZE];
80 sprintf(path, "%s%s%s.sav", home_director_get(), PATH_SEPARATOR, filename);
82 debug("Saving game [%s]", path);
84 textFile = text_file_new(path);
86 if (textFile != NULL) {
87 saveContextArenaToTextFile(textFile, arena);
88 text_file_save(textFile);
89 text_file_destroy(textFile);
90 } else {
91 error("Unable to save game [%s]", path);
95 static arena_t *load_arenaFromLine(char *line)
97 char cmd[STR_SIZE];
98 char name[STR_SIZE];
99 int countRound;
100 int max_countRound;
101 arena_t *arena;
103 sscanf(line, "%s %s %d %d", cmd, name, &countRound, &max_countRound);
105 world_set_arena(arena_file_get_file_format_net_name(name));
107 arena = arena_get_current();
108 arena->max_countRound = max_countRound;
109 arena->countRound = countRound;
111 return arena;
114 static void loadTuxFromLine(char *line, arena_t *arena)
116 char cmd[STR_PROTO_SIZE];
117 char name[STR_NAME_SIZE];
118 int id, x, y, status, position, frame, score, control;
119 int myGun, myBonus;
120 int gun1, gun2, gun3, gun4, gun5, gun6, gun7;
121 int time1, time2;
122 tux_t *tux;
124 sscanf(line,
125 "%s %d %d %d %d %d %d %d %d %s %d %d %d %d %d %d %d %d %d %d %d",
126 cmd, &id, &x, &y, &status, &position, &control, &frame, &score,
127 name, &myGun, &myBonus, &gun1, &gun2, &gun3, &gun4, &gun5, &gun6,
128 &gun7, &time1, &time2);
130 tux = tux_new();
131 tux_replace_id(tux, id);
132 space_add(arena->spaceTux, tux);
134 tux->control = control;
136 if (tux->control == TUX_CONTROL_KEYBOARD_RIGHT) {
137 world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_RIGHT);
140 if (tux->control == TUX_CONTROL_KEYBOARD_LEFT) {
141 world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_LEFT);
144 if (tux->control == TUX_CONTROL_AI) {
145 module_load("libmodAI");
146 world_set_control_tux(tux, TUX_CONTROL_KEYBOARD_LEFT);
149 space_move_object(arena->spaceTux, tux, x, y);
150 tux->status = status;
151 tux->position = position;
152 tux->frame = frame;
153 tux->score = score;
154 strcpy(tux->name, name);
155 tux->gun = myGun;
156 tux->bonus = myBonus;
157 tux->shot[GUN_SIMPLE] = gun1;
158 tux->shot[GUN_DUAL_SIMPLE] = gun2;
159 tux->shot[GUN_SCATTER] = gun3;
160 tux->shot[GUN_TOMMY] = gun4;
161 tux->shot[GUN_LASSER] = gun5;
162 tux->shot[GUN_MINE] = gun6;
163 tux->shot[GUN_BOMBBALL] = gun7;
164 tux->bonus_time = time1;
165 tux->pickup_time = time2;
168 static void loadShotFromLine(char *line, arena_t *arena)
170 char cmd[STR_PROTO_SIZE];
171 int x, y, px, py, position, gun, shot_id, author_id, isCanKillAuthor;
172 shot_t *shot;
174 sscanf(line, "%s %d %d %d %d %d %d %d %d %d",
175 cmd, &shot_id, &x, &y, &px, &py, &position, &gun, &author_id,
176 &isCanKillAuthor);
178 shot = shot_new(x, y, px, py, gun, author_id);
180 shot_replace_id(shot, shot_id);
181 shot->isCanKillAuthor = isCanKillAuthor;
182 shot->position = position;
184 if (shot->gun == GUN_LASSER) {
185 shot_transform_lasser(shot);
188 space_add(arena->spaceShot, shot);
191 static void loadItemFromLine(char *line, arena_t *arena)
193 char cmd[STR_PROTO_SIZE];
194 int id, type, x, y, count, frame, author_id, check_id;
195 item_t *item;
197 sscanf(line, "%s %d %d %d %d %d %d %d %d",
198 cmd, &id, &type, &x, &y, &count, &frame, &author_id, &check_id);
200 item = item_new(x, y, type, author_id);
202 item_replace_id(item, id);
203 item->count = count;
204 item->frame = frame;
206 space_add(arena->spaceItem, item);
209 static arena_t *loadContextArenaFromTextFile(textFile_t *textFile)
211 arena_t *arena;
212 int i;
214 arena = NULL;
216 for (i = 0; i < textFile->text->count; i++) {
217 char *line;
219 line = (char *) textFile->text->list[i];
221 if (strncmp(line, "ARENA", 5) == 0) {
222 arena = load_arenaFromLine(line);
225 if (strncmp(line, "TUX", 3) == 0) {
226 loadTuxFromLine(line, arena);
229 if (strncmp(line, "SHOT", 4) == 0) {
230 loadShotFromLine(line, arena);
233 if (strncmp(line, "ITEM", 4) == 0) {
234 loadItemFromLine(line, arena);
238 return arena;
241 void load_arena(char *filename)
243 textFile_t *textFile;
244 char path[STR_PATH_SIZE];
246 sprintf(path, "%s%s%s", home_director_get(), PATH_SEPARATOR, filename);
248 debug("Loading arena [%s]", path);
250 textFile = text_file_load(path);
252 if (textFile != NULL) {
253 loadContextArenaFromTextFile(textFile);
254 text_file_destroy(textFile);
255 } else {
256 error("Unable to load saved game [%s]", path);