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