]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_map.c
JSMN_STATIC is commented out for more memory
[16.git] / src / lib / 16_map.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2019 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 //TODO USE CA AND THIS FILE FORMAT\r
24 \r
25 #include "src/lib/16_map.h"\r
26 //#define JSMN_STATIC\r
27 #include "src/lib/jsmn/jsmn.h"\r
28 #include <malloc.h>\r
29 \r
30 // Ideally, preprocess json during compilation and read serialized data\r
31 \r
32 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {\r
33         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&\r
34                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {\r
35                 return 0;\r
36         }\r
37         return -1;\r
38 }\r
39 \r
40 //TODO: WORK ON THIS LIKE CRAZY!        --sparky4\r
41 //FIXME: please FIX THIS AND USE CA!    --sparky4\r
42 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {\r
43         int i, j, k, indent=0, inner_end;\r
44         char *s;\r
45         boolean objlay=0;\r
46         //bitmap_t bp;\r
47 \r
48         i = 0;\r
49 #define MAPLNAMESIZE t[i+1].end - t[i+1].start\r
50         while(i<count) {\r
51                 if(jsoneq(js, &(t[i]), "layers") == 0) {\r
52                         i++;\r
53                         inner_end = t[i].end;\r
54                         k = 0;\r
55                         while(t[i].start < inner_end) {\r
56 //#define DEBUG_DUMPVARS\r
57 //#ifdef DEBUG_DUMPVARS\r
58 //                              printf("t[%d].start=%d, %d\n", i, t[i].start, inner_end);\r
59 //#endif\r
60                                 if(!objlay){\r
61                                 if(jsoneq(js, &(t[i]), "data") == 0) {\r
62 #ifdef DEBUG_MAPDATA\r
63                                         printf("Layer %d data: (size is %d)[\n", k, t[i+1].size);\r
64 #endif\r
65                                         map->layerdata[k].data = malloc(sizeof(byte) * t[i+1].size);//TODO: USE MM_ CA_ AND PM_\r
66                                         for(j = 0; j < t[i+1].size; j++) {\r
67                                                 map->layerdata[k].data[j] = (byte)atoi(js + t[i+2+j].start);\r
68 #ifdef DEBUG_MAPDATA\r
69                                                 printf("%c",  map->layerdata[k].data[j]+44);\r
70 #endif\r
71                                         }\r
72                                         i += j + 2;\r
73 #ifdef DEBUG_MAPDATA\r
74                                         puts("\n]");\r
75 #endif\r
76                                 }else if(jsoneq(js, &(t[i]), "name") == 0) {\r
77 #ifdef DEBUG_MAPVARS\r
78                                         printf("Layer %d's name: (size is %d)[\n", k, MAPLNAMESIZE);\r
79 #endif\r
80                                         //map->layerdata[k].layername = malloc(sizeof(byte) * MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
81                                         strncpy(&(map->layerdata[k].layername), js+t[i+1].start, MAPLNAMESIZE);\r
82                                         if((map->layerdata[k].layername[MAPLNAMESIZE])!=0) map->layerdata[k].layername[MAPLNAMESIZE]='\0';\r
83                                         if(strstr(&map->layerdata[k].layername, "ob")) objlay=1;\r
84 #ifdef DEBUG_MAPDATA\r
85                                         printf("%s", map->layerdata[k].layername);\r
86                                         printf("\n]\n");\r
87 #endif\r
88                                         k++;\r
89                                 }\r
90                                 }else{ //objlay\r
91                                         if(jsoneq(js, &(t[i]), "objects") == 0) {\r
92 #ifdef DEBUG_OBVARS\r
93                                                 printf("objects detected\n");\r
94 #endif\r
95 #ifdef DEBUG_OBVARS\r
96 \r
97 #endif\r
98 //                                              map->layerdata[k].layername = mAlloc(sizeof(byte) * MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
99 //                                              strncpy(map->layerdata[k].layername, js+t[i+1].start, MAPLNAMESIZE);//TODO: USE MM_ CA_ AND PM_\r
100 //                                              if(map->layerdata[k].layername[MAPLNAMESIZE]!=0) map->layerdata[k].layername[MAPLNAMESIZE]='\0';\r
101                                         }else if(jsoneq(js, &(t[i]), "name") == 0) {\r
102 #ifdef DEBUG_OBVARS\r
103                                                 printf("Object %d's name: ", k);//, MAPLNAMESIZE\r
104                                                 printf("'%.*s'\n", t[i+1].end - t[i+1].start, js+t[i+1].start);\r
105 #endif\r
106                                         }else if(jsoneq(js, &(t[i]), "properties") == 0) {\r
107 #ifdef DEBUG_OBVARS\r
108                                                 printf("        properties: %.*s\n", t[i+1].end - t[i+1].start, js+t[i+1].start);\r
109 #endif\r
110                                         }else if(jsoneq(js, &(t[i]), "walkable") == 0) {\r
111                                                         printf("                walkable: %d\n", atoi(js + t[i+1].start));\r
112                                         }\r
113 \r
114                                 }//end objlay\r
115                                 i++;//next token\r
116                         }//tokens\r
117                 }//layers\r
118 \r
119 \r
120                 if(jsoneq(js, &(t[i]), "tilesets") == 0) {\r
121                         i++;\r
122                         inner_end = t[i].end;\r
123                         k = 0;\r
124                         while(t[i].start < inner_end) {\r
125                                 if(jsoneq(js, &(t[i]), "image") == 0) {\r
126                                         map->tiles = malloc(sizeof(tiles_t));//TODO: USE MM_ CA_ AND PM_\r
127                                         s = remove_ext((char *)js+t[i+1].start, '.', '/');\r
128                                         strcpy(map->tiles->imgname, s);\r
129                                         //And move to vrs, probably\r
130 //                                      bp = bitmapLoadPcx("data/ed.pcx");\r
131 //                                      map->tiles->btdata = &bp;\r
132                                         map->tiles->rows = 1;\r
133                                         map->tiles->cols = 1;\r
134 #ifdef __DEBUG_MAP__\r
135                                         dbg_maptext=false;\r
136 #endif\r
137                                         i++;\r
138                                 }else if(jsoneq(js, &(t[i]), "tileheight") == 0) {\r
139                                         map->tiles->tileHeight = atoi(js + t[i+1].start);\r
140 #ifdef DEBUG_MAPVARS\r
141                                         printf("Tile Height: %d\n", map->tiles->tileHeight);\r
142 #endif\r
143                                         i++;\r
144                                 }else if(jsoneq(js, &(t[i]), "tilewidth") == 0) {\r
145                                         map->tiles->tileWidth = atoi(js + t[i+1].start);\r
146 #ifdef DEBUG_MAPVARS\r
147                                         printf("Tile Width: %d\n", map->tiles->tileWidth);\r
148 #endif\r
149                                         i++;\r
150                                 }\r
151                                 i++;\r
152                                 k++;\r
153                         }\r
154                 }\r
155 \r
156                 if (jsoneq(js, &(t[i]), "height") == 0 && indent<=1) {\r
157                         map->height = atoi(js + t[i+1].start);\r
158 #ifdef DEBUG_MAPVARS\r
159                         printf("Height: %d\n", map->height);\r
160 #endif\r
161                         i++;\r
162                 }\r
163                 else if(jsoneq(js, &(t[i]), "width") == 0 && indent<=1) {\r
164                         map->width = atoi(js + t[i+1].start);\r
165 #ifdef DEBUG_MAPVARS\r
166                         printf("Width: %d\n", map->width);\r
167 #endif\r
168                         i++;\r
169                 }\r
170                 i++;\r
171         }\r
172 }\r
173 \r
174 int newloadmap(char *mn, map_t *map) {\r
175         char *js;\r
176 \r
177         jsmn_parser p;\r
178         jsmntok_t *tok = NULL;\r
179         size_t tokcount, file_s;\r
180 \r
181         FILE *fh = fopen(mn, "r");\r
182         int status;\r
183 \r
184         /* Prepare parser */\r
185         jsmn_init(&p);\r
186 \r
187         file_s = filesize(fh);\r
188         js = malloc(file_s);//TODO: USE MM_ CA_ AND PM_\r
189         if(js == NULL) {\r
190                 fprintf(stderr, "malloc(): errno = %d", 2);\r
191                 fclose(fh);\r
192                 return 3;\r
193         }\r
194         if(fread(js, 1, file_s, fh) != file_s) {\r
195                 fprintf(stderr, "Map read error");\r
196                 free(js);\r
197                 fclose(fh);\r
198                 return 1;\r
199         }\r
200         tokcount = jsmn_parse(&p, js, file_s, NULL, 0);\r
201         tok = malloc(tokcount*sizeof(jsmntok_t));//TODO: USE MM_ CA_ AND PM_\r
202         printf("Allocated %d tokens", tokcount);\r
203         jsmn_init(&p);\r
204         if((status = jsmn_parse(&p, js, file_s, tok, tokcount)) < 0)\r
205         {\r
206                 printf("Error: %d\n", status);\r
207                 return status;\r
208         }\r
209         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}\r
210         extract_map(js, tok, tokcount, map);\r
211 \r
212         free(js);       //TODO: USE MM_ CA_ AND PM_\r
213         free(tok);      //TODO: USE MM_ CA_ AND PM_\r
214         fclose(fh);     //TODO: USE MM_ CA_ AND PM_\r
215 \r
216         return 0;\r
217 }\r
218 \r
219 \r
220 //======\r
221 \r
222 \r
223 int CA_loadmap(char *mn, map_t *map, global_game_variables_t *gvar)\r
224 {\r
225         jsmn_parser p;\r
226         jsmntok_t *tok = NULL;\r
227         size_t tokcount, file_s;\r
228 \r
229         FILE *fh = fopen(mn, "r");\r
230         int status;\r
231 \r
232         /* Prepare parser */\r
233         jsmn_init(&p);\r
234 \r
235         file_s = filesize(fh);\r
236         CA_LoadFile(mn, MEMPTRCONV MAPSEGINLM, gvar);\r
237         tokcount = jsmn_parse(&p, (char const *)MAPSEGINLM, file_s, NULL, 0);\r
238         tok = malloc(tokcount*sizeof(jsmntok_t));//TODO: USE MM_ CA_ AND PM_\r
239 //      printf("Allocated %d tokens", tokcount);\r
240         jsmn_init(&p);\r
241         if((status = jsmn_parse(&p, (char const *)MAPSEGINLM, file_s, tok, tokcount)) < 0)\r
242         {\r
243                 printf("Error: %d\n", status);\r
244                 return status;\r
245         }\r
246         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}\r
247         extract_map((char const *)MAPSEGINLM, tok, tokcount, map);\r
248 \r
249 //      printf("freeing tok\n");\r
250         free(tok);      //TODO: USE MM_ CA_ AND PM_\r
251 //      printf("fclose fh\n");\r
252         fclose(fh);     //TODO: USE MM_ CA_ AND PM_\r
253 \r
254         return 0;\r
255 }\r