hedgewars/uScript.pas
branchqmlfrontend
changeset 10606 8e95911cb86b
parent 10603 bda5c7caf396
child 10611 58cad46782ff
equal deleted inserted replaced
10519:af019fa70080 10606:8e95911cb86b
   305         end
   305         end
   306     else
   306     else
   307         LuaToSpriteOrd:= i;
   307         LuaToSpriteOrd:= i;
   308 end;
   308 end;
   309 
   309 
       
   310 function LuaToMapGenOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   311 begin
       
   312     if lua_isnoneornil(L, i) then i:= -1
       
   313     else i:= lua_tointeger(L, i);
       
   314     if (i < ord(Low(TMapGen))) or (i > ord(High(TMapGen))) then
       
   315         begin
       
   316         LuaCallError('Invalid mapgen id!', call, paramsyntax);
       
   317         LuaToMapGenOrd:= -1;
       
   318         end
       
   319     else
       
   320         LuaToMapGenOrd:= i;
       
   321 end;
       
   322 
   310 // wrapped calls //
   323 // wrapped calls //
   311 
   324 
   312 // functions called from Lua:
   325 // functions called from Lua:
   313 // function(L : Plua_State) : LongInt; Cdecl;
   326 // function(L : Plua_State) : LongInt; Cdecl;
   314 // where L contains the state, returns the number of return values on the stack
   327 // where L contains the state, returns the number of return values on the stack
  1025     hh   : THedgehog;
  1038     hh   : THedgehog;
  1026     i, j : LongInt;
  1039     i, j : LongInt;
  1027 begin
  1040 begin
  1028     if CheckLuaParamCount(L, 2, 'SetClanColor', 'clan, color') then
  1041     if CheckLuaParamCount(L, 2, 'SetClanColor', 'clan, color') then
  1029         begin
  1042         begin
  1030         clan := ClansArray[lua_tointeger(L, 1)];
  1043 	i:= lua_tointeger(L,1);
       
  1044 	if i >= ClansCount then exit(0);
       
  1045         clan := ClansArray[i];
  1031         clan^.Color:= lua_tointeger(L, 2) shr 8;
  1046         clan^.Color:= lua_tointeger(L, 2) shr 8;
  1032 
  1047 
  1033         for i:= 0 to Pred(clan^.TeamsNumber) do
  1048         for i:= 0 to Pred(clan^.TeamsNumber) do
  1034             begin
  1049             begin
  1035             team:= clan^.Teams[i];
  1050             team:= clan^.Teams[i];
  1045                 end;
  1060                 end;
  1046             FreeTexture(team^.NameTagTex);
  1061             FreeTexture(team^.NameTagTex);
  1047             team^.NameTagTex:= RenderStringTex(ansistring(clan^.Teams[i]^.TeamName), clan^.Color, fnt16);
  1062             team^.NameTagTex:= RenderStringTex(ansistring(clan^.Teams[i]^.TeamName), clan^.Color, fnt16);
  1048             end;
  1063             end;
  1049 
  1064 
       
  1065 	FreeTexture(clan^.HealthTex);
  1050         clan^.HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, clan^.Teams[0]^.NameTagTex^.h, clan^.Color);
  1066         clan^.HealthTex:= makeHealthBarTexture(cTeamHealthWidth + 5, clan^.Teams[0]^.NameTagTex^.h, clan^.Color);
  1051         end;
  1067         end;
  1052 
  1068 
  1053     lc_setclancolor:= 0
  1069     lc_setclancolor:= 0
  1054 end;
  1070 end;
  1137             lua_pushnil(L);
  1153             lua_pushnil(L);
  1138         end
  1154         end
  1139     else
  1155     else
  1140         lua_pushnil(L); // return value on stack (nil)
  1156         lua_pushnil(L); // return value on stack (nil)
  1141     lc_gettimer:= 1
  1157     lc_gettimer:= 1
       
  1158 end;
       
  1159 
       
  1160 function lc_getflighttime(L : Plua_State) : LongInt; Cdecl;
       
  1161 var gear : PGear;
       
  1162 begin
       
  1163     if CheckLuaParamCount(L, 1, 'GetFlightTime', 'gearUid') then
       
  1164         begin
       
  1165         gear:= GearByUID(lua_tointeger(L, 1));
       
  1166         if gear <> nil then
       
  1167             lua_pushinteger(L, gear^.FlightTime)
       
  1168         else
       
  1169             lua_pushnil(L);
       
  1170         end
       
  1171     else
       
  1172         lua_pushnil(L); // return value on stack (nil)
       
  1173     lc_getflighttime:= 1
  1142 end;
  1174 end;
  1143 
  1175 
  1144 function lc_gethealth(L : Plua_State) : LongInt; Cdecl;
  1176 function lc_gethealth(L : Plua_State) : LongInt; Cdecl;
  1145 var gear : PGear;
  1177 var gear : PGear;
  1146 begin
  1178 begin
  1235             else
  1267             else
  1236                 s:= 0;
  1268                 s:= 0;
  1237             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
  1269             vgear:= AddVisualGear(0, 0, vgtSpeechBubble, s, true);
  1238             if vgear <> nil then
  1270             if vgear <> nil then
  1239                begin
  1271                begin
       
  1272                vgear^.Text:= lua_tostring(L, 2);
  1240                if Gear^.Kind = gtHedgehog then
  1273                if Gear^.Kind = gtHedgehog then
  1241                    begin
  1274                    begin
  1242                    vgear^.Text:= lua_tostring(L, 2);
       
  1243                    AddChatString(#1+'[' + gear^.Hedgehog^.Name + '] '+vgear^.text);
  1275                    AddChatString(#1+'[' + gear^.Hedgehog^.Name + '] '+vgear^.text);
  1244                    vgear^.Hedgehog:= gear^.Hedgehog
  1276                    vgear^.Hedgehog:= gear^.Hedgehog
  1245                    end
  1277                    end
  1246                else vgear^.Frame:= gear^.uid;
  1278                else vgear^.Frame:= gear^.uid;
  1247 
  1279 
  1373         begin
  1405         begin
  1374         gear:= GearByUID(lua_tointeger(L, 1));
  1406         gear:= GearByUID(lua_tointeger(L, 1));
  1375         if gear <> nil then gear^.Timer:= lua_tointeger(L, 2)
  1407         if gear <> nil then gear^.Timer:= lua_tointeger(L, 2)
  1376         end;
  1408         end;
  1377     lc_settimer:= 0
  1409     lc_settimer:= 0
       
  1410 end;
       
  1411 
       
  1412 function lc_setflighttime(L : Plua_State) : LongInt; Cdecl;
       
  1413 var gear : PGear;
       
  1414 begin
       
  1415     if CheckLuaParamCount(L, 2, 'SetFlightTime', 'gearUid, flighttime') then
       
  1416         begin
       
  1417         gear:= GearByUID(lua_tointeger(L, 1));
       
  1418         if gear <> nil then gear^.FlightTime:= lua_tointeger(L, 2)
       
  1419         end;
       
  1420     lc_setflighttime:= 0
  1378 end;
  1421 end;
  1379 
  1422 
  1380 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1423 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1381 var gear: PGear;
  1424 var gear: PGear;
  1382     t   : LongInt;
  1425     t   : LongInt;
  1514                 else
  1557                 else
  1515                     LuaCallError(EnumToStr(statInfo) + ' requires the teamname parameter', call, params);
  1558                     LuaCallError(EnumToStr(statInfo) + ' requires the teamname parameter', call, params);
  1516                 end
  1559                 end
  1517             else // count is correct!
  1560             else // count is correct!
  1518                 begin
  1561                 begin
  1519                 if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
  1562                 if needsTn then
  1520                     begin
  1563                     begin
  1521                     // 3: team name
  1564                     // 3: team name
  1522                     for i:= 0 to Pred(TeamsCount) do
  1565                     for i:= 0 to Pred(TeamsCount) do
  1523                         begin
  1566                         begin
  1524                         color:= _S'0';
  1567                         color:= _S'0';
  1632         //lua_pushnil(L)
  1675         //lua_pushnil(L)
  1633     lc_addteam:= 0;//1;
  1676     lc_addteam:= 0;//1;
  1634 end;
  1677 end;
  1635 
  1678 
  1636 function lc_dismissteam(L : Plua_State) : LongInt; Cdecl;
  1679 function lc_dismissteam(L : Plua_State) : LongInt; Cdecl;
       
  1680 var HHGear: PGear;
       
  1681     i, h  : LongInt;
       
  1682     hidden: boolean;
  1637 begin
  1683 begin
  1638     if CheckLuaParamCount(L, 1, 'DismissTeam', 'teamname') then
  1684     if CheckLuaParamCount(L, 1, 'DismissTeam', 'teamname') then
  1639         ParseCommand('teamgone ' + lua_tostring(L, 1), true, true);
  1685         begin
  1640     lc_dismissteam:= 0;;
  1686         if TeamsCount > 0 then
       
  1687             for i:= 0 to Pred(TeamsCount) do
       
  1688                 begin
       
  1689                 // skip teams that don't have matching name
       
  1690                 if TeamsArray[i]^.TeamName <> lua_tostring(L, 1) then
       
  1691                     continue;
       
  1692 
       
  1693                 // destroy all hogs of matching team, including the hidden ones
       
  1694                 for h:= 0 to cMaxHHIndex do
       
  1695                     begin
       
  1696                     hidden:= (TeamsArray[i]^.Hedgehogs[h].GearHidden <> nil);
       
  1697                     if hidden then
       
  1698                         RestoreHog(@TeamsArray[i]^.Hedgehogs[h]);
       
  1699                     // destroy hedgehog gear, if any
       
  1700                     HHGear:= TeamsArray[i]^.Hedgehogs[h].Gear;
       
  1701                     if HHGear <> nil then
       
  1702                         begin
       
  1703                         // smoke effect
       
  1704                         if (not hidden) then
       
  1705                             begin
       
  1706                             AddVisualGear(hwRound(HHGear^.X), hwRound(HHGear^.Y), vgtSmokeWhite);
       
  1707                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1708                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1709                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1710                             AddVisualGear(hwRound(HHGear^.X) - 16 + Random(32), hwRound(HHGear^.Y) - 16 + Random(32), vgtSmokeWhite);
       
  1711                             end;
       
  1712                         HHGear^.Message:= HHGear^.Message or gmDestroy;
       
  1713                         end;
       
  1714                     end;
       
  1715                 // can't dismiss more than one team
       
  1716                 break;
       
  1717                 end;
       
  1718         end;
       
  1719     lc_dismissteam:= 0;
  1641 end;
  1720 end;
  1642 
  1721 
  1643 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
  1722 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
  1644 var temp: ShortString;
  1723 var temp: ShortString;
  1645 begin
  1724 begin
  1983     lf    : Word;
  2062     lf    : Word;
  1984     i, n : LongInt;
  2063     i, n : LongInt;
  1985     placed: boolean;
  2064     placed: boolean;
  1986 const
  2065 const
  1987     call = 'PlaceSprite';
  2066     call = 'PlaceSprite';
  1988     params = 'x, y, sprite, frameIdx [, landFlags, ... ]';
  2067     params = 'x, y, sprite, frameIdx [, landFlag, ... ]';
  1989 begin
  2068 begin
  1990     placed:= false;
  2069     placed:= false;
  1991     if CheckAndFetchLuaParamMinCount(L, 4, call, params, n) then
  2070     if CheckAndFetchLuaParamMinCount(L, 4, call, params, n) then
  1992         begin
  2071         begin
  1993         lf:= 0;
  2072         lf:= 0;
  2245     exit;
  2324     exit;
  2246 
  2325 
  2247 ScriptSetString('Seed', cSeed);
  2326 ScriptSetString('Seed', cSeed);
  2248 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2327 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2249 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2328 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2250 ScriptSetInteger('MapGen', cMapGen);
  2329 ScriptSetInteger('MapGen', ord(cMapGen));
  2251 
  2330 
  2252 ScriptCall('onPreviewInit');
  2331 ScriptCall('onPreviewInit');
  2253 
  2332 
  2254 // pop game variables
  2333 // pop game variables
  2255 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2334 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2256 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2335 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2257 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2336 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2258 cMapGen          := ScriptGetInteger('MapGen');
  2337 cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
  2259 end;
  2338 end;
  2260 
  2339 
  2261 procedure ScriptOnGameInit;
  2340 procedure ScriptOnGameInit;
  2262 var i, j, k: LongInt;
  2341 var i, j, k: LongInt;
  2263 begin
  2342 begin
  2269 ScriptSetInteger('BorderColor', ExplosionBorderColor);
  2348 ScriptSetInteger('BorderColor', ExplosionBorderColor);
  2270 ScriptSetInteger('GameFlags', GameFlags);
  2349 ScriptSetInteger('GameFlags', GameFlags);
  2271 ScriptSetString('Seed', cSeed);
  2350 ScriptSetString('Seed', cSeed);
  2272 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2351 ScriptSetInteger('TemplateFilter', cTemplateFilter);
  2273 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2352 ScriptSetInteger('TemplateNumber', LuaTemplateNumber);
  2274 ScriptSetInteger('MapGen', cMapGen);
  2353 ScriptSetInteger('MapGen', ord(cMapGen));
  2275 ScriptSetInteger('ScreenHeight', cScreenHeight);
  2354 ScriptSetInteger('ScreenHeight', cScreenHeight);
  2276 ScriptSetInteger('ScreenWidth', cScreenWidth);
  2355 ScriptSetInteger('ScreenWidth', cScreenWidth);
  2277 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
  2356 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
  2278 ScriptSetInteger('CaseFreq', cCaseFactor);
  2357 ScriptSetInteger('CaseFreq', cCaseFactor);
  2279 ScriptSetInteger('HealthCaseProb', cHealthCaseProb);
  2358 ScriptSetInteger('HealthCaseProb', cHealthCaseProb);
  2298 
  2377 
  2299 // pop game variables
  2378 // pop game variables
  2300 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2379 ParseCommand('seed ' + ScriptGetString('Seed'), true, true);
  2301 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2380 cTemplateFilter  := ScriptGetInteger('TemplateFilter');
  2302 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2381 LuaTemplateNumber:= ScriptGetInteger('TemplateNumber');
  2303 cMapGen          := ScriptGetInteger('MapGen');
  2382 cMapGen          := TMapGen(ScriptGetInteger('MapGen'));
  2304 GameFlags        := ScriptGetInteger('GameFlags');
  2383 GameFlags        := ScriptGetInteger('GameFlags');
  2305 cHedgehogTurnTime:= ScriptGetInteger('TurnTime');
  2384 cHedgehogTurnTime:= ScriptGetInteger('TurnTime');
  2306 cCaseFactor      := ScriptGetInteger('CaseFreq');
  2385 cCaseFactor      := ScriptGetInteger('CaseFreq');
  2307 cHealthCaseProb  := ScriptGetInteger('HealthCaseProb');
  2386 cHealthCaseProb  := ScriptGetInteger('HealthCaseProb');
  2308 cHealthCaseAmount:= ScriptGetInteger('HealthCaseAmount');
  2387 cHealthCaseAmount:= ScriptGetInteger('HealthCaseAmount');
  2496 end;
  2575 end;
  2497 
  2576 
  2498 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
  2577 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
  2499 begin
  2578 begin
  2500 if (not ScriptLoaded) or (not ScriptExists(fname)) then
  2579 if (not ScriptLoaded) or (not ScriptExists(fname)) then
  2501     exit;
  2580     exit(0);
  2502 SetGlobals;
  2581 SetGlobals;
  2503 lua_getglobal(luaState, Str2PChar(fname));
  2582 lua_getglobal(luaState, Str2PChar(fname));
  2504 lua_pushinteger(luaState, par1);
  2583 lua_pushinteger(luaState, par1);
  2505 lua_pushinteger(luaState, par2);
  2584 lua_pushinteger(luaState, par2);
  2506 lua_pushinteger(luaState, par3);
  2585 lua_pushinteger(luaState, par3);
  2632     si : TStatInfoType;
  2711     si : TStatInfoType;
  2633     st : TSound;
  2712     st : TSound;
  2634     he : THogEffect;
  2713     he : THogEffect;
  2635     cg : TCapGroup;
  2714     cg : TCapGroup;
  2636     spr: TSprite;
  2715     spr: TSprite;
       
  2716     mg : TMapGen;
  2637 begin
  2717 begin
  2638 // initialize lua
  2718 // initialize lua
  2639 luaState:= lua_open;
  2719 luaState:= lua_open;
  2640 TryDo(luaState <> nil, 'lua_open failed', true);
  2720 TryDo(luaState <> nil, 'lua_open failed', true);
  2641 
  2721 
  2723     ScriptSetInteger(EnumToStr(cg), ord(cg));
  2803     ScriptSetInteger(EnumToStr(cg), ord(cg));
  2724 
  2804 
  2725 for spr:= Low(TSprite) to High(TSprite) do
  2805 for spr:= Low(TSprite) to High(TSprite) do
  2726     ScriptSetInteger(EnumToStr(spr), ord(spr));
  2806     ScriptSetInteger(EnumToStr(spr), ord(spr));
  2727 
  2807 
       
  2808 for mg:= Low(TMapGen) to High(TMapGen) do
       
  2809     ScriptSetInteger(EnumToStr(mg), ord(mg));
  2728 
  2810 
  2729 ScriptSetInteger('gstDrowning'      , gstDrowning);
  2811 ScriptSetInteger('gstDrowning'      , gstDrowning);
  2730 ScriptSetInteger('gstHHDriven'      , gstHHDriven);
  2812 ScriptSetInteger('gstHHDriven'      , gstHHDriven);
  2731 ScriptSetInteger('gstMoving'        , gstMoving);
  2813 ScriptSetInteger('gstMoving'        , gstMoving);
  2732 ScriptSetInteger('gstAttacked'      , gstAttacked);
  2814 ScriptSetInteger('gstAttacked'      , gstAttacked);
  2753 
  2835 
  2754 // land flags
  2836 // land flags
  2755 ScriptSetInteger('lfIndestructible', lfIndestructible);
  2837 ScriptSetInteger('lfIndestructible', lfIndestructible);
  2756 ScriptSetInteger('lfIce'           , lfIce);
  2838 ScriptSetInteger('lfIce'           , lfIce);
  2757 ScriptSetInteger('lfBouncy'        , lfBouncy);
  2839 ScriptSetInteger('lfBouncy'        , lfBouncy);
  2758 
       
  2759 // mapgen
       
  2760 ScriptSetInteger('mgRandom', mgRandom);
       
  2761 ScriptSetInteger('mgMaze'  , mgMaze);
       
  2762 ScriptSetInteger('mgPerlin', mgPerlin);
       
  2763 ScriptSetInteger('mgDrawn' , mgDrawn);
       
  2764 
  2840 
  2765 // register functions
  2841 // register functions
  2766 lua_register(luaState, _P'HideHog', @lc_hidehog);
  2842 lua_register(luaState, _P'HideHog', @lc_hidehog);
  2767 lua_register(luaState, _P'RestoreHog', @lc_restorehog);
  2843 lua_register(luaState, _P'RestoreHog', @lc_restorehog);
  2768 lua_register(luaState, _P'SaveCampaignVar', @lc_savecampaignvar);
  2844 lua_register(luaState, _P'SaveCampaignVar', @lc_savecampaignvar);
  2836 lua_register(luaState, _P'GetState', @lc_getstate);
  2912 lua_register(luaState, _P'GetState', @lc_getstate);
  2837 lua_register(luaState, _P'GetTag', @lc_gettag);
  2913 lua_register(luaState, _P'GetTag', @lc_gettag);
  2838 lua_register(luaState, _P'SetTag', @lc_settag);
  2914 lua_register(luaState, _P'SetTag', @lc_settag);
  2839 lua_register(luaState, _P'SetTimer', @lc_settimer);
  2915 lua_register(luaState, _P'SetTimer', @lc_settimer);
  2840 lua_register(luaState, _P'GetTimer', @lc_gettimer);
  2916 lua_register(luaState, _P'GetTimer', @lc_gettimer);
       
  2917 lua_register(luaState, _P'SetFlightTime', @lc_setflighttime);
       
  2918 lua_register(luaState, _P'GetFlightTime', @lc_getflighttime);
  2841 lua_register(luaState, _P'SetZoom', @lc_setzoom);
  2919 lua_register(luaState, _P'SetZoom', @lc_setzoom);
  2842 lua_register(luaState, _P'GetZoom', @lc_getzoom);
  2920 lua_register(luaState, _P'GetZoom', @lc_getzoom);
  2843 lua_register(luaState, _P'HogSay', @lc_hogsay);
  2921 lua_register(luaState, _P'HogSay', @lc_hogsay);
  2844 lua_register(luaState, _P'SwitchHog', @lc_switchhog);
  2922 lua_register(luaState, _P'SwitchHog', @lc_switchhog);
  2845 lua_register(luaState, _P'HogTurnLeft', @lc_hogturnleft);
  2923 lua_register(luaState, _P'HogTurnLeft', @lc_hogturnleft);