Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Script additions #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/scripting/KM_Scripting.pas
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,15 @@ function TKMScripting.ScriptOnUses(Sender: TPSPascalCompiler; const Name: AnsiSt

RegisterMethod('function GameTime: Cardinal');

RegisterMethod('function GetAllMPTeams: TIntegerArray');

RegisterMethod('function GroupAt(aX, aY: Word): Integer');
RegisterMethod('function GroupColumnCount(aGroupID: Integer): Integer');
RegisterMethod('function GroupDead(aGroupID: Integer): Boolean');
RegisterMethod('function GroupIdle(aGroupID: Integer): Boolean');
RegisterMethod('function GroupMember(aGroupID, aMemberIndex: Integer): Integer');
RegisterMethod('function GroupMemberCount(aGroupID: Integer): Integer');
RegisterMethod('function GroupOrdersBlocked(aGroupID: Integer): Boolean');
RegisterMethod('function GroupOwner(aGroupID: Integer): Integer');
RegisterMethod('function GroupType(aGroupID: Integer): Integer');

Expand Down Expand Up @@ -307,6 +310,7 @@ function TKMScripting.ScriptOnUses(Sender: TPSPascalCompiler; const Name: AnsiSt
RegisterMethod('function PlayerGetAllHouses(aPlayer: Byte): TIntegerArray');
RegisterMethod('function PlayerGetAllUnits(aPlayer: Byte): TIntegerArray');
RegisterMethod('function PlayerIsAI(aPlayer: Byte): Boolean');
RegisterMethod('function PlayerMPTeam(aPlayer: Byte): Integer');
RegisterMethod('function PlayerName(aPlayer: Byte): AnsiString');
RegisterMethod('function PlayerVictorious(aPlayer: Byte): Boolean');
RegisterMethod('function PlayerWareDistribution(aPlayer, aWareType, aHouseType: Byte): Byte');
Expand Down Expand Up @@ -703,13 +707,15 @@ procedure TKMScripting.LinkRuntime;
RegisterMethod(@TKMScriptStates.FogRevealed, 'FogRevealed');

RegisterMethod(@TKMScriptStates.GameTime, 'GameTime');
RegisterMethod(@TKMScriptStates.GetAllMPTeams, 'GetAllMPTeams');

RegisterMethod(@TKMScriptStates.GroupAt, 'GroupAt');
RegisterMethod(@TKMScriptStates.GroupColumnCount, 'GroupColumnCount');
RegisterMethod(@TKMScriptStates.GroupDead, 'GroupDead');
RegisterMethod(@TKMScriptStates.GroupIdle, 'GroupIdle');
RegisterMethod(@TKMScriptStates.GroupMember, 'GroupMember');
RegisterMethod(@TKMScriptStates.GroupMemberCount, 'GroupMemberCount');
RegisterMethod(@TKMScriptStates.GroupOrdersBlocked, 'GroupOrdersBlocked');
RegisterMethod(@TKMScriptStates.GroupOwner, 'GroupOwner');
RegisterMethod(@TKMScriptStates.GroupType, 'GroupType');

Expand Down Expand Up @@ -772,6 +778,7 @@ procedure TKMScripting.LinkRuntime;
RegisterMethod(@TKMScriptStates.PlayerGetAllHouses, 'PlayerGetAllHouses');
RegisterMethod(@TKMScriptStates.PlayerGetAllUnits, 'PlayerGetAllUnits');
RegisterMethod(@TKMScriptStates.PlayerIsAI, 'PlayerIsAI');
RegisterMethod(@TKMScriptStates.PlayerMPTeam, 'PlayerMPTeam');
RegisterMethod(@TKMScriptStates.PlayerName, 'PlayerName');
RegisterMethod(@TKMScriptStates.PlayerVictorious, 'PlayerVictorious');
RegisterMethod(@TKMScriptStates.PlayerWareDistribution, 'PlayerWareDistribution');
Expand Down
76 changes: 76 additions & 0 deletions src/scripting/KM_ScriptingStates.pas
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ TKMScriptStates = class(TKMScriptEntity)

function GameTime: Cardinal;

function GetAllMPTeams: TIntegerArray;

function GroupAt(aX, aY: Word): Integer;
function GroupColumnCount(aGroupID: Integer): Integer;
function GroupDead(aGroupID: Integer): Boolean;
function GroupIdle(aGroupID: Integer): Boolean;
function GroupMember(aGroupID, aMemberIndex: Integer): Integer;
function GroupMemberCount(aGroupID: Integer): Integer;
function GroupOrdersBlocked(aGroupID: Integer): Boolean;
function GroupOwner(aGroupID: Integer): Integer;
function GroupType(aGroupID: Integer): Integer;

Expand Down Expand Up @@ -90,6 +93,7 @@ TKMScriptStates = class(TKMScriptEntity)
function PlayerGetAllHouses(aPlayer: Byte): TIntegerArray;
function PlayerGetAllGroups(aPlayer: Byte): TIntegerArray;
function PlayerIsAI(aPlayer: Byte): Boolean;
function PlayerMPTeam(aPlayer: Byte): Integer;
function PlayerName(aPlayer: Byte): AnsiString;
function PlayerVictorious(aPlayer: Byte): Boolean;
function PlayerWareDistribution(aPlayer, aWareType, aHouseType: Byte): Byte;
Expand Down Expand Up @@ -507,6 +511,30 @@ function TKMScriptStates.GameTime: Cardinal;
end;


//* Version: 7000+
//* Returns an array with IDs of teams for all players (for multiplayer)
//* Result: Array of team IDs (player index to team ID lookup table). Note: script is valid for MP mode only. It returns array of -1 otherwise
function TKMScriptStates.GetAllMPTeams: TIntegerArray;
var
I, NetIndex: Integer;
begin
try
SetLength(Result, gHands.Count);
for I := 0 to gHands.Count - 1 do
begin
NetIndex := gGame.Networking.NetPlayers.PlayerIndexToLocal(I);
if NetIndex <> -1 then
Result[I] := gGame.Networking.NetPlayers[NetIndex].Team
else
Result[I] := -1;
end;
except
gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
raise;
end;
end;


//* Version: 5057
//* Length of peacetime in ticks (multiplayer)
//* Result: Ticks (~10 per second)
Expand Down Expand Up @@ -1104,6 +1132,30 @@ function TKMScriptStates.PlayerName(aPlayer: Byte): AnsiString;
end;


//* Version: 7000+
//* Get players team (for multiplayer)
//* Result: Player team. Note: script is valid for MP mode only. It returns -1 otherwise.
function TKMScriptStates.PlayerMPTeam(aPlayer: Byte): Integer;
var
NetIndex: Integer;
begin
try
Result := -1;
if InRange(aPlayer, 0, gHands.Count - 1) then
begin
NetIndex := gGame.Networking.NetPlayers.PlayerIndexToLocal(aPlayer);
if NetIndex <> -1 then
Result := gGame.Networking.NetPlayers[NetIndex].Team;
end
else
LogParamWarning('States.PlayerTeam', [aPlayer]);
except
gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
raise;
end;
end;


//* Version: 5057
//* Returns the ID of the house at the specified location or -1 if no house exists there
//* Result: House ID
Expand Down Expand Up @@ -2638,6 +2690,30 @@ function TKMScriptStates.GroupIdle(aGroupID: Integer): Boolean;
end;


//* Version: 7000+
//* See whether orders for specified group are blocked
//* Result: Orders block state
function TKMScriptStates.GroupOrdersBlocked(aGroupID: Integer): Boolean;
var
G: TKMUnitGroup;
begin
try
Result := False;
if aGroupID > 0 then
begin
G := fIDCache.GetGroup(aGroupID);
if G <> nil then
Result := G.BlockOrders;
end
else
LogParamWarning('States.GroupOrdersBlocked', [aGroupID]);
except
gScriptEvents.ExceptionOutsideScript := True; //Don't blame script for this exception
raise;
end;
end;


//* Version: 5057
//* Returns the owner of the specified group or -1 if Group ID invalid
//* Result: Player ID
Expand Down