1 #include "servership.h"
2 #include "servergame.h"
3 #include "serverdata.h"
4 #include "scorevals.h"
5 
6 
7 int ShipToShipCollision(int ship1, int ship2);
8 int ShotToShipCollision(int obj, int ship);
9 int PowerupToShipCollision(int obj, int ship);
10 int AsteroidToShipCollision(int obj, int ship);
11 int NukeToShipCollision(int obj, int ship);
12 int NukeExplosionToShipCollision(int obj, int ship);
13 int ShotToAsteroidCollision(int obj1, int obj2);
14 int ShotToPowerupCollision(int obj1, int obj2);
15 int AsteroidToAsteroidCollision(int obj1, int obj2);
16 int AsteroidToPowerupCollision(int obj1, int obj2);
17 int AsteroidToNukeCollision(int obj1, int obj2);
18 int AsteroidToNukeExplosionCollision(int obj1, int obj2);
19 int PowerupToPowerupCollision(int obj1, int obj2);
20 
21 
22 int (*ObjectToObjectCollisionFunctions[MAX_TYPES][MAX_TYPES])(int, int);
23 int (*ObjectToShipCollisionFunctions[MAX_TYPES])(int, int);
24 
25 
InitCollisionFunctions()26 void InitCollisionFunctions()
27 {
28   int i;
29   int j;
30 
31   for (i=0; i<MAX_TYPES; i++) {
32     for (j=0; j<MAX_TYPES; j++) {
33       ObjectToObjectCollisionFunctions[i][j]=NULL;
34     }
35     ObjectToShipCollisionFunctions[i]=NULL;
36   }
37 
38   ObjectToShipCollisionFunctions[SHOT_TYPE]=ShotToShipCollision;
39   for (i=0; i<NUM_POWERUPS; i++) {
40     ObjectToShipCollisionFunctions[SHIELD_POWERUP_TYPE+i]=PowerupToShipCollision;
41   }
42   ObjectToShipCollisionFunctions[NUKE_TYPE]=NukeToShipCollision;
43   ObjectToShipCollisionFunctions[NUKE_EXPLOSION_TYPE]=NukeExplosionToShipCollision;
44   ObjectToShipCollisionFunctions[ASTEROID_TYPE]=AsteroidToShipCollision;
45 
46   ObjectToObjectCollisionFunctions[SHOT_TYPE][ASTEROID_TYPE]=ShotToAsteroidCollision;
47   for (i=0; i<NUM_POWERUPS; i++) {
48     ObjectToObjectCollisionFunctions[SHOT_TYPE][SHIELD_POWERUP_TYPE+i]=ShotToPowerupCollision;
49   }
50   ObjectToObjectCollisionFunctions[ASTEROID_TYPE][ASTEROID_TYPE]=AsteroidToAsteroidCollision;
51   for (i=0; i<NUM_POWERUPS; i++) {
52     ObjectToObjectCollisionFunctions[ASTEROID_TYPE][SHIELD_POWERUP_TYPE+i]=AsteroidToPowerupCollision;
53   }
54   ObjectToObjectCollisionFunctions[ASTEROID_TYPE][NUKE_TYPE]=AsteroidToNukeCollision;
55   ObjectToObjectCollisionFunctions[ASTEROID_TYPE][NUKE_EXPLOSION_TYPE]=AsteroidToNukeExplosionCollision;
56   for (i=0; i<NUM_POWERUPS; i++) {
57     for (j=0; j<NUM_POWERUPS; j++) {
58       ObjectToObjectCollisionFunctions[SHIELD_POWERUP_TYPE+i][SHIELD_POWERUP_TYPE+j]=PowerupToPowerupCollision;
59     }
60   }
61 }
62 
63 
ShipToShipCollision(int ship1,int ship2)64 int ShipToShipCollision(int ship1, int ship2)
65 {
66   int Damage;
67 
68   Damage=BounceObjects(ServerShips[ship1], ServerShips[ship2]);
69 
70   if (!ServerShips[ship1]->Action.shield || !ServerShips[ship1]->Shield) {
71     if (ServerShips[ship1]->TakeDamage(Damage)) {
72       if (ServerGameType==DEATHMATCH) {
73         ServerShips[ship1]->Score--;
74         ServerShips[ship2]->Score++;
75       } else if (ServerGameType==FREE_FOR_ALL || ServerGameType==SINGLE_PLAYER) {
76         ServerShips[ship2]->Score+=ShipScore;
77       }
78     }
79   } else {
80     if (Damage) {
81       AddSound(ship1, &ServerShips[ship1]->Position, PingSound);
82     }
83   }
84 
85   if (!ServerShips[ship2]->Action.shield || !ServerShips[ship2]->Shield) {
86     if (ServerShips[ship2]->TakeDamage(Damage)) {
87       if (ServerGameType==DEATHMATCH) {
88         ServerShips[ship1]->Score++;
89         ServerShips[ship2]->Score--;
90       } else if (ServerGameType==FREE_FOR_ALL || ServerGameType==SINGLE_PLAYER) {
91         ServerShips[ship1]->Score+=ShipScore;
92       }
93       return(1);
94     }
95   } else {
96     if (Damage) {
97       AddSound(ship2, &ServerShips[ship2]->Position, PingSound);
98     }
99   }
100 
101   return(0);
102 }
103 
104 
ShotToShipCollision(int obj,int ship)105 int ShotToShipCollision(int obj, int ship)
106 {
107   int rc;
108   int Damage;
109 
110   if (ship==ServerObjects[obj]->PlayerNum) {
111     return(0);
112   }
113 
114   rc=BounceObjects(ServerShips[ship], ServerObjects[obj]);
115   if (ServerShips[ship]->Action.shield && ServerShips[ship]->Shield && rc) {
116     AddSoundAll(&ServerShips[ship]->Position, PingSound);
117   } else {
118     Damage=ShotDamage;
119     if (ServerShips[ServerObjects[obj]->PlayerNum]->Bot) {
120       Damage=(int)((float)Damage*BotRatio);
121     }
122 
123     if (ServerShips[ship]->TakeDamage(Damage)) {
124       if (ServerGameType==DEATHMATCH) {
125         ServerShips[ServerObjects[obj]->PlayerNum]->Score++;
126       } else {
127         ServerShips[ServerObjects[obj]->PlayerNum]->Score+=ShipScore;
128       }
129       return(1);
130     } else {
131       CreateExplosion(obj, 0);
132     }
133     delete ServerObjects[obj];
134     ServerObjects[obj]=NULL;
135   }
136 
137   return(0);
138 }
139 
140 
PowerupToShipCollision(int obj,int ship)141 int PowerupToShipCollision(int obj, int ship)
142 {
143   AddSound(ship, &ServerShips[ship]->Position, PrizeSound);
144 
145   if (ServerObjects[obj]->ObjectType==SHIELD_POWERUP_TYPE) {
146     ServerShips[ship]->IncreaseShield(ShieldPowerupValue);
147   } else if (ServerObjects[obj]->ObjectType==ARMOR_POWERUP_TYPE) {
148     ServerShips[ship]->IncreaseArmor(ArmorPowerupValue);
149   } else if (ServerObjects[obj]->ObjectType==HYPERWARP_POWERUP_TYPE) {
150     ServerShips[ship]->Warps++;
151   } else if (ServerObjects[obj]->ObjectType==NUKE_POWERUP_TYPE) {
152     ServerShips[ship]->Nukes++;
153   }
154 
155   delete ServerObjects[obj];
156   ServerObjects[obj]=NULL;
157 
158   return(0);
159 }
160 
161 
NukeToShipCollision(int obj,int ship)162 int NukeToShipCollision(int obj, int ship)
163 {
164   if (ship==ServerObjects[obj]->PlayerNum) {
165     return(0);
166   }
167 
168   delete ServerObjects[obj];
169   ServerObjects[obj]=NULL;
170 
171   return(0);
172 }
173 
174 
NukeExplosionToShipCollision(int obj,int ship)175 int NukeExplosionToShipCollision(int obj, int ship)
176 {
177   if (!ServerObjects[obj]->PlayersHit[ship]) {
178     if (ServerShips[ship]->Action.shield && ServerShips[ship]->Shield) {
179       ServerShips[ship]->DecreaseShield(NukeShieldDrain);
180       ServerObjects[obj]->PlayersHit[ship]=1;
181       return(0);
182     } else {
183       if (ServerShips[ship]->TakeDamage(NukeDamage)) {
184         if (ServerGameType==DEATHMATCH) {
185           ServerShips[ServerObjects[obj]->PlayerNum]->Score++;
186         } else {
187           ServerShips[ServerObjects[obj]->PlayerNum]->Score+=ShipScore;
188         }
189       }
190     }
191     ServerObjects[obj]->PlayersHit[ship]=1;
192     return(1);
193   }
194   return(0);
195 }
196 
197 
AsteroidToShipCollision(int obj,int ship)198 int AsteroidToShipCollision(int obj, int ship)
199 {
200   if (ServerShips[ship]->Action.shield && ServerShips[ship]->Shield) {
201     if (BounceObjects(ServerShips[ship], ServerObjects[obj])) {
202       AddSoundAll(&ServerShips[ship]->Position, PingSound);
203     }
204     return(0);
205   } else {
206     if (ServerShips[ship]->TakeDamage(BounceObjects(ServerShips[ship], ServerObjects[obj]))) {
207       if (ServerGameType==DEATHMATCH) {
208         ServerShips[ship]->Score--;
209       } else if (ServerGameType==FREE_FOR_ALL) {
210         ServerShips[ship]->Score-=ShipScore;
211       }
212     }
213     return(1);
214   }
215 }
216 
217 
ShotToAsteroidCollision(int obj1,int obj2)218 int ShotToAsteroidCollision(int obj1, int obj2)
219 {
220   CreateExplosion(obj1, 0);
221   AddSoundAll(&ServerObjects[obj1]->Position, ExplosionSound);
222 
223   if (ServerObjects[obj2]->ObjectSize > MinRockSize) {
224     CreateExplosionRandomRock(obj2);
225   }
226   if (ServerShips[ServerObjects[obj1]->PlayerNum] && ServerGameType!=DEATHMATCH) {
227     ServerShips[ServerObjects[obj1]->PlayerNum]->Score+=AsteroidScore;
228   }
229 
230   delete ServerObjects[obj1];
231   ServerObjects[obj1]=NULL;
232   delete ServerObjects[obj2];
233   ServerObjects[obj2]=NULL;
234 
235   return(3);
236 }
237 
238 
ShotToPowerupCollision(int obj1,int obj2)239 int ShotToPowerupCollision(int obj1, int obj2)
240 {
241   if (ServerShips[ServerObjects[obj1]->PlayerNum]) {
242     AddSound(ServerObjects[obj1]->PlayerNum, &ServerShips[ServerObjects[obj1]->PlayerNum]->Position, IdiotSound);
243   }
244 
245   CreateExplosion(obj2, 0);
246   AddSoundAll(&ServerObjects[obj2]->Position, ExplosionSound);
247 
248   delete ServerObjects[obj1];
249   ServerObjects[obj1]=NULL;
250   delete ServerObjects[obj2];
251   ServerObjects[obj2]=NULL;
252 
253   return(3);
254 }
255 
256 
AsteroidToAsteroidCollision(int obj1,int obj2)257 int AsteroidToAsteroidCollision(int obj1, int obj2)
258 {
259   if (BounceObjects(ServerObjects[obj1], ServerObjects[obj2])) {
260     AddSoundAll(&ServerObjects[obj1]->Position, CrunchSound);
261   }
262 
263   return(0);
264 }
265 
266 
AsteroidToPowerupCollision(int obj1,int obj2)267 int AsteroidToPowerupCollision(int obj1, int obj2)
268 {
269   if (BounceObjects(ServerObjects[obj1], ServerObjects[obj2])) {
270     AddSoundAll(&ServerObjects[obj2]->Position, PingSound);
271   }
272 
273   return(0);
274 }
275 
276 
AsteroidToNukeCollision(int obj1,int obj2)277 int AsteroidToNukeCollision(int obj1, int obj2)
278 {
279   delete ServerObjects[obj2];
280   ServerObjects[obj2]=NULL;
281 
282   return(2);
283 }
284 
285 
AsteroidToNukeExplosionCollision(int obj1,int obj2)286 int AsteroidToNukeExplosionCollision(int obj1, int obj2)
287 {
288   CreateExplosion(obj1, 0);
289   AddSoundAll(&ServerObjects[obj1]->Position, ExplosionSound);
290 
291   if (ServerShips[ServerObjects[obj1]->PlayerNum] && ServerGameType!=DEATHMATCH) {
292     ServerShips[ServerObjects[obj1]->PlayerNum]->Score+=AsteroidScore;
293   }
294 
295   delete ServerObjects[obj1];
296   ServerObjects[obj1]=NULL;
297 
298   return(1);
299 }
300 
301 
PowerupToPowerupCollision(int obj1,int obj2)302 int PowerupToPowerupCollision(int obj1, int obj2)
303 {
304   if (BounceObjects(ServerObjects[obj1], ServerObjects[obj2])) {
305     AddSoundAll(&ServerObjects[obj1]->Position, PingSound);
306   }
307 
308   return(0);
309 }
310