]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_map.c
cannot work on this much more i gotta do school project and final
[16.git] / src / lib / 16_map.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2017 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 \r
23 #include "src/lib/16_map.h"\r
24 \r
25 // Ideally, preprocess json during compilation and read serialized data\r
26 \r
27 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {\r
28         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&\r
29                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {\r
30                 return 0;\r
31         }\r
32         return -1;\r
33 }\r
34 \r
35 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {\r
36         int i, j, k, indent=0, inner_end;\r
37         char *s;\r
38         boolean objlay=0;\r
39         //bitmap_t bp;\r
40 \r
41         i = 0;\r
42 #define MAPLNAMESIZE t[i+1].end - t[i+1].start\r
43         while(i<count) {\r
44                 if(jsoneq(js, &(t[i]), "layers") == 0) {\r
45                         i++;\r
46                         inner_end = t[i].end;\r
47                         k = 0;\r
48                         while(t[i].start < inner_end) {\r
49 //#define DEBUG_DUMPVARS\r
50 //#ifdef DEBUG_DUMPVARS\r
51 //                              printf("t[%d].start=%d, %d\n", i, t[i].start, inner_end);\r
52 //#endif\r
53                                 if(!objlay){\r
54                                 if(jsoneq(js, &(t[i]), "data") == 0) {\r
55 #ifdef DEBUG_MAPDATA\r
56                                         printf("Layer %d data: (size is %d)[\n", k, t[i+1].size);\r
57 #endif\r
58                                         map->layerdata[k].data = malloc(sizeof(byte) * t[i+1].size);//TODO: USE MM_ CA_ AND PM_\r
59                                         for(j = 0; j < t[i+1].size; j++) {\r
60                                                 map->layerdata[k].data[j] = (byte)atoi(js + t[i+2+j].start);\r
61 #ifdef DEBUG_MAPDATA\r
62                                                 printf("%c",  map->layerdata[k].data[j]+44);\r
63 #endif\r
64                                         }\r
65                                         i += j + 2;\r
66 #ifdef DEBUG_MAPDATA\r
67                                         puts("\n]");\r
68 #endif\r
69                                 }else if(jsoneq(js, &(t[i]), "name") == 0) {\r
70 #ifdef DEBUG_MAPVARS\r
71                                         printf("Layer %d's name: (size is %d)[\n", k, MAPLNAMESIZE);\r
72 #endif\r
73                                         //map->layerdata[k].layername = malloc(sizeof(byte) * MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
74                                         strncpy(&(map->layerdata[k].layername), js+t[i+1].start, MAPLNAMESIZE);\r
75                                         if((map->layerdata[k].layername[MAPLNAMESIZE])!=0) map->layerdata[k].layername[MAPLNAMESIZE]='\0';\r
76                                         if(strstr(&map->layerdata[k].layername, "ob")) objlay=1;\r
77 #ifdef DEBUG_MAPDATA\r
78                                         printf("%s", map->layerdata[k].layername);\r
79                                         printf("\n]\n");\r
80 #endif\r
81                                         k++;\r
82                                 }\r
83                                 }else{ //objlay\r
84                                         if(jsoneq(js, &(t[i]), "objects") == 0) {\r
85 #ifdef DEBUG_OBVARS\r
86                                                 printf("objects detected\n");\r
87 #endif\r
88 #ifdef DEBUG_OBVARS\r
89 \r
90 #endif\r
91 //                                              map->layerdata[k].layername = mAlloc(sizeof(byte) * MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
92 //                                              strncpy(map->layerdata[k].layername, js+t[i+1].start, MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
93 //                                              if(map->layerdata[k].layername[MAPLNAMESIZE]!=0) map->layerdata[k].layername[MAPLNAMESIZE]='\0';\r
94                                         }else if(jsoneq(js, &(t[i]), "name") == 0) {\r
95 #ifdef DEBUG_OBVARS\r
96                                                 printf("Object %d's name: ", k);//, MAPLNAMESIZE\r
97                                                 printf("'%.*s'\n", t[i+1].end - t[i+1].start, js+t[i+1].start);\r
98 #endif\r
99                                         }else if(jsoneq(js, &(t[i]), "properties") == 0) {\r
100 #ifdef DEBUG_OBVARS\r
101                                                 printf("        properties: %.*s\n", t[i+1].end - t[i+1].start, js+t[i+1].start);\r
102 #endif\r
103                                         }else if(jsoneq(js, &(t[i]), "walkable") == 0) {\r
104                                                         printf("                walkable: %d\n", atoi(js + t[i+1].start));\r
105                                         }\r
106 \r
107                                 }//end objlay\r
108                                 i++;//next token\r
109                         }//tokens\r
110                 }//layers\r
111 \r
112 \r
113                 if(jsoneq(js, &(t[i]), "tilesets") == 0) {\r
114                         i++;\r
115                         inner_end = t[i].end;\r
116                         k = 0;\r
117                         while(t[i].start < inner_end) {\r
118                                 if(jsoneq(js, &(t[i]), "image") == 0) {\r
119                                         map->tiles = malloc(sizeof(tiles_t));//TODO: USE MM_ CA_ AND PM_\r
120                                         s = remove_ext((char *)js+t[i+1].start, '.', '/');\r
121                                         strcpy(map->tiles->imgname, s);\r
122                                         //And move to vrs, probably\r
123 //                                      bp = bitmapLoadPcx("data/ed.pcx");\r
124 //                                      map->tiles->btdata = &bp;\r
125                                         map->tiles->rows = 1;\r
126                                         map->tiles->cols = 1;\r
127 #ifdef __DEBUG_MAP__\r
128                                         dbg_maptext=false;\r
129 #endif\r
130                                         i++;\r
131                                 }else if(jsoneq(js, &(t[i]), "tileheight") == 0) {\r
132                                         map->tiles->tileHeight = atoi(js + t[i+1].start);\r
133 #ifdef DEBUG_MAPVARS\r
134                                         printf("Tile Height: %d\n", map->tiles->tileHeight);\r
135 #endif\r
136                                         i++;\r
137                                 }else if(jsoneq(js, &(t[i]), "tilewidth") == 0) {\r
138                                         map->tiles->tileWidth = atoi(js + t[i+1].start);\r
139 #ifdef DEBUG_MAPVARS\r
140                                         printf("Tile Width: %d\n", map->tiles->tileWidth);\r
141 #endif\r
142                                         i++;\r
143                                 }\r
144                                 i++;\r
145                                 k++;\r
146                         }\r
147                 }\r
148 \r
149                 if (jsoneq(js, &(t[i]), "height") == 0 && indent<=1) {\r
150                         map->height = atoi(js + t[i+1].start);\r
151 #ifdef DEBUG_MAPVARS\r
152                         printf("Height: %d\n", map->height);\r
153 #endif\r
154                         i++;\r
155                 }\r
156                 else if(jsoneq(js, &(t[i]), "width") == 0 && indent<=1) {\r
157                         map->width = atoi(js + t[i+1].start);\r
158 #ifdef DEBUG_MAPVARS\r
159                         printf("Width: %d\n", map->width);\r
160 #endif\r
161                         i++;\r
162                 }\r
163                 i++;\r
164         }\r
165 }\r
166 \r
167 int newloadmap(char *mn, map_t *map) {\r
168         char *js;\r
169 \r
170         jsmn_parser p;\r
171         jsmntok_t *tok = NULL;\r
172         size_t tokcount, file_s;\r
173 \r
174         FILE *fh = fopen(mn, "r");\r
175         int status;\r
176 \r
177         /* Prepare parser */\r
178         jsmn_init(&p);\r
179 \r
180         file_s = filesize(fh);\r
181         js = malloc(file_s);//TODO: USE MM_ CA_ AND PM_\r
182         if(js == NULL) {\r
183                 fprintf(stderr, "malloc(): errno = %d", 2);\r
184                 fclose(fh);\r
185                 return 3;\r
186         }\r
187         if(fread(js, 1, file_s, fh) != file_s) {\r
188                 fprintf(stderr, "Map read error");\r
189                 free(js);\r
190                 fclose(fh);\r
191                 return 1;\r
192         }\r
193         tokcount = jsmn_parse(&p, js, file_s, NULL, 0);\r
194         tok = malloc(tokcount*sizeof(jsmntok_t));//TODO: USE MM_ CA_ AND PM_\r
195         printf("Allocated %d tokens", tokcount);\r
196         jsmn_init(&p);\r
197         if((status = jsmn_parse(&p, js, file_s, tok, tokcount)) < 0)\r
198         {\r
199                 printf("Error: %d\n", status);\r
200                 return status;\r
201         }\r
202         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}\r
203         extract_map(js, tok, tokcount, map);\r
204 \r
205         free(js);\r
206         free(tok);\r
207         fclose(fh);\r
208 \r
209         return 0;\r
210 }\r
211 \r
212 \r
213 //======\r
214 \r
215 \r
216 #define MAPBUFINLM (gvar->ca.camap.mapsegs)\r
217 int CA_loadmap(char *mn, map_t *map, global_game_variables_t *gvar)\r
218 {\r
219         jsmn_parser p;\r
220         jsmntok_t *tok = NULL;\r
221         size_t tokcount, file_s;\r
222 \r
223         FILE *fh = fopen(mn, "r");\r
224         int status;\r
225 \r
226         /* Prepare parser */\r
227         jsmn_init(&p);\r
228 \r
229         file_s = filesize(fh);\r
230         CA_LoadFile(mn, &MAPBUFINLM, gvar);\r
231         tokcount = jsmn_parse(&p, MAPBUFINLM, file_s, NULL, 0);\r
232         tok = malloc(tokcount*sizeof(jsmntok_t));\r
233 //      printf("Allocated %d tokens", tokcount);\r
234         jsmn_init(&p);\r
235         if((status = jsmn_parse(&p, MAPBUFINLM, file_s, tok, tokcount)) < 0)\r
236         {\r
237                 printf("Error: %d\n", status);\r
238                 return status;\r
239         }\r
240         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}\r
241         extract_map(MAPBUFINLM, tok, tokcount, map);\r
242 \r
243         free(tok);\r
244         fclose(fh);\r
245 \r
246         return 0;\r
247 }\r