implement flag ownership and network change on team switch and hostility change

issue1247
Maikel de Vries 2015-02-11 00:01:07 +01:00
parent 45a8c02cd0
commit cba73bafca
3 changed files with 90 additions and 8 deletions

View File

@ -22,6 +22,8 @@ protected func Construction()
// Initialize the flag list if not done already.
if (GetType(LIB_FLAG_FlagList) != C4V_Array)
LIB_FLAG_FlagList = [];
// Make this object also a C4D_Environment for hostility change callbacks.
SetCategory(GetCategory() | C4D_Environment);
// Initialize the single proplist for the flag library.
if (lib_flag == nil)
lib_flag = {};
@ -286,6 +288,10 @@ public func FindFlagsInRadius(object center_object, int radius, int owner)
public func RefreshLinkedFlags()
{
// Debugging logs.
//Log("FLAG - RefreshLinkedFlags(): flag = %v", this);
//LogFlags();
// Safety check: the global flag list should exist.
if (GetType(LIB_FLAG_FlagList) != C4V_Array)
return;
@ -340,7 +346,7 @@ public func RefreshLinkedFlags()
var old_network = lib_flag.power_helper;
// Create a new power network for ths flag since we don't know whether flag links have been lost.
// We then just possibly remove the old ones if they exist.
lib_flag.power_hhelper = CreateObject(Library_Power, 0, 0, NO_OWNER);
lib_flag.power_helper = CreateObject(Library_Power, 0, 0, NO_OWNER);
PushBack(LIB_POWR_Networks, lib_flag.power_helper);
// Make a list of the power networks which need to be merged into the new one.
var to_merge = [old_network];
@ -358,6 +364,8 @@ public func RefreshLinkedFlags()
continue;
RefreshPowerNetwork(network);
}
// Debugging logs.
//LogFlags();
return;
}
@ -391,10 +399,41 @@ protected func OnOwnerChanged(int new_owner, int old_owner)
marker->ResetColor();
}
// Also change the ownership of the surrounding buildings.
RefreshOwnershipOfSurrounding();
RefreshOwnershipOfSurrounding();
return _inherited(new_owner, old_owner, ...);
}
// Engine callback: a player has changed its hostility.
protected func OnHostilityChange(int player1, int player2, bool hostile, bool old_hostility)
{
// Debugging logs.
//Log("FLAG - OnHostilityChange(): player1 = %d, player2 = %d, hostile = %v, old_hostility = %v", player1, player2, hostile, old_hostility);
// Redraw radiuses of all flags.
RedrawFlagRadius();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
ScheduleRefreshLinkedFlags();
return _inherited(player1, player2, hostile, old_hostility);
}
// Engine callback: a player has switched its team.
protected func OnTeamSwitch(int player, int new_team, int old_team)
{
// Debugging logs.
//Log("FLAG - OnTeamSwitch(): player = %d, new_team = %d, old_team = %d", player, new_team, old_team);
// Redraw radiuses of all flags.
RedrawFlagRadius();
// Refresh the ownership of the flag's surroundings.
RefreshOwnershipOfSurrounding();
// Linked flags - optimization for the power system.
ScheduleRefreshLinkedFlags();
return _inherited(player, new_team, old_team, ...);
}
/*-- Construction --*/
// Callback from construction library: create a special preview that gives extra info about affected buildings / flags.
public func CreateConstructionPreview(object constructing_clonk)
{
@ -503,6 +542,19 @@ private func RefreshPowerNetwork(object network)
}
/*-- Logging --*/
private func LogFlags()
{
for (var flag in LIB_FLAG_FlagList)
{
Log("FLAG - Flag (%v): owner = %d, con_time = %d, radius = %d, power_network = %v", flag, flag->GetOwner(), flag.lib_flag.construction_time, flag.lib_flag.radius, flag.lib_flag.power_helper);
Log("\tlinked flags = %v", flag.lib_flag.linked_flags);
}
return;
}
/*-- Saving --*/
public func SaveScenarioObject(props)

View File

@ -32,6 +32,10 @@ protected func InitializePlayer(int plr)
// No FoW to see everything happening.
SetFoW(false, plr);
// All players belong to the first team.
// The second team only exists for testing.
SetPlayerTeam(plr, 1);
// Initialize script player.
if (GetPlayerType(plr) == C4PT_Script)
{
@ -617,7 +621,7 @@ global func Test10_OnFinished()
return;
}
// Test connecting two networks by different allied players and then elimination of one player.
// Test connecting two networks by different allied players and then hostility change, team switch and elimination of one players.
global func Test11_OnStart(int plr)
{
// First network is owned by the player.
@ -635,14 +639,31 @@ global func Test11_OnStart(int plr)
// Networks are disconnected so let the script player bridge the gap.
ScheduleCall(nil, "CreateObjectAbove", 3 * 36, 0, Compensator, 272, 160, script_plr);
// Eliminate the script player and see if the normal player takes over the network correctly.
ScheduleCall(nil, "EliminatePlayer", 6 * 36, 0, script_plr);
// Make the players hostile for a short time.
ScheduleCall(nil, "SetHostility", 6 * 36, 0, plr, script_plr, true);
ScheduleCall(nil, "SetHostility", 6 * 36, 0, script_plr, plr, true);
// Rejoin the script player for other tests.
ScheduleCall(nil, "CreateScriptPlayer", 9 * 36, 0, "PowerBuddy", RGB(0, 0, 255), nil, CSPF_NoEliminationCheck);
// Make the players allies again.
ScheduleCall(nil, "SetHostility", 9 * 36, 0, plr, script_plr, false);
ScheduleCall(nil, "SetHostility", 9 * 36, 0, script_plr, plr, false);
// Switch the team of the script player.
var team = GetPlayerTeam(script_plr);
ScheduleCall(nil, "SetPlayerTeam", 12 * 36, 0, script_plr, team + 1);
// And switch the team of the script player back again.
ScheduleCall(nil, "SetPlayerTeam", 15 * 36, 0, script_plr, team);
// Eliminate the script player and see if the normal player takes over the network correctly.
if (script_plr != nil)
{
ScheduleCall(nil, "EliminatePlayer", 18 * 36, 0, script_plr);
// Rejoin the script player for other tests.
ScheduleCall(nil, "CreateScriptPlayer", 21 * 36, 0, "PowerBuddy", RGB(0, 0, 255), nil, CSPF_NoEliminationCheck);
}
// Log what the test is about.
Log("Two connected networks by different allied players with the elimination of one of the two players.");
Log("Two connected networks by different allied players with hostility change, team switch and elimination of one players.");
return true;
}

View File

@ -0,0 +1,9 @@
[Teams]
Active=1
TeamDistribution=Random
[Team]
id=1
[Team]
id=2