hedgewars/uScript.pas
changeset 10611 58cad46782ff
parent 10603 bda5c7caf396
child 10613 f2597a21f562
equal deleted inserted replaced
10609:15f2908113a1 10611:58cad46782ff
    98     ScriptAmmoProbability : shortstring;
    98     ScriptAmmoProbability : shortstring;
    99     ScriptAmmoDelay : shortstring;
    99     ScriptAmmoDelay : shortstring;
   100     ScriptAmmoReinforcement : shortstring;
   100     ScriptAmmoReinforcement : shortstring;
   101     ScriptLoaded : boolean;
   101     ScriptLoaded : boolean;
   102     mapDims : boolean;
   102     mapDims : boolean;
       
   103     PointsBuffer: shortString;
   103 
   104 
   104 procedure ScriptPrepareAmmoStore; forward;
   105 procedure ScriptPrepareAmmoStore; forward;
   105 procedure ScriptApplyAmmoStore; forward;
   106 procedure ScriptApplyAmmoStore; forward;
   106 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   107 procedure ScriptSetAmmo(ammo : TAmmoType; count, probability, delay, reinforcement: Byte); forward;
   107 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   108 procedure ScriptSetAmmoDelay(ammo : TAmmoType; delay: Byte); forward;
   186         LuaParameterCountError('either ' + inttostr(count1) + ' or ' + inttostr(count2), call, paramsyntax, actual);
   187         LuaParameterCountError('either ' + inttostr(count1) + ' or ' + inttostr(count2), call, paramsyntax, actual);
   187         exit(false);
   188         exit(false);
   188         end;
   189         end;
   189 
   190 
   190     CheckAndFetchParamCount:= true;
   191     CheckAndFetchParamCount:= true;
       
   192 end;
       
   193 
       
   194 // check if is in range of count1 and count2
       
   195 function CheckAndFetchParamCountRange(L : Plua_State; count1, count2: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
       
   196 begin
       
   197     actual:= lua_gettop(L);
       
   198     if (actual < count1) or (actual > count2) then
       
   199         begin
       
   200         LuaParameterCountError('at least ' + inttostr(count1) + ', but at most ' + inttostr(count2), call, paramsyntax, actual);
       
   201         exit(false);
       
   202         end;
       
   203 
       
   204     CheckAndFetchParamCountRange:= true;
   191 end;
   205 end;
   192 
   206 
   193 // check if is same or higher as minCount
   207 // check if is same or higher as minCount
   194 function CheckAndFetchLuaParamMinCount(L : Plua_State; minCount: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
   208 function CheckAndFetchLuaParamMinCount(L : Plua_State; minCount: LongInt; call, paramsyntax: shortstring; out actual: LongInt): boolean; inline;
   195 begin
   209 begin
  2248 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
  2262 function lc_declareachievement(L : Plua_State) : LongInt; Cdecl;
  2249 begin
  2263 begin
  2250     if CheckLuaParamCount(L, 4, 'DeclareAchievement', 'achievementId, teamname, location, value') then
  2264     if CheckLuaParamCount(L, 4, 'DeclareAchievement', 'achievementId, teamname, location, value') then
  2251         declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
  2265         declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4));
  2252     lc_declareachievement:= 0
  2266     lc_declareachievement:= 0
       
  2267 end;
       
  2268 
       
  2269 
       
  2270 procedure ScriptFlushPoints();
       
  2271 begin
       
  2272     ParseCommand('draw ' + PointsBuffer, true, true);
       
  2273     PointsBuffer:= '';
       
  2274 end;
       
  2275 
       
  2276 
       
  2277 function lc_addPoint(L : Plua_State) : LongInt; Cdecl;
       
  2278 var np, param: integer;
       
  2279 begin
       
  2280     if CheckAndFetchParamCountRange(L, 2, 4, 'AddPoint', 'x, y, width [, erase]', np) then
       
  2281         begin
       
  2282         // x
       
  2283         param:= lua_tointeger(L,1);
       
  2284         PointsBuffer:= PointsBuffer + char((param and $FF00) shr 8);
       
  2285         PointsBuffer:= PointsBuffer + char((param and $FF));
       
  2286         // y
       
  2287         param:= lua_tointeger(L,2);
       
  2288         PointsBuffer:= PointsBuffer + char((param and $FF00) shr 8);
       
  2289         PointsBuffer:= PointsBuffer + char((param and $FF));
       
  2290         // width
       
  2291         if np > 2 then
       
  2292             param:= lua_tointeger(L,3)
       
  2293         else
       
  2294             param:= 0;
       
  2295 
       
  2296         if param <> 0 then
       
  2297             begin
       
  2298             param:= (param or $80);
       
  2299             // erase
       
  2300             if (np > 3) and lua_toboolean(L, 4) then
       
  2301                 param:= (param or $40);
       
  2302             PointsBuffer:= PointsBuffer + char(param);
       
  2303             end
       
  2304         // width is 0
       
  2305         else
       
  2306             PointsBuffer:= PointsBuffer + char(0);
       
  2307 
       
  2308         // flush before shortstring limit length is reached
       
  2309         if length(PointsBuffer) > 245 then
       
  2310             ScriptFlushPoints();
       
  2311         end;
       
  2312     lc_addPoint:= 0
       
  2313 end;
       
  2314 
       
  2315 
       
  2316 function lc_flushPoints(L : Plua_State) : LongInt; Cdecl;
       
  2317 begin
       
  2318     if CheckLuaParamCount(L, 0, 'FlushPoints', '') then
       
  2319         ScriptFlushPoints();
       
  2320     lc_flushPoints:= 0
  2253 end;
  2321 end;
  2254 
  2322 
  2255 // stuff for testing the lua API
  2323 // stuff for testing the lua API
  2256 function lc_endluatest(L : Plua_State) : LongInt; Cdecl;
  2324 function lc_endluatest(L : Plua_State) : LongInt; Cdecl;
  2257 begin
  2325 begin
  2945 lua_register(luaState, _P'GetGravity', @lc_getgravity);
  3013 lua_register(luaState, _P'GetGravity', @lc_getgravity);
  2946 lua_register(luaState, _P'SetGravity', @lc_setgravity);
  3014 lua_register(luaState, _P'SetGravity', @lc_setgravity);
  2947 lua_register(luaState, _P'SetWaterLine', @lc_setwaterline);
  3015 lua_register(luaState, _P'SetWaterLine', @lc_setwaterline);
  2948 lua_register(luaState, _P'SetNextWeapon', @lc_setnextweapon);
  3016 lua_register(luaState, _P'SetNextWeapon', @lc_setnextweapon);
  2949 lua_register(luaState, _P'SetWeapon', @lc_setweapon);
  3017 lua_register(luaState, _P'SetWeapon', @lc_setweapon);
       
  3018 // drawn map functions
       
  3019 lua_register(luaState, _P'AddPoint', @lc_addPoint);
       
  3020 lua_register(luaState, _P'FlushPoints', @lc_flushPoints);
  2950 
  3021 
  2951 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
  3022 lua_register(luaState, _P'SetGearAIHints', @lc_setaihintsongear);
  2952 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
  3023 lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload);
  2953 lua_register(luaState, _P'DeclareAchievement', @lc_declareachievement);
  3024 lua_register(luaState, _P'DeclareAchievement', @lc_declareachievement);
  2954 
  3025 
  3049 end;
  3120 end;
  3050 
  3121 
  3051 procedure initModule;
  3122 procedure initModule;
  3052 begin
  3123 begin
  3053 mapDims:= false;
  3124 mapDims:= false;
       
  3125 PointsBuffer:= '';
  3054 end;
  3126 end;
  3055 
  3127 
  3056 procedure freeModule;
  3128 procedure freeModule;
  3057 begin
  3129 begin
  3058 end;
  3130 end;