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 >
Text File  |  2005-03-11  |  15KB  |  459 lines

  1. /* Deathmatch-like things: We check all sort of conditions if somebody dies, and rise events when 
  2.  * they are fulfilled. 
  3.  *
  4.  * Comments and bugs to: chris@digitalreality.hu
  5.  *
  6.  * INITIAL VERSION:
  7.  * - Player side hero mustn't die. If failed: _HERO_DIED
  8.  * - Team eliminate: X piece of units must be killed (or all) from a team. Success is if they escape from
  9.  *        the map too. (Buildings included.) UE_TEAMELIM_SUCC
  10.  * - Group survive: At least X piece of units (or all) must survive from a group and be in the player's team.
  11.  *        If not, an event is rised: UE_GROUP_SURV_FAIL
  12.  * - Group eliminate: X piece of units must be killed (or all) from a group. Success is if they escape from
  13.  *        the map too. (Buildings included.) UE_GROUPELIM_SUCC
  14.  * 
  15.  * LATER:
  16.  * - Team survive: At least X piece of units (or all) must survive from a team. If not, an event is rised:
  17.  *        UE_TEAM_SURV_FAIL
  18.  * - Capture: Capture X piece (ie. change their team) from the units in the given group.
  19.  *        (HQ included) UE_HQCAPTURE_SUCC, UE_HQCAPTURE_FAIL
  20.  * - Guard Civilians: _Nobody_must_not_ kill anybody from the given team. UE_CIVILSURV_FAIL
  21.  * - Mustn't kill Civilians: Given team member _must_not_ kill anybody from the given team. UE_CIVIL_KILLED
  22.  *
  23.  * Wait for test: artillery, activate, deactivate, born
  24. */
  25.  
  26.  
  27. #include "script.inc"
  28.  
  29.  
  30.  
  31. // GLOBALS:
  32. new gNoOfPlayerUnits, gNoOfFriendlyUnits, gNoOfEnemyUnits, gNoOfNeutralUnits;
  33. //new DrID:gClassIDUnit;
  34. //new DrID:gSelf;
  35. //new DrID:gComp;
  36.  
  37. // CONSTANTS FROM THE VARIABLE BASE:
  38. new gEnemyUnitLimit;                            // Event is risen if No of Enemy units cross this limit: UE_ENEMY_CLEARED
  39. new bool:gEliminateSent = false;
  40.  
  41. new gGroupSurvive_Group[STR_PROP_MAX_LENGTH];    // Group which must survive
  42. new gGroupSurvive_NumberLimit;                    // Number of units in group, who must survive
  43. new gGroupSurvive_Number;                        // The current number of units in the group
  44. new bool:gGroupSurviveSent = false;
  45.  
  46. new gGroupElim_Group[STR_PROP_MAX_LENGTH];        // Group which must be eliminated
  47. new gGroupElim_NumberLimit;                        // Number of units in group, who must be reached
  48. new gGroupElim_Number;                            // The actual number of units in the group
  49. new bool:gGroupElim_Sent = false;
  50.  
  51.  
  52. //======================================================================================================
  53. public CommonDMInit()
  54. {
  55.     AddAKEntityEvent( "IterateDebugLevels", ANY_ENTITY, DEBUG_EVENT );
  56.  
  57.     gPlayerTeam = GetPropertyInt( GetWorld(), "PlayerTeam" );
  58. //    gClassIDUnit = GetClassID( "cUnit" );
  59.     new DrID:Self = GetEntity( "self" );
  60.     new DrID:Comp = GetComponent( Self, "cCpScriptVariable" );
  61.  
  62.     // Read up group data:
  63.     GetVarString( Comp, "GroupSurvive_Group", STR_PROP_MAX_LENGTH, gGroupSurvive_Group );
  64.     if( gGroupSurvive_Group[0] ) {        // Ha van megadva group
  65.         gGroupSurvive_NumberLimit = floatround( CountEntitiesInSelector( gGroupSurvive_Group ) * Float:GetVar( Comp, "GroupSurvive_MultiplierToSurvive" ));    
  66.     }
  67.     else
  68.         gGroupSurvive_NumberLimit = -1;
  69.  
  70.     // Read up group data:
  71.     GetVarString( Comp, "GroupEliminate_Group", STR_PROP_MAX_LENGTH, gGroupElim_Group );
  72.     if( gGroupElim_Group[0] ) { // Ha van megadva group
  73.         gGroupElim_NumberLimit = floatround( CountEntitiesInSelector( gGroupElim_Group ) * Float:GetVar( Comp, "GroupEliminate_MultiplierToSurvive" ));    
  74.     }
  75.     else
  76.         gGroupElim_NumberLimit = -1;
  77.  
  78.     CountUnits();
  79.     if( IsDebugLevelOK( DL_BEHAVIOR )) 
  80.         DebugOverview();
  81.  
  82.     // Calculate limits:
  83.     new Float:Multiplier = Float:GetVar( Comp, "MultiplierOfEnemyUnitsToBeKilled" );
  84.     if( Multiplier != -1000.0 )
  85.         gEnemyUnitLimit = floatround( gNoOfEnemyUnits * ( 1.0 - Multiplier ));
  86.     else
  87.         gEnemyUnitLimit = -1;
  88.  
  89.     AddAKEntityEvent( "mDieCheck", ANY_ENTITY, UNIT_DIED );
  90.     AddAKEntityEvent( "mBornCheck", ANY_ENTITY, ENTITY_CREATE );
  91.     AddAKEntityEvent( "mActivateCheck", ANY_ENTITY, ENTITY_ACTIVATE );
  92.     AddAKEntityEvent( "mDeactivateCheck", ANY_ENTITY, ENTITY_DEACTIVATE );
  93.     AddAKEntityEvent( "mSideChangeCheck", ANY_ENTITY, UNIT_TEAM_CHANGED );
  94.     
  95.     AddObjective( "HeroMustSurvive", OT_PRIMARY, "#iOBJECTIVE_HERO#", "", OS_IN_PROGRESS );
  96. }
  97.  
  98.  
  99.  
  100.  
  101. //======================================================================================================
  102. CountUnits()
  103. {
  104.     new DrID:Unit;
  105.     new Team;
  106.     new Alliance;
  107.  
  108.     gNoOfFriendlyUnits = gNoOfEnemyUnits = gNoOfNeutralUnits = gNoOfPlayerUnits = 0;
  109.  
  110.     DebugMessage( "Counting units:", DL_BEHAVIOR );
  111.     new Iterator:i = Iterate( "Entities", "cUnit" );
  112.     while( ItNext(i) ) {
  113.         Unit = ItEntity(i);
  114.         if( IsInSelector( Unit, "Irregular" )) // If not a unit or not _on_ the map, let's see the next entity
  115.             continue;
  116.  
  117.         if( !IsEntityActive( Unit ))                    // Skip if not active
  118.             continue;
  119.             
  120.         Team = GetPropertyInt( Unit, "Team" );
  121.         Alliance = GetAlliance( Team, gPlayerTeam );
  122.         
  123.         IncreaseCounters( Unit, Team, Alliance );
  124.  
  125.         if( IsDebugLevelOK( DL_BEHAVIOR ))
  126.             DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  127.         
  128.     }
  129.     ItStop(i);
  130.     DebugMessage( "-----------------------------------------", DL_BEHAVIOR );
  131.     
  132.     if( gGroupSurvive_Number < gGroupSurvive_NumberLimit )
  133.         DebugMessage( "beh_CommonDeathmatch.sma:CountUnits: gGroupSurvive_Number < gGroupSurvive_NumberLimit", DL_WARNING );
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. //======================================================================================================
  141. DecreaseCounters( DrID:Unit, Team, Alliance ) // If somebody is lost from any reason, this function is called
  142. {
  143.     switch( Alliance ) {
  144.         case AL_ENEMY: {
  145.             gNoOfEnemyUnits--;
  146.             if ( gNoOfEnemyUnits <= gEnemyUnitLimit )
  147.                 if( !gEliminateSent ) {
  148.                     gEliminateSent = true;
  149.                     CallUserEvent( UE_TEAMELIM_SUCC, Team );
  150.                 }
  151.             
  152.             if( gGroupElim_NumberLimit > -1 )
  153.                 if( IsInSelector( Unit, gGroupElim_Group )) // If we have to think about a group too:
  154.                     if( --gGroupElim_Number <= gGroupElim_NumberLimit )
  155.                         if( !gGroupElim_Sent ) {
  156.                             gGroupElim_Sent = true;
  157.                             CallUserEventEntity( UE_GROUPELIM_SUCC, Unit );
  158.                         }
  159.         }
  160.         case AL_NEUTRAL:
  161.             gNoOfNeutralUnits--;
  162.         case AL_FRIENDLY: {
  163.             gNoOfFriendlyUnits--;
  164.             if( Team == gPlayerTeam ) {
  165.                 gNoOfPlayerUnits--;
  166.                 
  167.                 if( eUnitCategory:GetPropertyInt( GetBaseEntity( Unit ), "Category" ) == UC_HERO )  // Untested:GetBaseEntity
  168.                     mHeroDied( Unit );
  169.                     
  170.                 if( gGroupSurvive_NumberLimit > -1 )
  171.                     if( IsInSelector( Unit, gGroupSurvive_Group )) // If we have to think about a group too:
  172.                         if( --gGroupSurvive_Number <= gGroupSurvive_NumberLimit )
  173.                             if( !gGroupSurviveSent ) {
  174.                                 gGroupSurviveSent = true;
  175.                                 CallUserEventEntity( UE_GROUP_SURV_FAIL, Unit );
  176.                             }
  177.             }
  178.         }
  179.         default:
  180.             DebugMessage( "beh_CommonDeathmatch.sma:DecreaseCounters: Invalid relationship!", DL_BEHAVIOR );
  181.     }
  182. }
  183.  
  184.  
  185.  
  186.  
  187. //======================================================================================================
  188. IncreaseCounters( DrID:Unit, Team, Alliance )
  189. {
  190.     switch( Alliance ) {
  191.         case AL_ENEMY: {
  192.             gNoOfEnemyUnits++;
  193.             if( gGroupElim_NumberLimit > -1 )
  194.                 if( IsInSelector( Unit, gGroupElim_Group )) // If we have to think about a group too:
  195.                     gGroupElim_Number++;
  196.         }
  197.         case AL_NEUTRAL:
  198.             gNoOfNeutralUnits++;
  199.         case AL_FRIENDLY: {
  200.             gNoOfFriendlyUnits++;
  201.             if( Team == gPlayerTeam ) {
  202.                 gNoOfPlayerUnits++;
  203.                 
  204.                 if( gGroupSurvive_NumberLimit > -1 ) // If we have to think about a group too:
  205.                     if( IsInSelector( Unit, gGroupSurvive_Group )) 
  206.                         gGroupSurvive_Number++;
  207.             }
  208.         }
  209.         default:
  210.             DebugMessage( "beh_CommonDeathmatch.sma:IncreaseCounters: Invalid relationship!", DL_BEHAVIOR );
  211.     }
  212. }
  213.  
  214.  
  215.  
  216.  
  217. //======================================================================================================
  218. public mDieCheck( DrID:Unit )
  219. {
  220.     if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
  221.         return false;
  222.     
  223.     new Team = GetPropertyInt( Unit, "Team" );
  224.     new Alliance = GetAlliance( Team, gPlayerTeam );
  225.     
  226.     DecreaseCounters( Unit, Team, Alliance );
  227.     
  228.     if( IsDebugLevelOK( DL_BEHAVIOR )) {
  229.         DebugMessage( "Entity died:", DL_BEHAVIOR );
  230.         DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  231.         DebugNumberOfUnits();
  232.     }
  233.     
  234.     return true;
  235. }
  236.  
  237.  
  238.  
  239.  
  240. //======================================================================================================
  241. public mBornCheck( DrID:Unit )
  242. {
  243.     if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
  244.         return false;
  245.     
  246.     new Team = GetPropertyInt( Unit, "Team" );
  247.     new Alliance = GetAlliance( Team, gPlayerTeam );
  248.     
  249.     IncreaseCounters( Unit, Team, Alliance );
  250.     
  251.     if( IsDebugLevelOK( DL_BEHAVIOR )) {
  252.         DebugMessage( "Entity created:", DL_BEHAVIOR );
  253.         DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  254.         DebugNumberOfUnits();
  255.     }
  256.  
  257.     return true;
  258. }
  259.  
  260.  
  261.  
  262.  
  263. //======================================================================================================
  264. public mActivateCheck( DrID:Unit )
  265. {
  266.     if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
  267.         return false;
  268.     
  269.     new Team = GetPropertyInt( Unit, "Team" );
  270.     new Alliance = GetAlliance( Team, gPlayerTeam );
  271.     
  272.     IncreaseCounters( Unit, Team, Alliance );
  273.     
  274.     if( IsDebugLevelOK( DL_BEHAVIOR )) {
  275.         DebugMessage( "Entity activated:", DL_BEHAVIOR );
  276.         DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  277.         DebugNumberOfUnits();
  278.     }
  279.  
  280.     return true;
  281. }
  282.  
  283.  
  284.  
  285.  
  286. //======================================================================================================
  287. public mDeactivateCheck( DrID:Unit )
  288. {
  289.     if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
  290.         return false;
  291.     
  292.     new Team = GetPropertyInt( Unit, "Team" );
  293.     new Alliance = GetAlliance( Team, gPlayerTeam );
  294.     
  295.     IncreaseCounters( Unit, Team, Alliance );
  296.     
  297.     if( IsDebugLevelOK( DL_BEHAVIOR )) {
  298.         DebugMessage( "Entity deactivated:", DL_BEHAVIOR );
  299.         DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  300.         DebugNumberOfUnits();
  301.     }
  302.  
  303.     return true;
  304. }
  305.  
  306.  
  307.  
  308. //======================================================================================================
  309. public mSideChangeCheck( DrID:Unit )
  310. {
  311.     if( !IsRegularInstanceOf( Unit, GetClassID( "cUnit" )))
  312.         return false;
  313.  
  314.     new Team = GetPropertyInt( Unit, "Team" );
  315.     new PreviousTeam = GetMsgParamInt();
  316.     new Alliance = GetAlliance( Team, gPlayerTeam );
  317.     new PreviousAlliance = GetAlliance( PreviousTeam, gPlayerTeam );
  318.         
  319.     DecreaseCounters( Unit, PreviousTeam, PreviousAlliance );
  320.     IncreaseCounters( Unit, Team, Alliance );
  321.     
  322.      if( IsDebugLevelOK( DL_BEHAVIOR )) {
  323.         DebugMessage( "Unit changed team. (Displayed in previous + current form.)", DL_BEHAVIOR );
  324.         DebugUnit( Unit, PreviousTeam, PreviousAlliance, DL_BEHAVIOR );
  325.         DebugUnit( Unit, Team, Alliance, DL_BEHAVIOR );
  326.         DebugNumberOfUnits();
  327.     }
  328.     return true;
  329. }
  330.  
  331.  
  332.  
  333.  
  334. //======================================================================================================
  335. mHeroDied( DrID:Unit )
  336. {
  337.     CallUserEventEntity( UE_HERO_DIED, Unit );
  338.     SetObjectiveState( "HeroMustSurvive", OS_FAILED );
  339.     CallUserEvent( UE_OBJECTIVE_CHANGED );
  340. }
  341.  
  342.  
  343.  
  344.  
  345. //======================================================================================================
  346. DebugUnit( DrID:Unit, Team, Alliance, eDebugLevel:DebugLevel )
  347. {
  348.     new strTeam[3];
  349.     new strMessage[200] = "        Unit: ";
  350.     new strTmp[200];
  351.     new strID[STR_PROP_MAX_LENGTH];
  352.     
  353.     // ID:
  354.     GetPropertyString( Unit, "StringID", STR_PROP_MAX_LENGTH, strID );
  355.     ConcatString( strMessage, 200, strMessage, strID );
  356.  
  357.     // Team:
  358.     strTmp = ", team: ";
  359.     ConcatString( strMessage, 200, strMessage, strTmp );
  360.     Int2Str( Team, strTeam, 3 );
  361.     ConcatString( strMessage, 200, strMessage, strTeam );
  362.  
  363.     // Alliance:
  364.     strTmp = ", alliance: ";
  365.     ConcatString( strMessage, 200, strMessage, strTmp );
  366.  
  367.     switch( Alliance )
  368.     {
  369.         case -1: ConcatString( strMessage, 200, strMessage, "enemy." );
  370.         case  0: ConcatString( strMessage, 200, strMessage, "neutral." );
  371.         case  1: ConcatString( strMessage, 200, strMessage, "friendly." );
  372.         default: ConcatString( strMessage, 200, strMessage, "!!! Error: Alliance is invalid !!!" );
  373.     }
  374.     DebugMessage( strMessage, DebugLevel );
  375.     
  376.     return true;
  377. }
  378.  
  379.  
  380.  
  381.  
  382. //======================================================================================================
  383. DebugOverview()
  384. {
  385.     DebugMessage( "Deathmatch overview:", DL_BEHAVIOR );
  386.     
  387.     if( gEnemyUnitLimit    == -1 )
  388.         DebugMessage( "No enemy elimination was requested.", DL_BEHAVIOR );
  389.     else {
  390.         new tmpStr[200];
  391.         Int2Str( gEnemyUnitLimit, tmpStr, 200 );
  392.         ConcatString( tmpStr, 200, "Number of enemy units when message sent: ", tmpStr );
  393.         DebugMessage( tmpStr, DL_BEHAVIOR );
  394.     }
  395.     
  396.     if( gGroupSurvive_NumberLimit != -1 ) {
  397.         new Message[200] = "Group survive requested. Group Name: '";
  398.         ConcatString( Message, 200, Message, gGroupSurvive_Group );
  399.  
  400.         ConcatString( Message, 200, Message, "', number of group members when message sent: " );
  401.         
  402.         new TmpStr[20];
  403.         Int2Str( gGroupSurvive_NumberLimit, TmpStr, 20 );
  404.         ConcatString( Message, 200, Message, TmpStr );
  405.  
  406.         DebugMessage( Message, DL_BEHAVIOR );
  407.     }
  408.     else
  409.         DebugMessage( "No group survive requested.", DL_BEHAVIOR );
  410.     
  411.     if( gGroupElim_NumberLimit != -1 ) {
  412.         new Message[200] = "Group elimination requested. Group Name: '";
  413.         ConcatString( Message, 200, Message, gGroupElim_Group );
  414.  
  415.         ConcatString( Message, 200, Message, "' number of group members when message sent: " );
  416.  
  417.         new TmpStr[20];
  418.         Int2Str( gGroupElim_NumberLimit, TmpStr, 20 );
  419.         ConcatString( Message, 200, Message, TmpStr );
  420.  
  421.         DebugMessage( Message, DL_BEHAVIOR );
  422.     }
  423.     else
  424.         DebugMessage( "No group elimination requested.", DL_BEHAVIOR );
  425.  
  426.     DebugMessage( "---------------------------------------------------------------", DL_BEHAVIOR );
  427. }
  428.  
  429.  
  430.  
  431.  
  432. //======================================================================================================
  433. DebugNumberOfUnits() {
  434.     new tmpStr[200];
  435.     new Message[200] = "        Number of player units: ";
  436.     
  437.     Int2Str( gNoOfPlayerUnits, tmpStr, 200 );
  438.     ConcatString( Message, 200, Message, tmpStr );
  439.     
  440.     ConcatString( Message, 200, Message, ", friendly units: " );
  441.     Int2Str( gNoOfFriendlyUnits, tmpStr, 200 );
  442.     ConcatString( Message, 200, Message, tmpStr );
  443.     
  444.     ConcatString( Message, 200, Message, ", enemy units: " );
  445.     Int2Str( gNoOfEnemyUnits, tmpStr, 200 );
  446.     ConcatString( Message, 200, Message, tmpStr );
  447.     
  448.     ConcatString( Message, 200, Message, ", neutral units: " );
  449.     Int2Str( gNoOfNeutralUnits, tmpStr, 200 );
  450.     ConcatString( Message, 200, Message, tmpStr );
  451.  
  452.     DebugMessage( Message, DL_BEHAVIOR );
  453. }
  454.  
  455.  
  456.  
  457.  
  458.  
  459.