hedgewars/uScript.pas
changeset 2793 6bcf173ba9be
parent 2791 ac4b69e5f9f7
child 2794 befd3808c63a
equal deleted inserted replaced
2792:a3efbf1c4500 2793:6bcf173ba9be
    50 	uKeys,
    50 	uKeys,
    51 	typinfo;
    51 	typinfo;
    52 
    52 
    53 var luaState : Plua_State;
    53 var luaState : Plua_State;
    54 	ScriptAmmoStore : string;
    54 	ScriptAmmoStore : string;
       
    55 	ScriptLoaded : boolean;
    55 	
    56 	
    56 procedure ScriptPrepareAmmoStore; forward;
    57 procedure ScriptPrepareAmmoStore; forward;
    57 procedure ScriptApplyAmmoStore; forward;
    58 procedure ScriptApplyAmmoStore; forward;
    58 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte); forward;
    59 procedure ScriptSetAmmo(ammo : TAmmoType; count, propability: Byte); forward;
    59 
    60 
   312 lua_pop(luaState, 1);
   313 lua_pop(luaState, 1);
   313 end;
   314 end;
   314 
   315 
   315 procedure ScriptOnGameInit;
   316 procedure ScriptOnGameInit;
   316 begin
   317 begin
   317 		// push game variables so they may be modified by the script
   318 	// not required if there's no script to run
   318 		ScriptSetInteger('GameFlags', GameFlags);
   319 	if not ScriptLoaded then
   319 		ScriptSetString('Seed', cSeed);
   320 		exit;
   320 		ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   321 			
   321 		ScriptSetInteger('CaseFreq', cCaseFactor);
   322 	// push game variables so they may be modified by the script
   322 		ScriptSetInteger('LandAdds', cLandAdditions);
   323 	ScriptSetInteger('GameFlags', GameFlags);
   323 		ScriptSetInteger('Delay', cInactDelay);
   324 	ScriptSetString('Seed', cSeed);
   324 		ScriptSetString('Map', '');
   325 	ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   325 		ScriptSetString('Theme', '');
   326 	ScriptSetInteger('CaseFreq', cCaseFactor);
   326 
   327 	ScriptSetInteger('LandAdds', cLandAdditions);
   327 		ScriptCall('onGameInit');
   328 	ScriptSetInteger('Delay', cInactDelay);
   328 		
   329 	ScriptSetString('Map', '');
   329 		// pop game variables
   330 	ScriptSetString('Theme', '');
   330 		ParseCommand('seed ' + ScriptGetString('Seed'), true);
   331 
   331 		ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   332 	ScriptCall('onGameInit');
   332 		ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   333 	
   333 		ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   334 	// pop game variables
   334 		ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   335 	ParseCommand('seed ' + ScriptGetString('Seed'), true);
   335 		ParseCommand('$delay ' + ScriptGetString('Delay'), true);
   336 	ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   336 		if ScriptGetString('Map') <> '' then
   337 	ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   337 			ParseCommand('map ' + ScriptGetString('Map'), true);
   338 	ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   338 		if ScriptGetString('Theme') <> '' then
   339 	ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   339 			ParseCommand('theme ' + ScriptGetString('Theme'), true);	
   340 	ParseCommand('$delay ' + ScriptGetString('Delay'), true);
       
   341 	if ScriptGetString('Map') <> '' then
       
   342 		ParseCommand('map ' + ScriptGetString('Map'), true);
       
   343 	if ScriptGetString('Theme') <> '' then
       
   344 		ParseCommand('theme ' + ScriptGetString('Theme'), true);	
   340 
   345 
   341 	ScriptPrepareAmmoStore;
   346 	ScriptPrepareAmmoStore;
   342 	ScriptCall('onAmmoStoreInit');
   347 	ScriptCall('onAmmoStoreInit');
   343 	ScriptApplyAmmoStore;
   348 	ScriptApplyAmmoStore;
   344 end;
   349 end;
   351 		WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   356 		WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   352 	else
   357 	else
   353 		begin
   358 		begin
   354 		WriteLnToConsole('LUA: ' + name + ' loaded');
   359 		WriteLnToConsole('LUA: ' + name + ' loaded');
   355 		// call the script file
   360 		// call the script file
   356 		lua_pcall(luaState, 0, 0, 0);	
   361 		lua_pcall(luaState, 0, 0, 0);
       
   362 		ScriptLoaded:= true
   357 		end
   363 		end
   358 end;
   364 end;
   359 
   365 
   360 procedure ScriptCall(fname : string);
   366 procedure ScriptCall(fname : string);
   361 begin
   367 begin
       
   368 	if not ScriptLoaded then
       
   369 		exit;
   362 	lua_getglobal(luaState, Str2PChar(fname));
   370 	lua_getglobal(luaState, Str2PChar(fname));
   363 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   371 	if lua_pcall(luaState, 0, 0, 0) <> 0 then
   364 		begin
   372 		begin
   365 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   373 		WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   366 		lua_pop(luaState, 1)
   374 		lua_pop(luaState, 1)
   382 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   390 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   383 end;
   391 end;
   384 
   392 
   385 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
   393 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
   386 begin
   394 begin
       
   395 	if not ScriptLoaded then
       
   396 		exit;
       
   397 
   387 	lua_getglobal(luaState, Str2PChar(fname));
   398 	lua_getglobal(luaState, Str2PChar(fname));
   388 	lua_pushinteger(luaState, par1);
   399 	lua_pushinteger(luaState, par1);
   389 	lua_pushinteger(luaState, par2);
   400 	lua_pushinteger(luaState, par2);
   390 	lua_pushinteger(luaState, par3);
   401 	lua_pushinteger(luaState, par3);
   391 	lua_pushinteger(luaState, par4);
   402 	lua_pushinteger(luaState, par4);
   501 lua_register(luaState, 'PlaySound', @lc_playsound);
   512 lua_register(luaState, 'PlaySound', @lc_playsound);
   502 lua_register(luaState, 'AddTeam', @lc_addteam);
   513 lua_register(luaState, 'AddTeam', @lc_addteam);
   503 lua_register(luaState, 'AddHog', @lc_addhog);
   514 lua_register(luaState, 'AddHog', @lc_addhog);
   504 
   515 
   505 ScriptClearStack; // just to be sure stack is empty
   516 ScriptClearStack; // just to be sure stack is empty
       
   517 ScriptLoaded:= false;
   506 end;
   518 end;
   507 
   519 
   508 procedure free_uScript;
   520 procedure free_uScript;
   509 begin
   521 begin
   510 lua_close(luaState);
   522 lua_close(luaState);