Advertisement
ZoriaRPG

ZScript: Tribble or Merge NPCs

Jan 2nd, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. ////////////////
  2. /// Settings ///
  3. ////////////////
  4.  
  5. const int SFX_NPC_TRIBBLE = 44; //The sound to play when tribbling.
  6. const int SFX_NPC_MERGE = 45; //The sound to play when two NPCs merge into one, different NPC.
  7. const int NPC_COMBINE_DISTANCE = 10; //The distance in pixels that npcs must be in relation to one-alother to combine.
  8.  
  9. /////////////////
  10. /// Functions ///
  11. /////////////////
  12.  
  13. //! Function to merge npcs.
  14. // Call as CombineNPCs(ghost, true, ...) if you wish to use collision to determine if two enemies combine
  15. // Call as CombineNPCs(ghost, false,...) if you wish to use a distance check, instead.  
  16.  
  17. //If you wish to update Screen->GuyCOunt, so that when two enemies are replaced by one, that leaving and
  18. //reentering the screen updates the number of npcs, set 'bool changeGuyCount' to true. If you wish to respawn
  19. //the two enemies, which is probably better, set this false.
  20.     // Example:     CombineNPCs(ghost, false, true) : This uses distance, and updates GuyCount[]
  21.     //      CombineNPCs(ghost, false, false) : This uses distance, and does not update GuyCount[]
  22.    
  23. void CombineNPCs(npc n, bool useCollision, bool changeGuyCount){
  24.     int new_npc->ID = ; //Get the ID of the enemy that this will become, from it's 'Death Attribute 3'
  25.     int npcs[3]; int q[2]; //Arrays to hold the npc pointers for the base enemy, a second base enemy, and the replacement
  26.                 //and an array to use with for loops.
  27.                 //! Indices:
  28.                 // q[0] First loop variable.
  29.                 // q[1] Second loop variable
  30.                
  31.  
  32.    for ( q[0] = Screen->NumNPCs(); q[0] > 0; q[0]++ ) { //Read the npcs on the screen
  33.           npcs[0] = Screen->LoadNPC(q[0]); //Load each one into a pointer...
  34.           if ( npcs[0]->ID == this->ID ) { //...and check if it is the correct ID, matching this npc.
  35.              for ( q[1] = q[0]+1; q[1] <= Screen->NumNPCs(); q[1]++ ) { //If it matched, check for a second instance of this enemy.
  36.             //! Note: The loop operator starts at where we left off in the prior loop.
  37.             //! Because the loop using q[0] will return the 'first' instance of the correct type that it
  38.             //! locates, the second loop begins there, +1, so that it does not load the
  39.             //! same NPC, twice.
  40.        
  41.                 //! I'm using two loops here, so that the script can;t be bugged and the enemy that you use as the
  42.                 //! 'first' instance of the enemy, canna also find itself, and grow up on its own.
  43.                     npcs[1] = Screen->LoadNPC(q[1]);
  44.                     //Load each npc after the first one that we found, into a pointer.
  45.                     if ( q[1]->ID == old_npc_id ) { //If the ID matches that of the first npc...
  46.                        if ( NPCS_COMBINE_ON_COLLISION ) { //If we check for collision, not distance...
  47.                               if ( Collision(npcs[0], npcs[1] ) { //If the two enemies collide...
  48.                                  npcs[2] = Screen->CreateNPC(n->Attributes[4]);
  49.                                 //Create a new npc using the 'Death Attribute 3' value of the
  50.                                 //npc running this ghost script.
  51.                             if ( SFX_NPC_MERGE > 0 ) Game->PlaySound(SFX_NPC_MERGE);
  52.                             //If we set a sound for merging npcs, play it.
  53.                                     
  54.                                 //Then, remove the two old npcs that it replaces.          
  55.                             Remove(npcs[1]); Remove([npcs[1]);
  56.                             if ( changeGuyCount ) Game->GuyCount[Game->GetCurScreen()] -= 1; //If we're changing GuyCount[]...
  57.                             //Correct GuyCount so that we spawn the correct number of npcs on returning.
  58.                            
  59.  
  60.                               } 
  61.                        }
  62.                     //Otherwise, if we check by distance, not collision
  63.                        else {
  64.                         //If the two enemies are within the distance specified
  65.                         //by NPC_COMBINE_DISTANCE...
  66.                               if ( NPC_DistXY(npcs[0], npcs[1], NPC_COMBINE_DISTANCE ) {
  67.                                 
  68.                             npcs[2] = Screen->CreateNPC(n->Attributes[4]);
  69.                                 //Create a new npc using the 'Death Attribute 3' value of the
  70.                                 //npc running this ghost script.
  71.                                     
  72.                             if ( SFX_NPC_MERGE > 0 ) Game->PlaySound(SFX_NPC_MERGE);
  73.                             //If we set a sound for merging npcs, play it.
  74.                                 
  75.                                 //Then, remove the two old npcs that it replaces.          
  76.                             Remove(npcs[1]); Remove([npcs[1]);
  77.                             if ( changeGuyCount ) Game->GuyCount[Game->GetCurScreen()] -= 1; //If we're changing GuyCount[]...
  78.                             //Correct GuyCount so that we spawn the correct number of npcs on returning.
  79.                            
  80.                               } 
  81.                        }
  82.                     }
  83.              }
  84.           }
  85.    }
  86. }
  87.  
  88. //Function to tribble an npc when it dies.
  89. //Call as TribbleNPCs(ghost, factor) , where 'factor' is the number of pixels distant to randomise
  90. //spawning the tribble enemies. THus, a value of '3' will spawn the replacement enemies
  91. //from X-3, to X+3, and Y-3 to Y+3, based on the coordinates of npc ghost, when it dies.
  92.  
  93. void TribbleNPCs(npc n, int random_distance_factor){
  94.     npcs[2]; //Array to hold the npcs to create.
  95.     if ( n->HP <- 0 && n->HP > -999 ) { //If the base enemy is at the brink of death...
  96.         npcs[0] = Screen->CreateNPC(n->Attributes[4]); //Create two enemies in its place.
  97.         npcs[1] = Screen->CreateNPC(n->Attributes[4]);//Based on its 'Death Attribute 3' value.
  98.        
  99.         if ( SFX_NPC_TRIBBLE > 0 ) Game->PlaySound(SFX_NPC_MERGE);
  100.         //If we set a sound for tribbling npcs, play it.
  101.                                 
  102.         npcs[0]->X = n->X + Rand((random_distance_factor*-1), random_distance_factor); //Place the new npcs at the location of the
  103.         npcs[0]->Y = n->Y + Rand((random_distance_factor*-1), random_distance_factor); //enemy that we are replacing, -/+
  104.         npcs[1]->X = n->X + Rand((random_distance_factor*-1), random_distance_factor); //a randomised distance.
  105.         npcs[1]->Y = n->Y + Rand((random_distance_factor*-1), random_distance_factor);
  106.         n->HP = -9999; //Silently kill the npc that is dying, removing it fully.
  107.     }
  108. }
  109.   
  110. //Global function to check proximity of two npcs.
  111. //npc a is the first npc, npc b is the second, and 'distance' is
  112. //the number of pixels apart they may be for this to return true.
  113.  
  114. //Called by the other functions, but you could call it like this:
  115. //NPC_DistXY(n, n1, 16); //Would return true if n, and n1 are within
  116. //16 pixels of one-another; otherwise it would return false.    
  117.  
  118. bool NPC_DistXY(npc a, npc b, int distance) {
  119.     int distx; int disty;
  120.    if ( a->X > b->X ) distx = a->X - b->X;
  121.    else distx = b->X - a->X;
  122.  
  123.    if ( a->Y > b->Y ) disty = a->Y - b->Y;
  124.    else disty = b->Y - a->Y;
  125.    return ( distx <= distance && disty <= distance );
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement