hedgewars/uScript.pas
changeset 2948 3f21a9dc93d0
parent 2924 908851e59958
child 2949 d137a9da7701
equal deleted inserted replaced
2947:803b277e4894 2948:3f21a9dc93d0
    37 procedure free_uScript;
    37 procedure free_uScript;
    38 
    38 
    39 implementation
    39 implementation
    40 {$IFNDEF IPHONEOS}
    40 {$IFNDEF IPHONEOS}
    41 uses LuaPas in 'LuaPas.pas',
    41 uses LuaPas in 'LuaPas.pas',
    42 	uConsole,
    42     uConsole,
    43 	uMisc,
    43     uMisc,
    44 	uConsts,
    44     uConsts,
    45 	uGears,
    45     uGears,
    46 	uFloat,
    46     uFloat,
    47 	uWorld,
    47     uWorld,
    48 	uAmmos,
    48     uAmmos,
    49 	uSound,
    49     uSound,
    50 	uTeams,
    50     uTeams,
    51 	uKeys,
    51     uKeys,
    52 	typinfo;
    52     typinfo;
    53 	
    53     
    54 var luaState : Plua_State;
    54 var luaState : Plua_State;
    55 	ScriptAmmoStore : shortstring;
    55     ScriptAmmoStore : shortstring;
    56 	ScriptLoaded : boolean;
    56     ScriptLoaded : boolean;
    57 	
    57     
    58 procedure ScriptPrepareAmmoStore; forward;
    58 procedure ScriptPrepareAmmoStore; forward;
    59 procedure ScriptApplyAmmoStore; forward;
    59 procedure ScriptApplyAmmoStore; forward;
    60 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte); forward;
    60 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte); forward;
    61 
    61 
    62 // wrapped calls //
    62 // wrapped calls //
    66 // where L contains the state, returns the number of return values on the stack
    66 // where L contains the state, returns the number of return values on the stack
    67 // call lua_gettop(L) to receive number of parameters passed
    67 // call lua_gettop(L) to receive number of parameters passed
    68 
    68 
    69 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
    69 function lc_writelntoconsole(L : Plua_State) : LongInt; Cdecl;
    70 begin
    70 begin
    71 	if lua_gettop(L) = 1 then
    71     if lua_gettop(L) = 1 then
    72 		begin
    72         begin
    73 		WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
    73         WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
    74 		end
    74         end
    75 	else
    75     else
    76 		WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
    76         WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
    77 	lc_writelntoconsole:= 0;
    77     lc_writelntoconsole:= 0;
    78 end;
    78 end;
    79 
    79 
    80 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
    80 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
    81 begin
    81 begin
    82 	if lua_gettop(L) = 1 then
    82     if lua_gettop(L) = 1 then
    83 		begin
    83         begin
    84 		ParseCommand(lua_tostring(L ,1), true);
    84         ParseCommand(lua_tostring(L ,1), true);
    85 		end
    85         end
    86 	else
    86     else
    87 		WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
    87         WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
    88 	lc_parsecommand:= 0;
    88     lc_parsecommand:= 0;
    89 end;
    89 end;
    90 
    90 
    91 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
    91 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
    92 begin
    92 begin
    93 	if lua_gettop(L) = 5 then
    93     if lua_gettop(L) = 5 then
    94 		begin
    94         begin
    95 		ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
    95         ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
    96 		end
    96         end
    97 	else
    97     else
    98 		WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
    98         WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
    99 	lc_showmission:= 0;
    99     lc_showmission:= 0;
   100 end;
   100 end;
   101 
   101 
   102 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   102 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   103 begin
   103 begin
   104 	HideMission;
   104     HideMission;
   105 	lc_hidemission:= 0;
   105     lc_hidemission:= 0;
   106 end;
   106 end;
   107 
   107 
   108 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   108 function lc_addgear(L : Plua_State) : LongInt; Cdecl;
   109 var gear : PGear;
   109 var gear : PGear;
   110 	x, y, s, t: LongInt;
   110     x, y, s, t: LongInt;
   111 	dx, dy: hwFloat;
   111     dx, dy: hwFloat;
   112 	gt: TGearType;
   112     gt: TGearType;
   113 begin
   113 begin
   114 	if lua_gettop(L) <> 7 then
   114     if lua_gettop(L) <> 7 then
   115 		begin
   115         begin
   116 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
   116         WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
   117 		lua_pushnil(L); // return value on stack (nil)
   117         lua_pushnil(L); // return value on stack (nil)
   118 		end
   118         end
   119 	else
   119     else
   120 		begin
   120         begin
   121 		x:= lua_tointeger(L, 1);
   121         x:= lua_tointeger(L, 1);
   122 		y:= lua_tointeger(L, 2);
   122         y:= lua_tointeger(L, 2);
   123 		gt:= TGearType(lua_tointeger(L, 3));
   123         gt:= TGearType(lua_tointeger(L, 3));
   124 		s:= lua_tointeger(L, 4);
   124         s:= lua_tointeger(L, 4);
   125 		dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
   125         dx:= int2hwFloat(round(lua_tonumber(L, 5) * 1000)) / 1000;
   126 		dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
   126         dy:= int2hwFloat(round(lua_tonumber(L, 6) * 1000)) / 1000;
   127 		t:= lua_tointeger(L, 7);
   127         t:= lua_tointeger(L, 7);
   128 
   128 
   129 		gear:= AddGear(x, y, gt, s, dx, dy, t);
   129         gear:= AddGear(x, y, gt, s, dx, dy, t);
   130 		lua_pushnumber(L, gear^.uid)
   130         lua_pushnumber(L, gear^.uid)
   131 		end;
   131         end;
   132 	lc_addgear:= 1; // 1 return value
   132     lc_addgear:= 1; // 1 return value
   133 end;
   133 end;
   134 
   134 
   135 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   135 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   136 var gear : PGear;
   136 var gear : PGear;
   137 begin
   137 begin
   138 	if lua_gettop(L) <> 1 then
   138     if lua_gettop(L) <> 1 then
   139 		begin
   139         begin
   140 		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
   140         WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
   141 		lua_pushnil(L); // return value on stack (nil)
   141         lua_pushnil(L); // return value on stack (nil)
   142 		end
   142         end
   143 	else
   143     else
   144 		begin
   144         begin
   145 		gear:= GearByUID(lua_tointeger(L, 1));
   145         gear:= GearByUID(lua_tointeger(L, 1));
   146 		if gear <> nil then
   146         if gear <> nil then
   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;
   152 function lc_sethealth(L : Plua_State) : LongInt; Cdecl;
   153 var gear : PGear;
   153 var gear : PGear;
   154 begin
   154 begin
   155 	if lua_gettop(L) <> 2 then
   155     if lua_gettop(L) <> 2 then
   156 		begin
   156         begin
   157 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
   157         WriteLnToConsole('LUA: Wrong number of parameters passed to SetHealth!');
   158 		end
   158         end
   159 	else
   159     else
   160 		begin
   160         begin
   161 		gear:= GearByUID(lua_tointeger(L, 1));
   161         gear:= GearByUID(lua_tointeger(L, 1));
   162 		if (gear <> nil) and (gear^.Kind = gtHedgehog) then gear^.Health:= lua_tointeger(L, 2)
   162         if (gear <> nil) and (gear^.Kind = gtHedgehog) then gear^.Health:= lua_tointeger(L, 2)
   163 		end;
   163         end;
   164 	lc_sethealth:= 0
   164     lc_sethealth:= 0
   165 end;
   165 end;
   166 
   166 
   167 function lc_endgame(L : Plua_State) : LongInt; Cdecl;
   167 function lc_endgame(L : Plua_State) : LongInt; Cdecl;
   168 begin
   168 begin
   169 	GameState:= gsExit;
   169     GameState:= gsExit;
   170 	lc_endgame:= 0
   170     lc_endgame:= 0
   171 end;
   171 end;
   172 
   172 
   173 function lc_findplace(L : Plua_State) : LongInt; Cdecl;
   173 function lc_findplace(L : Plua_State) : LongInt; Cdecl;
   174 var gear: PGear;
   174 var gear: PGear;
   175 	fall: boolean;
   175     fall: boolean;
   176 	left, right: LongInt;
   176     left, right: LongInt;
   177 begin
   177 begin
   178 	if lua_gettop(L) <> 4 then
   178     if lua_gettop(L) <> 4 then
   179 		WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
   179         WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
   180 	else
   180     else
   181 		begin
   181         begin
   182 		gear:= GearByUID(lua_tointeger(L, 1));
   182         gear:= GearByUID(lua_tointeger(L, 1));
   183 		fall:= lua_toboolean(L, 2);
   183         fall:= lua_toboolean(L, 2);
   184 		left:= lua_tointeger(L, 3);
   184         left:= lua_tointeger(L, 3);
   185 		right:= lua_tointeger(L, 4);
   185         right:= lua_tointeger(L, 4);
   186 		if gear <> nil then
   186         if gear <> nil then
   187 			FindPlace(gear, fall, left, right)
   187             FindPlace(gear, fall, left, right)
   188 		end;
   188         end;
   189 	lc_findplace:= 0
   189     lc_findplace:= 0
   190 end;
   190 end;
   191 
   191 
   192 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
   192 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
   193 begin
   193 begin
   194 	if lua_gettop(L) <> 1 then
   194     if lua_gettop(L) <> 1 then
   195 		WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
   195         WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
   196 	else
   196     else
   197 		PlaySound(TSound(lua_tointeger(L, 1)));
   197         PlaySound(TSound(lua_tointeger(L, 1)));
   198 	lc_playsound:= 0;
   198     lc_playsound:= 0;
   199 end;
   199 end;
   200 
   200 
   201 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
   201 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
   202 begin
   202 begin
   203 	if lua_gettop(L) <> 5 then
   203     if lua_gettop(L) <> 5 then
   204 		begin
   204         begin
   205 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!');
   205         WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!');
   206 		//lua_pushnil(L)
   206         //lua_pushnil(L)
   207 		end
   207         end
   208 	else
   208     else
   209 		begin
   209         begin
   210 		ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
   210         ParseCommand('addteam x ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
   211 		ParseCommand('grave ' + lua_tostring(L, 3), true);
   211         ParseCommand('grave ' + lua_tostring(L, 3), true);
   212 		ParseCommand('fort ' + lua_tostring(L, 4), true);
   212         ParseCommand('fort ' + lua_tostring(L, 4), true);
   213 		ParseCommand('voicepack ' + lua_tostring(L, 5), true);
   213         ParseCommand('voicepack ' + lua_tostring(L, 5), true);
   214 		CurrentTeam^.Binds:= DefaultBinds;
   214         CurrentTeam^.Binds:= DefaultBinds;
   215 		// fails on x64
   215         // fails on x64
   216 		//lua_pushinteger(L, LongInt(CurrentTeam));
   216         //lua_pushinteger(L, LongInt(CurrentTeam));
   217 		end;
   217         end;
   218 	lc_addteam:= 0;//1;
   218     lc_addteam:= 0;//1;
   219 end;
   219 end;
   220 
   220 
   221 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
   221 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
   222 begin
   222 begin
   223 	if lua_gettop(L) <> 4 then
   223     if lua_gettop(L) <> 4 then
   224 		begin
   224         begin
   225 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
   225         WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
   226 		lua_pushnil(L)
   226         lua_pushnil(L)
   227 		end
   227         end
   228 	else
   228     else
   229 		begin
   229         begin
   230 		ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
   230         ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
   231 		ParseCommand('hat ' + lua_tostring(L, 4), true);
   231         ParseCommand('hat ' + lua_tostring(L, 4), true);
   232 		lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
   232         lua_pushinteger(L, CurrentHedgehog^.Gear^.uid);
   233 		end;
   233         end;
   234 	lc_addhog:= 1;
   234     lc_addhog:= 1;
   235 end;
   235 end;
   236 
   236 
   237 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
   237 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
   238 var gear: PGear;
   238 var gear: PGear;
   239 begin
   239 begin
   240 	if lua_gettop(L) <> 1 then
   240     if lua_gettop(L) <> 1 then
   241 		begin
   241         begin
   242 		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
   242         WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
   243 		lua_pushnil(L);
   243         lua_pushnil(L);
   244 		lua_pushnil(L)
   244         lua_pushnil(L)
   245 		end
   245         end
   246 	else
   246     else
   247 		begin
   247         begin
   248 		gear:= GearByUID(lua_tointeger(L, 1));
   248         gear:= GearByUID(lua_tointeger(L, 1));
   249 		if gear <> nil then
   249         if gear <> nil then
   250 			begin
   250             begin
   251 			lua_pushinteger(L, hwRound(gear^.X));
   251             lua_pushinteger(L, hwRound(gear^.X));
   252 			lua_pushinteger(L, hwRound(gear^.Y))
   252             lua_pushinteger(L, hwRound(gear^.Y))
   253 			end
   253             end
   254 		end;
   254         end;
   255 	lc_getgearposition:= 2;
   255     lc_getgearposition:= 2;
   256 end;
   256 end;
   257 
   257 
   258 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
   258 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
   259 var gear: PGear;
   259 var gear: PGear;
   260 	x, y: LongInt;
   260     x, y: LongInt;
   261 begin
   261 begin
   262 	if lua_gettop(L) <> 3 then
   262     if lua_gettop(L) <> 3 then
   263 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
   263         WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
   264 	else
   264     else
   265 		begin
   265         begin
   266 		gear:= GearByUID(lua_tointeger(L, 1));
   266         gear:= GearByUID(lua_tointeger(L, 1));
   267 		if gear <> nil then
   267         if gear <> nil then
   268 			begin
   268             begin
   269 			x:= lua_tointeger(L, 2);
   269             x:= lua_tointeger(L, 2);
   270 			y:= lua_tointeger(L, 3);
   270             y:= lua_tointeger(L, 3);
   271 			gear^.X:= int2hwfloat(x);
   271             gear^.X:= int2hwfloat(x);
   272 			gear^.Y:= int2hwfloat(y);
   272             gear^.Y:= int2hwfloat(y);
   273 			end
   273             end
   274 		end;
   274         end;
   275 	lc_setgearposition:= 0
   275     lc_setgearposition:= 0
   276 end;
   276 end;
   277 
   277 
   278 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
   278 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
   279 begin
   279 begin
   280 	if lua_gettop(L) <> 3 then
   280     if lua_gettop(L) <> 3 then
   281 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
   281         WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
   282 	else
   282     else
   283 		begin
   283         begin
   284 		ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3));
   284         ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3));
   285 		end;
   285         end;
   286 	lc_setammo:= 0
   286     lc_setammo:= 0
   287 end;
   287 end;
   288 ///////////////////
   288 ///////////////////
   289 
   289 
   290 procedure ScriptPrintStack;
   290 procedure ScriptPrintStack;
   291 var n, i : LongInt;
   291 var n, i : LongInt;
   292 begin
   292 begin
   293 	n:= lua_gettop(luaState);
   293     n:= lua_gettop(luaState);
   294 	WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
   294     WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
   295 	for i:= 1 to n do
   295     for i:= 1 to n do
   296 		if not lua_isboolean(luaState, i) then
   296         if not lua_isboolean(luaState, i) then
   297 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
   297             WriteLnToConsole('LUA:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
   298 		else if lua_toboolean(luaState, i) then
   298         else if lua_toboolean(luaState, i) then
   299 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': true')
   299             WriteLnToConsole('LUA:  ' + inttostr(i) + ': true')
   300 		else
   300         else
   301 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': false');
   301             WriteLnToConsole('LUA:  ' + inttostr(i) + ': false');
   302 end;
   302 end;
   303 
   303 
   304 procedure ScriptClearStack;
   304 procedure ScriptClearStack;
   305 begin
   305 begin
   306 lua_settop(luaState, 0)
   306 lua_settop(luaState, 0)
   332 lua_pop(luaState, 1);
   332 lua_pop(luaState, 1);
   333 end;
   333 end;
   334 
   334 
   335 procedure ScriptOnGameInit;
   335 procedure ScriptOnGameInit;
   336 begin
   336 begin
   337 	// not required if there's no script to run
   337     // not required if there's no script to run
   338 	if not ScriptLoaded then
   338     if not ScriptLoaded then
   339 		exit;
   339         exit;
   340 			
   340             
   341 	// push game variables so they may be modified by the script
   341     // push game variables so they may be modified by the script
   342 	ScriptSetInteger('GameFlags', GameFlags);
   342     ScriptSetInteger('GameFlags', GameFlags);
   343 	ScriptSetString('Seed', cSeed);
   343     ScriptSetString('Seed', cSeed);
   344 	ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   344     ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   345 	ScriptSetInteger('CaseFreq', cCaseFactor);
   345     ScriptSetInteger('CaseFreq', cCaseFactor);
   346 	ScriptSetInteger('LandAdds', cLandAdditions);
   346     ScriptSetInteger('LandAdds', cLandAdditions);
   347 	ScriptSetInteger('Delay', cInactDelay);
   347     ScriptSetInteger('Delay', cInactDelay);
   348 	ScriptSetString('Map', '');
   348     ScriptSetString('Map', '');
   349 	ScriptSetString('Theme', '');
   349     ScriptSetString('Theme', '');
   350 
   350 
   351 	ScriptCall('onGameInit');
   351     ScriptCall('onGameInit');
   352 	
   352     
   353 	// pop game variables
   353     // pop game variables
   354 	ParseCommand('seed ' + ScriptGetString('Seed'), true);
   354     ParseCommand('seed ' + ScriptGetString('Seed'), true);
   355 	ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   355     ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   356 	ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   356     ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   357 	ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   357     ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   358 	ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   358     ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   359 	ParseCommand('$delay ' + ScriptGetString('Delay'), true);
   359     ParseCommand('$delay ' + ScriptGetString('Delay'), true);
   360 	if ScriptGetString('Map') <> '' then
   360     if ScriptGetString('Map') <> '' then
   361 		ParseCommand('map ' + ScriptGetString('Map'), true);
   361         ParseCommand('map ' + ScriptGetString('Map'), true);
   362 	if ScriptGetString('Theme') <> '' then
   362     if ScriptGetString('Theme') <> '' then
   363 		ParseCommand('theme ' + ScriptGetString('Theme'), true);	
   363         ParseCommand('theme ' + ScriptGetString('Theme'), true);    
   364 
   364 
   365 	ScriptPrepareAmmoStore;
   365     ScriptPrepareAmmoStore;
   366 	ScriptCall('onAmmoStoreInit');
   366     ScriptCall('onAmmoStoreInit');
   367 	ScriptApplyAmmoStore;
   367     ScriptApplyAmmoStore;
   368 end;
   368 end;
   369 
   369 
   370 procedure ScriptLoad(name : shortstring);
   370 procedure ScriptLoad(name : shortstring);
   371 var ret : LongInt;
   371 var ret : LongInt;
   372 begin
   372 begin
   373 	ret:= luaL_loadfile(luaState, Str2PChar(name));
   373     ret:= luaL_loadfile(luaState, Str2PChar(name));
   374 	if ret <> 0 then
   374     if ret <> 0 then
   375 		WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   375         WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   376 	else
   376     else
   377 		begin
   377         begin
   378 		WriteLnToConsole('LUA: ' + name + ' loaded');
   378         WriteLnToConsole('LUA: ' + name + ' loaded');
   379 		// call the script file
   379         // call the script file
   380 		lua_pcall(luaState, 0, 0, 0);
   380         lua_pcall(luaState, 0, 0, 0);
   381 		ScriptLoaded:= true
   381         ScriptLoaded:= true
   382 		end
   382         end
   383 end;
   383 end;
   384 
   384 
   385 procedure SetGlobals;
   385 procedure SetGlobals;
   386 begin
   386 begin
   387 	ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
   387     ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
   388 end;
   388 end;
   389 
   389 
   390 procedure GetGlobals;
   390 procedure GetGlobals;
   391 begin
   391 begin
   392 	TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   392     TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   393 end;
   393 end;
   394 
   394 
   395 procedure ScriptCall(fname : shortstring);
   395 procedure ScriptCall(fname : shortstring);
   396 begin
   396 begin
   397 	if not ScriptLoaded then
   397     if not ScriptLoaded then
   398 		exit;
   398         exit;
   399 	SetGlobals;
   399     SetGlobals;
   400 	lua_getglobal(luaState, Str2PChar(fname));
   400     lua_getglobal(luaState, Str2PChar(fname));
   401 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   401     if lua_pcall(luaState, 0, 0, 0) <> 0 then
   402 		begin
   402         begin
   403 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   403         WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   404 		lua_pop(luaState, 1)
   404         lua_pop(luaState, 1)
   405 		end;
   405         end;
   406 	GetGlobals;
   406     GetGlobals;
   407 end;
   407 end;
   408 
   408 
   409 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
   409 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
   410 begin
   410 begin
   411 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   411 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   421 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   421 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   422 end;
   422 end;
   423 
   423 
   424 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   424 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   425 begin
   425 begin
   426 	if not ScriptLoaded then
   426     if not ScriptLoaded then
   427 		exit;
   427         exit;
   428 	SetGlobals;
   428     SetGlobals;
   429 	lua_getglobal(luaState, Str2PChar(fname));
   429     lua_getglobal(luaState, Str2PChar(fname));
   430 	lua_pushinteger(luaState, par1);
   430     lua_pushinteger(luaState, par1);
   431 	lua_pushinteger(luaState, par2);
   431     lua_pushinteger(luaState, par2);
   432 	lua_pushinteger(luaState, par3);
   432     lua_pushinteger(luaState, par3);
   433 	lua_pushinteger(luaState, par4);
   433     lua_pushinteger(luaState, par4);
   434 	ScriptCall:= 0;
   434     ScriptCall:= 0;
   435 	if lua_pcall(luaState, 4, 1, 0) <> 0 then
   435     if lua_pcall(luaState, 4, 1, 0) <> 0 then
   436 		begin
   436         begin
   437 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   437         WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   438 		lua_pop(luaState, 1)
   438         lua_pop(luaState, 1)
   439 		end
   439         end
   440 	else
   440     else
   441 		begin
   441         begin
   442 		ScriptCall:= lua_tointeger(luaState, -1);
   442         ScriptCall:= lua_tointeger(luaState, -1);
   443 		lua_pop(luaState, 1)
   443         lua_pop(luaState, 1)
   444 		end;
   444         end;
   445 	GetGlobals;
   445     GetGlobals;
   446 end;
   446 end;
   447 
   447 
   448 procedure ScriptPrepareAmmoStore;
   448 procedure ScriptPrepareAmmoStore;
   449 var i: ShortInt;
   449 var i: ShortInt;
   450 begin
   450 begin
   451 ScriptAmmoStore:= '';
   451 ScriptAmmoStore:= '';
   452 for i:=1 to ord(High(TAmmoType)) do
   452 for i:=1 to ord(High(TAmmoType)) do
   453 	ScriptAmmoStore:= ScriptAmmoStore + '0000';
   453     ScriptAmmoStore:= ScriptAmmoStore + '0000';
   454 end;
   454 end;
   455 
   455 
   456 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte);
   456 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte);
   457 begin
   457 begin
   458 if (ord(ammo) < 1) or (count > 9) or (count < 0) or (propability < 0) or (propability > 8) then
   458 if (ord(ammo) < 1) or (count > 9) or (count < 0) or (propability < 0) or (propability > 8) then
   459 	exit;
   459     exit;
   460 ScriptAmmoStore[ord(ammo)]:= inttostr(count)[1];
   460 ScriptAmmoStore[ord(ammo)]:= inttostr(count)[1];
   461 ScriptAmmoStore[ord(ammo) + ord(high(TAmmoType))]:= inttostr(propability)[1];
   461 ScriptAmmoStore[ord(ammo) + ord(high(TAmmoType))]:= inttostr(propability)[1];
   462 end;
   462 end;
   463 
   463 
   464 procedure ScriptApplyAmmoStore;
   464 procedure ScriptApplyAmmoStore;
   465 begin
   465 begin
   466 	AddAmmoStore(ScriptAmmoStore);
   466     AddAmmoStore(ScriptAmmoStore);
   467 end;
   467 end;
   468 
   468 
   469 // small helper functions making registering enums a lot easier
   469 // small helper functions making registering enums a lot easier
   470 function str(const en : TGearType) : shortstring; overload;
   470 function str(const en : TGearType) : shortstring; overload;
   471 begin
   471 begin
   483 end;
   483 end;
   484 ///////////////////
   484 ///////////////////
   485 
   485 
   486 procedure init_uScript;
   486 procedure init_uScript;
   487 var at : TGearType;
   487 var at : TGearType;
   488 	am : TAmmoType;
   488     am : TAmmoType;
   489 	st : TSound;
   489     st : TSound;
   490 begin
   490 begin
   491 // initialize lua
   491 // initialize lua
   492 luaState:= lua_open;
   492 luaState:= lua_open;
   493 
   493 
   494 // open internal libraries
   494 // open internal libraries
   517 ScriptSetInteger('gfRandomOrder',gfRandomOrder);
   517 ScriptSetInteger('gfRandomOrder',gfRandomOrder);
   518 ScriptSetInteger('gfKing',gfKing);
   518 ScriptSetInteger('gfKing',gfKing);
   519 
   519 
   520 // register gear types
   520 // register gear types
   521 for at:= Low(TGearType) to High(TGearType) do
   521 for at:= Low(TGearType) to High(TGearType) do
   522 	ScriptSetInteger(str(at), ord(at));
   522     ScriptSetInteger(str(at), ord(at));
   523 
   523 
   524 // register sounds
   524 // register sounds
   525 for st:= Low(TSound) to High(TSound) do
   525 for st:= Low(TSound) to High(TSound) do
   526 	ScriptSetInteger(str(st), ord(st));
   526     ScriptSetInteger(str(st), ord(st));
   527 
   527 
   528 // register ammo types
   528 // register ammo types
   529 for am:= Low(TAmmoType) to High(TAmmoType) do
   529 for am:= Low(TAmmoType) to High(TAmmoType) do
   530 	ScriptSetInteger(str(am), ord(am));
   530     ScriptSetInteger(str(am), ord(am));
   531 	
   531     
   532 // register functions
   532 // register functions
   533 lua_register(luaState, 'AddGear', @lc_addgear);
   533 lua_register(luaState, 'AddGear', @lc_addgear);
   534 lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
   534 lua_register(luaState, 'WriteLnToConsole', @lc_writelntoconsole);
   535 lua_register(luaState, 'GetGearType', @lc_getgeartype);
   535 lua_register(luaState, 'GetGearType', @lc_getgeartype);
   536 lua_register(luaState, 'EndGame', @lc_endgame);
   536 lua_register(luaState, 'EndGame', @lc_endgame);