home *** CD-ROM | disk | FTP | other *** search
/ Enter 2005 March / ENTER.ISO / files / fwp-0.0.6-win32-installer.exe / Shot.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-01-17  |  5.7 KB  |  220 lines

  1. #include "Shot.h"
  2.  
  3. #include "Network.h"
  4. #include "log.h"
  5. #include "Game.h"
  6. #include "matrixmath.h"
  7.  
  8. #include "LaserShot.h"
  9. #include "ChaingunShot.h"
  10. #include "RailgunShot.h"
  11. #include "RocketlauncherShot.h"
  12. #include "PlasmagunShot.h"
  13.  
  14. Shot::Shot(int clientId, int weaponId, unsigned int lifetimeMillis): GameObject(){
  15.     this->type = GAME_WEAPON_NO_WEAPON;
  16.     this->weaponId = weaponId;
  17.     this->clientId = clientId;
  18.     this->lifetimeMillis = lifetimeMillis;
  19.     this->spawntimeMillis = SDL_GetTicks();
  20.  
  21.     this->boundingSphereRadius = 1.0f;
  22.     this->inflictedDamage = 0;
  23. }
  24.  
  25. Shot::~Shot(){
  26. }
  27.  
  28. void Shot::leaveMuzzle(){
  29.  
  30.     // "shoot through wall"-check
  31.     if( Network::server->clients[clientId] != NULL && Network::server->clients[clientId]->vehicle != NULL ){
  32.         Vehicle* v = Network::server->clients[clientId]->vehicle;
  33.  
  34.         unsigned int i;
  35.         trace_t trace;
  36.         trace.ignoreFlags = 0 | COLLISION_FLAG_BACKFACES | COLLISION_FLAG_SHOOT_THROUGH | COLLISION_FLAG_VEHICLES;
  37.  
  38.         traceLinesegment(v->pos, pos, &trace);
  39.  
  40.         for(i=0;i<trace.hits.size();i++){
  41.             if( trace.hits[i].face != NULL ){    // hit a face of the arena
  42.                 hitFace( trace.hits[i].face, trace.hits[i].pos );
  43.                 break;
  44.             }
  45.             if( trace.hits[i].vehicle != NULL ){    // ignore vehicle-hits
  46.                 continue;
  47.             }
  48.         }
  49.     }
  50.  
  51.     // reload animation
  52.     if( Network::server->clients[clientId] != NULL && Network::server->clients[clientId]->vehicle != NULL
  53.         && Network::server->clients[clientId]->vehicle->weapons[weaponId] != NULL && Network::server->clients[clientId]->vehicle->weapons[weaponId]->animator != NULL ){
  54.         Animator* a = Network::server->clients[clientId]->vehicle->weapons[weaponId]->animator;
  55.         a->setCurrentAnimation(ANIMATION_WEAPON_RELOAD);
  56. //        a->setLooping(false);
  57.         a->start();
  58.     }
  59.  
  60. }
  61.  
  62. void Shot::move(float deltaT){
  63.  
  64.     vec3_t newPos;
  65.     vectorMA3d(pos, deltaT*moveSpeed, dir, newPos);
  66.  
  67.     unsigned int i;
  68.     trace_t trace;
  69.     trace.ignoreFlags = 0 | COLLISION_FLAG_BACKFACES | COLLISION_FLAG_SHOOT_THROUGH;
  70.  
  71.     traceLinesegment(pos, newPos, &trace);
  72.  
  73.     for(i=0;i<trace.hits.size();i++){
  74.         if( trace.hits[i].face != NULL ){    // hit a face of the arena
  75.             hitFace( trace.hits[i].face, trace.hits[i].pos );
  76.             break;
  77.         }
  78.         if( trace.hits[i].vehicle != NULL ){    // hit a vehicle
  79.             if( trace.hits[i].vehicle == Network::server->clients[clientId]->vehicle )
  80.                 continue;
  81.             
  82.             hitVehicle( trace.hits[i].vehicle, trace.hits[i].pos );
  83.             break;
  84.         }
  85.     }
  86.  
  87.     vectorCopy3d(newPos, pos);
  88. }
  89.  
  90. void Shot::hitFace(Face* face, vec3_t hp){
  91. //    log("HIT A FACE!\n");
  92. //    Shot::hitVehicle(Network::client->vehicle, hp);
  93.  
  94.     this->lifetimeMillis = 0;
  95. }
  96.  
  97. void Shot::hitVehicle(Vehicle* vehicle, vec3_t hp){
  98. //    log("HIT A VEHICLE!\n");
  99.  
  100.     if( !Game::info.var.clientGame ){    // we are the server -> send client Hurt
  101.         if( Network::server->clients[clientId] != NULL ){    // client has not yet disconnected
  102.  
  103.             // do hurt teammate if friendly fire is disabled
  104.             if( !(Game::info.var.mode == GAME_MODE_TEAM_DEATHMATCH && !Game::info.var.enableFriendlyFire && vehicle->client->ci.team == Network::server->clients[clientId]->ci.team) ){
  105.                 vehicle->takeDamage(this->inflictedDamage);
  106.                 if( vehicle->armor > 0 ){
  107.                     Network::server->sendClientHurt(vehicle->client->clientId, this->clientId, this->inflictedDamage);
  108.                 }else{
  109.                     Network::server->sendClientKill(vehicle->client->clientId, this->clientId, this->type);
  110.                     Game::killClient(vehicle->client, Network::server->clients[this->clientId], this->type);
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     this->lifetimeMillis = 0;
  117. }
  118.  
  119. void Shot::render(){
  120.     Renderer::renderBillboard(pos, 1.0f, 1.0f, NULL);
  121. }
  122.  
  123.  
  124.  
  125. Shot* Shot::createShotForShotSpawnPacket(shotSpawnPacket_t* ss){
  126.     Shot* ret = NULL;
  127.  
  128.     switch( ss->type ){
  129.         case GAME_WEAPON_LASER:
  130.             ret = new LaserShot(ss->clientId, ss->weaponId);
  131.             break;
  132.  
  133.         case GAME_WEAPON_CHAINGUN:
  134.             ret = new ChaingunShot(ss->clientId, ss->weaponId);
  135.             break;
  136.     
  137.         case GAME_WEAPON_RAILGUN:
  138.             ret = new RailgunShot(ss->clientId, ss->weaponId);
  139.             break;
  140.  
  141.         case GAME_WEAPON_ROCKETLAUNCHER:
  142.             ret = new RocketlauncherShot(ss->clientId, ss->weaponId);
  143.             break;
  144.  
  145.         case GAME_WEAPON_PLASMAGUN:
  146.             ret = new PlasmagunShot(ss->clientId, ss->weaponId);
  147.             break;
  148.  
  149.         case GAME_WEAPON_NO_WEAPON:
  150.         default:
  151.             return NULL;
  152.     }
  153.  
  154.     vectorCopy3d(ss->pos, ret->pos);
  155.     vectorCopy3d(ss->dir, ret->dir);
  156.     vectorCopy3d(ss->up, ret->up);
  157.     vectorCrossP3d(ret->up, ret->dir, ret->left);
  158.  
  159.     return ret;
  160.  
  161. }
  162.  
  163. Shot* Shot::createShotForWeapon(Weapon* weapon){
  164.     Shot* ret = NULL;
  165.  
  166.     Vehicle* v = weapon->vehicle;
  167.     int weaponId = -1;
  168.     for(int i=0;i<4;i++){
  169.         if( weapon == v->weapons[i] ){
  170.             weaponId = i;
  171.             break;
  172.         }
  173.     }
  174.  
  175.     switch( weapon->type ){
  176.         case GAME_WEAPON_LASER:
  177.             ret = new LaserShot(v->client->clientId, weaponId);
  178.             break;
  179.     
  180.         case GAME_WEAPON_CHAINGUN:
  181.             ret = new ChaingunShot(v->client->clientId, weaponId);
  182.             break;
  183.     
  184.         case GAME_WEAPON_RAILGUN:
  185.             ret = new RailgunShot(v->client->clientId, weaponId);
  186.             break;
  187.  
  188.         case GAME_WEAPON_ROCKETLAUNCHER:
  189.             ret = new RocketlauncherShot(v->client->clientId, weaponId);
  190.             break;
  191.  
  192.         case GAME_WEAPON_PLASMAGUN:
  193.             ret = new PlasmagunShot(v->client->clientId, weaponId);
  194.             break;
  195.  
  196.         case GAME_WEAPON_NO_WEAPON:
  197.         default:
  198.             return NULL;
  199.     }
  200.  
  201.     vec4_t pos;
  202.     vectorAdd3d(weapon->mountPoint, weapon->muzzlePoint, pos);
  203.     float m[] = {    v->left[0], v->left[1], v->left[2], 0.0f,
  204.                     v->up[0], v->up[1], v->up[2], 0.0f,
  205.                     v->dir[0], v->dir[1], v->dir[2], 0.0f,
  206.                     v->pos[0], v->pos[1], v->pos[2], 1.0f
  207.                 };
  208.     pos[3] = 1.0f;
  209.     matrixMultVector(m, pos, 4, pos);
  210.  
  211.     vectorCopy3d(pos, ret->pos);
  212.     vectorCopy3d(v->dir, ret->dir);
  213.     vectorCopy3d(v->up, ret->up);
  214.     vectorCopy3d(v->left, ret->left);
  215. //    vectorCopy3d(ss->vel, ret->vel);
  216.  
  217.     return ret;
  218.  
  219. }
  220.