-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameloader.c
executable file
·92 lines (85 loc) · 2.61 KB
/
gameloader.c
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
/*
Copyright 2017-2020 Anthony Cohn-Richardby
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "gameloader.h"
#include "entity.h"
#include "creature.h"
#include "ground.h"
#include "utility.h"
#include "sound.h"
// Make sure the file is seeked to the start of the types section.
void
load_game(FILE *fp)
{
char *line;
int i = 0;
all_entities = malloc(100 * sizeof (struct ent));
all_creatures = malloc(100 * sizeof (struct ctr));
all_grounds = malloc(100 * sizeof (struct grnd));
all_sound_types = malloc(100 * sizeof (struct sound));
while (*(line = read_line(fp)) != '\t') {
printf("loading entity %d with data '%s'\n", i, line);
char **splits = split_line(line, '\t');
char *name = splits[0];
char *icon = splits[1];
char *weight = splits[2];
char *size_class = splits[3];
all_entities[i] = new_entity(name, "No description", *icon, intval(weight), entity_size(size_class));
++i;
}
i = 0;
while (*(line = read_line(fp)) != '\t') {
printf("loading ground %d with data '%s'\n", i, line);
char **splits = split_line(line, '\t');
char *name = splits[0];
char *icon = splits[1];
all_grounds[i] = new_ground(name, *icon);
++i;
}
i = 0;
while (*(line = read_line(fp)) != '\t') {
printf("loading creature %d with data '%s'\n", i, line);
char **splits = split_line(line, '\t');
char *name = splits[0];
char *health = splits[1];
char *tp = splits[2];
char *inventory_size = splits[3];
all_creatures[i] = new_creature(name, "No description", intval(health), intval(tp), (size_t) intval(inventory_size));
++i;
}
i = 0;
while (*(line = read_line(fp)) != '\t') {
printf("loading sound %d with data '%s'\n", i, line);
char **splits = split_line(line, '\t');
char *id = splits[0];
char *text = splits[1];
all_sound_types[i].id = id;
all_sound_types[i].text = text;
++i;
}
all_sound_types_count = i;
}
int
intval(char *s)
{
int len = strlen(s);
int v = 0;
for (int i = 0; i < len; ++i) {
v += pow(10, len - i - 1) * ((int) s[i] - 48);
}
return v;
}