Nilorea Library

- Main Page
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros
n_worldmap.c
Go to the documentation of this file.
1
15 #include "n_worldmap.h"
16
17
18
19 N_WORLDMAP *load_worldmap_config( char *config )
20 {
21 N_WORLDMAP *world = NULL ;
22 FILE *in = NULL ;
23 char tmpstr[ 256 ];
24
25 in = fopen( config , "r" );
26 if( !in )
27 {
28 return NULL ;
29 }
30
31 Malloc( world , N_WORLDMAP , 1 );
32
33 fill_str( tmpstr , 0 , 256 );
34
35 /* reading comment */
36 fgets( tmpstr , 256 , in );
37 /* reading world name */
38 fgets( tmpstr , 256 , in );
39 world -> name = strdup( tmpstr );
40
41 /* reading comment */
42 fgets( tmpstr , 256 , in );
43 /* reading data path */
44 fgets( tmpstr , 256 , in );
45 world -> datapath = strdup( tmpstr );
46
47 /* reading comment */
48 fgets( tmpstr , 256 , in );
49 /* reading box X */
50 fgets( tmpstr , 256 , in );
51 world -> box_xsize = atoi( tmpstr );
52
53 /* reading comment */
54 fgets( tmpstr , 256 , in );
55 /* reading box Y */
56 fgets( tmpstr , 256 , in );
57 world -> box_ysize = atoi( tmpstr );
58
59 /* reading comment */
60 fgets( tmpstr , 256 , in );
61 /* reading box Z */
62 fgets( tmpstr , 256 , in );
63 world -> box_zsize = atoi( tmpstr );
64
65 /* reading comment */
66 fgets( tmpstr , 256 , in );
67 /* reading cell width */
68 fgets( tmpstr , 256 , in );
69 world -> cell_width = atoi( tmpstr );
70
71 /* reading comment */
72 fgets( tmpstr , 256 , in );
73 /* reading cell height */
74 fgets( tmpstr , 256 , in );
75 world -> cell_height = atoi( tmpstr );
76
77 /* reading comment */
78 fgets( tmpstr , 256 , in );
79 /* reading cell depth */
80 fgets( tmpstr , 256 , in );
81 world -> cell_depth = atoi( tmpstr );
82
83 world -> box_width = world -> box_xsize * world -> cell_width ;
84 world -> box_height = world -> box_ysize * world -> cell_height ;
85 world -> box_depth = world -> box_zsize * world -> cell_depth ;
86 world -> box_nb_cells = world -> box_xsize * world -> box_ysize * world -> box_zsize ;
87
88 world -> box_list = new_generic_list( -1 );
89 _assert( world -> box_list , return NULL );
90 world -> draw_list = new_generic_list( -1 );
91 _assert( world -> draw_list , return NULL );
92
93 return world ;
94 }
95
96 N_MAP_BOX *load_box( N_WORLDMAP *world , int x , int y , int z )
97 {
98 N_MAP_BOX *map_box = NULL ;
99 char name[1024] ;
100
101 struct stat filestat ;
102
103 sprintf( name , "%s%d_%d_%d.dat" , world -> datapath , x , y , z );
104
105 Malloc( map_box , N_MAP_BOX , 1 );
106 map_box -> x = x ;
107 map_box -> y = y ;
108 map_box -> z = z ;
109 Malloc( map_box -> cell , N_MAP_CELL ** , world -> box_nb_cells );
110
111 if( stat( name , &filestat ) == -1 )
112 {
113 /* it's a new, unvisited box */
114 save_box( world , map_box );
115 }
116 else
117 {
118 /* load existing box */
119 FILE *in = fopen( name , "r" );
120 fread( map_box -> cell , sizeof( N_MAP_CELL ) , world -> box_nb_cells , in );
121 fclose( in );
122 }
123
124 map_box -> draw_id = 0 ;
125
126 /* add it to world storage */
127 list_push( world -> box_list , map_box );
128
129 return map_box ;
130 }
131
132
133
134 int save_box( N_WORLDMAP *world , N_MAP_BOX *box )
135 {
136 char name[1024] ;
137
138 sprintf( name , "%s%d_%d_%d.dat" , world -> datapath , box -> x , box -> y , box -> z );
139
140 FILE *out = fopen( name , "w" );
141 fwrite( box -> cell , sizeof( N_MAP_CELL ) , world -> box_nb_cells , out);
142 fclose( out );
143 return TRUE ;
144 }
145
146
147
148 int unload_box( N_WORLDMAP *world , N_MAP_BOX **box )
149 {
150 LIST_NODE *node = world -> box_list -> start ;
151
152 while( node && node -> ptr != (*box) )
153 {
154 node = node -> next ;
155 }
156 if( node )
157 {
158 remove_list_node( world -> box_list , node , N_MAP_BOX );
159 }
160 Free( (*box) -> cell );
161 Free( (*box) );
162
163 return TRUE ;
164 }
165
166 int draw_cell( N_WORLDMAP *world , int x , int y , int z )
167 {
168 float x1 = x * world -> cell_width ;
169 float y1 = y * world -> cell_height ;
170 float z1 = z * world -> cell_depth ;
171
172 float x2 = x1 + world -> cell_width ;
173 float y2 = y1 ;
174 float z2 = z1 ;
175
176 float x3 = x1 + world -> cell_width ;
177 float y3 = y1 + world -> cell_height ;
178 float z3 = z1 ;
179
180 float x4 = x1 ;
181 float y4 = y1 + world -> cell_height;
182 float z4 = z1 ;
183
184 float x5 = x1 ;
185 float y5 = y1 ;
186 float z5 = z1 - world -> cell_width ;
187
188 float x6 = x1 + world -> cell_width ;
189 float y6 = y1 ;
190 float z6 = z1 - world -> cell_width;
191
192 float x7 = x1 + world -> cell_width ;
193 float y7 = y1 + world -> cell_height;
194 float z7 = z1 - world -> cell_width;
195
196 float x8 = x1 ;
197 float y8 = y1 + world -> cell_height;
198 float z8 = z1 - world -> cell_width;
199
200 // front
201 glVertex3f( x1 , y1 , z1 );
202 glVertex3f( x2 , y2 , z2 );
203 glVertex3f( x3 , y3 , z3 );
204 glVertex3f( x4 , y4 , z4 );
205 // back
206 glVertex3f( x5 , y5 , z5 );
207 glVertex3f( x6 , y6 , z6 );
208 glVertex3f( x7 , y7 , z7 );
209 glVertex3f( x8 , y8 , z8 );
210 // right
211 glVertex3f( x2 , y2 , z2 );
212 glVertex3f( x6 , y6 , z6 );
213 glVertex3f( x7 , y7 , z7 );
214 glVertex3f( x3 , y3 , z3 );
215 // left
216 glVertex3f( x1 , y1 , z1 );
217 glVertex3f( x5 , y5 , z5 );
218 glVertex3f( x8 , y8 , z8 );
219 glVertex3f( x4 , y4 , z4 );
220 // top
221 glVertex3f( x4 , y4 , z4 );
222 glVertex3f( x3 , y3 , z3 );
223 glVertex3f( x7 , y7 , z7 );
224 glVertex3f( x8 , y8 , z8 );
225 // bottom
226 glVertex3f( x1 , y1 , z1 );
227 glVertex3f( x2 , y2 , z2 );
228 glVertex3f( x6 , y6 , z6 );
229 glVertex3f( x5 , y5 , z5 );
230
231 return TRUE ;
232 }
233
234
235 int update_cell_draw_list( N_WORLDMAP *world , N_MAP_BOX *box )
236 {
237 if( box -> draw_id == 0 )
238 {
239 box -> draw_id = glGenLists( 1 );
240 }
241 else
242 {
243 glDeleteLists( box -> draw_id , 1 );
244 box -> draw_id = glGenLists( 1 );
245 }
246
247 glNewList( box -> draw_id , GL_COMPILE );
248 glBegin(GL_QUADS);
249
250 for( int x = 0 ; x < world -> box_xsize ; x ++ )
251 {
252 for( int y = 0 ; x < world -> box_ysize ; y ++ )
253 {
254 for( int z = 0 ; x < world -> box_zsize ; z ++ )
255 {
256 /* ignore empty cells */
257 if( box -> cell[ x ][ y ][ z ] . exist == 0 )
258 continue ;
259
260 /* ignore surrounded cells */
261 int surrounded = 1 ;
262 if( ( x - 1 ) > 0 && box -> cell[ x - 1 ][ y ][ z ] . exist == 0 )
263 surrounded = 0 ;
264 if( ( x + 1 ) < world -> box_xsize && box -> cell[ x + 1 ][ y ][ z ] . exist == 0 )
265 surrounded = 0 ;
266 if( ( y - 1 ) > 0 && box -> cell[ x ][ y - 1 ][ z ] . exist == 0 )
267 surrounded = 0 ;
268 if( ( y + 1 ) < world -> box_ysize && box -> cell[ x ][ y + 1 ][ z ] . exist == 0 )
269 surrounded = 0 ;
270 if( ( z - 1 ) > 0 && box -> cell[ x ][ y ][ z - 1 ] . exist == 0 )
271 surrounded = 0 ;
272 if( ( z + 1 ) < world -> box_xsize && box -> cell[ x ][ y ][ z + 1 ] . exist == 0 )
273 surrounded = 0 ;
274
275 if( surrounded == 1 )
276 continue ;
277
278 /* add cell to display list */
279 draw_cell( world , x , y , z );
280 }
281 }
282 }
283
284 glEnd();
285 glEndList();
286
287 return TRUE ;
288 }
289
290
291 int update_box_draw_list( N_WORLDMAP *world )
292 {
293 LIST_NODE *node = world -> box_list -> start ;
294
295 list_empty( world -> draw_list );
296
297 while( node )
298 {
299 N_MAP_BOX *boxptr = node -> ptr ;
300 if( boxptr )
301 {
302 /* add visibility check here */
303 list_push( world -> draw_list , boxptr );
304 }
305 node = node -> next ;
306 }
307 return TRUE ;
308 }
309
310 int draw_world( N_WORLDMAP *world )
311 {
312 LIST_NODE *ptr = world -> box_list -> start ;
313
314 while( ptr )
315 {
316
317 ptr = ptr -> next ;
318 }
319 return TRUE ;
320 }