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 1 commit
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 GetAllTeams: 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 @@ -308,6 +311,7 @@ function TKMScripting.ScriptOnUses(Sender: TPSPascalCompiler; const Name: AnsiSt
RegisterMethod('function PlayerGetAllUnits(aPlayer: Byte): TIntegerArray');
RegisterMethod('function PlayerIsAI(aPlayer: Byte): Boolean');
RegisterMethod('function PlayerName(aPlayer: Byte): AnsiString');
RegisterMethod('function PlayerTeam(aPlayer: Byte): Integer');
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.GetAllTeams, 'GetAllTeams');

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 @@ -773,6 +779,7 @@ procedure TKMScripting.LinkRuntime;
RegisterMethod(@TKMScriptStates.PlayerGetAllUnits, 'PlayerGetAllUnits');
RegisterMethod(@TKMScriptStates.PlayerIsAI, 'PlayerIsAI');
RegisterMethod(@TKMScriptStates.PlayerName, 'PlayerName');
RegisterMethod(@TKMScriptStates.PlayerTeam, 'PlayerTeam');
RegisterMethod(@TKMScriptStates.PlayerVictorious, 'PlayerVictorious');
RegisterMethod(@TKMScriptStates.PlayerWareDistribution, 'PlayerWareDistribution');

Expand Down
77 changes: 77 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 GetAllTeams: 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 @@ -91,6 +94,7 @@ TKMScriptStates = class(TKMScriptEntity)
function PlayerGetAllGroups(aPlayer: Byte): TIntegerArray;
function PlayerIsAI(aPlayer: Byte): Boolean;
function PlayerName(aPlayer: Byte): AnsiString;
function PlayerTeam(aPlayer: Byte): Integer;
function PlayerVictorious(aPlayer: Byte): Boolean;
function PlayerWareDistribution(aPlayer, aWareType, aHouseType: Byte): Byte;

Expand Down Expand Up @@ -507,6 +511,31 @@ function TKMScriptStates.GameTime: Cardinal;
end;


//* Version: 7000+
//* Returns an array with IDs of teams for all players
//* Result: Array of team IDs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear what this function returns. Is it a mapping (lookup table) from Hand Index to Team? (that would make the most sense for the script). In that case you should iterate over hands count not NetPlayers count (so the length of the returned array is always hands count).

Otherwise it's unclear how this function could be useful to the script, since the indexes of the array will not match up with player indexes that the script uses in other functions (e.g. the player/hand index in Actions.OverlayTextSet would not be the same as the index into the array returned by this function).

function TKMScriptStates.GetAllTeams: TIntegerArray;
var
I, NetIndex: Integer;
begin
try
SetLength(Result, 0);
for I := 1 to gGame.Networking.NetPlayers.Count do
begin
NetIndex := gGame.Networking.NetPlayers.PlayerIndexToLocal(I);
SetLength(Result, Length(Result) + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to resize the array every loop (inefficient), just set the size once at the start.

if NetIndex <> -1 then
Result[Length(Result) - 1] := gGame.Networking.NetPlayers[NetIndex].Team
else
Result[Length(Result) - 1] := -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 +1133,30 @@ function TKMScriptStates.PlayerName(aPlayer: Byte): AnsiString;
end;


//* Version: 7000+
//* Get players team (usually for multiplayer)
//* Result: Player team
function TKMScriptStates.PlayerTeam(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 +2691,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