home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GameStar 2006 April
/
Gamestar_83_2006-04_dvd.iso
/
Dema
/
demowot_english.exe
/
Script
/
Source
/
beh_CommonDeathmatch.sma
< prev
next >
Wrap
Text File
|
2005-03-11
|
15KB
|
459 lines
/* Deathmatch-like things: We check all sort of conditions if somebody dies, and rise events when
* they are fulfilled.
*
* Comments and bugs to: chris@digitalreality.hu
*
* INITIAL VERSION:
* - Player side hero mustn't die. If failed: _HERO_DIED
* - Team eliminate: X piece of units must be killed (or all) from a team. Success is if they escape from
* the map too. (Buildings included.) UE_TEAMELIM_SUCC
* - Group survive: At least X piece of units (or all) must survive from a group and be in the player's team.
* If not, an event is rised: UE_GROUP_SURV_FAIL
* - Group eliminate: X piece of units must be killed (or all) from a group. Success is if they escape from
* the map too. (Buildings included.) UE_GROUPELIM_SUCC
*
* LATER:
* - Team survive: At least X piece of units (or all) must survive from a team. If not, an event is rised:
* UE_TEAM_SURV_FAIL
* - Capture: Capture X piece (ie. change their team) from the units in the given group.
* (HQ included) UE_HQCAPTURE_SUCC, UE_HQCAPTURE_FAIL
* - Guard Civilians: _Nobody_must_not_ kill anybody from the given team. UE_CIVILSURV_FAIL
* - Mustn't kill Civilians: Given team member _must_not_ kill anybody from the given team. UE_CIVIL_KILLED
*
* Wait for test: artillery, activate, deactivate, born
*/
#include "script.inc"
// GLOBALS:
new gNoOfPlayerUnits, gNoOfFriendlyUnits, gNoOfEnemyUnits, gNoOfNeutralUnits;
//new DrID:gClassIDUnit;
//new DrID:gSelf;
//new DrID:gComp;
// CONSTANTS FROM THE VARIABLE BASE:
new gEnemyUnitLimit; // Event is risen if No of Enemy units cross this limit: UE_ENEMY_CLEARED
new bool:gEliminateSent = false;
new gGroupSurvive_Group[STR_PROP_MAX_LENGTH]; // Group which must survive
new gGroupSurvive_NumberLimit; // Number of units in group, who must survive
new gGroupSurvive_Number; // The current number of units in the group
new bool:gGroupSurviveSent = false;
new gGroupElim_Group[STR_PROP_MAX_LENGTH]; // Group which must be eliminated
new gGroupElim_NumberLimit; // Number of units in group, who must be reached
new gGroupElim_Number; // The actual number of units in the group
new bool:gGroupElim_Sent = false;
//======================================================================================================
public CommonDMInit()
{
AddAKEntityEvent( "IterateDebugLevels", ANY_ENTITY, DEBUG_EVENT );
gPlayerTeam = GetPropertyInt( GetWorld(), "PlayerTeam" );
// gClassIDUnit = GetClassID( "cUnit" );
new DrID:Self = GetEntity( "self" );
new DrID:Comp = GetComponent( Self, "cCpScriptVariable" );
// Read up group data:
GetVarString( Comp, "GroupSurvive_Group", STR_PROP_MAX_LENGTH, gGroupSurvive_Group );
if( gGroupSurvive_Group[0] ) { // Ha van megadva group
gGroupSurvive_NumberLimit = floatround( CountEntitiesInSelector( gGroupSurvive_Group ) * Float:GetVar( Comp, "GroupSurvive_MultiplierToSurvive" ));
}
else
gGroupSurvive_NumberLimit = -1;
// Read up group data:
GetVarString( Comp, "GroupEliminate_Group", STR_PROP_MAX_LENGTH, gGroupElim_Group );
if( gGroupElim_Group[0] ) { // Ha van megadva group
gGroupElim_NumberLimit = floatround( CountEntitiesInSelector( gGroupElim_Group ) * Float:GetVar( Comp, "GroupEliminate_MultiplierToSurvive" ));
}
else
gGroupElim_NumberLimit = -1;
CountUnits();
if( IsDebugLevelOK( DL_BEHAVIOR ))
DebugOverview();
// Calculate limits:
new Float:Multiplier = Float:GetVar( Comp, "MultiplierOfEnemyUnitsToBeKilled" );
if( Multiplier != -1000.0 )
gEnemyUnitLimit = floatround( gNoOfEnemyUnits * ( 1.0 - Multiplier ));
else
gEnemyUnitLimit = -1;
AddAKEntityEvent( "mDieCheck", ANY_ENTITY, UNIT_DIED );
AddAKEntityEvent( "mBornCheck", ANY_ENTITY, ENTITY_CREATE );
AddAKEntityEvent( "mActivateCheck", ANY_ENTITY, ENTITY_ACTIVATE );
AddAKEntityEvent( "mDeactivateCheck", ANY_ENTITY, ENTITY_DEACTIVATE );
AddAKEntityEvent( "mSideChangeCheck", ANY_ENTITY, UNIT_TEAM_CHANGED );
AddObjective( "HeroMustSurvive", OT_PRIMARY, "#iOBJECTIVE_HERO#", "", OS_IN_PROGRESS );
}
//======================================================================================================
CountUnits()
{
new DrID:Unit;
new Team;
new Alliance;
gNoOfFriendlyUnits = gNoOfEnemyUnits = gNoOfNeutralUnits = gNoOfPlayerUnits = 0;
DebugMessage( "Counting units:", DL_BEHAVIOR );
new Iterator:i = Iterate( "Entities", "cUnit" );
while( ItNext(i) ) {
Unit = ItEntity(i);
if( IsInSelector( Unit, "Irregular" )) // If not a unit or not _on_ the map, let's see the next entity
continue;
if( !IsEntityActive( Unit )) // Skip if not active
continue;
Team = GetPropertyInt( Unit, "Team" );
Alliance = GetAlliance( Team, gPlayerTeam );
IncreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR ))
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
}
ItStop(i);
DebugMessage( "-----------------------------------------", DL_BEHAVIOR );
if( gGroupSurvive_Number < gGroupSurvive_NumberLimit )
DebugMessage( "beh_CommonDeathmatch.sma:CountUnits: gGroupSurvive_Number < gGroupSurvive_NumberLimit", DL_WARNING );
}
//======================================================================================================
DecreaseCounters( DrID:Unit, Team, Alliance ) // If somebody is lost from any reason, this function is called
{
switch( Alliance ) {
case AL_ENEMY: {
gNoOfEnemyUnits--;
if ( gNoOfEnemyUnits <= gEnemyUnitLimit )
if( !gEliminateSent ) {
gEliminateSent = true;
CallUserEvent( UE_TEAMELIM_SUCC, Team );
}
if( gGroupElim_NumberLimit > -1 )
if( IsInSelector( Unit, gGroupElim_Group )) // If we have to think about a group too:
if( --gGroupElim_Number <= gGroupElim_NumberLimit )
if( !gGroupElim_Sent ) {
gGroupElim_Sent = true;
CallUserEventEntity( UE_GROUPELIM_SUCC, Unit );
}
}
case AL_NEUTRAL:
gNoOfNeutralUnits--;
case AL_FRIENDLY: {
gNoOfFriendlyUnits--;
if( Team == gPlayerTeam ) {
gNoOfPlayerUnits--;
if( eUnitCategory:GetPropertyInt( GetBaseEntity( Unit ), "Category" ) == UC_HERO ) // Untested:GetBaseEntity
mHeroDied( Unit );
if( gGroupSurvive_NumberLimit > -1 )
if( IsInSelector( Unit, gGroupSurvive_Group )) // If we have to think about a group too:
if( --gGroupSurvive_Number <= gGroupSurvive_NumberLimit )
if( !gGroupSurviveSent ) {
gGroupSurviveSent = true;
CallUserEventEntity( UE_GROUP_SURV_FAIL, Unit );
}
}
}
default:
DebugMessage( "beh_CommonDeathmatch.sma:DecreaseCounters: Invalid relationship!", DL_BEHAVIOR );
}
}
//======================================================================================================
IncreaseCounters( DrID:Unit, Team, Alliance )
{
switch( Alliance ) {
case AL_ENEMY: {
gNoOfEnemyUnits++;
if( gGroupElim_NumberLimit > -1 )
if( IsInSelector( Unit, gGroupElim_Group )) // If we have to think about a group too:
gGroupElim_Number++;
}
case AL_NEUTRAL:
gNoOfNeutralUnits++;
case AL_FRIENDLY: {
gNoOfFriendlyUnits++;
if( Team == gPlayerTeam ) {
gNoOfPlayerUnits++;
if( gGroupSurvive_NumberLimit > -1 ) // If we have to think about a group too:
if( IsInSelector( Unit, gGroupSurvive_Group ))
gGroupSurvive_Number++;
}
}
default:
DebugMessage( "beh_CommonDeathmatch.sma:IncreaseCounters: Invalid relationship!", DL_BEHAVIOR );
}
}
//======================================================================================================
public mDieCheck( DrID:Unit )
{
if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
return false;
new Team = GetPropertyInt( Unit, "Team" );
new Alliance = GetAlliance( Team, gPlayerTeam );
DecreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR )) {
DebugMessage( "Entity died:", DL_BEHAVIOR );
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
DebugNumberOfUnits();
}
return true;
}
//======================================================================================================
public mBornCheck( DrID:Unit )
{
if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
return false;
new Team = GetPropertyInt( Unit, "Team" );
new Alliance = GetAlliance( Team, gPlayerTeam );
IncreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR )) {
DebugMessage( "Entity created:", DL_BEHAVIOR );
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
DebugNumberOfUnits();
}
return true;
}
//======================================================================================================
public mActivateCheck( DrID:Unit )
{
if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
return false;
new Team = GetPropertyInt( Unit, "Team" );
new Alliance = GetAlliance( Team, gPlayerTeam );
IncreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR )) {
DebugMessage( "Entity activated:", DL_BEHAVIOR );
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
DebugNumberOfUnits();
}
return true;
}
//======================================================================================================
public mDeactivateCheck( DrID:Unit )
{
if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
return false;
new Team = GetPropertyInt( Unit, "Team" );
new Alliance = GetAlliance( Team, gPlayerTeam );
IncreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR )) {
DebugMessage( "Entity deactivated:", DL_BEHAVIOR );
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
DebugNumberOfUnits();
}
return true;
}
//======================================================================================================
public mSideChangeCheck( DrID:Unit )
{
if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
return false;
new Team = GetPropertyInt( Unit, "Team" );
new PreviousTeam = GetMsgParamInt();
new Alliance = GetAlliance( Team, gPlayerTeam );
new PreviousAlliance = GetAlliance( PreviousTeam, gPlayerTeam );
DecreaseCounters( Unit, PreviousTeam, PreviousAlliance );
IncreaseCounters( Unit, Team, Alliance );
if( IsDebugLevelOK( DL_BEHAVIOR )) {
DebugMessage( "Unit changed team. (Displayed in previous + current form.)", DL_BEHAVIOR );
DebugUnit( Unit, PreviousTeam, PreviousAlliance, DL_BEHAVIOR );
DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
DebugNumberOfUnits();
}
return true;
}
//======================================================================================================
mHeroDied( DrID:Unit )
{
CallUserEventEntity( UE_HERO_DIED, Unit );
SetObjectiveState( "HeroMustSurvive", OS_FAILED );
CallUserEvent( UE_OBJECTIVE_CHANGED );
}
//======================================================================================================
DebugUnit( DrID:Unit, Team, Alliance, eDebugLevel:DebugLevel )
{
new strTeam[3];
new strMessage[200] = " Unit: ";
new strTmp[200];
new strID[STR_PROP_MAX_LENGTH];
// ID:
GetPropertyString( Unit, "StringID", STR_PROP_MAX_LENGTH, strID );
ConcatString( strMessage, 200, strMessage, strID );
// Team:
strTmp = ", team: ";
ConcatString( strMessage, 200, strMessage, strTmp );
Int2Str( Team, strTeam, 3 );
ConcatString( strMessage, 200, strMessage, strTeam );
// Alliance:
strTmp = ", alliance: ";
ConcatString( strMessage, 200, strMessage, strTmp );
switch( Alliance )
{
case -1: ConcatString( strMessage, 200, strMessage, "enemy." );
case 0: ConcatString( strMessage, 200, strMessage, "neutral." );
case 1: ConcatString( strMessage, 200, strMessage, "friendly." );
default: ConcatString( strMessage, 200, strMessage, "!!! Error: Alliance is invalid !!!" );
}
DebugMessage( strMessage, DebugLevel );
return true;
}
//======================================================================================================
DebugOverview()
{
DebugMessage( "Deathmatch overview:", DL_BEHAVIOR );
if( gEnemyUnitLimit == -1 )
DebugMessage( "No enemy elimination was requested.", DL_BEHAVIOR );
else {
new tmpStr[200];
Int2Str( gEnemyUnitLimit, tmpStr, 200 );
ConcatString( tmpStr, 200, "Number of enemy units when message sent: ", tmpStr );
DebugMessage( tmpStr, DL_BEHAVIOR );
}
if( gGroupSurvive_NumberLimit != -1 ) {
new Message[200] = "Group survive requested. Group Name: '";
ConcatString( Message, 200, Message, gGroupSurvive_Group );
ConcatString( Message, 200, Message, "', number of group members when message sent: " );
new TmpStr[20];
Int2Str( gGroupSurvive_NumberLimit, TmpStr, 20 );
ConcatString( Message, 200, Message, TmpStr );
DebugMessage( Message, DL_BEHAVIOR );
}
else
DebugMessage( "No group survive requested.", DL_BEHAVIOR );
if( gGroupElim_NumberLimit != -1 ) {
new Message[200] = "Group elimination requested. Group Name: '";
ConcatString( Message, 200, Message, gGroupElim_Group );
ConcatString( Message, 200, Message, "' number of group members when message sent: " );
new TmpStr[20];
Int2Str( gGroupElim_NumberLimit, TmpStr, 20 );
ConcatString( Message, 200, Message, TmpStr );
DebugMessage( Message, DL_BEHAVIOR );
}
else
DebugMessage( "No group elimination requested.", DL_BEHAVIOR );
DebugMessage( "---------------------------------------------------------------", DL_BEHAVIOR );
}
//======================================================================================================
DebugNumberOfUnits() {
new tmpStr[200];
new Message[200] = " Number of player units: ";
Int2Str( gNoOfPlayerUnits, tmpStr, 200 );
ConcatString( Message, 200, Message, tmpStr );
ConcatString( Message, 200, Message, ", friendly units: " );
Int2Str( gNoOfFriendlyUnits, tmpStr, 200 );
ConcatString( Message, 200, Message, tmpStr );
ConcatString( Message, 200, Message, ", enemy units: " );
Int2Str( gNoOfEnemyUnits, tmpStr, 200 );
ConcatString( Message, 200, Message, tmpStr );
ConcatString( Message, 200, Message, ", neutral units: " );
Int2Str( gNoOfNeutralUnits, tmpStr, 200 );
ConcatString( Message, 200, Message, tmpStr );
DebugMessage( Message, DL_BEHAVIOR );
}