re3-wiiu/src/weapons/ProjectileInfo.cpp

377 lines
11 KiB
C++
Raw Normal View History

#include "common.h"
2020-04-17 15:31:11 +02:00
2020-01-11 12:30:17 +01:00
#include "Camera.h"
#include "General.h"
#include "Heli.h"
#include "ModelIndices.h"
#include "Particle.h"
#include "Ped.h"
#include "Plane.h"
#include "ProjectileInfo.h"
#include "Projectile.h"
2020-01-11 12:30:17 +01:00
#include "Explosion.h"
#include "Weapon.h"
#include "World.h"
#ifdef SQUEEZE_PERFORMANCE
uint32 projectileInUse;
#endif
CProjectileInfo gaProjectileInfo[NUM_PROJECTILES];
CProjectile *CProjectileInfo::ms_apProjectile[NUM_PROJECTILES];
2020-01-07 16:26:23 +01:00
2020-01-11 12:30:17 +01:00
void
CProjectileInfo::Initialise()
{
debug("Initialising CProjectileInfo...\n");
for (int i = 0; i < ARRAY_SIZE(ms_apProjectile); i++) {
ms_apProjectile[i] = nil;
gaProjectileInfo[i].m_eWeaponType = WEAPONTYPE_GRENADE;
gaProjectileInfo[i].m_pSource = nil;
gaProjectileInfo[i].m_nExplosionTime = 0;
gaProjectileInfo[i].m_bInUse = false;
}
debug("CProjectileInfo ready\n");
#ifdef SQUEEZE_PERFORMANCE
projectileInUse = 0;
#endif
2020-01-11 12:30:17 +01:00
}
void
CProjectileInfo::Shutdown()
{
debug("Shutting down CProjectileInfo...\n");
debug("CProjectileInfo shut down\n");
}
2020-01-07 16:26:23 +01:00
CProjectileInfo*
CProjectileInfo::GetProjectileInfo(int32 id)
{
return &gaProjectileInfo[id];
}
2020-01-11 12:30:17 +01:00
2020-05-15 16:30:25 +02:00
// --MIAMI: Mostly done
2020-01-11 12:30:17 +01:00
bool
CProjectileInfo::AddProjectile(CEntity *entity, eWeaponType weapon, CVector pos, float speed)
{
int8 SpecialCollisionResponseCase = COLLRESPONSE_NONE;
bool gravity = true;
CMatrix matrix;
float elasticity = 0.75f;
CPed* ped = (CPed*)entity;
int time;
CVector velocity;
switch (weapon)
{
2020-05-15 16:30:25 +02:00
case WEAPONTYPE_ROCKET:
2020-01-11 12:30:17 +01:00
{
2020-05-15 16:30:25 +02:00
float vy = 0.35f;
time = CTimer::GetTimeInMilliseconds() + 2000;
if (entity->GetModelIndex() == MI_SPARROW || entity->GetModelIndex() == MI_HUNTER || entity->GetModelIndex() == MI_SENTINEL) {
matrix = ped->GetMatrix();
2020-01-11 12:30:17 +01:00
matrix.GetPosition() = pos;
2020-05-15 16:30:25 +02:00
CVector vecSpeed = ((CPhysical*)entity)->m_vecMoveSpeed;
vy += Max(0.0f, DotProduct(vecSpeed, entity->GetForward())) + Max(0.0f, DotProduct(vecSpeed, entity->GetUp()));
} else {
if (ped->IsPlayer()) {
matrix.GetForward() = TheCamera.Cams[TheCamera.ActiveCam].Front;
matrix.GetUp() = TheCamera.Cams[TheCamera.ActiveCam].Up;
matrix.GetRight() = CrossProduct(TheCamera.Cams[TheCamera.ActiveCam].Up, TheCamera.Cams[TheCamera.ActiveCam].Front);
matrix.GetPosition() = pos;
} else if (ped->m_pSeekTarget != nil) {
2020-01-11 12:30:17 +01:00
float ry = CGeneral::GetRadianAngleBetweenPoints(1.0f, ped->m_pSeekTarget->GetPosition().z, 1.0f, pos.z);
2020-01-12 12:31:04 +01:00
float rz = Atan2(-ped->GetForward().x, ped->GetForward().y);
2020-01-11 12:30:17 +01:00
vy = 0.35f * speed + 0.15f;
matrix.SetTranslate(0.0f, 1.0f, 1.0f);
matrix.Rotate(0.0f, ry, rz);
matrix.GetPosition() += pos;
2020-05-15 16:30:25 +02:00
} else {
matrix = ped->GetMatrix();
}
2020-01-11 12:30:17 +01:00
}
velocity = Multiply3x3(matrix, CVector(0.0f, vy, 0.0f));
gravity = false;
break;
}
case WEAPONTYPE_MOLOTOV:
{
time = CTimer::GetTimeInMilliseconds() + 2000;
float scale = 0.22f * speed + 0.15f;
2020-01-13 15:56:47 +01:00
if (scale < 0.2f)
2020-01-11 12:30:17 +01:00
scale = 0.2f;
2020-01-12 12:31:04 +01:00
float angle = Atan2(-ped->GetForward().x, ped->GetForward().y);
2020-01-11 12:30:17 +01:00
matrix.SetTranslate(0.0f, 0.0f, 0.0f);
matrix.RotateZ(angle);
matrix.GetPosition() += pos;
velocity.x = -1.0f * scale * Sin(angle);
velocity.y = scale * Cos(angle);
velocity.z = (0.2f * speed + 0.4f) * scale;
break;
}
case WEAPONTYPE_GRENADE:
2020-05-15 16:30:25 +02:00
case WEAPONTYPE_DETONATOR_GRENADE:
2020-01-11 12:30:17 +01:00
{
time = CTimer::GetTimeInMilliseconds() + 2000;
float scale = 0.0f;
if (speed != 0.0f)
scale = 0.22f * speed + 0.15f;
2020-01-12 12:31:04 +01:00
float angle = Atan2(-ped->GetForward().x, ped->GetForward().y);
2020-01-11 12:30:17 +01:00
matrix.SetTranslate(0.0f, 0.0f, 0.0f);
matrix.RotateZ(angle);
matrix.GetPosition() += pos;
SpecialCollisionResponseCase = COLLRESPONSE_UNKNOWN5;
velocity.x = -1.0f * scale * Sin(angle);
velocity.y = scale * Cos(angle);
velocity.z = (0.4f * speed + 0.4f) * scale;
elasticity = 0.5f;
break;
}
2020-05-15 16:30:25 +02:00
default:
Error("Undefined projectile type, AddProjectile, ProjectileInfo.cpp");
break;
2020-01-11 12:30:17 +01:00
}
int i = 0;
while (gaProjectileInfo[i].m_bInUse && i < ARRAY_SIZE(gaProjectileInfo)) i++;
if (i == ARRAY_SIZE(gaProjectileInfo))
return false;
switch (weapon)
{
2020-05-15 16:30:25 +02:00
case WEAPONTYPE_ROCKET:
2020-01-11 12:30:17 +01:00
ms_apProjectile[i] = new CProjectile(MI_MISSILE);
break;
case WEAPONTYPE_FLAMETHROWER:
break;
case WEAPONTYPE_MOLOTOV:
ms_apProjectile[i] = new CProjectile(MI_MOLOTOV);
break;
case WEAPONTYPE_GRENADE:
2020-05-15 16:30:25 +02:00
case WEAPONTYPE_DETONATOR_GRENADE:
2020-01-11 12:30:17 +01:00
ms_apProjectile[i] = new CProjectile(MI_GRENADE);
break;
2020-05-12 01:24:57 +02:00
default: break;
2020-01-11 12:30:17 +01:00
}
if (ms_apProjectile[i] == nil)
return false;
gaProjectileInfo[i].m_eWeaponType = weapon;
gaProjectileInfo[i].m_pSource = ped;
ms_apProjectile[i]->GetMatrix() = matrix;
ms_apProjectile[i]->SetMoveSpeed(velocity);
ms_apProjectile[i]->bAffectedByGravity = gravity;
gaProjectileInfo[i].m_nExplosionTime = time;
ms_apProjectile[i]->m_fElasticity = elasticity;
ms_apProjectile[i]->m_nSpecialCollisionResponseCases = SpecialCollisionResponseCase;
#ifdef SQUEEZE_PERFORMANCE
projectileInUse++;
#endif
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_bInUse = true;
CWorld::Add(ms_apProjectile[i]);
gaProjectileInfo[i].m_vecPos = ms_apProjectile[i]->GetPosition();
2020-05-15 16:30:25 +02:00
if (entity && entity->IsPed() && !ped->m_pCollidingEntity) {
ped->m_pCollidingEntity = ms_apProjectile[i];
}
2020-01-11 12:30:17 +01:00
return true;
}
void
CProjectileInfo::RemoveProjectile(CProjectileInfo *info, CProjectile *projectile)
{
RemoveNotAdd(info->m_pSource, info->m_eWeaponType, projectile->GetPosition());
#ifdef SQUEEZE_PERFORMANCE
projectileInUse--;
#endif
2020-01-11 12:30:17 +01:00
info->m_bInUse = false;
CWorld::Remove(projectile);
delete projectile;
}
void
2020-01-12 12:31:04 +01:00
CProjectileInfo::RemoveNotAdd(CEntity *entity, eWeaponType weaponType, CVector pos)
2020-01-11 12:30:17 +01:00
{
switch (weaponType)
{
case WEAPONTYPE_GRENADE:
CExplosion::AddExplosion(nil, entity, EXPLOSION_GRENADE, pos, 0);
break;
case WEAPONTYPE_MOLOTOV:
CExplosion::AddExplosion(nil, entity, EXPLOSION_MOLOTOV, pos, 0);
break;
2020-05-15 16:30:25 +02:00
case WEAPONTYPE_ROCKET:
2020-01-11 12:30:17 +01:00
CExplosion::AddExplosion(nil, entity, EXPLOSION_ROCKET, pos, 0);
break;
2020-05-12 01:24:57 +02:00
default: break;
2020-01-11 12:30:17 +01:00
}
}
void
CProjectileInfo::Update()
{
#ifdef SQUEEZE_PERFORMANCE
if (projectileInUse == 0)
return;
#endif
2020-01-11 12:30:17 +01:00
for (int i = 0; i < ARRAY_SIZE(gaProjectileInfo); i++) {
if (!gaProjectileInfo[i].m_bInUse) continue;
CPed *ped = (CPed*)gaProjectileInfo[i].m_pSource;
if (ped != nil && ped->IsPed() && !ped->IsPointerValid())
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_pSource = nil;
if (ms_apProjectile[i] == nil) {
#ifdef SQUEEZE_PERFORMANCE
projectileInUse--;
#endif
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_bInUse = false;
continue;
}
2020-05-15 16:30:25 +02:00
if (gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_ROCKET) {
2020-01-11 12:30:17 +01:00
CParticle::AddParticle(PARTICLE_SMOKE, ms_apProjectile[i]->GetPosition(), CVector(0.0f, 0.0f, 0.0f));
}
if (CTimer::GetTimeInMilliseconds() <= gaProjectileInfo[i].m_nExplosionTime) {
2020-05-15 16:30:25 +02:00
if (gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_ROCKET) {
2020-01-11 12:30:17 +01:00
CVector pos = ms_apProjectile[i]->GetPosition();
CWorld::pIgnoreEntity = ms_apProjectile[i];
if (ms_apProjectile[i]->bHasCollided
|| !CWorld::GetIsLineOfSightClear(gaProjectileInfo[i].m_vecPos, pos, true, true, true, true, false, false)
2020-05-15 16:30:25 +02:00
|| gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_ROCKET && (CHeli::TestRocketCollision(&pos) || CPlane::TestRocketCollision(&pos))) {
2020-01-11 12:30:17 +01:00
RemoveProjectile(&gaProjectileInfo[i], ms_apProjectile[i]);
}
CWorld::pIgnoreEntity = nil;
} else if (gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_MOLOTOV) {
CVector pos = ms_apProjectile[i]->GetPosition();
CWorld::pIgnoreEntity = ms_apProjectile[i];
if (gaProjectileInfo[i].m_pSource == nil
|| ((gaProjectileInfo[i].m_vecPos - gaProjectileInfo[i].m_pSource->GetPosition()).MagnitudeSqr() >= 2.0f))
{
if (ms_apProjectile[i]->bHasCollided
|| !CWorld::GetIsLineOfSightClear(gaProjectileInfo[i].m_vecPos, pos, true, true, true, true, false, false)
2020-05-15 16:30:25 +02:00
|| gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_ROCKET && (CHeli::TestRocketCollision(&pos) || CPlane::TestRocketCollision(&pos))) {
2020-01-11 12:30:17 +01:00
RemoveProjectile(&gaProjectileInfo[i], ms_apProjectile[i]);
}
}
CWorld::pIgnoreEntity = nil;
}
} else {
2020-05-15 16:30:25 +02:00
if (gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_DETONATOR_GRENADE) {
CEntity *ent = gaProjectileInfo[i].m_pSource;
if (ent->IsPed() && ((CPed*)ped)->IsPlayer()) {
CPed *ped = (CPed*)ent;
if (ped->GetWeapon(ped->GetWeaponSlot(WEAPONTYPE_DETONATOR)).m_eWeaponType != WEAPONTYPE_DETONATOR
|| ped->GetWeapon(ped->GetWeaponSlot(WEAPONTYPE_DETONATOR)).m_nAmmoTotal == 0) {
gaProjectileInfo[i].m_nExplosionTime = 0;
}
}
} else {
RemoveProjectile(&gaProjectileInfo[i], ms_apProjectile[i]);
}
2020-01-11 12:30:17 +01:00
}
gaProjectileInfo[i].m_vecPos = ms_apProjectile[i]->GetPosition();
}
}
bool
CProjectileInfo::IsProjectileInRange(float x1, float x2, float y1, float y2, float z1, float z2, bool remove)
{
bool result = false;
for (int i = 0; i < ARRAY_SIZE(ms_apProjectile); i++) {
if (gaProjectileInfo[i].m_bInUse) {
2020-05-15 16:30:25 +02:00
if (gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_ROCKET || gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_MOLOTOV || gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_GRENADE) {
2020-04-30 12:48:01 +02:00
const CVector &pos = ms_apProjectile[i]->GetPosition();
2020-01-11 12:30:17 +01:00
if (pos.x >= x1 && pos.x <= x2 && pos.y >= y1 && pos.y <= y2 && pos.z >= z1 && pos.z <= z2) {
result = true;
if (remove) {
#ifdef SQUEEZE_PERFORMANCE
projectileInUse--;
#endif
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_bInUse = false;
CWorld::Remove(ms_apProjectile[i]);
delete ms_apProjectile[i];
}
}
}
}
}
return result;
}
2020-05-15 16:30:25 +02:00
// --MIAMI: Done
void
CProjectileInfo::RemoveDetonatorProjectiles()
{
for (int i = 0; i < ARRAY_SIZE(ms_apProjectile); i++) {
if (gaProjectileInfo[i].m_bInUse && gaProjectileInfo[i].m_eWeaponType == WEAPONTYPE_DETONATOR_GRENADE) {
CExplosion::AddExplosion(nil, gaProjectileInfo[i].m_pSource, EXPLOSION_GRENADE, gaProjectileInfo[i].m_vecPos, 0); // TODO(Miami): New parameter (1)
gaProjectileInfo[i].m_bInUse = false;
CWorld::Remove(ms_apProjectile[i]);
delete ms_apProjectile[i];
}
}
}
2020-01-11 12:30:17 +01:00
void
CProjectileInfo::RemoveAllProjectiles()
{
#ifdef SQUEEZE_PERFORMANCE
if (projectileInUse == 0)
return;
#endif
2020-01-11 12:30:17 +01:00
for (int i = 0; i < ARRAY_SIZE(ms_apProjectile); i++) {
if (gaProjectileInfo[i].m_bInUse) {
#ifdef SQUEEZE_PERFORMANCE
projectileInUse--;
#endif
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_bInUse = false;
CWorld::Remove(ms_apProjectile[i]);
delete ms_apProjectile[i];
}
}
}
bool
2020-01-12 12:31:04 +01:00
CProjectileInfo::RemoveIfThisIsAProjectile(CObject *object)
2020-01-11 12:30:17 +01:00
{
#ifdef SQUEEZE_PERFORMANCE
if (projectileInUse == 0)
return false;
#endif
2020-01-11 12:30:17 +01:00
int i = 0;
while (ms_apProjectile[i++] != object) {
if (i >= ARRAY_SIZE(ms_apProjectile))
return false;
}
#ifdef SQUEEZE_PERFORMANCE
projectileInUse--;
#endif
2020-01-11 12:30:17 +01:00
gaProjectileInfo[i].m_bInUse = false;
CWorld::Remove(ms_apProjectile[i]);
delete ms_apProjectile[i];
ms_apProjectile[i] = nil;
return true;
}