· 4 years ago · Feb 10, 2021, 11:50 PM
1class MapperData
2{
3 Player = null
4 Object = null
5 SelectingObject = false
6 Timer = null
7}
8
9class ObjectsClass
10{
11 ID = null;
12 Object = null;
13 Model = null;
14
15 PX = null;
16 PY = null;
17 PZ = null;
18
19 AX = null;
20 AY = null;
21 AZ = null;
22
23}
24
25
26Mapper <- {
27
28 function LoadSystem()
29 {
30 QuerySQL(OBJ_DB, "CREATE TABLE IF NOT EXISTS Objects ( ID INTEGER PRIMARY KEY AUTOINCREMENT, Model NUMERIC, PX FLOAT, PY FLOAT, PZ FLOAT, AX FLOAT, AY FLOAT, AZ FLOAT, MappedBy TEXT )" );
31 QuerySQL(OBJ_DB, "CREATE TABLE IF NOT EXISTS Logger ( ID INTEGER, Model INTEGER, Position TEXT, Action TEXT, Mapper TEXT )" );
32
33 this.Users <- array( GetMaxPlayers(), null );
34 Objects <- array( 3001, null );
35 ObjectsArray <- array( 3001, null );
36
37 this.LoadObjects();
38 print("Mapper System loaded - By Naru^/Maximiliano");
39 }
40
41 function LoadObjects()
42 {
43 local cq = QuerySQL( OBJ_DB, "SELECT COUNT(*) FROM Objects" ), count = GetSQLColumnData( cq, 0 );
44
45 if ( !count )
46 {
47 print( "Objects Loaded: 0" );
48 return false;
49 }
50
51 local i = 0;
52 local q = QuerySQL( OBJ_DB, "SELECT * FROM Objects" );
53 while ( GetSQLColumnData( q, 0 ) != null )
54 {
55 local ID = GetSQLColumnData( q, 0 );
56 this.Objects[ID] = ObjectsClass();
57 this.Objects[ID].ID = ID;
58 this.Objects[ID].Model = GetSQLColumnData( q, 1 );
59 this.Objects[ID].PX = GetSQLColumnData( q, 2 );
60 this.Objects[ID].PY = GetSQLColumnData( q, 3 );
61 this.Objects[ID].PZ = GetSQLColumnData( q, 4 );
62 this.Objects[ID].AX = GetSQLColumnData( q, 5 );
63 this.Objects[ID].AY = GetSQLColumnData( q, 6 );
64 this.Objects[ID].AZ = GetSQLColumnData( q, 7 );
65
66 local Model = GetSQLColumnData( q, 1 ),
67 PX = this.Objects[ID].PX.tofloat(),
68 PY = this.Objects[ID].PY.tofloat(),
69 PZ = this.Objects[ID].PZ.tofloat(),
70 AX = this.Objects[ID].AX.tofloat(),
71 AY = this.Objects[ID].AY.tofloat(),
72 AZ = this.Objects[ID].AZ.tofloat();
73
74 this.Objects[ID].Object = ::CreateObject( Model, 1, Vector( PX, PY, PZ ), 255 );
75 this.Objects[ID].Object.RotateToEuler( Vector( AX, AY, AZ ), 0 );
76 this.Objects[ID].Object.TrackingShots = true;
77
78 this.ObjectsArray[ this.Objects[ID].Object.ID ] = ID;
79 i++;
80 GetSQLNextRow( q );
81 }
82 FreeSQLQuery( q );
83 print( "Objects Loaded: " + i );
84 }
85
86
87 function CreateObject(player, model)
88 {
89 if ( !this.Users[ player.ID ] )
90 {
91 this.Users[ player.ID ] = MapperData();
92 this.Users[ player.ID ].Player = player;
93 this.Users[ player.ID ].Object = ::CreateObject( model.tointeger(), player.World, Vector( player.Pos.x, player.Pos.y, player.Pos.z + 0.5 ), 255 );
94 this.Users[ player.ID ].Object.TrackingShots = true;
95 MessagePlayer("Objecto creado correctamente.", player);
96 }
97 }
98
99 function DeleteObject( player )
100 {
101 if ( this.Users[ player.ID ] )
102 {
103 local obj = this.Users[ player.ID ].Object;
104 if ( this.ObjectsArray[ obj.ID ] == null )
105 {
106 MessagePlayer("Objecto removido.", player);
107 this.Users[ player.ID ].Object.Delete();
108 this.Users[ player.ID ] = null;
109 }
110 else
111 {
112
113 local ID = this.ObjectsArray[ obj.ID ];
114 if ( this.Objects[ ID ] && this.Objects[ ID ].Object.ID == obj.ID )
115 {
116 QuerySQL( OBJ_DB, "INSERT INTO Logger ( ID, Model, Position, Action, Mapper ) VALUES ( '" + ID + "', '" + obj.Model + "', '" + obj.Pos + "', 'Delete', '" + player.Name + "' )" );
117
118 QuerySQL( OBJ_DB, "DELETE FROM Objects WHERE ID='" + ID + "'" );
119 this.Objects[ID] = null;
120 MessagePlayer("Objecto removido..", player);
121 this.ObjectsArray[ obj.ID ] = null;
122 this.Users[ player.ID ].Object.Delete();
123 this.Users[ player.ID ] = null;
124
125
126 }
127 }
128
129 }
130 }
131
132 function SaveObject( player )
133 {
134 if ( this.Users[ player.ID ] )
135 {
136 local obj = this.Users[ player.ID ].Object;
137 if ( this.ObjectsArray[ obj.ID ] == null )
138 {
139 local OBJ = this.Users[ player.ID ].Object;
140 QuerySQL( OBJ_DB, "INSERT INTO Objects ( Model, PX, PY, PZ, AX, AY, AZ, MappedBy ) VALUES ( '" + OBJ.Model + "', '" + OBJ.Pos.x + "', '" + OBJ.Pos.y + "', '" + OBJ.Pos.z + "', '" + OBJ.RotationEuler.x + "', '" + OBJ.RotationEuler.y + "', '" + OBJ.RotationEuler.z + "', '" + player.Name + "' )" );
141
142
143 local GetID = QuerySQL( OBJ_DB, "SELECT ID FROM Objects ORDER BY ID DESC LIMIT 1");
144 local ID = GetSQLColumnData( GetID, 0 ).tointeger();
145
146 local q = QuerySQL( OBJ_DB, "SELECT * FROM Objects WHERE ID='" + ID + "'" );
147 if ( q )
148 {
149 local ID = GetSQLColumnData( q, 0 );
150 this.Objects[ID] = ObjectsClass();
151 this.Objects[ID].ID = ID;
152 this.Objects[ID].Model = GetSQLColumnData( q, 1 );
153 this.Objects[ID].PX = GetSQLColumnData( q, 2 );
154 this.Objects[ID].PY = GetSQLColumnData( q, 3 );
155 this.Objects[ID].PZ = GetSQLColumnData( q, 4 );
156 this.Objects[ID].AX = GetSQLColumnData( q, 5 );
157 this.Objects[ID].AY = GetSQLColumnData( q, 6 );
158 this.Objects[ID].AZ = GetSQLColumnData( q, 7 );
159
160 local Model = GetSQLColumnData( q, 1 ),
161 PX = this.Objects[ID].PX.tofloat(),
162 PY = this.Objects[ID].PY.tofloat(),
163 PZ = this.Objects[ID].PZ.tofloat(),
164 AX = this.Objects[ID].AX.tofloat(),
165 AY = this.Objects[ID].AY.tofloat(),
166 AZ = this.Objects[ID].AZ.tofloat();
167
168 this.Objects[ID].Object = this.Users[ player.ID ].Object;
169 this.Objects[ID].Object.RotateToEuler( Vector( AX, AY, AZ ), 0 );
170
171 this.ObjectsArray[ this.Objects[ID].Object.ID ] = ID;
172 this.Users[ player.ID ] = null;
173 QuerySQL( OBJ_DB, "INSERT INTO Logger ( ID, Model, Position, Action, Mapper ) VALUES ( '" + ID + "', '" + OBJ.Model + "', '" + OBJ.Pos + "', 'Save', '" + player.Name + "' )" );
174 MessagePlayer("Objecto guardado.", player);
175 }
176 }
177 else
178 {
179
180 local ID = this.ObjectsArray[ obj.ID ];
181
182 if ( this.Objects[ ID ] && this.Objects[ ID ].Object && this.Objects[ ID ].Object.ID == obj.ID )
183 {
184 QuerySQL( OBJ_DB, "UPDATE Objects SET PX='" + obj.Pos.x + "', PY='" + obj.Pos.y + "', PZ='" + obj.Pos.z + "', AX='" + obj.RotationEuler.x + "', AY='" + obj.RotationEuler.y + "', AZ='" + obj.RotationEuler.z + "' WHERE ID='" + ID + "'" );
185 this.Users[ player.ID ] = null;
186 MessagePlayer("Objecto actualizado.", player);
187 QuerySQL( OBJ_DB, "INSERT INTO Logger ( ID, Model, Position, Action, Mapper ) VALUES ( '" + ID + "', '" + obj.Model + "', '" + obj.Pos + "', 'Update', '" + player.Name + "' )" );
188 }
189 }
190 }
191 }
192
193 function Select( player , type, obj)
194 {
195 switch ( type )
196 {
197 case 1: //This will put the player in select mode, he can shoot an obj to select it.
198 this.Users[ player.ID ] = MapperData();
199 this.Users[ player.ID ].SelectingObject = true;
200 this.Users[ player.ID ].Timer = ::_Timer.Create(this, function(){ Select(player, 2, null);}, 5000, 1);
201 this.Information("You have 5 seconds to select the object.", player);
202 break;
203
204 case 2: //This cancel the select mode :v
205 _Timer.Destroy(this.Users[ player.ID ].Timer);
206 this.Users[ player.ID ] = null;
207 break;
208
209 case 3: //This select the object :v
210 if ( this.Users[ player.ID ].Timer ) ::_Timer.Destroy(this.Users[ player.ID ].Timer);
211 this.Users[ player.ID ] = MapperData();
212 this.Users[ player.ID ].Player = player;
213 this.Users[ player.ID ].Object = obj;
214 ::MessagePlayer("Object selected correctly.", player);
215
216 break;
217 }
218 }
219
220 function Copy(player)
221 {
222 local obj = this.Users[ player.ID ].Object;
223 this.SaveObject( player );
224 this.Users[ player.ID ] = MapperData();
225 this.Users[ player.ID ].Player = player;
226 this.Users[ player.ID ].Object = ::CreateObject( obj.Model, player.World, Vector( obj.Pos.x, obj.Pos.y, obj.Pos.z ), 255 );
227 this.Users[ player.ID ].Object.RotateTo( obj.Rotation,100);
228 this.Users[ player.ID ].Object.TrackingShots = true;
229 MessagePlayer("Objecto copiado correctamente.", player);
230 }
231
232 function Mov( player, sign, movsign, amount ) //sign = +/- , movsign = z,x,y
233 {
234 if ( this.Users[ player.ID ] )
235 {
236 local OBJ = this.Users[ player.ID ].Object;
237
238 switch( sign )
239 {
240
241 case "+":
242 {
243 switch ( movsign )
244 {
245 case "x":
246 {
247 OBJ.MoveTo( Vector( OBJ.Pos.x + amount.tofloat(), OBJ.Pos.y, OBJ.Pos.z ), 1500 );
248 MessagePlayer( "Object Moved...", player );
249 }
250 break;
251
252 case "y":
253 {
254 OBJ.MoveTo( Vector( OBJ.Pos.x , OBJ.Pos.y + amount.tofloat(), OBJ.Pos.z ), 1500 );
255 MessagePlayer( "Object Moved...", player );
256 }
257 break;
258
259 case "z":
260 {
261 OBJ.MoveTo( Vector( OBJ.Pos.x , OBJ.Pos.y, OBJ.Pos.z + amount.tofloat() ), 1500 );
262 MessagePlayer( "Object Moved...", player );
263 }
264 break;
265 }
266 }
267 break;
268
269 case "-":
270 {
271 switch ( movsign )
272 {
273 case "x":
274 {
275 OBJ.MoveTo( Vector( OBJ.Pos.x - amount.tofloat(), OBJ.Pos.y, OBJ.Pos.z ), 1500 );
276 MessagePlayer( "Object Moved...", player );
277 }
278 break;
279
280 case "y":
281 {
282 OBJ.MoveTo( Vector( OBJ.Pos.x , OBJ.Pos.y - amount.tofloat(), OBJ.Pos.z ), 1500 );
283 MessagePlayer( "Object Moved...", player );
284 }
285 break;
286
287 case "z":
288 {
289 OBJ.MoveTo( Vector( OBJ.Pos.x , OBJ.Pos.y, OBJ.Pos.z - amount.tofloat() ), 1500 );
290 MessagePlayer( "Object Moved...", player );
291 }
292 break;
293 }
294 }
295 break;
296
297 }
298
299 }
300 }
301
302 function Rot( player, sign, rotsign, amount ) //sign = +/- , rotsign = z,x,y
303 {
304 if ( this.Users[ player.ID ] )
305 {
306 local OBJ = this.Users[ player.ID ].Object;
307
308 switch( sign )
309 {
310
311 case "+":
312 {
313 switch ( rotsign )
314 {
315 case "x":
316 {
317 OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x + amount.tofloat(), OBJ.RotationEuler.y, OBJ.RotationEuler.z ), 1500 );
318 MessagePlayer( "Rotated Object...", player );
319 }
320 break;
321
322 case "y":
323 {
324 OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y + amount.tofloat(), OBJ.RotationEuler.z ), 1500 );
325 MessagePlayer( "Rotated Object...", player );
326 }
327 break;
328
329 case "z":
330 {
331 OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y, OBJ.RotationEuler.z + amount.tofloat() ), 1500 );
332 MessagePlayer( "Rotated Object...", player );
333 }
334 break;
335 }
336 }
337 break;
338
339 case "-":
340 {
341 switch ( rotsign )
342 {
343 case "x":
344 {
345 OBJ.RotateToEuler( Vector( (OBJ.RotationEuler.x - amount.tofloat()), OBJ.RotationEuler.y, OBJ.RotationEuler.z ), 1500 );
346 MessagePlayer( "Rotated Object...", player );
347 }
348 break;
349
350 case "y":
351 {
352 OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, (OBJ.RotationEuler.y - amount.tofloat()), OBJ.RotationEuler.z ), 1500 );
353 MessagePlayer( "Rotated Object...", player );
354 }
355 break;
356
357 case "z":
358 {
359 OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y, (OBJ.RotationEuler.z - amount.tofloat() )), 1500 );
360 MessagePlayer( "Rotated Object...", player );
361 }
362 break;
363 }
364 }
365 break;
366
367 }
368
369 }
370 }
371
372
373
374 function CountObjects()
375 {
376 try
377 {
378 local a = 0, q = QuerySQL( OBJ_DB, "SELECT * FROM Objects" );
379 while ( GetSQLColumnData( q, 0 ) )
380 {
381 a ++;
382 GetSQLNextRow( q );
383 }
384 return a;
385 FreeSQLQuery( q );
386 } catch(e) print( "[Error] CountObjects - " + e );
387 }
388
389 /*
390
391 OTHERS FUNCTIONS
392
393 */
394
395 function Sintax( text, player )
396 {
397 ::MessagePlayer( "[#FF00FF][SINTAX][#FFFFFF] " + text, player );
398 }
399
400 function Error( text, player )
401 {
402 ::MessagePlayer( "[#FF4500][ERROR][#FF0000] " + text, player );
403 }
404
405 function Information( text, player )
406 {
407 ::MessagePlayer( "[#FF00FF][INFO][#FFFFFF] " + text, player );
408 }
409
410 function Message( text )
411 {
412 ::Message( "[#FFB533][[#FFE633]MAPPER SYSTEM[#FFB533]][#FBFF8A] " + text );
413 }
414
415 function onObjectShot(player, object)
416 {
417 if ( Mapper.Users[ player.ID ] && Mapper.Users[ player.ID ].SelectingObject == true)
418 {
419 ::Mapper.Select(player, 3, object);
420 }
421 }
422
423 function onCommand( player, cmd, text )
424 {
425 if ( cmd == "createobj" || cmd == "obj" )
426 {
427 if ( Mapper.Users[ player.ID ] != null ) this.Error("You already have created an object.", player);
428 else if (!text) this.Sintax("/"+ cmd + " modelo", player)
429 else
430 {
431 Mapper.CreateObject(player, text.tointeger());
432 }
433 }
434
435 else if ( cmd == "save" )
436 {
437 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
438 else
439 {
440 Mapper.SaveObject( player );
441 }
442 }
443
444 else if ( cmd == "copy" )
445 {
446 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
447 else
448 {
449 Mapper.Copy( player );
450 }
451 }
452
453 else if ( cmd == "objdel" )
454 {
455 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
456 else
457 {
458 Mapper.DeleteObject( player );
459 }
460 }
461
462 else if ( cmd == "objselect" )
463 {
464 if ( Mapper.Users[ player.ID ] ) this.Error("You already has an object.", player);
465 else if (!text) this.Error("/objselect mouse/id", player)
466 else
467 {
468 if (text == "mouse") Mapper.Select(player, 1, null);
469 else if ( !IsNum(text)) this.Error("/objselect mouse/id", player)
470 else
471 {
472 if ( FindObject(text.tointeger()) )
473 {
474 if ( Mapper.Users[ player.ID ] && Mapper.Users[ player.ID ].Timer ) ::_Timer.Destroy(Mapper.Users[ player.ID ].Timer);
475 Mapper.Users[ player.ID ] = MapperData();
476 Mapper.Users[ player.ID ].Player = player;
477 Mapper.Users[ player.ID ].Object = FindObject(text.tointeger());
478 ::MessagePlayer("Object selected correctly.", player);
479 }
480 else this.Error("Object not found.", player);
481 }
482 }
483 }
484
485 else if ( cmd == "mov" )
486 {
487 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
488 else if (!GetTok( text, " ", 1) || !GetTok( text, " ", 2) || !GetTok( text, " ", 3)) this.Sintax("/mov x/y/z +/- amount")
489 else
490 {
491 local movsign = GetTok( text, " ", 1), sign = GetTok( text, " ", 2), amount = GetTok( text, " ", 3);
492 Mapper.Mov( player, sign, movsign, amount );
493 }
494 }
495
496 else if ( cmd == "rot" )
497 {
498 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
499 else if (!GetTok( text, " ", 1) || !GetTok( text, " ", 2) || !GetTok( text, " ", 3)) this.Sintax("/rot x/y/z +/- amount")
500 else
501 {
502 try
503 {
504 local rotsign = GetTok( text, " ", 1), sign = GetTok( text, " ", 2), amount = GetTok( text, " ", 3);
505 Mapper.Rot( player, sign, rotsign, amount );
506 } catch (e) this.Message(e);
507 }
508 }
509
510 else if ( cmd == "gotomyobj" )
511 {
512 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
513 else player.Pos = Mapper.Users[ player.ID ].Object.Pos;
514 }
515
516
517 else if ( cmd == "objinfo" )
518 {
519 if ( !Mapper.Users[ player.ID ] ) this.Error("You have not created an object.", player);
520 else
521 {
522 this.Message( Mapper.Users[ player.ID ].Object.Pos + " " + Mapper.Users[ player.ID ].Object.RotationEuler);
523 this.Message( Mapper.Users[ player.ID ].Object.Rotation.x + " " + Mapper.Users[ player.ID ].Object.Rotation.y + " " + Mapper.Users[ player.ID ].Object.Rotation.z);
524 }
525 }
526 }
527}