· 8 years ago · Mar 02, 2018, 03:50 PM
1#include <a_samp>
2#include <sscanf2>
3#include <sqlitei>
4#include <alltextures.pwn>
5
6#define FILE_INPUT "testmap.txt"
7#define FILE_OUTPUT "testmap.db"
8
9#define CODE_OBJECT "CreateDynamicObject"
10#define CODE_MATERIAL "SetDynamicObjectMaterial"
11
12#define MAX_C_OBJECTS 1000 // Max objects (they have to be saved temporarily)
13#define MAX_C_MATERIALS 5000 // Max total Materials in a file (also has to be saved)
14
15enum E_OBJECT // Stores data for each object
16{
17 obExists,
18 obModel,
19 Float:obX,
20 Float:obY,
21 Float:obZ,
22 Float:obrX,
23 Float:obrY,
24 Float:obrZ
25};
26
27enum E_MATERIAL // Stores data for each material
28{
29 matExists,
30 matObject, // Points to the object (see above) to which it should be applied
31 matIndex,
32 matModel,
33 matModelName[32],
34 matTextureName[32],
35 matColor
36};
37
38new Objects[MAX_C_OBJECTS][E_OBJECT], Materials[MAX_C_MATERIALS][E_MATERIAL];
39
40new bool:Imported;
41
42public OnFilterScriptInit()
43{
44 print("\nStarting TS Exporter ...\n");
45
46 ImportCode(FILE_INPUT);
47
48 ExportDatabase(FILE_OUTPUT);
49
50 print("\nDone!\n");
51}
52
53ImportCode(filename[])
54{
55 new File:FIn = fopen(filename, io_read), text[200], len, line;
56
57 // Obj & Mat Tmp Data
58 new model, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz, targetvar[32], index, modelname[32], texturename[32], color, lastob = -1, pos;
59
60 if(!FIn) return print("Import Error: File does not exist or cannot be opened."), 0;
61
62 while((len = fread(FIn, text)))
63 {
64 line ++;
65
66 if(len < 3 || text[0] == '#' || text[0] == '/') continue;
67
68 for(new i = 0; i < len; i ++)
69 {
70 if(text[i] == ',') text[i] = ' '; // Replace commas with spaces
71
72 if(text[i] == '\"') text[i] = ' '; // Replace " with spaces
73
74 if(text[i] == '(') text[i] = ' '; // Replace ( with spaces
75
76 if(text[i] == ')') text[i] = ' '; // Replace ) with spaces
77
78 if(text[i] == '\r' || text[i] == '\n' || text[i] == ';') // End string at \r, \n or ;
79 {
80 text[i] = 0;
81 len = i + 1;
82 break;
83 }
84 }
85
86 pos = strfind(text, CODE_OBJECT" ", false);
87
88 if(pos != -1) // Object to import
89 {
90 strmid(text, text, pos, len);
91
92 if(!sscanf(text, "'"CODE_OBJECT"'iffffff", model, x, y, z, rx, ry, rz))
93 {
94 lastob = AddObject(model, x, y, z, rx, ry, rz);
95 }
96 else
97 {
98 printf("Import Warning: Failed to load object. Line: %d", line);
99 }
100 }
101
102 pos = strfind(text, CODE_MATERIAL" ", false);
103
104 if(pos != -1) // Material to import
105 {
106 if(!sscanf(text, "'"CODE_MATERIAL"'s[32]iis[32]s[32]i", targetvar, index, model, modelname, texturename, color) || !sscanf(text, "'"CODE_MATERIAL"'s[32]iis[32]s[32]x", targetvar, index, model, modelname, texturename, color))
107 {
108 if(lastob != -1)
109 {
110 AddMaterial(lastob, index, model, modelname, texturename, color);
111 }
112 else
113 {
114 printf("Import Warning: Material could not be loaded, no prior object. Line: %d", line);
115 continue;
116 }
117 }
118 else
119 {
120 printf("Import Warning: Failed to load material. Line: %d", line);
121 }
122 }
123 }
124
125 Imported = true;
126
127 PrintImportInfo();
128
129 return 1;
130}
131
132ExportDatabase(filename[])
133{
134 if(!Imported) return print("Export Error: No Map imported."), 0;
135
136 if(fexist(filename)) fremove(filename);
137
138 new DB:dbExport = db_open(filename);
139
140 if(!dbExport) return print("Export Error: DB cannot be opened."), 0;
141
142 new DBResult:result = db_query(dbExport, "CREATE TABLE IF NOT EXISTS `Objects`"\
143 "(IndexID INTEGER,"\
144 "ModelID INTEGER,"\
145 "xPos REAL,"\
146 "yPos REAL,"\
147 "zPos REAL,"\
148 "rxRot REAL,"\
149 "ryRot REAL,"\
150 "rzRot REAL,"\
151 "TextureIndex TEXT,"\
152 "ColorIndex TEXT,"\
153 "usetext INTEGER,"\
154 "FontFace INTEGER,"\
155 "FontSize INTEGER,"\
156 "FontBold INTEGER,"\
157 "FontColor INTEGER,"\
158 "BackColor INTEGER,"\
159 "Alignment INTEGER,"\
160 "TextFontSize INTEGER,"\
161 "ObjectText TEXT,"\
162 "GroupID INTEGER,"\
163 "Note TEXT,"\
164 "DrawDistance REAL DEFAULT 300.0);");
165
166 db_free_result(result);
167
168 new texarray[16], colarray[16], text[200];
169
170 for(new ob = 0; ob < MAX_C_OBJECTS; ob ++) if(Objects[ob][obExists]) // Loop all objects
171 {
172 for(new i = 0; i < 16; i ++) // Clear object materials
173 {
174 texarray[i] = 0;
175 colarray[i] = 0;
176 }
177
178 for(new mat = 0; mat < MAX_C_MATERIALS; mat ++) if(Materials[mat][matExists] && Materials[mat][matObject] == ob) // Find all materials for this object
179 {
180 new bool:found = false;
181
182 for(new i = 0; i < sizeof(ObjectTextures); i ++) // Find the Texture ID for this Material
183 {
184 if(Materials[mat][matModel] == ObjectTextures[i][TModel] && strcmp(Materials[mat][matModelName], ObjectTextures[i][TXDName], true) == 0 && strcmp(Materials[mat][matTextureName], ObjectTextures[i][TextureName], true) == 0)
185 {
186 texarray[Materials[mat][matIndex]] = i;
187 colarray[Materials[mat][matIndex]] = Materials[mat][matColor];
188
189 found = true;
190 break;
191 }
192 }
193
194 if(!found) printf("Export Warning: Couldn't find Material %d, \"%s\", \"%s\"", Materials[mat][matModel], Materials[mat][matModelName], Materials[mat][matTextureName]);
195 }
196
197 format(text, sizeof(text), "INSERT INTO `Objects` VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
198
199 new DBStatement:stmt = db_prepare(dbExport, text);
200
201 stmt_bind_value(stmt, 0, DB::TYPE_INT, ob);
202 stmt_bind_value(stmt, 1, DB::TYPE_INT, Objects[ob][obModel]);
203 stmt_bind_value(stmt, 2, DB::TYPE_FLOAT, Objects[ob][obX]);
204 stmt_bind_value(stmt, 3, DB::TYPE_FLOAT, Objects[ob][obY]);
205 stmt_bind_value(stmt, 4, DB::TYPE_FLOAT, Objects[ob][obZ]);
206 stmt_bind_value(stmt, 5, DB::TYPE_FLOAT, Objects[ob][obrX]);
207 stmt_bind_value(stmt, 6, DB::TYPE_FLOAT, Objects[ob][obrY]);
208 stmt_bind_value(stmt, 7, DB::TYPE_FLOAT, Objects[ob][obrZ]);
209 stmt_bind_value(stmt, 8, DB::TYPE_ARRAY, texarray, 16);
210 stmt_bind_value(stmt, 9, DB::TYPE_ARRAY, colarray, 16);
211 stmt_bind_value(stmt, 10, DB::TYPE_INT, 0);
212 stmt_bind_value(stmt, 11, DB::TYPE_INT, 0);
213 stmt_bind_value(stmt, 12, DB::TYPE_INT, 0);
214 stmt_bind_value(stmt, 13, DB::TYPE_INT, 0);
215 stmt_bind_value(stmt, 14, DB::TYPE_INT, 0);
216 stmt_bind_value(stmt, 15, DB::TYPE_INT, 0);
217 stmt_bind_value(stmt, 16, DB::TYPE_INT, 0);
218 stmt_bind_value(stmt, 17, DB::TYPE_INT, 0);
219 stmt_bind_value(stmt, 18, DB::TYPE_STRING, "x");
220 stmt_bind_value(stmt, 19, DB::TYPE_INT, 0);
221 stmt_bind_value(stmt, 20, DB::TYPE_STRING, "x");
222 stmt_bind_value(stmt, 21, DB::TYPE_FLOAT, 300.0);
223
224 stmt_execute(stmt);
225 stmt_close(stmt);
226 }
227
228 db_close(dbExport);
229
230 return 1;
231}
232
233AddObject(model, Float:x, Float:y, Float:z, Float:rx, Float:ry, Float:rz)
234{
235 new slot = -1;
236
237 for(new i = 0; i < MAX_C_OBJECTS; i ++) if(!Objects[i][obExists])
238 {
239 slot = i;
240 break;
241 }
242
243 if(slot == -1) return -1;
244
245 Objects[slot][obExists] = 1;
246 Objects[slot][obModel] = model;
247 Objects[slot][obX] = x;
248 Objects[slot][obY] = y;
249 Objects[slot][obZ] = z;
250 Objects[slot][obrX] = rx;
251 Objects[slot][obrY] = ry;
252 Objects[slot][obrZ] = rz;
253
254 //printf("Added Object %d", slot);
255
256 return slot;
257}
258
259AddMaterial(object, index, model, modelname[], texturename[], color)
260{
261 if(index >= 16) return -1;
262
263 new slot = -1;
264
265 for(new i = 0; i < MAX_C_MATERIALS; i ++) if(!Materials[i][matExists])
266 {
267 slot = i;
268 break;
269 }
270
271 if(slot == -1) return -1;
272
273 Materials[slot][matExists] = 1;
274 Materials[slot][matObject] = object;
275 Materials[slot][matIndex] = index;
276 Materials[slot][matModel] = model;
277 format(Materials[slot][matModelName], 32, modelname);
278 format(Materials[slot][matTextureName], 32, texturename);
279 Materials[slot][matColor] = color;
280
281 //printf("Added Material %d for object %d (%d, %d, %s, %s, %d)", slot, object, index, model, modelname, texturename, color);
282
283 return slot;
284}
285
286stock ClearObjects()
287{
288 for(new i = 0; i < MAX_C_OBJECTS; i ++) Objects[i][obExists] = 0;
289
290 return 1;
291}
292
293stock ClearMaterials()
294{
295 for(new i = 0; i < MAX_C_MATERIALS; i ++) Materials[i][matExists] = 0;
296
297 return 1;
298}
299
300PrintImportInfo()
301{
302 if(!Imported) return 0;
303
304 new co, cm;
305
306 for(new i = 0; i < MAX_C_OBJECTS; i ++) if(Objects[i][obExists]) co ++;
307 for(new i = 0; i < MAX_C_MATERIALS; i ++) if(Materials[i][matExists]) cm ++;
308
309 printf("Imported %d Objects and %d Materials", co, cm);
310
311 return 1;
312}