⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chars.cpp

📁 [游戏开发参考书-用DirectX编写RPG游戏]这是一个系列的丛书如果你都看并且懂的话你就可以你工作啦!
💻 CPP
📖 第 1 页 / 共 5 页
字号:

  return TRUE;
}

BOOL cCharacterController::Attack(sCharacter *Attacker,       \
                                  sCharacter *Victim)
{
  // Error checking
  if(Attacker == NULL || Victim == NULL)
    return FALSE;

  // Don't attack dead or hurt people
  if(Victim->Action == CHAR_DIE || Victim->Action == CHAR_HURT)
    return FALSE;

  // Set attacker and victim
  Victim->Attacker = Attacker;
  Attacker->Victim = Victim;

  // Return if hit missed
  if((rand() % 1000) > GetToHit(Attacker)) {
    SetMessage(Victim, "Missed!", 500);
    return FALSE;
  }

  // Return if hit dodged
  if((rand() % 1000) <= GetAgility(Victim)) {
    SetMessage(Victim, "Dodged!", 500);
    return FALSE;
  }

  // If character is asleep, randomly wake them up (50% chance)
  if(Victim->Ailments & AILMENT_SLEEP) {
    if((rand() % 100) < 50)
      Victim->Ailments &= ~AILMENT_SLEEP;
  }

  // Attack landed, apply damage
  Damage(Victim, TRUE, GetAttack(Attacker), -1, -1);

  return TRUE;
}

BOOL cCharacterController::Damage(sCharacter *Victim,         \
                                  BOOL PhysicalAttack,        \
                                  long Amount,                \
                                  long DmgClass,              \
                                  long CureClass)
{
  char Text[128];
  float Resist;
  float Range;
  long  DmgAmount;

  // Error checking
  if(Victim == NULL)
    return FALSE;

  // Can't attack if already dead or being hurt (or not enabled)
  if(Victim->Enabled == FALSE ||                              \
     Victim->Action==CHAR_DIE ||                              \
     Victim->Action==CHAR_HURT)
    return FALSE;

  // Adjust for defense if physical attack
  if(PhysicalAttack == TRUE) {
 
    // Random value for less/more damage (-+20%)
    Range     = (float)((rand() % 20) + 90) / 100.0f;
    DmgAmount = (long)((float)Amount * Range);

   // Subtract for defense of victim (allow -20% difference)
    Range     = (float)((rand() % 20) + 80) / 100.0f;
    DmgAmount -= (long)((float)GetDefense(Victim) * Range);
  } else {
    // Adjust for magical attack
    Resist = 1.0f - ((float)GetResistance(Victim) / 100.0f);
    DmgAmount = (long)((float)Amount * Resist);
  }

  // Bounds check value
  if(DmgAmount < 0)
    DmgAmount = 0;

  // Check for double damage
  if(Victim->Def.Class == DmgClass)
    DmgAmount *= 2;

  // Check for cure damage
  if(Victim->Def.Class == CureClass)
    DmgAmount = -(labs(DmgAmount)/2);

  // If no physical damage is dealt then randomly deal 
  // 10-20% of damage from the original amount.
  if(!DmgAmount && PhysicalAttack == TRUE) {
    Range     = (float)((rand() % 10) + 10) / 100.0f;
    DmgAmount = (long)((float)Amount * Range);
  }

  // Subtract damage amount
  Victim->HealthPoints -= DmgAmount;

  // Set hurt status and display message
  if(DmgAmount >= 0) {
    sprintf(Text, "-%lu HP", DmgAmount);
    SetMessage(Victim, Text, 500, D3DCOLOR_RGBA(255,64,0,255));

    // Only set hurt if any damage (and idle or moving)
    if(DmgAmount) {
      if(Victim->Action==CHAR_MOVE || Victim->Action==CHAR_IDLE)
        SetAction(Victim, CHAR_HURT);
    }
  } 

  // Display cure amount
  if(DmgAmount < 0) {
    sprintf(Text, "+%lu HP", -DmgAmount);
    SetMessage(Victim, Text, 500, D3DCOLOR_RGBA(0,64,255,255));
  }

  return TRUE;
}

BOOL cCharacterController::Death(sCharacter *Attacker,        \
                                 sCharacter *Victim)
{
  char Text[128];

  // If a PC or NPC dies, then don't remove from list
  if(Victim->Type == CHAR_PC) {
    // Mark character as disabled so no updates
    Victim->Enabled = FALSE;

    // Call outside death function for PC's
    if(Victim->Type == CHAR_PC)
      PCDeath(Victim);
    else
      NPCDeath(Victim);
  } else {
    // Give attacker the victim's experience
    if(Attacker != NULL) {
      if(Experience(Attacker, Victim->Def.Experience) == TRUE) {
        sprintf(Text, "+%lu exp.", Victim->Def.Experience);
        SetMessage(Attacker, Text, 500);
      }
    }

    // Drop character's money
    if(m_MIL != NULL && Victim->Def.Money)
      DropMoney(Victim->XPos, Victim->YPos, Victim->ZPos,     \
                Victim->Def.Money);

    // Randomly drop an item (as specified in definition)
    if((rand() % 100) < Victim->Def.DropChance)
      DropItem(Victim->XPos, Victim->YPos, Victim->ZPos,      \
               Victim->Def.DropItem);

    // Decrease mesh count and release if no more
    m_Meshes[Victim->Def.MeshNum].Count--;
    if(!m_Meshes[Victim->Def.MeshNum].Count) {
      m_Meshes[Victim->Def.MeshNum].Mesh.Free();
      m_Meshes[Victim->Def.MeshNum].Animation.Free();
    }

    // Remove dead character from list
    if(Victim->Prev != NULL)
      Victim->Prev->Next = Victim->Next;
    else
      m_CharacterParent = Victim->Next;

    if(Victim->Next != NULL)
      Victim->Next->Prev = Victim->Prev;

    if(Victim->Prev == NULL && Victim->Next == NULL)
      m_CharacterParent = NULL;

    Victim->Prev = Victim->Next = NULL;
    delete Victim;
  }

  return TRUE;
}

BOOL cCharacterController::Spell(sCharacter *Caster,          \
                                 sSpellTracker *SpellTracker, \
                                 sSpell *Spells)
{
  float XDiff, YDiff, ZDiff, Dist, YDist, XZDist;
  float MinX, MaxX, MinY, MaxY, MinZ, MaxZ;
  float SpellRadius, XZRadius, YRadius;
  sSpell *SpellPtr;
  sCharacter *CharPtr, *ClosestChar;
  float Closest;
  BOOL Allow;

  // Error checking
  if(Caster == NULL || SpellTracker == NULL || Spells == NULL)
    return TRUE;

  // Get pointer to spell
  SpellPtr = &Spells[SpellTracker->SpellNum];

  // Reduce magic
  Caster->ManaPoints -= SpellPtr->Cost;
  if(Caster->ManaPoints < 0)
    Caster->ManaPoints = 0;

  // Can't cast if silenced
  if(Caster->Ailments & AILMENT_SILENCED) {
    SetMessage(Caster, "Silenced!", 500);
    return FALSE;
  }

  // Get radius of spell
  SpellRadius = SpellPtr->Range * SpellPtr->Range;

  // Handle self-targeting spells instantly
  if(SpellPtr->Target == TARGET_SELF) {
    SpellEffect(Caster, Caster, SpellPtr);
    return TRUE;
  }

  // Reset closest character pointer
  ClosestChar = NULL;
  Closest = 0.0f;

  // Scan through all characters and look for hits
  if((CharPtr = m_CharacterParent) == NULL)
    return FALSE;
  while(CharPtr != NULL) {

    // Only bother with characters of allowed types
    // as well as not targeting self. Also, allow 
    // a RAISE_DEAD PC spell to affect any character.
    Allow = FALSE;
    if(CharPtr!=Caster && SpellTracker->Type==CharPtr->Type)
      Allow = TRUE;
    if(CharPtr->Type==CHAR_PC && SpellPtr->Effect==RAISE_DEAD)
      Allow = TRUE;

    // Find target(s) if allowed
    if(Allow == TRUE) {

      // Get distance from target to character
      XDiff = (float)fabs(CharPtr->XPos - SpellTracker->TargetX);
      YDiff = (float)fabs(CharPtr->YPos - SpellTracker->TargetY);
      ZDiff = (float)fabs(CharPtr->ZPos - SpellTracker->TargetZ);

      // Get X/Z and Y distances
      XZDist = (XDiff * XDiff + ZDiff * ZDiff) - SpellRadius;
      YDist  = (YDiff * YDiff) - SpellRadius;

      // Get target X/Z and Y radius
      CharPtr->Object.GetBounds(&MinX,&MinY,&MinZ,
                                &MaxX,&MaxY,&MaxZ,NULL);
      XZRadius = max(MaxX-MinX, MaxZ-MinZ) * 0.5f;
      YRadius = (MaxY-MinY) * 0.5f;

      // Check if character in range
      if(XZDist - (XZRadius * XZRadius) <= 0.0f && 
          YDist - (YRadius  * YRadius)  <= 0.0f) {
       
        // Determine what to do if in range
        if(SpellPtr->Target == TARGET_SINGLE) {
          // Record closest character in range
          Dist = XDiff * XDiff + YDiff * YDiff + ZDiff * ZDiff;
          if(ClosestChar == NULL) {
            ClosestChar = CharPtr;
            Closest = Dist;
          } else {
            if(Dist < Closest) {
              ClosestChar = CharPtr;
              Closest = Dist;
            }
          }
        } else {
          // Spell hit target if area target
          SpellEffect(Caster, CharPtr, SpellPtr);
        }
      }
    }

    // Go to next character
    CharPtr = CharPtr->Next;
  }

  // Process spell on closest character if needed
  if(SpellPtr->Target==TARGET_SINGLE && ClosestChar!=NULL)
    SpellEffect(Caster, ClosestChar, SpellPtr);

  return TRUE;
}

BOOL cCharacterController::SpellEffect(sCharacter *Caster,    \
                                       sCharacter *Target,    \
                                       sSpell *Spell)
{
  BOOL CanHit;
  long Chance;
  char Text[64];

  // Error checking
  if(Target == NULL || Spell == NULL)
    return FALSE;

  // Calculate chance of hitting
  if(Caster != NULL) {
    // A spell always lands if target=caster
    if(Caster == Target)
      Chance = 100;
    else
      Chance = (long)(((float)GetMental(Caster) / 100.0f +    \
                           1.0f) * (float)Spell->Chance);
  } else {
    Chance = Spell->Chance;
  }

  // Alter chance by target's resistance
  if(Caster != Target)
    Chance = (long)((1.0f - (float)GetResistance(Target) /    \
                        100.0f) * (float)Chance);

  // See if spell failed
  if(Chance != 100 && (rand() % 100) >= Chance) {
    SetMessage(Target, "Failed!", 500);
    return FALSE;
  }

  // Flag character to allow effect
  CanHit = TRUE;
  if(Target->Action==CHAR_HURT || Target->Action==CHAR_DIE)
    CanHit = FALSE;

  // Store attacker and victim
  if((Target->Attacker = Caster) != NULL)
    Caster->Victim = Target;
    
  // Process spell effect
  switch(Spell->Effect) {
    case ALTER_HEALTH:     // Alter health
      if(CanHit == FALSE)
        break;

      // Apply damage if value < 0
      if(Spell->Value[0] < 0.0f)
        Damage(Target,FALSE,-(long)Spell->Value[0],           \
               Spell->DmgClass, Spell->CureClass);

      // Cure damage if value > 0
      if(Spell->Value[0] > 0.0f) {
        Target->HealthPoints += (long)Spell->Value[0];
        if(Target->HealthPoints > Target->Def.HealthPoints)
          Target->HealthPoints = Target->Def.HealthPoints;
        
        // Display amount healed
        sprintf(Text, "+%lu HP", (long)Spell->Value[0]);
        SetMessage(Target,Text,500,D3DCOLOR_RGBA(0,64,255,255));
      }

      break;

    case ALTER_MANA:       // Alter mana
      if(CanHit == FALSE)
        break;

      // Alter mana value
      Target->ManaPoints += (long)Spell->Value[0];
      if(Target->ManaPoints < 0)
        Target->ManaPoints = 0;
      if(Target->ManaPoints > Target->Def.ManaPoints)
        Target->ManaPoints = Target->Def.ManaPoints;

      // Display remove mana message
      if(Spell->Value[0] < 0.0f) {
        sprintf(Text, "%ld MP", (long)Spell->Value[0]);
        SetMessage(Target,Text,500,D3DCOLOR_RGBA(0,128,64,255));
      }

      // Display add mana message
      if(Spell->Value[0] > 0.0f) {
        sprintf(Text, "+%lu MP", (long)Spell->Value[0]);
        SetMessage(Target,Text,500,D3DCOLOR_RGBA(0,255,0,255));
      }

      break;

    case CURE_AILMENT:      // Clear ailment flag
      if(CanHit == FALSE)
        break;

      // Apply ailment and display message
      Target->Ailments |= ~(long)Spell->Value[0];
      SetMessage(Target, "Cure", 500);

      break;

    case CAUSE_AILMENT:     // Set ailment flag
      if(CanHit == FALSE)
        break;

      // Cure ailment and display message
      Target->Ailments |= (long)Spell->Value[0];
      SetMessage(Target, "Ailment", 500);

      break;

    case RAISE_DEAD:       // Raise from dead
      if(Target->Action == CHAR_DIE) {
        Target->HealthPoints = 1;
        Target->ManaPoints   = 0;
        Target->Action       = CHAR_IDLE;
        Target->Locked       = FALSE;
        Target->ActionTimer  = 0;
        Target->Ailments      = 0;
        Target->Enabled      = TRUE;
      }
      break;

    case INSTANT_KILL:     // Kill character
      if(CanHit == FALSE)
        break;

      // Set die action
      SetAction(Target, CHAR_DIE);
      break;

    case DISPEL_MAGIC:     // Remove all ailments
      if(CanHit == FALSE)
        break;

      // Clear all ailments
      Target->Ailments = 0;
      break;

    case TELEPORT:         // Teleport PC/NPC/MON

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -