labyrinth

annotate src/level.c @ 6:345db647383a

fixed the unloadable so bug
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 03 May 2015 02:23:54 +0300
parents d46f0947a96d
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <ctype.h>
nuclear@0 5 #include <math.h>
nuclear@0 6 #include "opengl.h"
nuclear@0 7 #include "level.h"
nuclear@0 8
nuclear@0 9 #define C_WALL '#'
nuclear@0 10 #define C_PILLAR 'o'
nuclear@0 11 #define C_START 's'
nuclear@0 12 #define C_GOLD 'x'
nuclear@0 13
nuclear@0 14 #define IS_SOLID(x) ((x) == C_WALL)
nuclear@0 15
nuclear@5 16 #define DEF_LVL_SZ 5
nuclear@5 17 static const char def_lvl_cells[DEF_LVL_SZ][DEF_LVL_SZ] = {
nuclear@5 18 "#####",
nuclear@5 19 "##x##",
nuclear@5 20 "#o o#",
nuclear@5 21 "# s #",
nuclear@5 22 "#####"
nuclear@5 23 };
nuclear@5 24
nuclear@5 25
nuclear@5 26 static int default_level(struct level *lvl);
nuclear@5 27
nuclear@0 28
nuclear@0 29 void level_init(struct level *lvl)
nuclear@0 30 {
nuclear@0 31 memset(lvl, 0, sizeof *lvl);
nuclear@0 32 lvl->cell_size = 3.0;
nuclear@0 33 lvl->cell_height = 2.5;
nuclear@0 34 lvl->floor_tex_scale = lvl->wall_tex_scale = lvl->ceil_tex_scale = 1.0;
nuclear@0 35 }
nuclear@0 36
nuclear@5 37 static int default_level(struct level *lvl)
nuclear@5 38 {
nuclear@5 39 int i, j;
nuclear@5 40
nuclear@5 41 lvl->num_cells[0] = lvl->num_cells[1] = DEF_LVL_SZ;
nuclear@5 42
nuclear@5 43 for(i=0; i<DEF_LVL_SZ; i++) {
nuclear@5 44 for(j=0; j<DEF_LVL_SZ; j++) {
nuclear@5 45 char c = def_lvl_cells[i][j];
nuclear@5 46 lvl->cells[i][j] = c;
nuclear@5 47 if(c == 's') {
nuclear@5 48 lvl->start_pos[0] = j;
nuclear@5 49 lvl->start_pos[1] = i;
nuclear@5 50 } else if(c == 'x') {
nuclear@5 51 lvl->goal_pos[0] = j;
nuclear@5 52 lvl->goal_pos[1] = i;
nuclear@5 53 }
nuclear@5 54 }
nuclear@5 55 }
nuclear@5 56 return 0;
nuclear@5 57 }
nuclear@5 58
nuclear@0 59 static void clean_line(char *buf)
nuclear@0 60 {
nuclear@0 61 char *end = buf + strlen(buf) - 1;
nuclear@0 62
nuclear@0 63 if(end <= buf) return;
nuclear@0 64
nuclear@0 65 while(end >= buf && !isprint(*end)) {
nuclear@0 66 *end-- = 0;
nuclear@0 67 }
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 int level_load(struct level *lvl, const char *fname)
nuclear@0 71 {
nuclear@0 72 FILE *fp;
nuclear@0 73 char buf[256];
nuclear@0 74 int i, size[2], nlines;
nuclear@0 75
nuclear@0 76 if(!(fp = fopen(fname, "r"))) {
nuclear@0 77 fprintf(stderr, "failed to open file: %s\n", fname);
nuclear@5 78 return default_level(lvl);
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 if(!fgets(buf, sizeof buf, fp)) {
nuclear@0 82 fprintf(stderr, "level file %s is empty\n", fname);
nuclear@0 83 fclose(fp);
nuclear@5 84 return default_level(lvl);
nuclear@0 85 }
nuclear@0 86 if(sscanf(buf, "s %dx%d", size, size + 1) != 2) {
nuclear@0 87 fprintf(stderr, "level file %s doesn't start with size definition\n", fname);
nuclear@0 88 fclose(fp);
nuclear@5 89 return default_level(lvl);
nuclear@0 90 }
nuclear@0 91 if(size[0] > MAX_LEVEL_SIZE || size[1] > MAX_LEVEL_SIZE) {
nuclear@0 92 fprintf(stderr, "level size %dx%d is larger than compile-time maximum (%d)\n", size[0], size[1], MAX_LEVEL_SIZE);
nuclear@0 93 fclose(fp);
nuclear@5 94 return default_level(lvl);
nuclear@0 95 }
nuclear@0 96
nuclear@0 97 lvl->num_cells[0] = size[0];
nuclear@0 98 lvl->num_cells[1] = size[1];
nuclear@0 99
nuclear@0 100 nlines = 0;
nuclear@0 101 while(fgets(buf, sizeof buf, fp)) {
nuclear@0 102 if(nlines >= size[0]) {
nuclear@0 103 fprintf(stderr, "warning: level contains more lines than specified, ignoring the rest\n");
nuclear@0 104 break;
nuclear@0 105 }
nuclear@0 106 clean_line(buf);
nuclear@0 107
nuclear@0 108 for(i=0; buf[i]; i++) {
nuclear@0 109 if(i >= size[1]) {
nuclear@0 110 fprintf(stderr, "warning: line %d is longer than the level size definition says. Skipping the rest.\n", nlines + 1);
nuclear@0 111 break;
nuclear@0 112 }
nuclear@0 113 lvl->cells[nlines][i] = buf[i];
nuclear@0 114
nuclear@0 115 if(buf[i] == C_START) {
nuclear@0 116 lvl->start_pos[0] = i;
nuclear@0 117 lvl->start_pos[1] = nlines;
nuclear@0 118 printf("start cell found (%d,%d)\n", lvl->start_pos[0], lvl->start_pos[1]);
nuclear@0 119 }
nuclear@0 120 if(buf[i] == C_GOLD) {
nuclear@0 121 level_cell_to_pos(lvl, i, nlines, lvl->goal_pos, lvl->goal_pos + 1);
nuclear@0 122 printf("gold cell found (%d, %d)\n", i, nlines);
nuclear@0 123 }
nuclear@0 124 }
nuclear@0 125 nlines++;
nuclear@0 126 }
nuclear@0 127
nuclear@0 128 fclose(fp);
nuclear@0 129 return 0;
nuclear@0 130 }
nuclear@0 131
nuclear@0 132
nuclear@0 133 static int clamp(int x, int low, int high)
nuclear@0 134 {
nuclear@0 135 return x < low ? low : (x > high ? high : x);
nuclear@0 136 }
nuclear@0 137
nuclear@0 138 void level_pos_to_cell(struct level *lvl, float x, float y, int *res_cx, int *res_cy)
nuclear@0 139 {
nuclear@0 140 int cx = (int)(x / lvl->cell_size + 0.5);
nuclear@0 141 int cy = (int)(y / lvl->cell_size + 0.5);
nuclear@0 142
nuclear@0 143 *res_cx = clamp(cx, 0, lvl->num_cells[1] - 1);
nuclear@0 144 *res_cy = clamp(cy, 0, lvl->num_cells[0] - 1);
nuclear@0 145 }
nuclear@0 146
nuclear@0 147 void level_cell_to_pos(struct level *lvl, int cx, int cy, float *resx, float *resy)
nuclear@0 148 {
nuclear@0 149 cx = clamp(cx, 0, lvl->num_cells[1] - 1);
nuclear@0 150 cy = clamp(cy, 0, lvl->num_cells[0] - 1);
nuclear@0 151
nuclear@0 152 *resx = (float)cx * lvl->cell_size;
nuclear@0 153 *resy = (float)cy * lvl->cell_size;
nuclear@0 154 }
nuclear@0 155
nuclear@0 156 int level_cell(struct level *lvl, int cx, int cy)
nuclear@0 157 {
nuclear@0 158 cx = clamp(cx, 0, lvl->num_cells[1] - 1);
nuclear@0 159 cy = clamp(cy, 0, lvl->num_cells[0] - 1);
nuclear@0 160
nuclear@0 161 return lvl->cells[cy][cx];
nuclear@0 162 }
nuclear@0 163
nuclear@0 164 int level_cell_at(struct level *lvl, float x, float y)
nuclear@0 165 {
nuclear@0 166 int cx, cy;
nuclear@0 167 level_pos_to_cell(lvl, x, y, &cx, &cy);
nuclear@0 168 return level_cell(lvl, cx, cy);
nuclear@0 169 }
nuclear@0 170
nuclear@0 171 int level_obj_pos(struct level *lvl, int objname, float *resx, float *resy)
nuclear@0 172 {
nuclear@0 173 int i, j;
nuclear@0 174
nuclear@0 175 for(i=0; i<lvl->num_cells[0]; i++) {
nuclear@0 176 for(j=0; j<lvl->num_cells[1]; j++) {
nuclear@0 177 if(lvl->cells[i][j] == objname) {
nuclear@0 178 level_cell_to_pos(lvl, j, i, resx, resy);
nuclear@0 179 return 1;
nuclear@0 180 }
nuclear@0 181 }
nuclear@0 182 }
nuclear@0 183 return 0;
nuclear@0 184 }
nuclear@0 185
nuclear@1 186 /* NOTE: this function could be simpler */
nuclear@0 187 int level_collide(struct level *lvl, float rad, float x, float y, float *dxp, float *dyp)
nuclear@0 188 {
nuclear@0 189 int i, val, cxy[2], collided = 0;
nuclear@0 190 float pos[2], dir[2], center[2], len;
nuclear@0 191 int adj_cxy[2];
nuclear@0 192
nuclear@0 193 pos[0] = x;
nuclear@0 194 pos[1] = y;
nuclear@0 195 dir[0] = *dxp;
nuclear@0 196 dir[1] = *dyp;
nuclear@0 197
nuclear@0 198 /* clamp the direction magnitude (manhattan) to the cell size */
nuclear@0 199 for(i=0; i<2; i++) {
nuclear@0 200 if(dir[i] > lvl->cell_size) dir[i] = lvl->cell_size;
nuclear@0 201 if(dir[i] < -lvl->cell_size) dir[i] = -lvl->cell_size;
nuclear@0 202 }
nuclear@0 203
nuclear@0 204 level_pos_to_cell(lvl, pos[0], pos[1], cxy, cxy + 1);
nuclear@0 205 level_cell_to_pos(lvl, cxy[0], cxy[1], center, center + 1);
nuclear@0 206
nuclear@0 207 /* check collision and clamp dx/dy with each of the 4 directions */
nuclear@0 208 for(i=0; i<4; i++) {
nuclear@0 209 float wall_pos, dist;
nuclear@0 210 int axis = (i & 2) >> 1;
nuclear@0 211 int sign = (i & 1) ? -1 : 1;
nuclear@0 212
nuclear@0 213 adj_cxy[0] = cxy[0];
nuclear@0 214 adj_cxy[1] = cxy[1];
nuclear@0 215 adj_cxy[axis] += sign;
nuclear@0 216
nuclear@0 217 val = level_cell(lvl, adj_cxy[0], adj_cxy[1]);
nuclear@0 218
nuclear@0 219 if(!IS_SOLID(val)) continue;
nuclear@0 220
nuclear@0 221 wall_pos = center[axis] + (lvl->cell_size * 0.5 - rad) * (float)sign;
nuclear@0 222 dist = fabs(wall_pos - pos[axis]);
nuclear@0 223
nuclear@0 224 if(dir[axis] * (float)sign > dist) {
nuclear@0 225 dir[axis] = (float)sign * dist;
nuclear@0 226 collided = 1;
nuclear@0 227 }
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 /* finally to make sure we don't slip through cracks in corners, also check
nuclear@0 231 * the destination cell
nuclear@0 232 */
nuclear@0 233 len = sqrt(dir[0] * dir[0] + dir[1] * dir[1]);
nuclear@0 234 pos[0] += dir[0] + dir[0] / len * rad;
nuclear@0 235 pos[1] += dir[1] + dir[1] / len * rad;
nuclear@0 236
nuclear@0 237 val = level_cell_at(lvl, pos[0], pos[1]);
nuclear@0 238 if(IS_SOLID(val)) {
nuclear@0 239 dir[0] = dir[1] = 0;
nuclear@0 240 }
nuclear@0 241
nuclear@0 242
nuclear@0 243 *dxp = dir[0];
nuclear@0 244 *dyp = dir[1];
nuclear@0 245 return collided;
nuclear@0 246 }
nuclear@0 247
nuclear@0 248 static void wall_faces(float x, float y, float width, float height, float uscale, float vscale)
nuclear@0 249 {
nuclear@0 250 float u0 = 0.5 - 0.5 * uscale;
nuclear@0 251 float u1 = 0.5 + 0.5 * uscale;
nuclear@0 252 float v0 = 0.5 - 0.5 * vscale;
nuclear@0 253 float v1 = 0.5 + 0.5 * vscale;
nuclear@0 254
nuclear@0 255 width /= 2.0;
nuclear@0 256
nuclear@0 257 glNormal3f(0, 0, 1);
nuclear@0 258 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
nuclear@0 259 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y + width);
nuclear@0 260 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y + width);
nuclear@0 261 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
nuclear@0 262 glNormal3f(0, 0, -1);
nuclear@0 263 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y - width);
nuclear@0 264 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
nuclear@0 265 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
nuclear@0 266 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y - width);
nuclear@0 267 glNormal3f(1, 0, 0);
nuclear@0 268 glTexCoord2f(u0, v1); glVertex3f(x + width, 0, y + width);
nuclear@0 269 glTexCoord2f(u1, v1); glVertex3f(x + width, 0, y - width);
nuclear@0 270 glTexCoord2f(u1, v0); glVertex3f(x + width, height, y - width);
nuclear@0 271 glTexCoord2f(u0, v0); glVertex3f(x + width, height, y + width);
nuclear@0 272 glNormal3f(-1, 0, 0);
nuclear@0 273 glTexCoord2f(u0, v0); glVertex3f(x - width, height, y + width);
nuclear@0 274 glTexCoord2f(u1, v0); glVertex3f(x - width, height, y - width);
nuclear@0 275 glTexCoord2f(u1, v1); glVertex3f(x - width, 0, y - width);
nuclear@0 276 glTexCoord2f(u0, v1); glVertex3f(x - width, 0, y + width);
nuclear@0 277 }
nuclear@0 278
nuclear@0 279 void level_draw(struct level *lvl)
nuclear@0 280 {
nuclear@0 281 static int first = 1;
nuclear@0 282 int i, j, k;
nuclear@0 283
nuclear@0 284 set_mtl_diffuse(1, 1, 1, 1);
nuclear@0 285 set_mtl_specular(0, 0, 0);
nuclear@0 286
nuclear@0 287 glMatrixMode(GL_TEXTURE);
nuclear@0 288 glLoadIdentity();
nuclear@0 289 glScalef(lvl->wall_tex_scale, lvl->wall_tex_scale, lvl->wall_tex_scale);
nuclear@0 290
nuclear@0 291 glEnable(GL_TEXTURE_2D);
nuclear@0 292
nuclear@0 293 /* draw walls */
nuclear@0 294 glBindTexture(GL_TEXTURE_2D, lvl->wall_tex);
nuclear@0 295 glBegin(GL_QUADS);
nuclear@0 296 for(i=0; i<lvl->num_cells[0]; i++) {
nuclear@0 297 int cy = i;
nuclear@0 298 for(j=0; j<lvl->num_cells[1]; j++) {
nuclear@0 299 float x, y;
nuclear@0 300 int cx = j;
nuclear@0 301 int c = level_cell(lvl, cx, cy);
nuclear@0 302
nuclear@0 303 level_cell_to_pos(lvl, cx, cy, &x, &y);
nuclear@0 304
nuclear@0 305 if(c == C_WALL) {
nuclear@0 306 wall_faces(x, y, lvl->cell_size, lvl->cell_height, 1, 1);
nuclear@0 307 } else if(c == C_PILLAR) {
nuclear@0 308 wall_faces(x, y, 0.2, lvl->cell_height, 0.04, 1);
nuclear@0 309 }
nuclear@0 310
nuclear@0 311 if(first) putchar(c);
nuclear@0 312 }
nuclear@0 313 if(first) putchar('\n');
nuclear@0 314 }
nuclear@0 315 glEnd();
nuclear@0 316
nuclear@0 317 /* draw floor & ceiling */
nuclear@0 318 for(k=0; k<2; k++) {
nuclear@0 319 int do_floor = k == 0;
nuclear@0 320 float height, normy;
nuclear@0 321
nuclear@0 322 if(do_floor) {
nuclear@0 323 glBindTexture(GL_TEXTURE_2D, lvl->floor_tex);
nuclear@0 324 glScalef(lvl->floor_tex_scale, lvl->floor_tex_scale, lvl->floor_tex_scale);
nuclear@0 325 height = 0;
nuclear@0 326 normy = 1;
nuclear@0 327 glFrontFace(GL_CCW);
nuclear@0 328 } else {
nuclear@0 329 glBindTexture(GL_TEXTURE_2D, lvl->ceil_tex);
nuclear@0 330 glScalef(lvl->ceil_tex_scale, lvl->ceil_tex_scale, lvl->ceil_tex_scale);
nuclear@0 331 height = lvl->cell_height;
nuclear@0 332 normy = -1;
nuclear@0 333 glFrontFace(GL_CW);
nuclear@0 334 }
nuclear@0 335
nuclear@0 336 glBegin(GL_TRIANGLES);
nuclear@0 337 for(i=0; i<lvl->num_cells[0]; i++) {
nuclear@0 338 int cy = i;
nuclear@0 339 for(j=0; j<lvl->num_cells[1]; j++) {
nuclear@0 340 float x, y;
nuclear@0 341 int cx = j;
nuclear@0 342 int c = level_cell(lvl, cx, cy);
nuclear@0 343
nuclear@0 344 level_cell_to_pos(lvl, cx, cy, &x, &y);
nuclear@0 345
nuclear@0 346 if(c != C_WALL) {
nuclear@0 347 float hsz = lvl->cell_size / 2.0f;
nuclear@0 348
nuclear@0 349 glNormal3f(0, normy, 0);
nuclear@0 350 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
nuclear@0 351 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
nuclear@0 352 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
nuclear@0 353 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
nuclear@0 354 glTexCoord2f(1.0, 0.0); glVertex3f(x + hsz, height, y + hsz);
nuclear@0 355 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
nuclear@0 356 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
nuclear@0 357 glTexCoord2f(1.0, 1.0); glVertex3f(x + hsz, height, y - hsz);
nuclear@0 358 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
nuclear@0 359 glTexCoord2f(0.5, 0.5); glVertex3f(x, height, y);
nuclear@0 360 glTexCoord2f(0.0, 1.0); glVertex3f(x - hsz, height, y - hsz);
nuclear@0 361 glTexCoord2f(0.0, 0.0); glVertex3f(x - hsz, height, y + hsz);
nuclear@0 362 }
nuclear@0 363 }
nuclear@0 364 }
nuclear@0 365 glEnd();
nuclear@0 366 }
nuclear@0 367
nuclear@0 368 glFrontFace(GL_CCW); /* restore front-face mode */
nuclear@0 369 glDisable(GL_TEXTURE_2D);
nuclear@0 370
nuclear@0 371 glLoadIdentity(); /* restore the texture matrix to identity */
nuclear@0 372 glMatrixMode(GL_MODELVIEW);
nuclear@0 373
nuclear@0 374 /* draw other objects
nuclear@0 375 for(i=0; i<lvl->num_cells[0]; i++) {
nuclear@0 376 int cy = i;
nuclear@0 377 for(j=0; j<lvl->num_cells[1]; j++) {
nuclear@0 378 float x, y;
nuclear@0 379 int cx = j;
nuclear@0 380 int c = level_cell(lvl, cx, cy);
nuclear@0 381
nuclear@0 382 level_cell_to_pos(lvl, cx, cy, &x, &y);
nuclear@0 383
nuclear@0 384 switch(c) {
nuclear@0 385 case C_PILLAR:
nuclear@0 386 break;
nuclear@0 387 }
nuclear@0 388 }
nuclear@0 389 }
nuclear@0 390 */
nuclear@0 391
nuclear@0 392 first = 0;
nuclear@0 393 }