hedgewars/uScript.pas
changeset 2999 30c4d62cd0c3
parent 2997 ca60b5638fff
child 3003 0afdba08a858
equal deleted inserted replaced
2998:5b74906c14bb 2999:30c4d62cd0c3
    30 procedure ScriptCall(fname : shortstring);
    30 procedure ScriptCall(fname : shortstring);
    31 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
    31 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
    32 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
    32 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
    33 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
    33 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
    34 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
    34 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
       
    35 function ScriptExists(fname : shortstring) : boolean;
    35 
    36 
    36 procedure init_uScript;
    37 procedure init_uScript;
    37 procedure free_uScript;
    38 procedure free_uScript;
    38 
    39 
    39 implementation
    40 implementation
   143     else
   144     else
   144         begin
   145         begin
   145         gear:= GearByUID(lua_tointeger(L, 1));
   146         gear:= GearByUID(lua_tointeger(L, 1));
   146         if gear <> nil then
   147         if gear <> nil then
   147             lua_pushinteger(L, ord(gear^.Kind))
   148             lua_pushinteger(L, ord(gear^.Kind))
       
   149         else
       
   150             lua_pushnil(L);
   148         end;
   151         end;
   149     lc_getgeartype:= 1
   152     lc_getgeartype:= 1
       
   153 end;
       
   154 
       
   155 function lc_gethogclan(L : Plua_State) : LongInt; Cdecl;
       
   156 var gear : PGear;
       
   157 begin
       
   158     if lua_gettop(L) <> 1 then
       
   159         begin
       
   160         WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogClan!');
       
   161         lua_pushnil(L); // return value on stack (nil)
       
   162         end
       
   163     else
       
   164         begin
       
   165         gear:= GearByUID(lua_tointeger(L, 1));
       
   166         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
   167             begin
       
   168             lua_pushinteger(L, PHedgehog(gear^.Hedgehog)^.Team^.Clan^.ClanIndex)
       
   169             end
       
   170         else
       
   171             lua_pushnil(L);
       
   172         end;
       
   173     lc_gethogclan:= 1
       
   174 end;
       
   175 
       
   176 function lc_gethogname(L : Plua_State) : LongInt; Cdecl;
       
   177 var gear : PGear;
       
   178 begin
       
   179     if lua_gettop(L) <> 1 then
       
   180         begin
       
   181         WriteLnToConsole('LUA: Wrong number of parameters passed to GetHogName!');
       
   182         lua_pushnil(L); // return value on stack (nil)
       
   183         end
       
   184     else
       
   185         begin
       
   186         gear:= GearByUID(lua_tointeger(L, 1));
       
   187         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
   188             begin
       
   189             lua_pushstring(L, str2pchar(PHedgehog(gear^.Hedgehog)^.Name))
       
   190             end
       
   191         else
       
   192             lua_pushnil(L);
       
   193         end;
       
   194     lc_gethogname:= 1
   150 end;
   195 end;
   151 
   196 
   152 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
   197 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
   153 var gear : PGear;
   198 var gear : PGear;
   154 begin
   199 begin
   336 var s, t : ansistring;
   381 var s, t : ansistring;
   337 begin
   382 begin
   338 // not required if there's no script to run
   383 // not required if there's no script to run
   339 if not ScriptLoaded then
   384 if not ScriptLoaded then
   340     exit;
   385     exit;
   341         
   386 
   342 // push game variables so they may be modified by the script
   387 // push game variables so they may be modified by the script
   343 ScriptSetInteger('GameFlags', GameFlags);
   388 ScriptSetInteger('GameFlags', GameFlags);
   344 ScriptSetString('Seed', cSeed);
   389 ScriptSetString('Seed', cSeed);
   345 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   390 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   346 ScriptSetInteger('CaseFreq', cCaseFactor);
   391 ScriptSetInteger('CaseFreq', cCaseFactor);
   368 if ScriptGetString('Map') <> '' then
   413 if ScriptGetString('Map') <> '' then
   369     ParseCommand('map ' + ScriptGetString('Map'), true);
   414     ParseCommand('map ' + ScriptGetString('Map'), true);
   370 if ScriptGetString('Theme') <> '' then
   415 if ScriptGetString('Theme') <> '' then
   371     ParseCommand('theme ' + ScriptGetString('Theme'), true);    
   416     ParseCommand('theme ' + ScriptGetString('Theme'), true);    
   372 
   417 
   373 ScriptPrepareAmmoStore;
   418 if ScriptExists('onAmmoStoreInit') then
   374 ScriptCall('onAmmoStoreInit');
   419     begin
   375 ScriptApplyAmmoStore;
   420     ScriptPrepareAmmoStore;
       
   421     ScriptCall('onAmmoStoreInit');
       
   422     ScriptApplyAmmoStore
       
   423     end;
       
   424 
       
   425 ScriptSetInteger('ClansCount', ClansCount)
   376 end;
   426 end;
   377 
   427 
   378 procedure ScriptLoad(name : shortstring);
   428 procedure ScriptLoad(name : shortstring);
   379 var ret : LongInt;
   429 var ret : LongInt;
   380 begin
   430 begin
   400 TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   450 TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   401 end;
   451 end;
   402 
   452 
   403 procedure ScriptCall(fname : shortstring);
   453 procedure ScriptCall(fname : shortstring);
   404 begin
   454 begin
   405 if not ScriptLoaded then
   455 if not ScriptLoaded or not ScriptExists(fname) then
   406     exit;
   456     exit;
   407 SetGlobals;
   457 SetGlobals;
   408 lua_getglobal(luaState, Str2PChar(fname));
   458 lua_getglobal(luaState, Str2PChar(fname));
   409 if lua_pcall(luaState, 0, 0, 0) <> 0 then
   459 if lua_pcall(luaState, 0, 0, 0) <> 0 then
   410     begin
   460     begin
   429 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   479 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   430 end;
   480 end;
   431 
   481 
   432 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   482 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   433 begin
   483 begin
   434 if not ScriptLoaded then
   484 if not ScriptLoaded or not ScriptExists(fname) then
   435     exit;
   485     exit;
   436 SetGlobals;
   486 SetGlobals;
   437 lua_getglobal(luaState, Str2PChar(fname));
   487 lua_getglobal(luaState, Str2PChar(fname));
   438 lua_pushinteger(luaState, par1);
   488 lua_pushinteger(luaState, par1);
   439 lua_pushinteger(luaState, par2);
   489 lua_pushinteger(luaState, par2);
   451     lua_pop(luaState, 1)
   501     lua_pop(luaState, 1)
   452     end;
   502     end;
   453 GetGlobals;
   503 GetGlobals;
   454 end;
   504 end;
   455 
   505 
       
   506 function ScriptExists(fname : shortstring) : boolean;
       
   507 begin
       
   508 if not ScriptLoaded then
       
   509     begin
       
   510     ScriptExists:= false;
       
   511     exit
       
   512     end;
       
   513 lua_getglobal(luaState, Str2PChar(fname));
       
   514 ScriptExists:= not lua_isnoneornil(luaState, -1);
       
   515 lua_pop(luaState, -1)
       
   516 end;
       
   517 
   456 procedure ScriptPrepareAmmoStore;
   518 procedure ScriptPrepareAmmoStore;
   457 var i: ShortInt;
   519 var i: ShortInt;
   458 begin
   520 begin
       
   521 // reset ammostore (quite unclean, but works?)
       
   522 free_uAmmos;
       
   523 init_uAmmos;
   459 ScriptAmmoStore:= '';
   524 ScriptAmmoStore:= '';
   460 for i:=1 to ord(High(TAmmoType)) do
   525 for i:=1 to ord(High(TAmmoType)) do
   461     ScriptAmmoStore:= ScriptAmmoStore + '0000';
   526     ScriptAmmoStore:= ScriptAmmoStore + '0000';
   462 end;
   527 end;
   463 
   528 
   558 lua_register(luaState, 'SetAmmo', @lc_setammo);
   623 lua_register(luaState, 'SetAmmo', @lc_setammo);
   559 lua_register(luaState, 'PlaySound', @lc_playsound);
   624 lua_register(luaState, 'PlaySound', @lc_playsound);
   560 lua_register(luaState, 'AddTeam', @lc_addteam);
   625 lua_register(luaState, 'AddTeam', @lc_addteam);
   561 lua_register(luaState, 'AddHog', @lc_addhog);
   626 lua_register(luaState, 'AddHog', @lc_addhog);
   562 lua_register(luaState, 'SetHealth', @lc_sethealth);
   627 lua_register(luaState, 'SetHealth', @lc_sethealth);
       
   628 lua_register(luaState, 'GetHogClan', @lc_gethogclan);
       
   629 lua_register(luaState, 'GetHogName', @lc_gethogname);
   563 
   630 
   564 ScriptClearStack; // just to be sure stack is empty
   631 ScriptClearStack; // just to be sure stack is empty
   565 ScriptLoaded:= false;
   632 ScriptLoaded:= false;
   566 end;
   633 end;
   567 
   634 
   609 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
   676 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
   610 begin
   677 begin
   611 ScriptCall:= 0
   678 ScriptCall:= 0
   612 end;
   679 end;
   613 
   680 
       
   681 function ScriptExists(fname : shortstring) : boolean;
       
   682 begin
       
   683 ScriptExists:= false
       
   684 end;
       
   685 
   614 procedure init_uScript;
   686 procedure init_uScript;
   615 begin
   687 begin
   616 end;
   688 end;
   617 
   689 
   618 procedure free_uScript;
   690 procedure free_uScript;