hedgewars/uScript.pas
changeset 2905 f3c79f7193a9
parent 2862 1d15ca7b7f64
child 2924 908851e59958
equal deleted inserted replaced
2904:209a0c573917 2905:f3c79f7193a9
    22 interface
    22 interface
    23 
    23 
    24 procedure ScriptPrintStack;
    24 procedure ScriptPrintStack;
    25 procedure ScriptClearStack;
    25 procedure ScriptClearStack;
    26 
    26 
    27 procedure ScriptLoad(name : string);
    27 procedure ScriptLoad(name : shortstring);
    28 procedure ScriptOnGameInit;
    28 procedure ScriptOnGameInit;
    29 
    29 
    30 procedure ScriptCall(fname : string);
    30 procedure ScriptCall(fname : shortstring);
    31 function ScriptCall(fname : string; par1: LongInt) : LongInt;
    31 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
    32 function ScriptCall(fname : string; par1, par2: LongInt) : LongInt;
    32 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
    33 function ScriptCall(fname : string; par1, par2, par3: LongInt) : LongInt;
    33 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
    34 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
    34 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
    35 
    35 
    36 procedure init_uScript;
    36 procedure init_uScript;
    37 procedure free_uScript;
    37 procedure free_uScript;
    38 
    38 
    39 implementation
    39 implementation
    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 : string;
    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;
   304 procedure ScriptClearStack;
   304 procedure ScriptClearStack;
   305 begin
   305 begin
   306 lua_settop(luaState, 0)
   306 lua_settop(luaState, 0)
   307 end;
   307 end;
   308 
   308 
   309 procedure ScriptSetInteger(name : string; value : LongInt);
   309 procedure ScriptSetInteger(name : shortstring; value : LongInt);
   310 begin
   310 begin
   311 lua_pushinteger(luaState, value);
   311 lua_pushinteger(luaState, value);
   312 lua_setglobal(luaState, Str2PChar(name));
   312 lua_setglobal(luaState, Str2PChar(name));
   313 end;
   313 end;
   314 
   314 
   315 procedure ScriptSetString(name : string; value : string);
   315 procedure ScriptSetString(name : shortstring; value : shortstring);
   316 begin
   316 begin
   317 lua_pushstring(luaState, Str2PChar(value));
   317 lua_pushstring(luaState, Str2PChar(value));
   318 lua_setglobal(luaState, Str2PChar(name));
   318 lua_setglobal(luaState, Str2PChar(name));
   319 end;
   319 end;
   320 
   320 
   321 function ScriptGetInteger(name : string) : LongInt;
   321 function ScriptGetInteger(name : shortstring) : LongInt;
   322 begin
   322 begin
   323 lua_getglobal(luaState, Str2PChar(name));
   323 lua_getglobal(luaState, Str2PChar(name));
   324 ScriptGetInteger:= lua_tointeger(luaState, -1);
   324 ScriptGetInteger:= lua_tointeger(luaState, -1);
   325 lua_pop(luaState, 1);
   325 lua_pop(luaState, 1);
   326 end;
   326 end;
   327 
   327 
   328 function ScriptGetString(name : string) : string;
   328 function ScriptGetString(name : shortstring) : shortstring;
   329 begin
   329 begin
   330 lua_getglobal(luaState, Str2PChar(name));
   330 lua_getglobal(luaState, Str2PChar(name));
   331 ScriptGetString:= lua_tostring(luaState, -1);
   331 ScriptGetString:= lua_tostring(luaState, -1);
   332 lua_pop(luaState, 1);
   332 lua_pop(luaState, 1);
   333 end;
   333 end;
   365 	ScriptPrepareAmmoStore;
   365 	ScriptPrepareAmmoStore;
   366 	ScriptCall('onAmmoStoreInit');
   366 	ScriptCall('onAmmoStoreInit');
   367 	ScriptApplyAmmoStore;
   367 	ScriptApplyAmmoStore;
   368 end;
   368 end;
   369 
   369 
   370 procedure ScriptLoad(name : string);
   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) + ')')
   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 : string);
   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));
   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 : string; 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)
   412 end;
   412 end;
   413 
   413 
   414 function ScriptCall(fname : string; par1, par2: LongInt) : LongInt;
   414 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
   415 begin
   415 begin
   416 ScriptCall:= ScriptCall(fname, par1, par2, 0, 0)
   416 ScriptCall:= ScriptCall(fname, par1, par2, 0, 0)
   417 end;
   417 end;
   418 
   418 
   419 function ScriptCall(fname : string; par1, par2, par3: LongInt) : LongInt;
   419 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
   420 begin
   420 begin
   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 : string; 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));
   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) : string; overload;
   470 function str(const en : TGearType) : shortstring; overload;
   471 begin
   471 begin
   472 str:= GetEnumName(TypeInfo(TGearType), ord(en))
   472 str:= GetEnumName(TypeInfo(TGearType), ord(en))
   473 end;
   473 end;
   474 
   474 
   475 function str(const en : TSound) : string; overload;
   475 function str(const en : TSound) : shortstring; overload;
   476 begin
   476 begin
   477 str:= GetEnumName(TypeInfo(TSound), ord(en))
   477 str:= GetEnumName(TypeInfo(TSound), ord(en))
   478 end;
   478 end;
   479 
   479 
   480 function str(const en : TAmmoType) : string; overload;
   480 function str(const en : TAmmoType) : shortstring; overload;
   481 begin
   481 begin
   482 str:= GetEnumName(TypeInfo(TAmmoType), ord(en))
   482 str:= GetEnumName(TypeInfo(TAmmoType), ord(en))
   483 end;
   483 end;
   484 ///////////////////
   484 ///////////////////
   485 
   485 
   562 
   562 
   563 procedure ScriptClearStack;
   563 procedure ScriptClearStack;
   564 begin
   564 begin
   565 end;
   565 end;
   566 
   566 
   567 procedure ScriptLoad(name : string);
   567 procedure ScriptLoad(name : shortstring);
   568 begin
   568 begin
   569 end;
   569 end;
   570 
   570 
   571 procedure ScriptOnGameInit;
   571 procedure ScriptOnGameInit;
   572 begin
   572 begin
   573 end;
   573 end;
   574 
   574 
   575 procedure ScriptCall(fname : string);
   575 procedure ScriptCall(fname : shortstring);
   576 begin
   576 begin
   577 end;
   577 end;
   578 
   578 
   579 function ScriptCall(fname : string; par1, par2, par3, par4 : LongInt) : LongInt;
   579 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   580 begin
   580 begin
   581 ScriptCall:= 0
   581 ScriptCall:= 0
   582 end;
   582 end;
   583 
   583 
   584 function ScriptCall(fname : string; par1: LongInt) : LongInt;
   584 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
   585 begin
   585 begin
   586 ScriptCall:= 0
   586 ScriptCall:= 0
   587 end;
   587 end;
   588 
   588 
   589 function ScriptCall(fname : string; par1, par2: LongInt) : LongInt;
   589 function ScriptCall(fname : shortstring; par1, par2: LongInt) : LongInt;
   590 begin
   590 begin
   591 ScriptCall:= 0
   591 ScriptCall:= 0
   592 end;
   592 end;
   593 
   593 
   594 function ScriptCall(fname : string; par1, par2, par3: LongInt) : LongInt;
   594 function ScriptCall(fname : shortstring; par1, par2, par3: LongInt) : LongInt;
   595 begin
   595 begin
   596 ScriptCall:= 0
   596 ScriptCall:= 0
   597 end;
   597 end;
   598 
   598 
   599 procedure init_uScript;
   599 procedure init_uScript;