hedgewars/uScript.pas
changeset 2949 d137a9da7701
parent 2948 3f21a9dc93d0
child 2996 dfc7507a21a0
equal deleted inserted replaced
2948:3f21a9dc93d0 2949:d137a9da7701
   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;
   334 
   334 
   335 procedure ScriptOnGameInit;
   335 procedure ScriptOnGameInit;
   336 begin
   336 var s, t : ansistring;
   337     // not required if there's no script to run
   337 begin
   338     if not ScriptLoaded then
   338 // not required if there's no script to run
   339         exit;
   339 if not ScriptLoaded then
   340             
   340     exit;
   341     // push game variables so they may be modified by the script
   341         
   342     ScriptSetInteger('GameFlags', GameFlags);
   342 // push game variables so they may be modified by the script
   343     ScriptSetString('Seed', cSeed);
   343 ScriptSetInteger('GameFlags', GameFlags);
   344     ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   344 ScriptSetString('Seed', cSeed);
   345     ScriptSetInteger('CaseFreq', cCaseFactor);
   345 ScriptSetInteger('TurnTime', cHedgehogTurnTime);
   346     ScriptSetInteger('LandAdds', cLandAdditions);
   346 ScriptSetInteger('CaseFreq', cCaseFactor);
   347     ScriptSetInteger('Delay', cInactDelay);
   347 ScriptSetInteger('LandAdds', cLandAdditions);
   348     ScriptSetString('Map', '');
   348 ScriptSetInteger('Delay', cInactDelay);
   349     ScriptSetString('Theme', '');
   349 ScriptSetString('Map', '');
   350 
   350 ScriptSetString('Theme', '');
   351     ScriptCall('onGameInit');
   351 
   352     
   352 // import locale
   353     // pop game variables
   353 s:= cLocaleFName;
   354     ParseCommand('seed ' + ScriptGetString('Seed'), true);
   354 SplitByChar(s, t, '.');
   355     ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   355 ScriptSetString('L', s);
   356     ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   356 
   357     ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   357 ScriptCall('onGameInit');
   358     ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   358 
   359     ParseCommand('$delay ' + ScriptGetString('Delay'), true);
   359 // pop game variables
   360     if ScriptGetString('Map') <> '' then
   360 ParseCommand('seed ' + ScriptGetString('Seed'), true);
   361         ParseCommand('map ' + ScriptGetString('Map'), true);
   361 ParseCommand('$gmflags ' + ScriptGetString('GameFlags'), true);
   362     if ScriptGetString('Theme') <> '' then
   362 ParseCommand('$turntime ' + ScriptGetString('TurnTime'), true);
   363         ParseCommand('theme ' + ScriptGetString('Theme'), true);    
   363 ParseCommand('$casefreq ' + ScriptGetString('CaseFreq'), true);
   364 
   364 ParseCommand('$landadds ' + ScriptGetString('LandAdds'), true);
   365     ScriptPrepareAmmoStore;
   365 ParseCommand('$delay ' + ScriptGetString('Delay'), true);
   366     ScriptCall('onAmmoStoreInit');
   366 if ScriptGetString('Map') <> '' then
   367     ScriptApplyAmmoStore;
   367     ParseCommand('map ' + ScriptGetString('Map'), true);
       
   368 if ScriptGetString('Theme') <> '' then
       
   369     ParseCommand('theme ' + ScriptGetString('Theme'), true);    
       
   370 
       
   371 ScriptPrepareAmmoStore;
       
   372 ScriptCall('onAmmoStoreInit');
       
   373 ScriptApplyAmmoStore;
   368 end;
   374 end;
   369 
   375 
   370 procedure ScriptLoad(name : shortstring);
   376 procedure ScriptLoad(name : shortstring);
   371 var ret : LongInt;
   377 var ret : LongInt;
   372 begin
   378 begin
   373     ret:= luaL_loadfile(luaState, Str2PChar(name));
   379 ret:= luaL_loadfile(luaState, Str2PChar(name));
   374     if ret <> 0 then
   380 if ret <> 0 then
   375         WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   381     WriteLnToConsole('LUA: Failed to load ' + name + '(error ' + IntToStr(ret) + ')')
   376     else
   382 else
   377         begin
   383     begin
   378         WriteLnToConsole('LUA: ' + name + ' loaded');
   384     WriteLnToConsole('LUA: ' + name + ' loaded');
   379         // call the script file
   385     // call the script file
   380         lua_pcall(luaState, 0, 0, 0);
   386     lua_pcall(luaState, 0, 0, 0);
   381         ScriptLoaded:= true
   387     ScriptLoaded:= true
   382         end
   388     end
   383 end;
   389 end;
   384 
   390 
   385 procedure SetGlobals;
   391 procedure SetGlobals;
   386 begin
   392 begin
   387     ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
   393 ScriptSetInteger('TurnTimeLeft', TurnTimeLeft);
   388 end;
   394 end;
   389 
   395 
   390 procedure GetGlobals;
   396 procedure GetGlobals;
   391 begin
   397 begin
   392     TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   398 TurnTimeLeft:= ScriptGetInteger('TurnTimeLeft');
   393 end;
   399 end;
   394 
   400 
   395 procedure ScriptCall(fname : shortstring);
   401 procedure ScriptCall(fname : shortstring);
   396 begin
   402 begin
   397     if not ScriptLoaded then
   403 if not ScriptLoaded then
   398         exit;
   404     exit;
   399     SetGlobals;
   405 SetGlobals;
   400     lua_getglobal(luaState, Str2PChar(fname));
   406 lua_getglobal(luaState, Str2PChar(fname));
   401     if lua_pcall(luaState, 0, 0, 0) <> 0 then
   407 if lua_pcall(luaState, 0, 0, 0) <> 0 then
   402         begin
   408     begin
   403         WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   409     WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   404         lua_pop(luaState, 1)
   410     lua_pop(luaState, 1)
   405         end;
   411     end;
   406     GetGlobals;
   412 GetGlobals;
   407 end;
   413 end;
   408 
   414 
   409 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
   415 function ScriptCall(fname : shortstring; par1: LongInt) : LongInt;
   410 begin
   416 begin
   411 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   417 ScriptCall:= ScriptCall(fname, par1, 0, 0, 0)
   421 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   427 ScriptCall:= ScriptCall(fname, par1, par2, par3, 0)
   422 end;
   428 end;
   423 
   429 
   424 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   430 function ScriptCall(fname : shortstring; par1, par2, par3, par4 : LongInt) : LongInt;
   425 begin
   431 begin
   426     if not ScriptLoaded then
   432 if not ScriptLoaded then
   427         exit;
   433     exit;
   428     SetGlobals;
   434 SetGlobals;
   429     lua_getglobal(luaState, Str2PChar(fname));
   435 lua_getglobal(luaState, Str2PChar(fname));
   430     lua_pushinteger(luaState, par1);
   436 lua_pushinteger(luaState, par1);
   431     lua_pushinteger(luaState, par2);
   437 lua_pushinteger(luaState, par2);
   432     lua_pushinteger(luaState, par3);
   438 lua_pushinteger(luaState, par3);
   433     lua_pushinteger(luaState, par4);
   439 lua_pushinteger(luaState, par4);
   434     ScriptCall:= 0;
   440 ScriptCall:= 0;
   435     if lua_pcall(luaState, 4, 1, 0) <> 0 then
   441 if lua_pcall(luaState, 4, 1, 0) <> 0 then
   436         begin
   442     begin
   437         WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   443     WriteLnToConsole('LUA: Error while calling ' + fname + ': ' + lua_tostring(luaState, -1));
   438         lua_pop(luaState, 1)
   444     lua_pop(luaState, 1)
   439         end
   445     end
   440     else
   446 else
   441         begin
   447     begin
   442         ScriptCall:= lua_tointeger(luaState, -1);
   448     ScriptCall:= lua_tointeger(luaState, -1);
   443         lua_pop(luaState, 1)
   449     lua_pop(luaState, 1)
   444         end;
   450     end;
   445     GetGlobals;
   451 GetGlobals;
   446 end;
   452 end;
   447 
   453 
   448 procedure ScriptPrepareAmmoStore;
   454 procedure ScriptPrepareAmmoStore;
   449 var i: ShortInt;
   455 var i: ShortInt;
   450 begin
   456 begin
   499 // import some variables
   505 // import some variables
   500 ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
   506 ScriptSetInteger('LAND_WIDTH', LAND_WIDTH);
   501 ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
   507 ScriptSetInteger('LAND_HEIGHT', LAND_HEIGHT);
   502 
   508 
   503 // import game flags
   509 // import game flags
   504 ScriptSetInteger('gfForts',gfForts);
   510 ScriptSetInteger('gfForts', gfForts);
   505 ScriptSetInteger('gfMultiWeapon',gfMultiWeapon);
   511 ScriptSetInteger('gfMultiWeapon', gfMultiWeapon);
   506 ScriptSetInteger('gfSolidLand',gfSolidLand);
   512 ScriptSetInteger('gfSolidLand', gfSolidLand);
   507 ScriptSetInteger('gfBorder',gfBorder);
   513 ScriptSetInteger('gfBorder', gfBorder);
   508 ScriptSetInteger('gfDivideTeams',gfDivideTeams);
   514 ScriptSetInteger('gfDivideTeams', gfDivideTeams);
   509 ScriptSetInteger('gfLowGravity',gfLowGravity);
   515 ScriptSetInteger('gfLowGravity', gfLowGravity);
   510 ScriptSetInteger('gfLaserSight',gfLaserSight);
   516 ScriptSetInteger('gfLaserSight', gfLaserSight);
   511 ScriptSetInteger('gfInvulnerable',gfInvulnerable);
   517 ScriptSetInteger('gfInvulnerable', gfInvulnerable);
   512 ScriptSetInteger('gfMines',gfMines);
   518 ScriptSetInteger('gfMines', gfMines);
   513 ScriptSetInteger('gfVampiric',gfVampiric);
   519 ScriptSetInteger('gfVampiric', gfVampiric);
   514 ScriptSetInteger('gfKarma',gfKarma);
   520 ScriptSetInteger('gfKarma', gfKarma);
   515 ScriptSetInteger('gfArtillery',gfArtillery);
   521 ScriptSetInteger('gfArtillery', gfArtillery);
   516 ScriptSetInteger('gfOneClanMode',gfOneClanMode);
   522 ScriptSetInteger('gfOneClanMode', gfOneClanMode);
   517 ScriptSetInteger('gfRandomOrder',gfRandomOrder);
   523 ScriptSetInteger('gfRandomOrder', gfRandomOrder);
   518 ScriptSetInteger('gfKing',gfKing);
   524 ScriptSetInteger('gfKing', gfKing);
       
   525 ScriptSetInteger('gfPlaceHog', gfPlaceHog);
       
   526 ScriptSetInteger('gfSharedAmmo', gfSharedAmmo);
       
   527 ScriptSetInteger('gfDisableGirders', gfDisableGirders);
       
   528 ScriptSetInteger('gfExplosives', gfExplosives);
   519 
   529 
   520 // register gear types
   530 // register gear types
   521 for at:= Low(TGearType) to High(TGearType) do
   531 for at:= Low(TGearType) to High(TGearType) do
   522     ScriptSetInteger(str(at), ord(at));
   532     ScriptSetInteger(str(at), ord(at));
   523 
   533