hedgewars/uScript.pas
changeset 2791 ac4b69e5f9f7
parent 2790 83630d5f94db
child 2793 6bcf173ba9be
equal deleted inserted replaced
2790:83630d5f94db 2791:ac4b69e5f9f7
    69 	if lua_gettop(L) = 1 then
    69 	if lua_gettop(L) = 1 then
    70 		begin
    70 		begin
    71 		WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
    71 		WriteLnToConsole('LUA: ' + lua_tostring(L ,1));
    72 		end
    72 		end
    73 	else
    73 	else
    74 		AddFileLog('LUA: Wrong number of parameters passed to WriteLnToConsole!');
    74 		WriteLnToConsole('LUA: Wrong number of parameters passed to WriteLnToConsole!');
    75 	lc_writelntoconsole:= 0;
    75 	lc_writelntoconsole:= 0;
    76 end;
    76 end;
    77 
    77 
    78 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
    78 function lc_parsecommand(L : Plua_State) : LongInt; Cdecl;
    79 begin
    79 begin
    80 	if lua_gettop(L) = 1 then
    80 	if lua_gettop(L) = 1 then
    81 		begin
    81 		begin
    82 		ParseCommand(lua_tostring(L ,1), true);
    82 		ParseCommand(lua_tostring(L ,1), true);
    83 		end
    83 		end
    84 	else
    84 	else
    85 		AddFileLog('LUA: Wrong number of parameters passed to ParseCommand!');
    85 		WriteLnToConsole('LUA: Wrong number of parameters passed to ParseCommand!');
    86 	lc_parsecommand:= 0;
    86 	lc_parsecommand:= 0;
    87 end;
    87 end;
    88 
    88 
    89 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
    89 function lc_showmission(L : Plua_State) : LongInt; Cdecl;
    90 begin
    90 begin
    91 	if lua_gettop(L) = 5 then
    91 	if lua_gettop(L) = 5 then
    92 		begin
    92 		begin
    93 		ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
    93 		ShowMission(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4), lua_tointeger(L, 5));
    94 		end
    94 		end
    95 	else
    95 	else
    96 		AddFileLog('LUA: Wrong number of parameters passed to ShowMission!');
    96 		WriteLnToConsole('LUA: Wrong number of parameters passed to ShowMission!');
    97 	lc_showmission:= 0;
    97 	lc_showmission:= 0;
    98 end;
    98 end;
    99 
    99 
   100 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   100 function lc_hidemission(L : Plua_State) : LongInt; Cdecl;
   101 begin
   101 begin
   109 	dx, dy: hwFloat;
   109 	dx, dy: hwFloat;
   110 	gt: TGearType;
   110 	gt: TGearType;
   111 begin
   111 begin
   112 	if lua_gettop(L) <> 7 then
   112 	if lua_gettop(L) <> 7 then
   113 		begin
   113 		begin
   114 		AddFileLog('LUA: Wrong number of parameters passed to AddGear!');
   114 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddGear!');
   115 		lua_pushnil(L); // return value on stack (nil)
   115 		lua_pushnil(L); // return value on stack (nil)
   116 		end
   116 		end
   117 	else
   117 	else
   118 		begin
   118 		begin
   119 		x:= lua_tointeger(L, 1);
   119 		x:= lua_tointeger(L, 1);
   133 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   133 function lc_getgeartype(L : Plua_State) : LongInt; Cdecl;
   134 var gear : PGear;
   134 var gear : PGear;
   135 begin
   135 begin
   136 	if lua_gettop(L) <> 1 then
   136 	if lua_gettop(L) <> 1 then
   137 		begin
   137 		begin
   138 		AddFileLog('LUA: Wrong number of parameters passed to GetGearType!');
   138 		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearType!');
   139 		lua_pushnil(L); // return value on stack (nil)
   139 		lua_pushnil(L); // return value on stack (nil)
   140 		end
   140 		end
   141 	else
   141 	else
   142 		begin
   142 		begin
   143 		gear:= GearByUID(lua_tointeger(L, 1));
   143 		gear:= GearByUID(lua_tointeger(L, 1));
   157 var gear: PGear;
   157 var gear: PGear;
   158 	fall: boolean;
   158 	fall: boolean;
   159 	left, right: LongInt;
   159 	left, right: LongInt;
   160 begin
   160 begin
   161 	if lua_gettop(L) <> 4 then
   161 	if lua_gettop(L) <> 4 then
   162 		AddFileLog('LUA: Wrong number of parameters passed to FindPlace!')
   162 		WriteLnToConsole('LUA: Wrong number of parameters passed to FindPlace!')
   163 	else
   163 	else
   164 		begin
   164 		begin
   165 		gear:= GearByUID(lua_tointeger(L, 1));
   165 		gear:= GearByUID(lua_tointeger(L, 1));
   166 		fall:= lua_toboolean(L, 2);
   166 		fall:= lua_toboolean(L, 2);
   167 		left:= lua_tointeger(L, 3);
   167 		left:= lua_tointeger(L, 3);
   173 end;
   173 end;
   174 
   174 
   175 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
   175 function lc_playsound(L : Plua_State) : LongInt; Cdecl;
   176 begin
   176 begin
   177 	if lua_gettop(L) <> 1 then
   177 	if lua_gettop(L) <> 1 then
   178 		AddFileLog('LUA: Wrong number of parameters passed to PlaySound!')
   178 		WriteLnToConsole('LUA: Wrong number of parameters passed to PlaySound!')
   179 	else
   179 	else
   180 		PlaySound(TSound(lua_tointeger(L, 1)));
   180 		PlaySound(TSound(lua_tointeger(L, 1)));
   181 	lc_playsound:= 0;
   181 	lc_playsound:= 0;
   182 end;
   182 end;
   183 
   183 
   184 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
   184 function lc_addteam(L : Plua_State) : LongInt; Cdecl;
   185 begin
   185 begin
   186 	if lua_gettop(L) <> 5 then
   186 	if lua_gettop(L) <> 5 then
   187 		AddFileLog('LUA: Wrong number of parameters passed to AddTeam!')
   187 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddTeam!')
   188 	else
   188 	else
   189 		begin
   189 		begin
   190 		ParseCommand('addteam ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
   190 		ParseCommand('addteam ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 1), true);
   191 		ParseCommand('grave ' + lua_tostring(L, 3), true);
   191 		ParseCommand('grave ' + lua_tostring(L, 3), true);
   192 		ParseCommand('fort ' + lua_tostring(L, 4), true);
   192 		ParseCommand('fort ' + lua_tostring(L, 4), true);
   199 
   199 
   200 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
   200 function lc_addhog(L : Plua_State) : LongInt; Cdecl;
   201 begin
   201 begin
   202 	if lua_gettop(L) <> 4 then
   202 	if lua_gettop(L) <> 4 then
   203 		begin
   203 		begin
   204 		AddFileLog('LUA: Wrong number of parameters passed to AddHog!');
   204 		WriteLnToConsole('LUA: Wrong number of parameters passed to AddHog!');
   205 		lua_pushnil(L)
   205 		lua_pushnil(L)
   206 		end
   206 		end
   207 	else
   207 	else
   208 		begin
   208 		begin
   209 		ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
   209 		ParseCommand('addhh ' + lua_tostring(L, 2) + ' ' + lua_tostring(L, 3) + ' ' + lua_tostring(L, 1), true);
   217 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
   217 function lc_getgearposition(L : Plua_State) : LongInt; Cdecl;
   218 var gear: PGear;
   218 var gear: PGear;
   219 begin
   219 begin
   220 	if lua_gettop(L) <> 1 then
   220 	if lua_gettop(L) <> 1 then
   221 		begin
   221 		begin
   222 		AddFileLog('LUA: Wrong number of parameters passed to GetGearPosition!');
   222 		WriteLnToConsole('LUA: Wrong number of parameters passed to GetGearPosition!');
   223 		lua_pushnil(L);
   223 		lua_pushnil(L);
   224 		lua_pushnil(L)
   224 		lua_pushnil(L)
   225 		end
   225 		end
   226 	else
   226 	else
   227 		begin
   227 		begin
   238 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
   238 function lc_setgearposition(L : Plua_State) : LongInt; Cdecl;
   239 var gear: PGear;
   239 var gear: PGear;
   240 	x, y: LongInt;
   240 	x, y: LongInt;
   241 begin
   241 begin
   242 	if lua_gettop(L) <> 3 then
   242 	if lua_gettop(L) <> 3 then
   243 		AddFileLog('LUA: Wrong number of parameters passed to SetGearPosition!')
   243 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetGearPosition!')
   244 	else
   244 	else
   245 		begin
   245 		begin
   246 		gear:= GearByUID(lua_tointeger(L, 1));
   246 		gear:= GearByUID(lua_tointeger(L, 1));
   247 		if gear <> nil then
   247 		if gear <> nil then
   248 			begin
   248 			begin
   256 end;
   256 end;
   257 
   257 
   258 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
   258 function lc_setammo(L : Plua_State) : LongInt; Cdecl;
   259 begin
   259 begin
   260 	if lua_gettop(L) <> 3 then
   260 	if lua_gettop(L) <> 3 then
   261 		AddFileLog('LUA: Wrong number of parameters passed to SetAmmo!')
   261 		WriteLnToConsole('LUA: Wrong number of parameters passed to SetAmmo!')
   262 	else
   262 	else
   263 		begin
   263 		begin
   264 		ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3));
   264 		ScriptSetAmmo(TAmmoType(lua_tointeger(L, 1)), lua_tointeger(L, 2), lua_tointeger(L, 3));
   265 		end;
   265 		end;
   266 	lc_setammo:= 0
   266 	lc_setammo:= 0
   269 
   269 
   270 procedure ScriptPrintStack;
   270 procedure ScriptPrintStack;
   271 var n, i : LongInt;
   271 var n, i : LongInt;
   272 begin
   272 begin
   273 	n:= lua_gettop(luaState);
   273 	n:= lua_gettop(luaState);
   274 	AddFileLog('LUA: Stack (' + inttostr(n) + ' elements):');
   274 	WriteLnToConsole('LUA: Stack (' + inttostr(n) + ' elements):');
   275 	for i:= 1 to n do
   275 	for i:= 1 to n do
   276 		if not lua_isboolean(luaState, i) then
   276 		if not lua_isboolean(luaState, i) then
   277 			AddFileLog('LUA:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
   277 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': ' + lua_tostring(luaState, i))
   278 		else if lua_toboolean(luaState, i) then
   278 		else if lua_toboolean(luaState, i) then
   279 			AddFileLog('LUA:  ' + inttostr(i) + ': true')
   279 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': true')
   280 		else
   280 		else
   281 			AddFileLog('LUA:  ' + inttostr(i) + ': false');
   281 			WriteLnToConsole('LUA:  ' + inttostr(i) + ': false');
   282 end;
   282 end;
   283 
   283 
   284 procedure ScriptClearStack;
   284 procedure ScriptClearStack;
   285 begin
   285 begin
   286 lua_settop(luaState, 0)
   286 lua_settop(luaState, 0)
   346 procedure ScriptLoad(name : string);
   346 procedure ScriptLoad(name : string);
   347 var ret : LongInt;
   347 var ret : LongInt;
   348 begin
   348 begin
   349 	ret:= luaL_loadfile(luaState, Str2PChar(name));
   349 	ret:= luaL_loadfile(luaState, Str2PChar(name));
   350 	if ret <> 0 then
   350 	if ret <> 0 then
   351 		AddFileLog('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   351 		WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   352 	else
   352 	else
   353 		begin
   353 		begin
   354 		AddFileLog('LUA: ' + name + ' loaded');
   354 		WriteLnToConsole('LUA: ' + name + ' loaded');
   355 		// call the script file
   355 		// call the script file
   356 		lua_pcall(luaState, 0, 0, 0);	
   356 		lua_pcall(luaState, 0, 0, 0);	
   357 		end
   357 		end
   358 end;
   358 end;
   359 
   359 
   360 procedure ScriptCall(fname : string);
   360 procedure ScriptCall(fname : string);
   361 begin
   361 begin
   362 	lua_getglobal(luaState, Str2PChar(fname));
   362 	lua_getglobal(luaState, Str2PChar(fname));
   363 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   363 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   364 		begin
   364 		begin
   365 		AddFileLog('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   365 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   366 		lua_pop(luaState, 1)
   366 		lua_pop(luaState, 1)
   367 		end;
   367 		end;
   368 end;
   368 end;
   369 
   369 
   370 function ScriptCall(fname : string; par1: LongInt) : LongInt;
   370 function ScriptCall(fname : string; par1: LongInt) : LongInt;
   390 	lua_pushinteger(luaState, par3);
   390 	lua_pushinteger(luaState, par3);
   391 	lua_pushinteger(luaState, par4);
   391 	lua_pushinteger(luaState, par4);
   392 	ScriptCall:= 0;
   392 	ScriptCall:= 0;
   393 	if lua_pcall(luaState, 4, 1, 0) <> 0 then
   393 	if lua_pcall(luaState, 4, 1, 0) <> 0 then
   394 		begin
   394 		begin
   395 		AddFileLog('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   395 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   396 		lua_pop(luaState, 1)
   396 		lua_pop(luaState, 1)
   397 		end
   397 		end
   398 	else
   398 	else
   399 		begin
   399 		begin
   400 		ScriptCall:= lua_tointeger(luaState, -1);
   400 		ScriptCall:= lua_tointeger(luaState, -1);