hedgewars/uScript.pas
changeset 10281 dedc8f23e35e
parent 10280 762c256552e9
child 10282 f02cf95b8424
equal deleted inserted replaced
10280:762c256552e9 10281:dedc8f23e35e
   123 begin
   123 begin
   124     // TODO: i18n?
   124     // TODO: i18n?
   125     LuaCallError('Wrong number of parameters (' + inttostr(wrongcount) + ')!', call, paramsyntax);
   125     LuaCallError('Wrong number of parameters (' + inttostr(wrongcount) + ')!', call, paramsyntax);
   126 end;
   126 end;
   127 
   127 
   128 function IsValidGearType(gt: lua_Integer; call, paramsyntax: shortstring): boolean; inline;
   128 function LuaToGearTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
   129 begin
   129 begin
   130     if (gt < ord(Low(TGearType))) or (gt > ord(High(TGearType))) then
   130     if lua_isnoneornil(L, i) then i:= -1
       
   131     else i:= lua_tointeger(L, i);
       
   132     if (i < ord(Low(TGearType))) or (i > ord(High(TGearType))) then
       
   133         begin
       
   134         LuaCallError('Invalid gearType!', call, paramsyntax);
       
   135         LuaToGearTypeOrd:= -1;
       
   136         end
       
   137     else
       
   138         LuaToGearTypeOrd:= i;
       
   139 end;
       
   140 
       
   141 function LuaToVisualGearTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   142 begin
       
   143     if lua_isnoneornil(L, i) then i:= -1
       
   144     else i:= lua_tointeger(L, i);
       
   145     if (i < ord(Low(TVisualGearType))) or (i > ord(High(TVisualGearType))) then
       
   146         begin
       
   147         LuaCallError('Invalid visualGearType!', call, paramsyntax);
       
   148         LuaToVisualGearTypeOrd:= -1;
       
   149         end
       
   150     else
       
   151         LuaToVisualGearTypeOrd:= i;
       
   152 end;
       
   153 
       
   154 function LuaToAmmoTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   155 begin
       
   156     if lua_isnoneornil(L, i) then i:= -1
       
   157     else i:= lua_tointeger(L, i);
       
   158     if (i < ord(Low(TAmmoType))) or (i > ord(High(TAmmoType))) then
       
   159         begin
       
   160         LuaCallError('Invalid ammoType!', call, paramsyntax);
       
   161         LuaToAmmoTypeOrd:= -1;
       
   162         end
       
   163     else
       
   164         LuaToAmmoTypeOrd:= i;
       
   165 end;
       
   166 
       
   167 function LuaToStatInfoTypeOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   168 begin
       
   169     if lua_isnoneornil(L, i) then i:= -1
       
   170     else i:= lua_tointeger(L, i);
       
   171     if (i < ord(Low(TStatInfoType))) or (i > ord(High(TStatInfoType))) then
       
   172         begin
       
   173         LuaCallError('Invalid statInfoType!', call, paramsyntax);
       
   174         LuaToStatInfoTypeOrd:= -1;
       
   175         end
       
   176     else
       
   177         LuaToStatInfoTypeOrd:= i;
       
   178 end;
       
   179 
       
   180 function LuaToSoundOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   181 begin
       
   182     if lua_isnoneornil(L, i) then i:= -1
       
   183     else i:= lua_tointeger(L, i);
       
   184     if (i < ord(Low(TSound))) or (i > ord(High(TSound))) then
       
   185         begin
       
   186         LuaCallError('Invalid soundId!', call, paramsyntax);
       
   187         LuaToSoundOrd:= -1;
       
   188         end
       
   189     else
       
   190         LuaToSoundOrd:= i;
       
   191 end;
       
   192 
       
   193 function LuaToHogEffectOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   194 begin
       
   195     if lua_isnoneornil(L, i) then i:= -1
       
   196     else i:= lua_tointeger(L, i);
       
   197     if (i < ord(Low(THogEffect))) or (i > ord(High(THogEffect))) then
   131         begin
   198         begin
   132         LuaCallError('Invalid gear type!', call, paramsyntax);
   199         LuaCallError('Invalid gear type!', call, paramsyntax);
   133         exit(false);
   200         LuaToHogEffectOrd:= -1;
   134         end;
   201         end
   135     IsValidGearType:= true;
   202     else
   136 end;
   203         LuaToHogEffectOrd:= i;
   137 
   204 end;
   138 function IsValidSprite(sprite: lua_Integer; call, paramsyntax: shortstring): boolean; inline;
   205 
   139 begin
   206 function LuaToCapGroupOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
   140     if (sprite < ord(Low(TSprite))) or (sprite > ord(High(TSprite))) then
   207 begin
       
   208     if lua_isnoneornil(L, i) then i:= -1
       
   209     else i:= lua_tointeger(L, i);
       
   210     if (i < ord(Low(TCapGroup))) or (i > ord(High(TCapGroup))) then
       
   211         begin
       
   212         LuaCallError('Invalid capgroup type!', call, paramsyntax);
       
   213         LuaToCapGroupOrd:= -1;
       
   214         end
       
   215     else
       
   216         LuaToCapGroupOrd:= i;
       
   217 end;
       
   218 
       
   219 function LuaToSpriteOrd(L : Plua_State; i: LongInt; call, paramsyntax: shortstring): LongInt; inline;
       
   220 begin
       
   221     if lua_isnoneornil(L, i) then i:= -1
       
   222     else i:= lua_tointeger(L, i);
       
   223     if (i < ord(Low(TSprite))) or (i > ord(High(TSprite))) then
   141         begin
   224         begin
   142         LuaCallError('Invalid sprite id!', call, paramsyntax);
   225         LuaCallError('Invalid sprite id!', call, paramsyntax);
   143         exit(false);
   226         LuaToSpriteOrd:= -1;
   144         end;
   227         end
   145     IsValidSprite:= true;
   228     else
       
   229         LuaToSpriteOrd:= i;
   146 end;
   230 end;
   147 
   231 
   148 // wrapped calls //
   232 // wrapped calls //
   149 
   233 
   150 // functions called from Lua:
   234 // functions called from Lua:
   329         end;
   413         end;
   330     lc_getgameflag:= 1;
   414     lc_getgameflag:= 1;
   331 end;
   415 end;
   332 
   416 
   333 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
   417 function lc_addcaption(L : Plua_State) : LongInt; Cdecl;
       
   418 var cg: LongInt;
       
   419 const
       
   420     call = 'AddCaption';
       
   421     params = 'text [, color, captiongroup]';
   334 begin
   422 begin
   335     if lua_gettop(L) = 1 then
   423     if lua_gettop(L) = 1 then
   336         AddCaption(lua_tostringA(L, 1), cWhiteColor, capgrpMessage)
   424         AddCaption(lua_tostringA(L, 1), cWhiteColor, capgrpMessage)
   337     else if lua_gettop(L) = 3 then
   425     else if lua_gettop(L) = 3 then
   338         begin
   426         begin
   339         AddCaption(lua_tostringA(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(lua_tointeger(L, 3)));
   427         cg:= LuaToCapGroupOrd(L, 3, call, params);
   340         end
   428         AddCaption(lua_tostringA(L, 1), lua_tointeger(L, 2) shr 8, TCapGroup(cg));
   341     else
   429         end
   342         LuaParameterCountError('AddCaption', 'text[, color, captiongroup]', lua_gettop(L));
   430     else
       
   431         LuaParameterCountError(call, params, lua_gettop(L));
   343     lc_addcaption:= 0;
   432     lc_addcaption:= 0;
   344 end;
   433 end;
   345 
   434 
   346 function lc_campaignlock(L : Plua_State) : LongInt; Cdecl;
   435 function lc_campaignlock(L : Plua_State) : LongInt; Cdecl;
   347 begin
   436 begin
   420 var gear: PGear;
   509 var gear: PGear;
   421 var health: LongInt;
   510 var health: LongInt;
   422 begin
   511 begin
   423     if (lua_gettop(L) < 2) or (lua_gettop(L) > 3) then
   512     if (lua_gettop(L) < 2) or (lua_gettop(L) > 3) then
   424         begin
   513         begin
   425         LuaParameterCountError('SpawnHealthCrate', 'x, y[, health]', lua_gettop(L));
   514         LuaParameterCountError('SpawnHealthCrate', 'x, y [, health]', lua_gettop(L));
   426         lua_pushnil(L);
   515         lua_pushnil(L);
   427         end
   516         end
   428     else
   517     else
   429         begin
   518         begin
   430         if lua_gettop(L) = 3 then
   519         if lua_gettop(L) = 3 then
   443 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   532 function lc_spawnammocrate(L: PLua_State): LongInt; Cdecl;
   444 var gear: PGear;
   533 var gear: PGear;
   445 begin
   534 begin
   446     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   535     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   447         begin
   536         begin
   448         LuaParameterCountError('SpawnAmmoCrate', 'x, y, content[, amount]', lua_gettop(L));
   537         LuaParameterCountError('SpawnAmmoCrate', 'x, y, content [, amount]', lua_gettop(L));
   449         lua_pushnil(L);
   538         lua_pushnil(L);
   450         end
   539         end
   451     else
   540     else
   452         begin
   541         begin
   453         if (lua_gettop(L) = 3) then
   542         if (lua_gettop(L) = 3) then
   464 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   553 function lc_spawnutilitycrate(L: PLua_State): LongInt; Cdecl;
   465 var gear: PGear;
   554 var gear: PGear;
   466 begin
   555 begin
   467     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   556     if (lua_gettop(L) <> 3) and (lua_gettop(L) <> 4) then
   468         begin
   557         begin
   469         LuaParameterCountError('SpawnUtilityCrate', 'x, y, content[, amount]', lua_gettop(L));
   558         LuaParameterCountError('SpawnUtilityCrate', 'x, y, content [, amount]', lua_gettop(L));
   470         lua_pushnil(L);
   559         lua_pushnil(L);
   471         end
   560         end
   472     else
   561     else
   473         begin
   562         begin
   474         if (lua_gettop(L) = 3) then
   563         if (lua_gettop(L) = 3) then
   496         LuaParameterCountError(call, params, lua_gettop(L));
   585         LuaParameterCountError(call, params, lua_gettop(L));
   497         lua_pushnil(L); // return value on stack (nil)
   586         lua_pushnil(L); // return value on stack (nil)
   498         end
   587         end
   499     else
   588     else
   500         begin
   589         begin
   501         t:= lua_tointeger(L, 3);
   590         t:= LuaToGearTypeOrd(L, 3, call, params);
   502         if IsValidGearType(t, call, params) then
   591         if t >= 0 then
   503             begin
   592             begin
       
   593             gt:= TGearType(t);
   504             x:= lua_tointeger(L, 1);
   594             x:= lua_tointeger(L, 1);
   505             y:= lua_tointeger(L, 2);
   595             y:= lua_tointeger(L, 2);
   506             gt:= TGearType(t);
       
   507             s:= lua_tointeger(L, 4);
   596             s:= lua_tointeger(L, 4);
   508             dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   597             dx:= int2hwFloat(lua_tointeger(L, 5)) / 1000000;
   509             dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   598             dy:= int2hwFloat(lua_tointeger(L, 6)) / 1000000;
   510             t:= lua_tointeger(L, 7);
   599             t:= lua_tointeger(L, 7);
   511 
   600 
   538 function lc_addvisualgear(L : Plua_State) : LongInt; Cdecl;
   627 function lc_addvisualgear(L : Plua_State) : LongInt; Cdecl;
   539 var vg : PVisualGear;
   628 var vg : PVisualGear;
   540     x, y, s: LongInt;
   629     x, y, s: LongInt;
   541     c: Boolean;
   630     c: Boolean;
   542     vgt: TVisualGearType;
   631     vgt: TVisualGearType;
   543 begin
   632     uid: Longword;
       
   633 const
       
   634     call = 'AddVisualGear';
       
   635     params = 'x, y, visualGearType, state, critical';
       
   636 begin
       
   637     uid:= 0;
   544     if lua_gettop(L) <> 5 then
   638     if lua_gettop(L) <> 5 then
   545         begin
   639         begin
   546         LuaParameterCountError('AddVisualGear', 'x, y, visualGearType, state, critical', lua_gettop(L));
   640         LuaParameterCountError(call, params, lua_gettop(L));
   547         lua_pushnil(L); // return value on stack (nil)
   641         lua_pushnil(L); // return value on stack (nil)
   548         end
   642         end
   549     else
   643     else
   550         begin
   644         begin
   551         x:= lua_tointeger(L, 1);
   645         s:= LuaToVisualGearTypeOrd(L, 3, call, params);
   552         y:= lua_tointeger(L, 2);
   646         if s >= 0 then
   553         vgt:= TVisualGearType(lua_tointeger(L, 3));
   647             begin
   554         s:= lua_tointeger(L, 4);
   648             vgt:= TVisualGearType(s);
   555         c:= lua_toboolean(L, 5);
   649             x:= lua_tointeger(L, 1);
   556 
   650             y:= lua_tointeger(L, 2);
   557         vg:= AddVisualGear(x, y, vgt, s, c);
   651             s:= lua_tointeger(L, 4);
   558         if vg <> nil then
   652             c:= lua_toboolean(L, 5);
   559             begin
   653 
   560             lastVisualGearByUID:= vg;
   654             vg:= AddVisualGear(x, y, vgt, s, c);
   561             lua_pushinteger(L, vg^.uid)
   655             if vg <> nil then
   562             end
   656                 begin
   563         else
   657                 lastVisualGearByUID:= vg;
   564             lua_pushinteger(L, 0)
   658                 uid:= vg^.uid;
   565         end;
   659                 end;
       
   660             end;
       
   661         end;
       
   662     lua_pushinteger(L, uid);
   566     lc_addvisualgear:= 1; // 1 return value
   663     lc_addvisualgear:= 1; // 1 return value
   567 end;
   664 end;
   568 
   665 
   569 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   666 function lc_deletevisualgear(L : Plua_State) : LongInt; Cdecl;
   570 var vg : PVisualGear;
   667 var vg : PVisualGear;
   571 begin
   668 begin
       
   669     vg:= nil;
   572     if lua_gettop(L) <> 1 then
   670     if lua_gettop(L) <> 1 then
   573         begin
   671         begin
   574         LuaParameterCountError('DeleteVisualGear', 'vgUid', lua_gettop(L));
   672         LuaParameterCountError('DeleteVisualGear', 'vgUid', lua_gettop(L));
   575         end
   673         end
   576     else
   674     else
   577         begin
   675         begin
   578         vg:= VisualGearByUID(lua_tointeger(L, 1));
   676         vg:= VisualGearByUID(lua_tointeger(L, 1));
   579         if vg <> nil then
   677         if vg <> nil then
   580             DeleteVisualGear(vg);
   678             DeleteVisualGear(vg);
   581         end;
   679         end;
   582     lc_deletevisualgear:= 0
   680     // allow caller to know whether there was something to delete
       
   681     lua_pushboolean(L, vg <> nil);
       
   682     lc_deletevisualgear:= 1
   583 end;
   683 end;
   584 
   684 
   585 function lc_getvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   685 function lc_getvisualgearvalues(L : Plua_State) : LongInt; Cdecl;
   586 var vg: PVisualGear;
   686 var vg: PVisualGear;
   587 begin
   687 begin
  1127                end
  1227                end
  1128             end
  1228             end
  1129             else
  1229             else
  1130                 lua_pushnil(L)
  1230                 lua_pushnil(L)
  1131         end
  1231         end
  1132     else LuaParameterCountError('HogSay', 'gearUid, text, manner[, vgState]', lua_gettop(L));
  1232     else LuaParameterCountError('HogSay', 'gearUid, text, manner [, vgState]', lua_gettop(L));
  1133     lc_hogsay:= 1
  1233     lc_hogsay:= 1
  1134 end;
  1234 end;
  1135 
  1235 
  1136 function lc_switchhog(L : Plua_State) : LongInt; Cdecl;
  1236 function lc_switchhog(L : Plua_State) : LongInt; Cdecl;
  1137 var gear, prevgear : PGear;
  1237 var gear, prevgear : PGear;
  1163             end
  1263             end
  1164         end;
  1264         end;
  1165     lc_switchhog:= 0
  1265     lc_switchhog:= 0
  1166 end;
  1266 end;
  1167 
  1267 
  1168 {function lc_addammo(L : Plua_State) : LongInt; Cdecl;
  1268 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
  1169 var gear : PGear;
  1269 var gear : PGear;
  1170 begin
  1270     at   : LongInt;
  1171 
  1271 const
  1172     if lua_gettop(L) = 3 then
  1272     call = 'AddAmmo';
  1173     begin
  1273     params = 'gearUid, ammoType [, ammoCount]';
  1174     gear:= GearByUID(lua_tointeger(L, 1));
  1274 begin
       
  1275     if (lua_gettop(L) = 3) or (lua_gettop(L) = 2) then
       
  1276         begin
       
  1277         at:= LuaToAmmoTypeOrd(L, 2, call, params);
       
  1278         if at >= 0 then
       
  1279             begin
       
  1280             gear:= GearByUID(lua_tointeger(L, 1));
       
  1281             if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1282                 if lua_gettop(L) = 2 then
       
  1283                     AddAmmo(gear^.Hedgehog^, TAmmoType(at))
       
  1284                 else
       
  1285                     SetAmmo(gear^.Hedgehog^, TAmmoType(at), lua_tointeger(L, 3))
       
  1286             end;
       
  1287         end
       
  1288     else LuaParameterCountError(call, params, lua_gettop(L));
       
  1289     lc_addammo:= 0
       
  1290 end;
       
  1291 
       
  1292 function lc_getammocount(L : Plua_State) : LongInt; Cdecl;
       
  1293 var gear : PGear;
       
  1294     ammo : PAmmo;
       
  1295     at   : LongInt;
       
  1296 const
       
  1297     call = 'GetAmmoCount';
       
  1298     params = 'gearUid, ammoType';
       
  1299 begin
       
  1300     if (lua_gettop(L) <> 2) then
       
  1301         begin
       
  1302         LuaParameterCountError(call, params, lua_gettop(L));
       
  1303         lua_pushnil(L);
       
  1304         end
       
  1305     else
       
  1306         begin
       
  1307         gear:= GearByUID(lua_tointeger(L, 1));
  1175         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1308         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1176             AddAmmoAmount(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L,3) );
  1309             begin
  1177     end else
  1310             at:= LuaToAmmoTypeOrd(L, 2, call, params);
  1178 
  1311             if at >= 0 then
  1179     if lua_gettop(L) = 2 then
  1312                 begin
  1180     begin
  1313                 ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(at));
  1181     gear:= GearByUID(lua_tointeger(L, 1));
  1314                 if ammo^.AmmoType = amNothing then
  1182         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1315                     lua_pushinteger(L, 0)
  1183             AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
  1316                 else
  1184     end else
  1317                     lua_pushinteger(L, ammo^.Count);
  1185     begin
  1318                 end;
  1186         LuaParameterCountError('AddAmmo', 'TODO', lua_gettop(L));
       
  1187     end;
       
  1188 
       
  1189     lc_addammo:= 0;
       
  1190 
       
  1191 end;}
       
  1192 
       
  1193 function lc_addammo(L : Plua_State) : LongInt; Cdecl;
       
  1194 var gear : PGear;
       
  1195 begin
       
  1196     if (lua_gettop(L) = 3) or (lua_gettop(L) = 2) then
       
  1197         begin
       
  1198         gear:= GearByUID(lua_tointeger(L, 1));
       
  1199         if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1200             if lua_gettop(L) = 2 then
       
  1201                 AddAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)))
       
  1202             else
       
  1203                 SetAmmo(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)), lua_tointeger(L, 3))
       
  1204         end
       
  1205     else LuaParameterCountError('AddAmmo', 'gearUid, ammoType[, ammoCount]', lua_gettop(L));
       
  1206     lc_addammo:= 0
       
  1207 end;
       
  1208 
       
  1209 function lc_getammocount(L : Plua_State) : LongInt; Cdecl;
       
  1210 var gear : PGear;
       
  1211     ammo : PAmmo;
       
  1212 begin
       
  1213     if (lua_gettop(L) = 2) then
       
  1214         begin
       
  1215         gear:= GearByUID(lua_tointeger(L, 1));
       
  1216         if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1217             begin
       
  1218             ammo:= GetAmmoEntry(gear^.Hedgehog^, TAmmoType(lua_tointeger(L, 2)));
       
  1219             if ammo^.AmmoType = amNothing then
       
  1220                 lua_pushinteger(L, 0)
       
  1221             else
       
  1222                 lua_pushinteger(L, ammo^.Count)
       
  1223             end
  1319             end
  1224         else lua_pushinteger(L, 0)
  1320         else lua_pushinteger(L, 0);
  1225         end
  1321         end;
  1226     else
  1322 
  1227         begin
       
  1228         LuaParameterCountError('GetAmmoCount', 'gearUid, ammoType', lua_gettop(L));
       
  1229         lua_pushnil(L)
       
  1230         end;
       
  1231     lc_getammocount:= 1
  1323     lc_getammocount:= 1
  1232 end;
  1324 end;
  1233 
  1325 
  1234 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
  1326 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
  1235 var gear : PGear;
  1327 var gear : PGear;
  1274     lc_settimer:= 0
  1366     lc_settimer:= 0
  1275 end;
  1367 end;
  1276 
  1368 
  1277 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1369 function lc_seteffect(L : Plua_State) : LongInt; Cdecl;
  1278 var gear: PGear;
  1370 var gear: PGear;
       
  1371     t   : LongInt;
       
  1372 const
       
  1373     call = 'SetEffect';
       
  1374     params = 'gearUid, effect, effectState';
  1279 begin
  1375 begin
  1280     if lua_gettop(L) <> 3 then
  1376     if lua_gettop(L) <> 3 then
  1281         LuaParameterCountError('SetEffect', 'gearUid, effect, effectState', lua_gettop(L))
  1377         LuaParameterCountError(call, params, lua_gettop(L))
  1282     else begin
  1378     else
  1283         gear := GearByUID(lua_tointeger(L, 1));
  1379         begin
  1284         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1380         t:= LuaToHogEffectOrd(L, 2, call, params);
  1285             gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))]:= lua_tointeger(L, 3);
  1381         if t >= 0 then
  1286     end;
  1382             begin
       
  1383             gear := GearByUID(lua_tointeger(L, 1));
       
  1384             if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1385                 gear^.Hedgehog^.Effects[THogEffect(t)]:= lua_tointeger(L, 3);
       
  1386             end;
       
  1387         end;
  1287     lc_seteffect := 0;
  1388     lc_seteffect := 0;
  1288 end;
  1389 end;
  1289 
  1390 
  1290 function lc_geteffect(L : Plua_State) : LongInt; Cdecl;
  1391 function lc_geteffect(L : Plua_State) : LongInt; Cdecl;
  1291 var gear : PGear;
  1392 var gear : PGear;
       
  1393     t    : LongInt;
       
  1394 const
       
  1395     call = 'GetEffect';
       
  1396     params = 'gearUid, effect';
  1292 begin
  1397 begin
  1293     if lua_gettop(L) <> 2 then
  1398     if lua_gettop(L) <> 2 then
  1294         begin
  1399         begin
  1295         LuaParameterCountError('GetEffect', 'gearUid, effect', lua_gettop(L));
  1400         LuaParameterCountError(call, params, lua_gettop(L));
  1296         end
  1401         end
  1297     else
  1402     else
  1298         begin
  1403         begin
  1299         gear:= GearByUID(lua_tointeger(L, 1));
  1404         t:= LuaToHogEffectOrd(L, 2, call, params);
  1300         if (gear <> nil) and (gear^.Hedgehog <> nil) then
  1405         if t >= 0 then
  1301             lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(lua_tointeger(L, 2))])
  1406             begin
  1302         else
  1407             gear:= GearByUID(lua_tointeger(L, 1));
  1303             lua_pushinteger(L, 0)
  1408             if (gear <> nil) and (gear^.Hedgehog <> nil) then
       
  1409                 lua_pushinteger(L, gear^.Hedgehog^.Effects[THogEffect(t)])
       
  1410             else
       
  1411                 lua_pushinteger(L, 0)
       
  1412             end;
  1304         end;
  1413         end;
  1305     lc_geteffect:= 1
  1414     lc_geteffect:= 1
  1306 end;
  1415 end;
  1307 
  1416 
  1308 function lc_setstate(L : Plua_State) : LongInt; Cdecl;
  1417 function lc_setstate(L : Plua_State) : LongInt; Cdecl;
  1387     lc_endgame:= 0
  1496     lc_endgame:= 0
  1388 end;
  1497 end;
  1389 
  1498 
  1390 function lc_sendstat(L : Plua_State) : LongInt; Cdecl;
  1499 function lc_sendstat(L : Plua_State) : LongInt; Cdecl;
  1391 var statInfo : TStatInfoType;
  1500 var statInfo : TStatInfoType;
  1392 var i : LongInt;
  1501     i, n : LongInt;
  1393 var color : shortstring;
  1502     color, tn : shortstring;
  1394 begin
  1503 const
  1395     statInfo := TStatInfoType(lua_tointeger(L, 1));
  1504     call = 'SendStat';
  1396     if (lua_gettop(L) <> 2) and ((statInfo <> siPlayerKills)
  1505     params2 = 'statInfoType, color';
  1397             and (statInfo <> siClanHealth)) then
  1506     params3 = 'siClanHealth, color, teamname';
  1398         begin
  1507     params = 'statInfoType, color [, teamname]';
  1399         LuaParameterCountError('SendStat', 'statInfoType, color', lua_gettop(L));
  1508 begin
  1400         end
  1509     n:= lua_gettop(L);
  1401     else if (lua_gettop(L) <> 3) and ((statInfo = siPlayerKills)
  1510     if (n < 2) or (n > 3) then
  1402             or (statInfo = siClanHealth)) then
  1511         begin
  1403         begin
  1512         LuaParameterCountError(call, params3, n);
  1404         LuaParameterCountError('SendStat', 'siClanHealth, color, teamname', lua_gettop(L));
  1513         end
  1405         end
  1514     else
  1406     else
  1515         begin
  1407         begin
  1516         i:= LuaToStatInfoTypeOrd(L, 1, call, params);
  1408         if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
  1517         if i >= 0 then
  1409             begin
  1518             begin
  1410             // 3: team name
  1519             statInfo := TStatInfoType(i);
  1411             for i:= 0 to Pred(TeamsCount) do
  1520             if (n <> 2) and ((statInfo <> siPlayerKills)
       
  1521                     and (statInfo <> siClanHealth)) then
  1412                 begin
  1522                 begin
  1413                 with TeamsArray[i]^ do
  1523                 LuaParameterCountError(call, params2, n);
       
  1524                 end
       
  1525             else if (n <> 3) and ((statInfo = siPlayerKills)
       
  1526                     or (statInfo = siClanHealth)) then
       
  1527                 begin
       
  1528                 LuaParameterCountError(call, params3, n);
       
  1529                 end
       
  1530             else
       
  1531                 begin
       
  1532                 if ((statInfo = siPlayerKills) or (statInfo = siClanHealth)) then
  1414                     begin
  1533                     begin
  1415                         if TeamName = lua_tostring(L, 3) then
  1534                     // 3: team name
       
  1535                     for i:= 0 to Pred(TeamsCount) do
       
  1536                         begin
       
  1537                         color:= '0';
       
  1538                         tn:= lua_tostring(L, 3);
       
  1539                         with TeamsArray[i]^ do
  1416                             begin
  1540                             begin
  1417                             color := uUtils.IntToStr(Clan^.Color);
  1541                                 if TeamName = tn then
  1418                             Break;
  1542                                     begin
       
  1543                                     color := uUtils.IntToStr(Clan^.Color);
       
  1544                                     Break;
       
  1545                                     end
  1419                             end
  1546                             end
       
  1547                         end;
       
  1548                     if (statInfo = siPlayerKills) then
       
  1549                         begin
       
  1550                             SendStat(siPlayerKills, color + ' ' +
       
  1551                                 lua_tostring(L, 2) + ' ' + tn);
       
  1552                         end
       
  1553                     else if (statInfo = siClanHealth) then
       
  1554                         begin
       
  1555                             SendStat(siClanHealth, color + ' ' +
       
  1556                                 lua_tostring(L, 2));
       
  1557                         end
  1420                     end
  1558                     end
       
  1559                 else
       
  1560                     begin
       
  1561                     SendStat(statInfo,lua_tostring(L, 2));
       
  1562                     end;
  1421                 end;
  1563                 end;
  1422             if (statInfo = siPlayerKills) then
       
  1423                 begin
       
  1424                     SendStat(siPlayerKills, color + ' ' +
       
  1425                         lua_tostring(L, 2) + ' ' + TeamsArray[i]^.TeamName);
       
  1426                 end
       
  1427             else if (statInfo = siClanHealth) then
       
  1428                 begin
       
  1429                     SendStat(siClanHealth, color + ' ' +
       
  1430                         lua_tostring(L, 2));
       
  1431                 end
       
  1432             end
       
  1433         else
       
  1434             begin
       
  1435             SendStat(statInfo,lua_tostring(L, 2));
       
  1436             end;
  1564             end;
  1437         end;
  1565         end;
  1438     lc_sendstat:= 0
  1566     lc_sendstat:= 0
  1439 end;
  1567 end;
  1440 
  1568 
  1451     tryhard: boolean;
  1579     tryhard: boolean;
  1452     left, right: LongInt;
  1580     left, right: LongInt;
  1453 begin
  1581 begin
  1454     tryhard:= false;
  1582     tryhard:= false;
  1455     if (lua_gettop(L) <> 4) and (lua_gettop(L) <> 5) then
  1583     if (lua_gettop(L) <> 4) and (lua_gettop(L) <> 5) then
  1456         LuaParameterCountError('FindPlace', 'gearUid, fall, left, right[, tryHarder]', lua_gettop(L))
  1584         LuaParameterCountError('FindPlace', 'gearUid, fall, left, right [, tryHarder]', lua_gettop(L))
  1457     else
  1585     else
  1458         begin
  1586         begin
  1459         gear:= GearByUID(lua_tointeger(L, 1));
  1587         gear:= GearByUID(lua_tointeger(L, 1));
  1460         fall:= lua_toboolean(L, 2);
  1588         fall:= lua_toboolean(L, 2);
  1461         left:= lua_tointeger(L, 3);
  1589         left:= lua_tointeger(L, 3);
  1472     lc_findplace:= 1
  1600     lc_findplace:= 1
  1473 end;
  1601 end;
  1474 
  1602 
  1475 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
  1603 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
  1476 var gear: PGear;
  1604 var gear: PGear;
  1477 begin
  1605     n, s: LongInt;
  1478     if lua_gettop(L) = 1 then
  1606 const
  1479         PlaySound(TSound(lua_tointeger(L, 1)))
  1607     call = 'PlaySound';
  1480     else if lua_gettop(L) = 2 then
  1608     params = 'soundId [, hhGearUid]';
  1481         begin
  1609 begin
  1482         gear:= GearByUID(lua_tointeger(L, 2));
  1610     n:= lua_gettop(L);
  1483         if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
  1611     if (n < 1) or (n > 2) then
  1484             AddVoice(TSound(lua_tointeger(L, 1)),gear^.Hedgehog^.Team^.Voicepack)
  1612         LuaParameterCountError(call, params, n)
  1485         end
  1613     else
  1486     else LuaParameterCountError('PlaySound', 'soundId', lua_gettop(L));
  1614         begin
       
  1615         s:= LuaToSoundOrd(L, 1, call, params);
       
  1616         if s >= 0 then
       
  1617             begin
       
  1618             // no gear specified
       
  1619             if n = 1 then
       
  1620                 PlaySound(TSound(s))
       
  1621             else if lua_gettop(L) = 2 then
       
  1622                 begin
       
  1623                 gear:= GearByUID(lua_tointeger(L, 2));
       
  1624                 if (gear <> nil) and (gear^.Kind = gtHedgehog) and (gear^.Hedgehog <> nil) then
       
  1625                     AddVoice(TSound(s),gear^.Hedgehog^.Team^.Voicepack)
       
  1626                 end;
       
  1627             end;
       
  1628         end;
  1487     lc_playsound:= 0;
  1629     lc_playsound:= 0;
  1488 end;
  1630 end;
  1489 
  1631 
  1490 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1632 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
  1491 var np: LongInt;
  1633 var np: LongInt;
  1492 begin
  1634 begin
  1493     np:= lua_gettop(L);
  1635     np:= lua_gettop(L);
  1494     if (np < 5) or (np > 6) then
  1636     if (np < 5) or (np > 6) then
  1495         begin
  1637         begin
  1496         LuaParameterCountError('AddTeam', 'teamname, color, grave, fort, voicepack[, flag]', lua_gettop(L));
  1638         LuaParameterCountError('AddTeam', 'teamname, color, grave, fort, voicepack [, flag]', lua_gettop(L));
  1497         //lua_pushnil(L)
  1639         //lua_pushnil(L)
  1498         end
  1640         end
  1499     else
  1641     else
  1500         begin
  1642         begin
  1501         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true, true);
  1643         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true, true);
  1709         lua_pushnumber(L, ZoomValue);
  1851         lua_pushnumber(L, ZoomValue);
  1710     lc_getzoom:= 1
  1852     lc_getzoom:= 1
  1711 end;
  1853 end;
  1712 
  1854 
  1713 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
  1855 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
  1714 var np: LongInt;
  1856 var np, at: LongInt;
       
  1857 const
       
  1858     call = 'SetAmmo';
       
  1859     params = 'ammoType, count, probability, delay [, numberInCrate]';
  1715 begin
  1860 begin
  1716     np:= lua_gettop(L);
  1861     np:= lua_gettop(L);
  1717     if (np < 4) or (np > 5) then
  1862     if (np < 4) or (np > 5) then
  1718         LuaParameterCountError('SetAmmo', 'ammoType, count, probability, delay[, numberInCrate]', lua_gettop(L))
  1863         LuaParameterCountError(call, params, lua_gettop(L))
  1719     else if np = 4 then
  1864     else
  1720         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
  1865         begin
  1721     else
  1866         at:= LuaToAmmoTypeOrd(L, 1, call, params);
  1722         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
  1867         if at >= 0 then
       
  1868             begin
       
  1869             if np = 4 then
       
  1870                 ScriptSetAmmo(TAmmoType(at), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), 1)
       
  1871             else
       
  1872                 ScriptSetAmmo(TAmmoType(at), lua_tointeger(L, 2), lua_tointeger(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
       
  1873             end;
       
  1874         end;
  1723     lc_setammo:= 0
  1875     lc_setammo:= 0
  1724 end;
  1876 end;
  1725 
  1877 
  1726 function lc_setammodelay(L : Plua_State) : LongInt; Cdecl;
  1878 function lc_setammodelay(L : Plua_State) : LongInt; Cdecl;
  1727 var np: LongInt;
  1879 var np: LongInt;
       
  1880 const
       
  1881     call = 'SetAmmoDelay';
       
  1882     params = 'ammoType, delay';
  1728 begin
  1883 begin
  1729     np:= lua_gettop(L);
  1884     np:= lua_gettop(L);
  1730     if (np <> 2) then
  1885     if (np <> 2) then
  1731         LuaParameterCountError('SetAmmoDelay', 'ammoType, delay', lua_gettop(L))
  1886         LuaParameterCountError(call, params, lua_gettop(L))
  1732     else
  1887     else
  1733         ScriptSetAmmoDelay(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2));
  1888         begin
       
  1889         np:= LuaToAmmoTypeOrd(L, 1, call, params);
       
  1890         if np >= 0 then
       
  1891             ScriptSetAmmoDelay(TAmmoType(np), lua_tointeger(L, 2));
       
  1892         end;
  1734     lc_setammodelay:= 0
  1893     lc_setammodelay:= 0
  1735 end;
  1894 end;
  1736 
  1895 
  1737 function lc_setammostore(L : Plua_State) : LongInt; Cdecl;
  1896 function lc_setammostore(L : Plua_State) : LongInt; Cdecl;
  1738 var np: LongInt;
  1897 var np: LongInt;
  1902         if n = 5 then
  2061         if n = 5 then
  1903             lf:= lua_tointeger(L, 5)
  2062             lf:= lua_tointeger(L, 5)
  1904         else
  2063         else
  1905             lf:= 0;
  2064             lf:= 0;
  1906 
  2065 
  1907         n:= lua_tointeger(L, 3);
  2066         n:= LuaToSpriteOrd(L, 3, call, params);
  1908         if IsValidSprite(n, call, params) then
  2067         if n >= 0 then
       
  2068             begin
  1909             spr:= TSprite(n);
  2069             spr:= TSprite(n);
  1910             if SpritesData[spr].Surface = nil then
  2070             if SpritesData[spr].Surface = nil then
  1911                 LuaError(call + ': ' + EnumToStr(spr) + ' cannot be placed! (required information not loaded)' )
  2071                 LuaError(call + ': ' + EnumToStr(spr) + ' cannot be placed! (required information not loaded)' )
  1912             else
  2072             else
  1913                 placed:= TryPlaceOnLand(
  2073                 placed:= TryPlaceOnLand(
  1914                     lua_tointeger(L, 1) - SpritesData[spr].Width div 2,
  2074                     lua_tointeger(L, 1) - SpritesData[spr].Width div 2,
  1915                     lua_tointeger(L, 2) - SpritesData[spr].Height div 2,
  2075                     lua_tointeger(L, 2) - SpritesData[spr].Height div 2,
  1916                     spr, lua_tointeger(L, 4), true, false, lfBouncy);
  2076                     spr, lua_tointeger(L, 4), true, false, lf);
       
  2077             end;
  1917         end;
  2078         end;
  1918 
  2079 
  1919     lua_pushboolean(L, placed);
  2080     lua_pushboolean(L, placed);
  1920     lc_placesprite:= 1
  2081     lc_placesprite:= 1
  1921 end;
  2082 end;