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