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