hedgewars/uScript.pas
changeset 2814 71e80c6e74bb
parent 2799 558b29bf00c5
child 2862 1d15ca7b7f64
equal deleted inserted replaced
2813:25213bcab42f 2814:71e80c6e74bb
   147 			lua_pushinteger(L, ord(gear^.Kind))
   147 			lua_pushinteger(L, ord(gear^.Kind))
   148 		end;
   148 		end;
   149 	lc_getgeartype:= 1
   149 	lc_getgeartype:= 1
   150 end;
   150 end;
   151 
   151 
       
   152 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
       
   153 var gear : PGear;
       
   154 begin
       
   155 	if lua_gettop(L) <> 2 then
       
   156 		begin
       
   157 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
       
   158 		end
       
   159 	else
       
   160 		begin
       
   161 		gear:= GearByUID(lua_tointeger(L, 1));
       
   162 		if (gear <> nil) and (gear^.Kind = gtHedgehog) then gear^.Health:= lua_tointeger(L, 2)
       
   163 		end;
       
   164 	lc_sethealth:= 0
       
   165 end;
       
   166 
   152 function lc_endgame(L : Plua_State) : LongInt; Cdecl;
   167 function lc_endgame(L : Plua_State) : LongInt; Cdecl;
   153 begin
   168 begin
   154 	GameState:= gsExit;
   169 	GameState:= gsExit;
   155 	lc_endgame:= 0
   170 	lc_endgame:= 0
   156 end;
   171 end;
   365 		lua_pcall(luaState, 0, 0, 0);
   380 		lua_pcall(luaState, 0, 0, 0);
   366 		ScriptLoaded:= true
   381 		ScriptLoaded:= true
   367 		end
   382 		end
   368 end;
   383 end;
   369 
   384 
       
   385 procedure SetGlobals;
       
   386 begin
       
   387 	ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
       
   388 end;
       
   389 
       
   390 procedure GetGlobals;
       
   391 begin
       
   392 	TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
       
   393 end;
       
   394 
   370 procedure ScriptCall(fname : string);
   395 procedure ScriptCall(fname : string);
   371 begin
   396 begin
   372 	if not ScriptLoaded then
   397 	if not ScriptLoaded then
   373 		exit;
   398 		exit;
       
   399 	SetGlobals;
   374 	lua_getglobal(luaState, Str2PChar(fname));
   400 	lua_getglobal(luaState, Str2PChar(fname));
   375 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   401 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   376 		begin
   402 		begin
   377 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   403 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   378 		lua_pop(luaState, 1)
   404 		lua_pop(luaState, 1)
   379 		end;
   405 		end;
       
   406 	GetGlobals;
   380 end;
   407 end;
   381 
   408 
   382 function ScriptCall(fname : string; par1: LongInt) : LongInt;
   409 function ScriptCall(fname : string; par1: LongInt) : LongInt;
   383 begin
   410 begin
   384 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   411 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   396 
   423 
   397 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
   424 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
   398 begin
   425 begin
   399 	if not ScriptLoaded then
   426 	if not ScriptLoaded then
   400 		exit;
   427 		exit;
   401 
   428 	SetGlobals;
   402 	lua_getglobal(luaState, Str2PChar(fname));
   429 	lua_getglobal(luaState, Str2PChar(fname));
   403 	lua_pushinteger(luaState, par1);
   430 	lua_pushinteger(luaState, par1);
   404 	lua_pushinteger(luaState, par2);
   431 	lua_pushinteger(luaState, par2);
   405 	lua_pushinteger(luaState, par3);
   432 	lua_pushinteger(luaState, par3);
   406 	lua_pushinteger(luaState, par4);
   433 	lua_pushinteger(luaState, par4);
   413 	else
   440 	else
   414 		begin
   441 		begin
   415 		ScriptCall:= lua_tointeger(luaState, -1);
   442 		ScriptCall:= lua_tointeger(luaState, -1);
   416 		lua_pop(luaState, 1)
   443 		lua_pop(luaState, 1)
   417 		end;
   444 		end;
       
   445 	GetGlobals;
   418 end;
   446 end;
   419 
   447 
   420 procedure ScriptPrepareAmmoStore;
   448 procedure ScriptPrepareAmmoStore;
   421 var i: ShortInt;
   449 var i: ShortInt;
   422 begin
   450 begin
   514 lua_register(luaState, 'HideMission', @lc_hidemission);
   542 lua_register(luaState, 'HideMission', @lc_hidemission);
   515 lua_register(luaState, 'SetAmmo', @lc_setammo);
   543 lua_register(luaState, 'SetAmmo', @lc_setammo);
   516 lua_register(luaState, 'PlaySound', @lc_playsound);
   544 lua_register(luaState, 'PlaySound', @lc_playsound);
   517 lua_register(luaState, 'AddTeam', @lc_addteam);
   545 lua_register(luaState, 'AddTeam', @lc_addteam);
   518 lua_register(luaState, 'AddHog', @lc_addhog);
   546 lua_register(luaState, 'AddHog', @lc_addhog);
       
   547 lua_register(luaState, 'SetHealth', @lc_sethealth);
   519 
   548 
   520 ScriptClearStack; // just to be sure stack is empty
   549 ScriptClearStack; // just to be sure stack is empty
   521 ScriptLoaded:= false;
   550 ScriptLoaded:= false;
   522 end;
   551 end;
   523 
   552