#include "ship.h" #include "objimport.h" #include "frame.h" #include "l3d.h" #include "world_view.h" #include "sbre/sbre.h" #include "space.h" Ship::Ship(ShipType::Type shipType) : RigidBody() { m_wheelTransition = 0; m_wheelState = 0; m_dockedWith = 0; m_mesh = 0; m_shipType = shipType; m_angThrusters[0] = m_angThrusters[1] = m_angThrusters[2] = 0; m_laserCollisionObj.owner = this; m_equipment = EquipSet(shipType); for(int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { m_tempLaserGeom[i] = 0; m_gunState[i] = 0; } dGeomSetData(m_geom, static_cast
(this)); } void Ship::UpdateMass(void) { shipstats_t s; CalcStats(&s); dMassAdjust(&m_mass, s.total_mass*1000); dBodySetMass(m_body, &m_mass); } void Ship::SetThrusterState(enum ShipType::Thruster t, float level) { m_thrusters[t] = level; } void Ship::ClearThrusterState(void) { for(int i = 0; i < ShipType::THRUSTER_MAX; i++) m_thrusters[i] = 0; } /* Hyperspace range is: * (200 * hyperspace_class^2) / total_mass (in tonnes) */ void Ship::CalcStats(shipstats_t* stats) { const ShipType& stype = GetShipType(); stats->max_capacity = stype.capacity; stats->used_capacity = 0; for(int i = 0; i < Equip::SLOT_MAX; i++) { for(int j = 0; j < stype.equipSlotCapacity[i]; j++) { Equip::Type t = m_equipment.Get((Equip::Slot)i, j); if(t) stats->used_capacity += EquipType::types[t].mass; } } stats->free_capacity = stats->max_capacity - stats->used_capacity; stats->total_mass = stats->used_capacity + stype.hullMass; Equip::Type t = m_equipment.Get(Equip::SLOT_ENGINE); float hyperclass = EquipType::types[t].pval; stats->hyperspace_range = 200 * hyperclass * hyperclass / stats->total_mass; } void Ship::AITurn(void) { const ShipType& stype = GetShipType(); float timeStep = L3D::GetTimeStep(); for(int i = 0; i < ShipType::THRUSTER_MAX; i++) { float force = timeStep * stype.linThrust[i] * m_thrusters[i]; switch(i) { case ShipType::THRUSTER_REAR: case ShipType::THRUSTER_FRONT: dBodyAddRelForce(m_body, 0, 0, force); break; case ShipType::THRUSTER_TOP: case ShipType::THRUSTER_BOTTOM: dBodyAddRelForce(m_body, 0, force, 0); break; case ShipType::THRUSTER_LEFT: case ShipType::THRUSTER_RIGHT: dBodyAddRelForce(m_body, force, 0, 0); break; } } dBodyAddRelTorque(m_body, stype.angThrust*m_angThrusters[0], stype.angThrust*m_angThrusters[1], stype.angThrust*m_angThrusters[2]); /* LASERZ!! */ for(int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { /* Free old temp laser geoms. */ if(m_tempLaserGeom[i]) dGeomDestroy(m_tempLaserGeom[i]); m_tempLaserGeom[i] = 0; if(!m_gunState[i]) continue; dGeomID ray = dCreateRay(GetFrame()->GetSpaceID(), 10000); const vector3d pos = GetPosition(); const vector3f _dir = stype.gunMount[i].dir; vector3d dir = vector3d(_dir.x, _dir.y, _dir.z); matrix4x4d m; GetRotMatrix(m); dir = m.ApplyRotationOnly(dir); dGeomRaySet(ray, pos.x, pos.y, pos.z, dir.x, dir.y, dir.z); dGeomSetData(ray, static_cast