OSDN Git Service

README-jp.txtのBOMを削除した
[starfighter-jp/starfighter-jp.git] / src / ship.cpp
1 /*
2 Copyright (C) 2003 Parallel Realities
3 Copyright (C) 2011, 2012 Guus Sliepen
4 Copyright (C) 2015, 2016 onpon4 <onpon4@riseup.net>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 3
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Starfighter.h"
21
22 bool ship_collision(object *ship, object *otherShip)
23 {
24         float x0 = ship->x;
25         float y0 = ship->y;
26         float w0 = ship->image[0]->w;
27         float h0 = ship->image[0]->h;
28
29         float x2 = otherShip->x;
30         float y2 = otherShip->y;
31         float w1 = otherShip->image[0]->w;
32         float h1 = otherShip->image[0]->h;
33
34         float x1 = x0 + w0;
35         float y1 = y0 + h0;
36
37         float x3 = x2 + w1;
38         float y3 = y2 + h1;
39
40         return !(x1<x2 || x3<x0 || y1<y2 || y3<y0);
41 }
42
43 /*
44 Fill in later...
45 */
46 void ship_fireBullet(object *ship, int weaponType)
47 {
48         if (ship->reload[weaponType] > 0)
49                 return;
50
51         int y = (ship->image[0]->h) / 5;
52
53         // Remove some ammo from the player
54         if ((ship == &player) && (weaponType == 1) && (!engine.cheatAmmo))
55                 player.ammo[1]--;
56
57         object *theWeapon = &weapon[ship->weaponType[weaponType]];
58
59         switch(theWeapon->id)
60         {
61                 case WT_PLASMA:
62                 case WT_SPREAD:
63                 case WT_DIRECTIONAL:
64                         audio_playSound(SFX_PLASMA, ship->x, ship->y);
65                         break;
66                 case WT_ROCKET:
67                         audio_playSound(SFX_MISSILE, ship->x, ship->y);
68                         break;
69                 case WT_LASER:
70                         audio_playSound(SFX_LASER, ship->x, ship->y);
71                         break;
72                 case WT_CHARGER:
73                         audio_playSound(SFX_PLASMA3, ship->x, ship->y);
74                         break;
75         }
76
77         if (theWeapon->flags & WF_SPREAD && theWeapon->ammo[0] >= 3)
78         {
79                 bullet_add(theWeapon, ship, y * 2, -1);
80                 bullet_add(theWeapon, ship, y * 4, 1);
81
82                 if (theWeapon->ammo[0] != 4)
83                         bullet_add(theWeapon, ship, y * 3, 0);
84                 else
85                 {
86                         bullet_add(theWeapon, ship, y * 2, 0);
87                         bullet_add(theWeapon, ship, y * 4, 0);
88                 }
89
90                 if (theWeapon->ammo[0] == 5)
91                 {
92                         bullet_add(theWeapon, ship, y * 1, -2);
93                         bullet_add(theWeapon, ship, y * 5, 2);
94                 }
95         }
96         else
97         {
98                 if (theWeapon->ammo[0] & 1)
99                         bullet_add(theWeapon, ship, y * 3, 0);
100
101                 if (theWeapon->ammo[0] >= 2)
102                 {
103                         bullet_add(theWeapon, ship, y * 2, 0);
104                         bullet_add(theWeapon, ship, y * 4, 0);
105                 }
106
107                 if (theWeapon->ammo[0] >= 4)
108                 {
109                         bullet_add(theWeapon, ship, y * 1, 0);
110                         bullet_add(theWeapon, ship, y * 5, 0);
111                 }
112         }
113
114         // Reset the weapon reload time. Double it if it is not friendly or
115         // a boss or Kline
116         ship->reload[weaponType] = theWeapon->reload[0];
117         if ((ship->flags & FL_WEAPCO) && (ship != &aliens[ALIEN_BOSS]) &&
118                         (ship != &aliens[ALIEN_KLINE]) && (theWeapon->id != W_LASER))
119                 ship->reload[weaponType] *= 2;
120
121         if ((engine.cheatAmmo) || (theWeapon->id == WT_LASER))
122                 return;
123
124         if ((ship == &player) && (weaponType == 0))
125         {
126                 if (player.ammo[0] > 0)
127                 {
128                         player.ammo[0]--;
129                         if (player.ammo[0] <= 0)
130                         {
131                                 weapon[W_PLAYER_WEAPON].ammo[0] = game.minPlasmaOutput;
132                                 weapon[W_PLAYER_WEAPON].damage = game.minPlasmaDamage;
133                                 weapon[W_PLAYER_WEAPON].reload[0] = rate2reload[game.minPlasmaRate];
134                         }
135                 }
136         }
137 }
138
139 /*
140 Fill in later...
141 */
142 void ship_fireRay(object *ship)
143 {
144         SDL_Rect ray;
145
146         if (ship->face == 0)
147         {
148                 ray.x = (int)(ship->x + ship->image[0]->w);
149         }
150         else
151         {
152                 ray.x = (int)(ship->x - 800);
153         }
154         ray.y = (int)(ship->y + ship->engineY - 1);
155         ray.h = 3;
156         ray.w = 800;
157
158         int red = SDL_MapRGB(screen->format, rand() % 256, 0x00, 0x00);
159         SDL_FillRect(screen, &ray, red);
160         screen_addBuffer(ray.x, ray.y, ray.w, ray.h);
161
162         if (ship != &player)
163         {
164                 if (player.shield > 0)
165                 {
166                         if (game_collision(player.x, player.y, player.image[0]->w,
167                                                 player.image[0]->h, ray.x, ray.y, ray.w, ray.h) &&
168                                         (!engine.cheatShield) && (engine.missionCompleteTimer == 0))
169                         {
170                                 if (player.shield > engine.lowShield)
171                                 {
172                                         if (player.shield - 1 <= engine.lowShield)
173                                         {
174                                                 setInfoLine("!!! \8cx\8d\90\83V\81[\83\8b\83h\92á\89º !!!", FONT_RED);
175                                         }
176                                 }
177                                 player.shield--;
178
179                                 explosion_add(player.x, player.y, SP_SMALL_EXPLOSION);
180                                 audio_playSound(SFX_HIT, player.x, player.y);
181                                 if (player.shield < 1)
182                                 {
183                                         audio_playSound(SFX_DEATH, player.x, player.y);
184                                         audio_playSound(SFX_EXPLOSION, player.x, player.y);
185                                 }
186                         }
187                 }
188         }
189
190         for (int i = 0 ; i < ALIEN_MAX ; i++)
191         {
192                 if (aliens[i].flags & FL_IMMORTAL)
193                         continue;
194
195                 if ((aliens[i].shield > 0) && (ship != &aliens[i]) &&
196                         (ship->classDef != aliens[i].classDef))
197                 {
198                         if (game_collision(aliens[i].x, aliens[i].y, aliens[i].image[0]->w,
199                                         aliens[i].image[0]->h, ray.x, ray.y, ray.w, ray.h))
200                         {
201                                 alien_hurt(&aliens[i], ship->owner, 1, false);
202                         }
203                 }
204         }
205
206         ship->ammo[0]--;
207         if (ship->ammo[0] < 1)
208                 ship->flags &= ~FL_FIRERAY;
209 }