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

📄 chars.cpp

📁 这是一个服务端/客户端模式的小型网络游戏
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	else {
		Character->ActionTimer = AddTime +                        \
			m_Meshes[MeshNum].Animation.GetLength(					  \
			m_Animations[Action].Name) * 30;
	}
	return TRUE;
}

BOOL cCharacterController::SetDistance(                       \
									   long IDNum, float Distance)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new value
	CharPtr->Distance = Distance;
	return TRUE;
}

float cCharacterController::GetDistance(long IDNum)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return 0.0f;
	return CharPtr->Distance;
}

BOOL cCharacterController::SetRoute(long IDNum,               \
                                    long NumPoints,           \
                                    sRoutePoint *Route)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Free old route
	delete [] CharPtr->Route;
	CharPtr->Route = NULL;
	
	// Set new route
	if((CharPtr->NumPoints = NumPoints) != NULL) {
		CharPtr->Route = new sRoutePoint[NumPoints];
		memcpy(CharPtr->Route,Route,NumPoints*sizeof(sRoutePoint));
		CharPtr->CurrentPoint = 0;
	}
	
	return TRUE;
}

BOOL cCharacterController::SetScript(long IDNum,              \
                                     char *ScriptFilename)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new script
	strcpy(CharPtr->ScriptFilename, ScriptFilename);
	return TRUE;
}

char *cCharacterController::GetScript(long IDNum)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return NULL;
	return CharPtr->ScriptFilename;
}

BOOL cCharacterController::SetEnable(long IDNum, BOOL Enabled)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new value
	CharPtr->Enabled = Enabled;
	return TRUE;
}

BOOL cCharacterController::GetEnable(long IDNum)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	return CharPtr->Enabled;
}

BOOL cCharacterController::SetMessage(sCharacter *Character,  \
									  char *Text, long Timer,    \
									  D3DCOLOR Color)
{
	strcpy(Character->Message, Text);
	Character->MessageTimer = Timer;
	Character->MessageColor = Color;
	
	return TRUE;
}

BOOL cCharacterController::Move(                              \
								long IDNum,                                    \
								float XPos, float YPos, float ZPos)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new values
	CharPtr->XPos = XPos;
	CharPtr->YPos = YPos;
	CharPtr->ZPos = ZPos;
	return TRUE;
}

BOOL cCharacterController::GetPosition(                       \
									   long IDNum,                                    \
									   float *XPos, float *YPos, float *ZPos)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	if(XPos != NULL)
		*XPos = CharPtr->XPos;
	if(YPos != NULL)
		*YPos = CharPtr->YPos;
	if(ZPos != NULL)
		*ZPos = CharPtr->ZPos;
	
	return TRUE;
}

BOOL cCharacterController::SetBounds(                         \
									 long IDNum,                                    \
									 float MinX, float MinY, float MinZ,            \
									 float MaxX, float MaxY, float MaxZ)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new values
	CharPtr->MinX = min(MinX, MaxX);
	CharPtr->MinY = min(MinY, MaxY);
	CharPtr->MinZ = min(MinZ, MaxZ);
	CharPtr->MaxX = max(MinX, MaxX);
	CharPtr->MaxY = max(MinY, MaxY);
	CharPtr->MaxZ = max(MinZ, MaxZ);
	
	return TRUE;
}   

BOOL cCharacterController::GetBounds(                         \
									 long IDNum,                                    \
									 float *MinX, float *MinY, float *MinZ,         \
									 float *MaxX, float *MaxY, float *MaxZ)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	if(MinX != NULL)
		*MinX = CharPtr->MinX;
	if(MinY != NULL)
		*MinY = CharPtr->MinY;
	if(MinZ != NULL)
		*MinZ = CharPtr->MinZ;
	if(MaxX != NULL)
		*MaxX = CharPtr->MaxX;
	if(MaxY != NULL)
		*MaxY = CharPtr->MaxY;
	if(MaxZ != NULL)
		*MaxZ = CharPtr->MaxZ;
	
	return TRUE;
}

BOOL cCharacterController::SetType(long IDNum, long Type)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new value
	CharPtr->Type = Type;
	return TRUE;
}

long cCharacterController::GetType(long IDNum)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return 0;
	return CharPtr->Type;
}

BOOL cCharacterController::SetAI(long IDNum, long Type)
{
	sCharacter *CharPtr;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Set new value
	CharPtr->AI = Type;
	return TRUE;
}

long cCharacterController::GetAI(long IDNum)
{
	sCharacter *CharPtr;
	
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return 0;
	return CharPtr->AI;
}

BOOL cCharacterController::SetTargetCharacter(long IDNum,     \
                                              long TargetNum)
{
	sCharacter *CharPtr, *CharTarget;
	
	// Get pointer to character
	if((CharPtr = GetCharacter(IDNum)) == NULL)
		return FALSE;
	
	// Clear if TargetNum == -1
	if(TargetNum == -1)
		CharPtr->TargetChar = NULL;
	else {
		// Scan through list and target 1st TargetNum found
		CharTarget = m_CharacterParent;
		while(CharTarget != NULL) {
			if(CharTarget->ID == TargetNum) {
				CharPtr->TargetChar = CharTarget;
				break;
			}
			CharTarget = CharTarget->Next;
		}
		
		// Clear target if not found in list
		if(CharTarget == NULL)
			CharPtr->TargetChar = NULL;
	}
	return TRUE;
}

BOOL cCharacterController::CharUpdate(                        \
									  sCharacter *Character, long Elapsed,           \
									  float *XMove, float *YMove, float *ZMove)
{
	float Speed;
	// Error checking
	if(Character == NULL)
		return FALSE;
	
	
	if(Character->Action == CHAR_MOVE){
		Speed = (float)Elapsed / 1000.0f * GetSpeed(Character);
		*XMove = (float)sin(Character->Direction) * Speed;
		*ZMove = (float)cos(Character->Direction) * Speed;
	}
	return TRUE;
}

BOOL cCharacterController::CheckMove(sCharacter *Character,   \
									 float *XMove, float *YMove, float *ZMove)
{
	sCharacter *CharPtr;
	float XDiff, YDiff, ZDiff, Dist;
	float Radius1, Radius2;
	float XPos, YPos, ZPos;
	float MinX, MaxX, MinZ, MaxZ;
	
	// Error checking
	if(Character == NULL)
		return FALSE;  // Don't allow movement
	
	XPos = Character->XPos + (*XMove);
	YPos = Character->YPos + (*YMove);
	ZPos = Character->ZPos + (*ZMove);
	
	// Get character's X/Z radius
	Character->Object.GetBounds(&MinX, NULL, &MinZ,             \
		&MaxX, NULL, &MaxZ, NULL);
	Radius1 = max(MaxX-MinX, MaxZ-MinZ) * 0.5f;

	// Check movement against other characters
	if((CharPtr = m_CharacterParent) != NULL) {
		while(CharPtr != NULL) {

			// Don't check against self or disabled characters
			if(Character != CharPtr && \
				!(Character->Type == CHAR_PC && CharPtr->Type == CHAR_PC)) {

				// Get distance between characters
				XDiff = (float)fabs(XPos - CharPtr->XPos);
				YDiff = (float)fabs(YPos - CharPtr->YPos);
				ZDiff = (float)fabs(ZPos - CharPtr->ZPos);
				Dist = XDiff*XDiff + YDiff*YDiff + ZDiff*ZDiff;
				
				// Get checking character's X/Z bounding radius
				CharPtr->Object.GetBounds(&MinX, NULL, &MinZ,         \
					&MaxX, NULL, &MaxZ, NULL);
				Radius2 = max(MaxX-MinX, MaxZ-MinZ) * 0.5f;

				// Don't allow movement if intersecting
				if(Dist <= (Radius1*Radius1 + Radius2*Radius2) && \
				Dist <= pow(Character->XPos-CharPtr->XPos,2)+pow(Character->YPos-CharPtr->YPos,2)+pow(Character->ZPos-CharPtr->ZPos,2))
					return FALSE;
			}
			
			CharPtr = CharPtr->Next;
		}
	}
	
	// Call overloaded check custom collisions (maps, etc)
	if(ValidateMove(Character, XMove, YMove, ZMove) == FALSE)
		return FALSE;  // Don't allow movement
	
	return TRUE;
}

BOOL cCharacterController::ProcessUpdate(                     \
										 sCharacter *Character,
										 float XMove, float YMove, float ZMove)
{
	// Move character
	
	Character->XPos += XMove;
	Character->YPos += YMove;
	Character->ZPos += ZMove;
	
	// Move object and point in direction
	Character->Object.Move(Character->XPos,                     \
		Character->YPos,                     \
		Character->ZPos);                    \
		Character->Object.Rotate(0.0f, Character->Direction, 0.0f);
	
	// Set new animation
	if(Character->LastAnim != Character->Action) {
		Character->LastAnim = Character->Action;
		
		if(m_NumAnimations && m_Animations != NULL) {
			Character->LastAnimTime = Character->Time / 30;
			Character->Object.SetAnimation(                         \
				&m_Meshes[Character->Def.MeshNum].Animation, \
				m_Animations[Character->Action].Name,        \
				Character->LastAnimTime);
		}
	}
	
	return TRUE;
}


BOOL cCharacterController::Death(sCharacter *Attacker,        \
                                 sCharacter *Victim)
{
	char Text[128];
	
	if(Victim!=NULL) Victim->Enabled = FALSE;
	// If a PC or NPC dies, then don't remove from list
	if(Victim->Type != CHAR_PC) {
		// 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);
			}
		}
	}
	
	return TRUE;
}


BOOL cCharacterController::Item(sCharacter *Owner,            \
                                sCharacter *Target,           \
                                long ItemNum,                 \
                                sCharItem *CharItem)
{
	sItem *Item;
	
	// Error checking
	if(Owner == NULL || Target == NULL || m_MIL == NULL)
		return FALSE;
	
	// Make sure restritions allow equiping of item
	if(!CheckUsageBit(m_MIL[ItemNum].Usage, Target->Def.Class))
		return FALSE;
	
	// Get pointer to item
	Item = (sItem*)&m_MIL[ItemNum];
	
	// Use specified item
	switch(Item->Category) {
    case EDIBLE:
		Target->HealthPoints += m_MIL[ItemNum].Value;
		if(Target->HealthPoints > Target->Def.HealthPoints)
			Target->HealthPoints = Target->Def.HealthPoints;
		break;
    case HEALING:
		// Alter health
		Target->ManaPoints += m_MIL[ItemNum].Value;
		if(Target->ManaPoints > Target->Def.ManaPoints)
			Target->ManaPoints = Target->Def.ManaPoints;
		break;
	}
	
	// Decrease quanity (and remove object) if needed
	if(CheckItemFlag(Item->Flags, USEONCE) && CharItem != NULL) {
		CharItem->Quantity--;
		if(CharItem->Quantity <= 0 && Owner->CharICS != NULL)
			Owner->CharICS->Remove(CharItem);
	}
	
	return TRUE;
}

BOOL cCharacterController::Drop(sCharacter *Character,
								sCharItem *Item, long Quantity)
{
	// Error checking
	if(Item == NULL || m_MIL == NULL || Character == NULL)
		return FALSE;
	
	// Make sure item can be dropped
	if(!CheckItemFlag(m_MIL[Item->ItemNum].Flags,CANDROP))
		return FALSE;
	
	// Decrease quantity from item
	Item->Quantity -= Quantity;
	
	// Remove item from ICS if no more left
	if(Item->Quantity <= 0 && Character->CharICS != NULL)
		Character->CharICS->Remove(Item);
	
	return TRUE;
}


BOOL cCharacterController::Spell(sCharacter *Caster,          \
                                 sSpellTracker *SpellTracker, \
                                 sSpell *Spells)
{
  sSpell *SpellPtr;

  // 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;

  return TRUE;
}

⌨️ 快捷键说明

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