eobish

view src/level.c @ 7:6a350c554e46

started DOS port
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jan 2015 15:49:14 +0200
parents ce0548d24918
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <math.h>
6 #if defined(_MSC_VER) || defined(__WATCOMC__)
7 #include <malloc.h>
8 #else
9 #include <alloca.h>
10 #endif
11 #include "level.h"
13 #define C_WALL '#'
14 #define C_START 's'
15 #define C_GOAL 'x'
17 #define IS_SOLID(x) ((x) == C_WALL)
19 static void clean_line(char *buf);
22 void level_init(struct level *lvl)
23 {
24 memset(lvl, 0, sizeof *lvl);
25 }
27 void level_destroy(struct level *lvl)
28 {
29 if(lvl) {
30 destroy_tileset(&lvl->tileset);
31 }
32 }
34 int level_load(struct level *lvl, const char *fname)
35 {
36 FILE *fp;
37 char buf[256], *dir, *slash;
38 int i, size[2], nlines;
40 dir = alloca(strlen(fname) + 1);
41 strcpy(dir, fname);
42 if((slash = strrchr(dir, '/'))) {
43 slash[1] = 0;
44 } else {
45 *dir = 0;
46 }
48 if(!(fp = fopen(fname, "r"))) {
49 fprintf(stderr, "failed to open file: %s\n", fname);
50 return -1;
51 }
53 nlines = 0;
54 while(fgets(buf, sizeof buf, fp)) {
55 clean_line(buf);
57 if(memcmp(buf, "@s", 2) == 0) {
58 if(sscanf(buf, "@s %dx%d", size, size + 1) != 2) {
59 fprintf(stderr, "level file %s doesn't start with size definition\n", fname);
60 fclose(fp);
61 return -1;
62 }
63 if(size[0] > MAX_LEVEL_SIZE || size[1] > MAX_LEVEL_SIZE) {
64 fprintf(stderr, "level size %dx%d is larger than compile-time maximum (%d)\n", size[0], size[1], MAX_LEVEL_SIZE);
65 fclose(fp);
66 return -1;
67 }
69 lvl->num_cells[0] = size[0];
70 lvl->num_cells[1] = size[1];
71 continue;
73 } else if(memcmp(buf, "@t", 2) == 0) {
74 char *path, *tname = buf + 3;
76 while(*tname && isspace(*tname)) ++tname;
78 path = alloca(strlen(tname) + strlen(dir) + 2);
79 sprintf(path, "%s%s", dir, tname);
81 if(load_tileset(&lvl->tileset, path) == -1) {
82 fprintf(stderr, "failed to load tileset for level %s\n", fname);
83 fclose(fp);
84 return -1;
85 }
86 continue;
87 }
89 if(nlines >= size[0]) {
90 fprintf(stderr, "warning: level contains more lines than specified, ignoring the rest\n");
91 break;
92 }
94 for(i=0; buf[i]; i++) {
95 if(i >= size[1]) {
96 fprintf(stderr, "warning: line %d is longer than the level size definition says. Skipping the rest.\n", nlines + 1);
97 break;
98 }
99 lvl->cells[nlines][i] = buf[i];
101 if(buf[i] == C_START) {
102 lvl->start_pos[0] = i;
103 lvl->start_pos[1] = nlines;
104 printf("start cell found (%d,%d)\n", lvl->start_pos[0], lvl->start_pos[1]);
105 }
106 if(buf[i] == C_GOAL) {
107 lvl->goal_pos[0] = i;
108 lvl->goal_pos[1] = nlines;
109 printf("gold cell found (%d, %d)\n", i, nlines);
110 }
111 }
112 nlines++;
113 }
115 fclose(fp);
116 return 0;
117 }
119 static int clamp(int x, int low, int high)
120 {
121 return x < low ? low : (x > high ? high : x);
122 }
124 int level_cell(struct level *lvl, int cx, int cy)
125 {
126 cx = clamp(cx, 0, lvl->num_cells[1] - 1);
127 cy = clamp(cy, 0, lvl->num_cells[0] - 1);
129 return lvl->cells[cy][cx];
130 }
133 static void clean_line(char *buf)
134 {
135 char *end = buf + strlen(buf) - 1;
137 if(end <= buf) return;
139 while(end >= buf && !isprint(*end)) {
140 *end-- = 0;
141 }
142 }