File: LuaLibGame.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (815 lines) | stat: -rw-r--r-- 27,074 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#include "LuaLibGame.h"
#include "helpers/VExcept.h"
#include "GameEvents.h"
#include "Input.h"
#include "Locales.h"
#include "xmscene/Scene.h"
#include "helpers/Log.h"
#include "xmscene/Level.h"
#include "xmscene/Block.h"
#include "xmscene/ScriptTimer.h"
#include "XMSession.h"

#define STIMER_DELAY_DEFAULT 100
#define STIMER_LOOPS_DEFAULT 0

luaL_Reg LuaLibGame::m_gameFuncs[] = {
  {"GetTime",                      LuaLibGame::L_Game_GetTime},
  {"Message",                      LuaLibGame::L_Game_Message},
  {"IsPlayerInZone",               LuaLibGame::L_Game_IsPlayerInZone},
  {"MoveBlock",                    LuaLibGame::L_Game_MoveBlock},
  {"GetBlockPos",                  LuaLibGame::L_Game_GetBlockPos},
  {"SetBlockPos",                  LuaLibGame::L_Game_SetBlockPos},
  {"SetPhysicsBlockPos",           LuaLibGame::L_Game_SetBlockPos},
  {"PlaceInGameArrow",             LuaLibGame::L_Game_PlaceInGameArrow},
  {"PlaceScreenArrow",             LuaLibGame::L_Game_PlaceScreenArrow},
  {"HideArrow",                    LuaLibGame::L_Game_HideArrow},
  {"ClearMessages",                LuaLibGame::L_Game_ClearMessages},
  {"SetGravity",                   LuaLibGame::L_Game_SetGravity},
  {"GetGravity",                   LuaLibGame::L_Game_GetGravity},
  {"SetPlayerPosition",            LuaLibGame::L_Game_SetPlayerPosition},
  {"AddForceToPlayer",             LuaLibGame::L_Game_AddForceToPlayer},
  {"GetPlayerPosition",            LuaLibGame::L_Game_GetPlayerPosition},
  {"GetEntityPos",                 LuaLibGame::L_Game_GetEntityPos},
  {"SetEntityPos",                 LuaLibGame::L_Game_SetEntityPos},
  {"SetKeyHook",                   LuaLibGame::L_Game_SetKeyHook},
  {"GetKeyByAction",               LuaLibGame::L_Game_GetKeyByAction},
  {"GetKeyByActionTech",           LuaLibGame::L_Game_GetKeyByActionTech},
  {"Log",                          LuaLibGame::L_Game_Log},
  {"SetBlockCenter",               LuaLibGame::L_Game_SetBlockCenter},
  {"SetBlockRotation",             LuaLibGame::L_Game_SetBlockRotation},
  {"SetDynamicEntitySelfRotation", LuaLibGame::L_Game_SetDynamicEntitySelfRotation},
  {"SetDynamicEntityRotation",     LuaLibGame::L_Game_SetDynamicEntityRotation},
  {"SetDynamicEntityTranslation",  LuaLibGame::L_Game_SetDynamicEntityTranslation},
  {"SetDynamicEntityNone",         LuaLibGame::L_Game_SetDynamicEntityNone},
  {"SetDynamicBlockRotation",      LuaLibGame::L_Game_SetDynamicBlockRotation},
  {"SetDynamicBlockSelfRotation",  LuaLibGame::L_Game_SetDynamicBlockSelfRotation},
  {"SetPhysicsBlockSelfRotation",  LuaLibGame::L_Game_SetPhysicsBlockSelfRotation},
  {"SetPhysicsBlockTranslation",   LuaLibGame::L_Game_SetPhysicsBlockTranslation},
  {"SetDynamicBlockTranslation",   LuaLibGame::L_Game_SetDynamicBlockTranslation},
  {"SetDynamicBlockNone",          LuaLibGame::L_Game_SetDynamicBlockNone},
  {"CameraZoom",        	   LuaLibGame::L_Game_CameraZoom},
  {"CameraMove",        	   LuaLibGame::L_Game_CameraMove},
  {"GetEntityRadius",       	   LuaLibGame::L_Game_GetEntityRadius},
  {"IsEntityTouched",       	   LuaLibGame::L_Game_IsEntityTouched},
  {"KillPlayer",                   LuaLibGame::L_Game_KillPlayer},
  {"KillEntity",                   LuaLibGame::L_Game_KillEntity},
  {"RemainingStrawberries",        LuaLibGame::L_Game_RemainingStrawberries},
  {"WinPlayer",                    LuaLibGame::L_Game_WinPlayer},
  {"AddPenaltyTime",               LuaLibGame::L_Game_PenaltyTime},
  {"IsAPlayerInZone",              LuaLibGame::L_Game_IsAPlayerInZone},
  {"SetAPlayerPosition", 	   LuaLibGame::L_Game_SetAPlayerPosition},
  {"GetAPlayerPosition", 	   LuaLibGame::L_Game_GetAPlayerPosition},
  {"KillAPlayer", 		   LuaLibGame::L_Game_KillAPlayer},
  {"WinAPlayer",  		   LuaLibGame::L_Game_WinAPlayer},
  {"NumberOfPlayers",              LuaLibGame::L_Game_NumberOfPlayers},
  {"CameraRotate",                 LuaLibGame::L_Game_CameraRotate},
  {"CameraAdaptToGravity",         LuaLibGame::L_Game_CameraAdaptToGravity},
  {"SetCameraRotationSpeed",       LuaLibGame::L_Game_SetCameraRotationSpeed},
  {"PlaySound",                    LuaLibGame::L_Game_PlaySound},
  {"PlayMusic",                    LuaLibGame::L_Game_PlayMusic},
  {"StopMusic",                    LuaLibGame::L_Game_StopMusic},
  {"GetPlayerVelocity",            LuaLibGame::L_Game_GetPlayerVelocity},
  {"GetPlayerSpeed",               LuaLibGame::L_Game_GetPlayerSpeed},
  {"GetPlayerAngle",               LuaLibGame::L_Game_GetPlayerAngle}, 
  {"GetPlayerProfileName",         LuaLibGame::L_Game_GetPlayerProfileName},
  {"StartTimer",                   LuaLibGame::L_Game_StartTimer},   
  {"SetTimerDelay",                LuaLibGame::L_Game_SetTimerDelay}, 
  {"StopTimer",                    LuaLibGame::L_Game_StopTimer}, 
  {"SetCameraPosition",            LuaLibGame::L_Game_SetCameraPosition}, 
  {NULL, NULL}
};

Scene*        LuaLibGame::m_exec_world              = NULL;
InputHandler* LuaLibGame::m_exec_activeInputHandler = NULL;

LuaLibGame::LuaLibGame(Scene *i_pScene) : LuaLibBase("Game", m_gameFuncs) {
  m_pScene              = i_pScene;
  m_pActiveInputHandler = InputHandler::instance();
}

LuaLibGame::~LuaLibGame() {
}

void LuaLibGame::setInstance() {
  m_exec_world              = m_pScene;
  m_exec_activeInputHandler = m_pActiveInputHandler;
}

/*===========================================================================
  Game.* 
  Lua game library functions
  ===========================================================================*/
int LuaLibGame::L_Game_Log(lua_State *pL) {
  /* no event for this */

  std::string Out;

  for(int i=0; i<args_numberOfArguments(pL); i++)
    Out.append(luaL_checkstring(pL, i+1));

  LogInfo((char *)Out.c_str());

  return 0;    
}
  
int LuaLibGame::L_Game_ClearMessages(lua_State *pL) {
  args_CheckNumberOfArguments(pL, 0);

  /* event for this */
  m_exec_world->createGameEvent(new MGE_ClearMessages(m_exec_world->getTime()));
  return 0;
}

int LuaLibGame::L_Game_PlaceInGameArrow(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_PlaceInGameArrow(m_exec_world->getTime(),
							X_luaL_check_number(pL,1),
							X_luaL_check_number(pL,2),
							X_luaL_check_number(pL,3)));
  return 0;
}

int LuaLibGame::L_Game_PlaceScreenArrow(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_PlaceScreenarrow(m_exec_world->getTime(),
							X_luaL_check_number(pL,1),
							X_luaL_check_number(pL,2),
							X_luaL_check_number(pL,3)));
  return 0;
}

int LuaLibGame::L_Game_HideArrow(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_HideArrow(m_exec_world->getTime()));
  return 0;
}
  
int LuaLibGame::L_Game_GetTime(lua_State *pL) {
  args_CheckNumberOfArguments(pL, 0);

  /* no event for this */

  /* Get current game time */
  lua_pushnumber(pL,m_exec_world->getTime() / 100.0);
  return 1;
}
  
int LuaLibGame::L_Game_Message(lua_State *pL) {  
  /* event for this */

  /* Convert all arguments to strings */
  std::string Out;
  std::string Temp;
  for(int i=0;i<lua_gettop(pL);i++) {
    Temp = luaL_checkstring(pL, i+1);
    if(Temp != "") {
      Out.append(_(Temp.c_str()));
    }
  }  

  m_exec_world->createGameEvent(new MGE_Message(m_exec_world->getTime(), Out));
  return 0;
}  
  
int LuaLibGame::L_Game_IsPlayerInZone(lua_State *pL) {
  /* no event for this */
  bool res = false;
  Zone* v_zone;

  try {
    v_zone = m_exec_world->getLevelSrc()->getZoneById(luaL_checkstring(pL, 1));

    for(unsigned int i=0; i<m_exec_world->Players().size(); i++) {
      if(m_exec_world->Players()[i]->isTouching(v_zone)) {
	res = true;
      }
    }
  } catch(Exception &e) {
    /* res will be false */
  }

  lua_pushboolean(pL, res?1:0);
  return 1;
}
  
int LuaLibGame::L_Game_MoveBlock(lua_State *pL) {
  /* event for this */

  m_exec_world->createGameEvent(new MGE_MoveBlock(m_exec_world->getTime(),
						 luaL_checkstring(pL,1),
						 X_luaL_check_number(pL,2),
						 X_luaL_check_number(pL,3)));
  return 0;
}
  
int LuaLibGame::L_Game_GetBlockPos(lua_State *pL) {
  /* no event for this */

  /* Find the specified block and return its position */
  Block* pBlock;

  try {
    pBlock = m_exec_world->getLevelSrc()->getBlockById(luaL_checkstring(pL,1));
    lua_pushnumber(pL,pBlock->DynamicPosition().x);
    lua_pushnumber(pL,pBlock->DynamicPosition().y);
  } catch(Exception &e) {
    lua_pushnumber(pL, 0);
    lua_pushnumber(pL, 0);
  }

  return 2;
}
  
int LuaLibGame::L_Game_SetBlockPos(lua_State *pL) {
  /* event for this */

  m_exec_world->createGameEvent(new MGE_SetBlockPos(m_exec_world->getTime(),
						   luaL_checkstring(pL,1),
						   X_luaL_check_number(pL,2),
						   X_luaL_check_number(pL,3)));
  return 0;
}  

int LuaLibGame::L_Game_SetGravity(lua_State *pL) {
  /* event for this */

  m_exec_world->createGameEvent(new MGE_SetGravity(m_exec_world->getTime(),
						  X_luaL_check_number(pL,1),
						  X_luaL_check_number(pL,2)));
  return 0;
}  

int LuaLibGame::L_Game_GetGravity(lua_State *pL) {
  /* no event for this */

  /* Get gravity */
  lua_pushnumber(pL, m_exec_world->getGravity().x);
  lua_pushnumber(pL, m_exec_world->getGravity().y);    
  return 2;
}

int LuaLibGame::L_Game_SetPlayerPosition(lua_State *pL) {
  /* event for this */
  bool bRight = X_luaL_check_number(pL,3) > 0.0f;
  m_exec_world->createGameEvent(new MGE_SetPlayersPosition(m_exec_world->getTime(),
								 X_luaL_check_number(pL,1),
								 X_luaL_check_number(pL,2),
								 bRight));
  return 0;
}  
  
int LuaLibGame::L_Game_GetPlayerPosition(lua_State *pL) {
  /* no event for this */
  float x = 0.0, y = 0.0;
  DriveDir v_direction = DD_RIGHT;

  if(m_exec_world->Players().size() > 0) {
    x = m_exec_world->Players()[0]->getState()->CenterP.x;
    y = m_exec_world->Players()[0]->getState()->CenterP.y;
    v_direction = m_exec_world->Players()[0]->getState()->Dir;
  }

  lua_pushnumber(pL, x);
  lua_pushnumber(pL, y);
  lua_pushnumber(pL, v_direction);

  return 3;
}

int LuaLibGame::L_Game_GetEntityPos(lua_State *pL) {
  /* no event for this */

  /* Find the specified entity and return its position */
  Entity *p;

  try {
    p = m_exec_world->getLevelSrc()->getEntityById(luaL_checkstring(pL,1));
  } catch(Exception &e) {
    p = NULL;
  }

  if(p != NULL) {
    lua_pushnumber(pL,p->DynamicPosition().x);
    lua_pushnumber(pL,p->DynamicPosition().y);
    return 2;
  }

  /* Entity not found, return <0,0> */
  lua_pushnumber(pL,0);
  lua_pushnumber(pL,0);
  return 2;
}  
  
int LuaLibGame::L_Game_GetEntityRadius(lua_State *pL) {
  /* no event for this */
  try {
    lua_pushnumber(pL, m_exec_world->getLevelSrc()->getEntityById(luaL_checkstring(pL,1))->Size());
  } catch(Exception &e) {
    lua_pushnumber(pL, 0);
  }
  return 1;
}

int LuaLibGame::L_Game_IsEntityTouched(lua_State *pL) {
  /* no event for this */

  bool v_touch = false;
  if(m_exec_world->Players().size() > 0) {
    try {
      v_touch = m_exec_world->Players()[0]->isTouching(m_exec_world->getLevelSrc()->getEntityById(luaL_checkstring(pL,1)));
    } catch(Exception &e) {
      /* v_touch will be false */
    }
  }

  lua_pushnumber(pL, v_touch? 1:0);
  return 1;
}


int LuaLibGame::L_Game_SetEntityPos(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetEntityPos(m_exec_world->getTime(),
						    luaL_checkstring(pL,1),
						    X_luaL_check_number(pL,2),
						    X_luaL_check_number(pL,3)));
  return 0;
}  
  
int LuaLibGame::L_Game_SetKeyHook(lua_State *pL) {
  /* no event for this */

  if(m_exec_activeInputHandler != NULL) {
    try {
      m_exec_activeInputHandler->addScriptKeyHook(m_exec_world, luaL_checkstring(pL,1), luaL_checkstring(pL,2));
    } catch(Exception &e) {
      std::string v_key  = luaL_checkstring(pL,1);
      std::string v_func = luaL_checkstring(pL,2);
      std::string v_msg = "SetKeyHook(key=" + v_key + ", function=" + v_func + ") failed\n" + e.getMsg();
	luaL_error (pL, v_msg.c_str());
    }
  }
  return 0;
}

int LuaLibGame::L_Game_GetKeyByAction(lua_State *pL) {
  /* no event for this */

  if(m_exec_activeInputHandler != NULL) {
    lua_pushstring(pL,m_exec_activeInputHandler->getKeyByAction(luaL_checkstring(pL,1)).c_str());
    return 1;
  }
  return 0;
}  

int LuaLibGame::L_Game_GetKeyByActionTech(lua_State *pL) {
  /* no event for this */

  if(m_exec_activeInputHandler != NULL) {
    lua_pushstring(pL,m_exec_activeInputHandler->getKeyByAction(luaL_checkstring(pL,1), true).c_str());
    return 1;
  }
  return 0;
}  

int LuaLibGame::L_Game_SetBlockCenter(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetBlockCenter(m_exec_world->getTime(),
						      luaL_checkstring(pL,1),
						      X_luaL_check_number(pL,2),
						      X_luaL_check_number(pL,3)));
  return 0;
}  
    
int LuaLibGame::L_Game_SetBlockRotation(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetBlockRotation(m_exec_world->getTime(),
							luaL_checkstring(pL,1),
							X_luaL_check_number(pL,2)));
  return 0;
}  
    
int LuaLibGame::L_Game_SetDynamicEntityRotation(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetDynamicEntityRotation(m_exec_world->getTime(),
								 luaL_checkstring(pL,1),
								 X_luaL_check_number(pL,2),
								 X_luaL_check_number(pL,3),
								 (int)(X_luaL_check_number(pL,4)),
								 (int)X_luaL_check_number(pL,5),
								 (int)X_luaL_check_number(pL,6))); 
  return 0;
}

int LuaLibGame::L_Game_SetDynamicEntitySelfRotation(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetDynamicEntitySelfRotation(m_exec_world->getTime(),
								     luaL_checkstring(pL,1),
								     (int)(X_luaL_check_number(pL,2)),
								     (int)X_luaL_check_number(pL,3),
								     (int)X_luaL_check_number(pL,4))); 
  return 0;
}

int LuaLibGame::L_Game_SetDynamicEntityTranslation(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetDynamicEntityTranslation(m_exec_world->getTime(),
								    luaL_checkstring(pL,1),
								    X_luaL_check_number(pL,2),
								    X_luaL_check_number(pL,3),
								    (int)(X_luaL_check_number(pL,4)),
								    (int)X_luaL_check_number(pL,5),
								    (int)X_luaL_check_number(pL,6)));
  return 0;
}

int LuaLibGame::L_Game_SetDynamicEntityNone(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetDynamicEntityNone(m_exec_world->getTime(),
							    luaL_checkstring(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_SetDynamicBlockRotation(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetDynamicBlockRotation(m_exec_world->getTime(),
								luaL_checkstring(pL,1),
								X_luaL_check_number(pL,2),
								X_luaL_check_number(pL,3),
								(int)(X_luaL_check_number(pL,4)),
								(int)X_luaL_check_number(pL,5),
								(int)X_luaL_check_number(pL,6)));
  return 0;
}

int LuaLibGame::L_Game_SetDynamicBlockSelfRotation(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetDynamicBlockSelfRotation(m_exec_world->getTime(),
								    luaL_checkstring(pL,1),
								    (int)(X_luaL_check_number(pL,2)),
								    (int)X_luaL_check_number(pL,3),
								    (int)X_luaL_check_number(pL,4))); 
  return 0;
}

int LuaLibGame::L_Game_SetPhysicsBlockSelfRotation(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_SetPhysicsBlockSelfRotation(m_exec_world->getTime(),
								    luaL_checkstring(pL,1),
								    (int)(X_luaL_check_number(pL,2)),
								    (int)X_luaL_check_number(pL,3),
								    (int)X_luaL_check_number(pL,4))); 
  return 0;
}

int LuaLibGame::L_Game_SetPhysicsBlockTranslation(lua_State* pL)
{
  m_exec_world->createGameEvent(new MGE_SetPhysicsBlockTranslation(m_exec_world->getTime(),
								   luaL_checkstring(pL, 1),
								   X_luaL_check_number(pL, 2),
								   X_luaL_check_number(pL, 3),
								   (int)X_luaL_check_number(pL, 4),
								   (int)X_luaL_check_number(pL, 5),
								   (int)X_luaL_check_number(pL, 6)));

  return 0;
}

int LuaLibGame::L_Game_SetDynamicBlockTranslation(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetDynamicBlockTranslation(m_exec_world->getTime(),
								   luaL_checkstring(pL,1),
								   X_luaL_check_number(pL,2),
								   X_luaL_check_number(pL,3),
								   (int)(X_luaL_check_number(pL,4)),
								   (int)X_luaL_check_number(pL,5),
								   (int)X_luaL_check_number(pL,6)));
  return 0;
}

int LuaLibGame::L_Game_SetDynamicBlockNone(lua_State *pL) {
  /* event for this */    
  m_exec_world->createGameEvent(new MGE_SetDynamicBlockNone(m_exec_world->getTime(),
							   luaL_checkstring(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_CameraZoom(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_CameraZoom(m_exec_world->getTime(),
						  X_luaL_check_number(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_CameraMove(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_CameraMove(m_exec_world->getTime(),
						  X_luaL_check_number(pL,1),
						  X_luaL_check_number(pL,2)));
  return 0;
}

int LuaLibGame::L_Game_SetCameraPosition(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_CameraSetPos(m_exec_world->getTime(),
						  X_luaL_check_number(pL,1),
						  X_luaL_check_number(pL,2)));
  return 0;
}

int LuaLibGame::L_Game_KillPlayer(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_PlayersDie(m_exec_world->getTime(), false));
  return 0;
}

int LuaLibGame::L_Game_KillEntity(lua_State *pL) {
  m_exec_world->createExternalKillEntityEvent(luaL_checkstring(pL,1));
  return 0;
}

int LuaLibGame::L_Game_RemainingStrawberries(lua_State *pL) {
  /* no event for this */
  lua_pushnumber(pL,m_exec_world->getNbRemainingStrawberries());
  return 1;
}

int LuaLibGame::L_Game_WinPlayer(lua_State *pL) {
  for(unsigned int i=0; i<m_exec_world->Players().size(); i++) {
    m_exec_world->makePlayerWin(i);
  }
  return 0;
}

int LuaLibGame::L_Game_PenaltyTime(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_PenalityTime(m_exec_world->getTime(),
						     (int)(X_luaL_check_number(pL,1)*100)));
  return 0;
}

int LuaLibGame::L_Game_IsAPlayerInZone(lua_State *pL) {
  /* no event for this */
  Zone* v_zone;
  int v_player;

  try {
    v_zone = m_exec_world->getLevelSrc()->getZoneById(luaL_checkstring(pL, 1));
    v_player = (int)X_luaL_check_number(pL, 2);
    
  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
    luaL_error (pL, "Invalid player number");
  }

    lua_pushboolean(pL, m_exec_world->Players()[v_player]->isTouching(v_zone)?1:0);
  } catch(Exception &e) {
    lua_pushboolean(pL, 0);
  }

  return 1;
}

int LuaLibGame::L_Game_SetAPlayerPosition(lua_State *pL) {
  /* event for this */
  bool bRight  = X_luaL_check_number(pL,3) > 0.0f;
  int v_player = (int)X_luaL_check_number(pL,4);

  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }

  m_exec_world->createGameEvent(new MGE_SetPlayerPosition(m_exec_world->getTime(),
								X_luaL_check_number(pL,1),
								X_luaL_check_number(pL,2),
								bRight,
								v_player));
  return 0;
}

int LuaLibGame::L_Game_GetAPlayerPosition(lua_State *pL) {
  /* no event for this */
  float x = 0.0, y = 0.0;
  DriveDir v_direction = DD_RIGHT;

  int v_player = (int)X_luaL_check_number(pL,1);

  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }

  x = m_exec_world->Players()[v_player]->getState()->CenterP.x;
  y = m_exec_world->Players()[v_player]->getState()->CenterP.y;
  v_direction = m_exec_world->Players()[v_player]->getState()->Dir;

  lua_pushnumber(pL, x);
  lua_pushnumber(pL, y);
  lua_pushnumber(pL, v_direction);

  return 3;
}

int LuaLibGame::L_Game_KillAPlayer(lua_State *pL) {
  int v_player = (int)X_luaL_check_number(pL,1);

  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }

  m_exec_world->createGameEvent(new MGE_PlayerDies(m_exec_world->getTime(), false, v_player));
  return 0;
}

int LuaLibGame::L_Game_WinAPlayer(lua_State *pL) {
  int v_player = (int)X_luaL_check_number(pL,1);

  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }

  m_exec_world->makePlayerWin(v_player);
  return 0;
}

int LuaLibGame::L_Game_NumberOfPlayers(lua_State *pL) {
  lua_pushnumber(pL, m_exec_world->Players().size());
  return 1;
}


int LuaLibGame::L_Game_CameraRotate(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_CameraRotate(m_exec_world->getTime(),
							   X_luaL_check_number(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_CameraAdaptToGravity(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_CameraAdaptToGravity(m_exec_world->getTime()));
  return 0;
}

int LuaLibGame::L_Game_AddForceToPlayer(lua_State *pL) {
  /* event for this */
  m_exec_world->createGameEvent(new MGE_AddForceToPlayer(m_exec_world->getTime(),
							 Vector2f(X_luaL_check_number(pL,1),
								  X_luaL_check_number(pL,2)),
							 (int)X_luaL_check_number(pL,3),
							 (int)X_luaL_check_number(pL,4),
							 (int)X_luaL_check_number(pL,5)));
  return 0;
}  

int LuaLibGame::L_Game_SetCameraRotationSpeed(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_SetCameraRotationSpeed(m_exec_world->getTime(),X_luaL_check_number(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_PlaySound(lua_State *pL) {
  if(lua_gettop(pL) == 1) { // if there are 2 arguments, consider the 2nd one
    m_exec_world->createGameEvent(new MGE_PlaySound(m_exec_world->getTime(),luaL_checkstring(pL,1)));
  } else {
    m_exec_world->createGameEvent(new MGE_PlaySound(m_exec_world->getTime(),luaL_checkstring(pL,1), X_luaL_check_number(pL,2)));
  }

  return 0;
}

int LuaLibGame::L_Game_PlayMusic(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_PlayMusic(m_exec_world->getTime(),luaL_checkstring(pL,1)));
  return 0;
}

int LuaLibGame::L_Game_StopMusic(lua_State *pL) {
  m_exec_world->createGameEvent(new MGE_StopMusic(m_exec_world->getTime()));
  return 0;
}

/* by Tuhoojabotti */
int LuaLibGame::L_Game_GetPlayerVelocity(lua_State *pL) {
  /* no event for this */
  int v_player = (int)X_luaL_check_number(pL,1);
  
  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }
  lua_pushnumber(pL, m_exec_world->Players()[v_player]->getBikeLinearVel());

  return 1;
}
/* by Tuhoojabotti */
int LuaLibGame::L_Game_GetPlayerSpeed(lua_State *pL) {
  /* no event for this */
  int v_player = (int)X_luaL_check_number(pL,1);
  
  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }
  lua_pushnumber(pL, m_exec_world->Players()[v_player]->getBikeEngineSpeed());

  return 1;
}
/* by Tuhoojabotti */
int LuaLibGame::L_Game_GetPlayerAngle(lua_State *pL) {
  /* no event for this */
  int v_player = (int)X_luaL_check_number(pL,1);
  
  if(v_player < 0 || (unsigned int)v_player >= m_exec_world->Players().size()) {
		luaL_error (pL, "Invalid player number");
  }
  lua_pushnumber(pL, m_exec_world->Players()[v_player]->getAngle());

  return 1; //return 1 value
}

int LuaLibGame::L_Game_GetPlayerProfileName(lua_State *pl) {
  /* no event for this */
  std::string v_profileName;
  // deprecated cause people used a test on it to make the level harder
  //v_profileName = XMSession::instance()->profile();
  v_profileName = "*deprecated*";
  lua_pushstring(pl,v_profileName.c_str());
  
  return 1;
}

/*=====================================================
        Script Timer Functions! by tuhoojabotti
=====================================================*/
//START
int LuaLibGame::L_Game_StartTimer(lua_State *pL) {
	args_CheckNumberOfArguments(pL, 1, 3); //we need 1 or 3 args
	//set the defaults
  int v_numargs=args_numberOfArguments(pL); 
  std::string v_name="";
	int v_delay = STIMER_DELAY_DEFAULT;
	int v_loops=STIMER_LOOPS_DEFAULT;
	ScriptTimer* v_timer=NULL;
  //get parameters
	v_name=(std::string)luaL_checkstring(pL,1);
	v_timer=m_exec_world->getScriptTimerByName(v_name);
	//check the optional args
	if(v_numargs>1){
		v_delay=(int)luaL_checknumber(pL,2);
		if(v_delay<1){luaL_error (pL, "StartTimer: delay can't be smaller than 1.");}
	}
	if(v_numargs>2){
		v_loops=(int)luaL_checknumber(pL,3);
		if(v_loops<0){luaL_error (pL, "StartTimer: loops can't be smaller than 0.");}
	}
	//if timer is not found
	if(v_timer==NULL){
		m_exec_world->createScriptTimer(v_name,v_delay,v_loops); //create timer
	}else{
		if(v_numargs == 1){ //only name given
			v_timer->StartTimer(); //start the timer
		}else{ //numarg is not 1 so it can only be 2 or 3 which is okay
			v_timer->ResetTimer(v_delay,v_loops,m_exec_world->getTime());
			/* For example: if only delay is given then the loops=0 (default) */
		}
	}
  return 0; //return no values to the script
}
//SET DELAY
int LuaLibGame::L_Game_SetTimerDelay(lua_State *pL) {
	args_CheckNumberOfArguments(pL, 2, 2); //exactly 2 args needed
  std::string v_name=(std::string)luaL_checkstring(pL,1);
	int v_delay=(int)luaL_checknumber(pL,2);
	ScriptTimer* v_timer=m_exec_world->getScriptTimerByName(v_name);
	if(v_timer!=NULL){
		v_timer->SetTimerDelay(v_delay);
	}else{
		luaL_error (pL, "An error occurred, timer doesn't exist!");
	}
	return 0; //return no values to the script
}
//STOP
int LuaLibGame::L_Game_StopTimer(lua_State *pL) {
	args_CheckNumberOfArguments(pL, 1, 1); //exactly 1 arg needed
  std::string v_name = (std::string)luaL_checkstring(pL,1); //get name
  ScriptTimer* v_timer=m_exec_world->getScriptTimerByName(v_name);
  if(v_timer!=NULL){ //timer is found
		v_timer->PauseTimer(); //pause it
  }else{
		luaL_error (pL, "An error occurred, timer doesn't exist!");
	}
  return 0; //return no values to the script
}