1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <sys/time.h>
5 #include <unistd.h>
6 
7 
8 #include "serverobject.h"
9 #include "servergame.h"
10 #include "serverdata.h"
11 #include "mainplayerthread.h"
12 #include "sizes.h"
13 #include "scorevals.h"
14 #include "sounddefs.h"
15 #include "matrix.h"
16 
17 
ServerObject(struct Point Pos,struct Point Vel,struct Point Rot,int Type,float Size,float Mass,int playernum)18 ServerObject::ServerObject(struct Point Pos, struct Point Vel,
19                            struct Point Rot, int Type, float Size,
20                            float Mass, int playernum)
21 {
22   int i;
23 
24   memcpy(&Position, &Pos, sizeof(struct Point));
25   memcpy(&Velocity, &Vel, sizeof(struct Point));
26   memcpy(&Rotation, &Rot, sizeof(struct Point));
27 
28   PlayerNum=playernum;
29   ObjectType=Type;
30   ObjectSize=Size;
31   ObjectMass=Mass;
32 
33   if (Type==SHIP_TYPE) {
34     AliveTime=-1;
35   } else if (Type==SHOT_TYPE) {
36     AliveTime=ShotAliveTime;
37   } else if (Type==EXPLOSION_TYPE) {
38     AliveTime=20;
39   } else if (Type==BIG_EXPLOSION_TYPE) {
40     AliveTime=65;
41   } else if (Type==ASTEROID_TYPE) {
42     AliveTime=-1;
43     NumAsteroids++;
44     TotalAsteroidSize+=ObjectSize;
45   } else if (Type==SHIELD_POWERUP_TYPE || Type==ARMOR_POWERUP_TYPE ||
46              Type==NUKE_POWERUP_TYPE || Type==HYPERWARP_POWERUP_TYPE) {
47     AliveTime=PowerupAliveTime;
48   } else if (Type==NUKE_TYPE) {
49     AliveTime=NukeAliveTime;
50   } else if (Type==NUKE_EXPLOSION_TYPE) {
51     AliveTime=NukeExplosionAliveTime;
52     for (i=0; i<MAX_PLAYERS; i++) {
53       PlayersHit[i]=0;
54     }
55   }
56 
57   InitMatrix(RotationMatrix);
58 }
59 
60 
~ServerObject()61 ServerObject::~ServerObject()
62 {
63   int i;
64   int type;
65 
66   if (ObjectType==ASTEROID_TYPE) {
67     NumAsteroids--;
68     TotalAsteroidSize-=ObjectSize;
69   }
70 
71   if (ObjectType==BIG_EXPLOSION_TYPE && rand()%1024 < PrizeOdds || ObjectType==NUKE_TYPE) {
72     for (i=0; i<MAX_OBJECTS; i++) {
73       if (!ServerObjects[i]) {
74         if (ObjectType==BIG_EXPLOSION_TYPE) {
75           type=rand()%4+SHIELD_POWERUP_TYPE;
76           ServerObjects[i]=new ServerObject(Position, Velocity, Rotation, type, 0.1, 0.0);
77           break;
78         } else if (ObjectType==NUKE_TYPE) {
79           Velocity.x=0.0;
80           Velocity.y=0.0;
81           Velocity.z=0.0;
82           ServerObjects[i]=new ServerObject(Position, Velocity, Rotation,
83                                             NUKE_EXPLOSION_TYPE, 0.1, 0.0, PlayerNum);
84           AddSoundAll(&Position, NukeExplodeSound);
85           break;
86         }
87       }
88     }
89   }
90 }
91 
92 
UpdatePosition()93 void ServerObject::UpdatePosition()
94 {
95   Position.x+=Velocity.x;
96   Position.y+=Velocity.y;
97   Position.z+=Velocity.z;
98 
99   if (Position.x > WorldWidth) {
100     Position.x-=WorldWidth*2.0;
101   }
102   if (Position.y > WorldWidth) {
103     Position.y-=WorldWidth*2.0;
104   }
105   if (Position.z > WorldWidth) {
106     Position.z-=WorldWidth*2.0;
107   }
108   if (Position.x < -WorldWidth) {
109     Position.x+=WorldWidth*2.0;
110   }
111   if (Position.y < -WorldWidth) {
112     Position.y+=WorldWidth*2.0;
113   }
114   if (Position.z < -WorldWidth) {
115     Position.z+=WorldWidth*2.0;
116   }
117 }
118 
119 
UpdateRotation()120 void ServerObject::UpdateRotation()
121 {
122   xRotateMatrix(Rotation.x, RotationMatrix, 2);
123   yRotateMatrix(Rotation.y, RotationMatrix, 2);
124   zRotateMatrix(Rotation.z, RotationMatrix, 2);
125 }
126