-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
173 lines (154 loc) · 4.99 KB
/
database.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
#include "database.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
/***
* Reads the character database
* @pre The format for the database is:
* first non-comment, non-empty line is the heading
* ignore comments (//) and empty lines
****/
static int read_chars() {
ifstream in;
char l[MAX_DB_LINE_LENGTH]; // line being read
//GLint field_count = 5; // how many fields there are in a row, TODO: use for flexible db reading
GLint counter = 0;
// Try opening the file
in.open("db\\chars\\chars.txt");
if (in == 0) {
exit_glut("Does character database exist?");
return 1;
}
// read db into array
while (!in.eof()) {
in.getline(l,(streamsize)MAX_DB_LINE_LENGTH);
#ifdef DEBUG_VALUE
//printf("%s", l);
#endif
// Ignore comments and whitespaces
if (l[0] == '\0' || l[0] == '\n' || l[0] == ' ' ||
(l[0] == '/' && l[1] == '/') ||
(l[0] == 'i' && l[1] == 'd'))
{
continue;
}
unit *p = new unit();
if (p == 0) cerr << "Memory Allocation error. read_chars()" << endl;
// Reading attributes
sscanf(l, "%d %s %s %lf,%lf,%lf %d",
&(p->id),
p->name,
p->tag,
&(p->position[0]),&(p->position[1]),&(p->position[2]),
&(p->map_id));
units[counter++] = p;
#ifdef DEBUG_VALUE
printf("Character ID: %d Name: %s Tag: %s Position: %d,%d,%d Map ID: %d\n",p->id,p->name,p->tag,(int)p->position[0],(int)p->position[1],(int)p->position[2],p->map_id);
#endif
}
#ifdef DEBUG_VALUE
//printf("Character Database parsed.\n");
#endif
in.close();
return 0;
}
static int read_maps() {
ifstream in;
char l[MAX_DB_LINE_LENGTH]; // line being read
//GLint field_count = 5; // how many fields there are in a row, TODO: use for flexible db reading
GLint counter = 0;
// Try opening the file
in.open("db\\maps\\maps.txt");
if (in == 0) {
exit_glut("Does map database exist?");
return 1;
}
// read db into array
while (!in.eof()) {
in.getline(l,(streamsize)MAX_DB_LINE_LENGTH);
#ifdef DEBUG_VALUE
//printf("%s", l);
#endif
// Ignore comments and whitespaces
if (l[0] == '\0' || l[0] == '\n' || l[0] == ' ' ||
(l[0] == '/' && l[1] == '/') ||
(l[0] == 'i' && l[1] == 'd'))
{
continue;
}
area *a = new area();
if (a == 0) fputs("Memory Allocation error. read_maps()\n", stderr);
// Reading attributes
sscanf(l, "%d %s %lf,%lf,%lf %d",
&(a->id),
a->name,
&(a->dimension[0]),&(a->dimension[1]),&(a->dimension[2]),
&(a->location));
areas[counter++] = a;
#ifdef DEBUG_VALUE
printf("Area ID: %d Name: %s Dimension: %d,%d,%d Location: %d\n",a->id,a->name,(int)a->dimension[0],(int)a->dimension[1],(int)a->dimension[2],a->location);
#endif
}
#ifdef DEBUG_VALUE
//printf("Map Database parsed.\n");
#endif
in.close();
return 0;
}
static int read_items() {
ifstream in;
char l[MAX_DB_LINE_LENGTH]; // line being read
//GLint field_count = 5; // how many fields there are in a row, TODO: use for flexible db reading
GLint counter = 0;
// Try opening the file
in.open("db\\items\\items.txt");
if (in == 0) {
exit_glut("Does map database exist?");
return 1;
}
// read db into array
while (!in.eof()) {
in.getline(l,(streamsize)MAX_DB_LINE_LENGTH);
#ifdef DEBUG_VALUE
//printf("%s", l);
#endif
// Ignore comments and whitespaces
if (l[0] == '\0' || l[0] == '\n' || l[0] == ' ' ||
(l[0] == '/' && l[1] == '/') ||
(l[0] == 'i' && l[1] == 'd'))
{
continue;
}
item *o = new item();
if (o == 0) fputs("Memory Allocation error. read_items()\n", stderr);
// Reading attributes
sscanf(l, "%d %s %lf,%lf,%lf %lf,%lf,%lf %d",
&(o->id),
o->name,
&(o->dimension[0]),&(o->dimension[1]),&(o->dimension[2]),
&(o->position[0]),&(o->position[1]),&(o->position[2]),
&(o->map_id));
items[counter++] = o;
#ifdef DEBUG_VALUE
printf("Item ID: %d Name: %s Dimension: %d,%d,%d Position: %d,%d,%d Location: %d\n",o->id,o->name,(int)o->dimension[0],(int)o->dimension[1],(int)o->dimension[2],(int)o->position[0],(int)o->position[1],(int)o->position[2],o->map_id);
#endif
}
#ifdef DEBUG_VALUE
//printf("Item Database parsed.\n");
#endif
in.close();
return 0;
}
void read_db() {
#ifdef DEBUG_VALUE
//char cur_dir[256];
//GetCurrentDirectory(256, cur_dir);
//printf("in read_db(): %s\n", cur_dir);
#endif
GLint charsRead = read_chars();
GLint mapsRead = read_maps();
GLint itemsRead = read_items();
if (charsRead || mapsRead || itemsRead) {
exit_glut("Error reading database!");
}
}