]> git.pond.sub.org Git - empserver/blob - src/lib/subs/mslsub.c
Import of Empire 4.2.12
[empserver] / src / lib / subs / mslsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  mslsub.c: Missile subroutine stuff
29  * 
30  *  Known contributors to this file:
31  *     Ken Stevens, 1995
32  *     Steve McClure, 1996-2000
33  */
34
35 #include "misc.h"
36 #include "queue.h"
37 #include "player.h"
38 #include "var.h"
39 #include "sect.h"
40 #include "ship.h"
41 #include "optlist.h"
42 #include "nuke.h"
43 #include "plane.h"
44 #include "land.h"
45 #include "news.h"
46 #include "item.h"
47 #include "xy.h"
48 #include "nsc.h"
49 #include "file.h"
50 #include "nat.h"
51 #include "path.h"
52 #include "mission.h"
53 #include "prototypes.h"
54
55 int
56 msl_equip(struct plnstr *pp)
57 {
58         struct  plist pl;
59
60         bzero((s_char *)&pl, sizeof(struct plist));
61         pl.pcp = plchr + pp->pln_type;
62         bcopy((s_char *)pp, (s_char *)&pl.plane, sizeof(struct plnstr));
63         return mission_pln_equip(&pl, 0, 0, 'p');
64 }
65
66 int
67 msl_hit(struct plnstr *pp, int hardtarget, int type, int news_item, int snews_item, s_char *what, coord x, coord y, int victim)
68 {
69         int     hit;
70         struct  shpstr ship;
71         struct  sctstr sect;
72         int     sublaunch = 0;
73         struct  plchrstr *pcp = plchr + pp->pln_type;
74         int     hitchance = pln_hitchance(pp, hardtarget, type);
75         s_char  *from;
76         int     dam, dummyi;
77                 
78         mpr(pp->pln_own,"Preparing to launch %s at %s %s %s%s\n",
79             prplane(pp),
80             cname(victim),
81             what,
82             (type==EF_SHIP||type==EF_PLANE)?"in ":"",
83             xyas(x, y, pp->pln_own));
84         mpr(pp->pln_own, "\tLaunching from ");
85         if (pp->pln_ship >= 0) {
86                 getship(pp->pln_ship, &ship);
87                 mpr(pp->pln_own, "%s in ", prship(&ship));
88                 if (mchr[(int)ship.shp_type].m_flags & M_SUB) {
89                         sublaunch = 1;
90                         from = "in hatch";
91                 } else
92                         from = "on deck";
93                 mpr(pp->pln_own, "%s\n",
94                     xyas(ship.shp_x, ship.shp_y, pp->pln_own));
95         } else {
96                 if (pp->pln_harden > 0) {
97                         mpr(pp->pln_own, "missile silo at ");
98                         from = "in cilo";
99                 } else
100                         from = "on launch pad";
101                 mpr(pp->pln_own, "%s\n",
102                     xyas(pp->pln_x, pp->pln_y, pp->pln_own));
103         }
104
105         if (chance((0.05 + (100 - pp->pln_effic)/100.0)
106                    * (1 - techfact(pp->pln_tech, 1.0)))) {
107                 mpr(pp->pln_own, "KABOOOOM!  Missile explodes %s!\n", from);
108                 if (chance(0.33)) {
109                         if (pp->pln_nuketype != (s_char)-1 &&
110                             opt_NUKEFAILDETONATE) {
111                                 pp->pln_flags &= ~PLN_AIRBURST;
112                                 detonate(pp, pp->pln_x, pp->pln_y);
113                         } else {
114                                 dam = pln_damage(pp, pp->pln_x, pp->pln_y,
115                                                  'p', &dummyi, 1) / 2;
116                                 if (dam) {
117                                         if (pp->pln_ship >= 0) {
118                                                 shipdamage(&ship, dam);
119                                                 putship(ship.shp_uid, &ship);
120                                         } else {
121                                                 pr("Explosion damages %s %d%%",
122                                                    xyas(pp->pln_x, pp->pln_y, pp->pln_own),
123                                                    dam);
124                                                 getsect(pp->pln_x, pp->pln_y, &sect);
125                                                 sectdamage(&sect, dam, 0);
126                                                 putsect(&sect);
127                                         }
128                                 }
129                         }
130                 }
131                 return 0;
132         }
133         
134         mpr(pp->pln_own, "\tSHWOOOOOSH!  Missile launched!\n");
135
136         if (pp->pln_nuketype != (s_char)-1)
137                 mpr(pp->pln_own, "\tArming nuclear warheads...\n");
138
139         if (pcp->pl_flags & P_T)
140                 mpr(victim, "Incoming %s missile...\n",sublaunch?(s_char *)"":cname(pp->pln_own));
141
142         if (opt_PINPOINTMISSILE == 0 || 
143             (pcp->pl_flags & P_T && !(pcp->pl_flags & P_MAR))) {
144                 if (msl_intercept(x, y, pp->pln_own, pcp->pl_def,
145                                   sublaunch, P_N, P_O)) {
146                         return 0;
147                 }
148         }
149         if (pcp->pl_flags & P_MAR) {
150                 if (shp_missile_defense(x, y, pp->pln_own, pcp->pl_def)) {
151                         return 0;
152                 }
153         }
154
155         if (pp->pln_nuketype != (s_char)-1)
156                 hitchance = 100;
157
158         mpr(pp->pln_own,"\t%d%% hitchance...", hitchance);
159         hit = (roll(100) <= hitchance);
160
161         mpr(pp->pln_own,hit?"HIT!\n":"miss\n");
162         if (pcp->pl_flags & P_T)
163                 mpr(victim, "...Incoming %s missile %s\n",sublaunch?(s_char *)"":cname(pp->pln_own), hit?"HIT!\n":"missed\n");
164         if (hit && news_item) {
165                 if (sublaunch)
166                         nreport(victim, snews_item, 0, 1);
167                 else
168                         nreport(pp->pln_own, news_item, victim, 1);
169         }
170         return hit;
171 }
172
173 void
174 msl_sel(struct emp_qelem *list, coord x, coord y, natid victim, int wantflags, int nowantflags, int mission)
175 {
176         register struct plchrstr *pcp;
177         struct  plnstr plane;
178         struct  plist *irv;
179         struct  nstr_item ni;
180
181         emp_initque (list);
182         snxtitem_all (&ni, EF_PLANE);
183         while (nxtitem (&ni, (s_char *)&plane)) {
184                 if (!plane.pln_own)
185                         continue;
186
187                 pcp = &plchr[(int)plane.pln_type];
188                 if (!(pcp->pl_flags & P_M))
189                         continue;
190                 if (wantflags && (pcp->pl_flags & wantflags) != wantflags)
191                         continue;
192                 if (nowantflags && pcp->pl_flags & nowantflags)
193                         continue;
194                 if (mission && plane.pln_mission != mission)
195                         continue;
196                 if (getrel(getnatp(plane.pln_own), victim) >= NEUTRAL)
197                         continue;
198                 /* missiles go one way, so we can use all the range */
199                 if (plane.pln_range < mapdist(x, y, plane.pln_x, plane.pln_y))
200                         continue;
201                 if (plane.pln_mobil <= (s_char)0)
202                         continue;
203                 if (plane.pln_effic < 100)
204                         continue;
205                 /* got a valid interceptor */
206                 irv = (struct plist *) malloc(sizeof(*irv));
207                 irv->state = P_OK;
208                 irv->bombs = 0;
209                 irv->misc = 0;
210                 irv->pcp = &plchr[(int)plane.pln_type];
211                 bcopy((s_char *)&plane, (s_char *)&irv->plane, sizeof(plane));
212                 emp_insque(&irv->queue, list);
213         }
214 }
215
216 int
217 msl_intercept(coord x, coord y, natid bombown, int hardtarget, int sublaunch, int wantflags, int nowantflags)
218 {
219         register struct plnstr *pp;
220         register struct plchrstr *pcp;
221         struct  sctstr  sect;
222         struct  emp_qelem   *irvlist;
223         struct  emp_qelem   foo;
224         struct  emp_qelem   *intlist;
225         struct  emp_qelem   intfoo;
226         struct  emp_qelem   *qp;
227         struct  emp_qelem   *next;
228         struct  plist   *ip;
229         int     icount = 0;
230         short   destroyed = 0;
231         s_char  *att_name;
232         s_char  *def_name;
233         int     news_item;
234         s_char  what[512];
235         s_char  *who = sublaunch?(s_char *)"":cname(bombown);
236
237         getsect(x, y, &sect);
238         if (wantflags == P_O && !nowantflags) {
239                 att_name = "satellite";
240                 def_name = "a-sat missile";
241                 news_item = N_SAT_KILL;
242                 if (sect.sct_own) {
243                         mpr(sect.sct_own, "%s has positioned a satellite over %s\n", sublaunch?(s_char *)"someone":cname(bombown), xyas(x,y,sect.sct_own));
244                 }
245         } else if (wantflags == P_N && nowantflags == P_O) {
246                 att_name = "warhead";
247                 def_name = "abm";
248                 news_item = sublaunch?N_NUKE_SSTOP:N_NUKE_STOP;
249         } else {
250                 att_name = "elephant";
251                 def_name = "tomato";  /* heh -KHS */
252                 news_item = N_NUKE_STOP;
253         }
254         sprintf(what,"%s %s",who, att_name);
255         irvlist = &foo;
256
257         /* get all hostile abms in range */
258         msl_sel(irvlist, x, y, bombown, wantflags, nowantflags, 0);
259         intlist = &intfoo;
260         emp_initque(intlist);
261         /* First choose interceptors belonging to the target sector */
262         /* only allow two defense missiles per missile attack */
263         for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
264                 next = qp->q_forw;
265                 ip = (struct plist *)qp;
266                 pp = &ip->plane;
267                 if (pp->pln_own != sect.sct_own)
268                         continue;
269                 pcp = ip->pcp;
270                 if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
271                         emp_remque(qp);
272                         free((s_char *)qp);
273                         continue;
274                 }
275                 /* got one interceptor, delete from irv_list and
276                  * add to  int_list.
277                  */
278                 emp_remque(qp);
279                 emp_insque(qp, intlist);
280                 putplane(pp->pln_uid, pp);
281                 icount++;
282         }
283         /* only allow two defense missiles per missile attack */
284         for (qp = irvlist->q_forw; qp != irvlist && icount < 2; qp = next) {
285                 next = qp->q_forw;
286                 ip = (struct plist *)qp;
287                 pp = &ip->plane;
288                 pcp = ip->pcp;
289                 if (mission_pln_equip(ip, 0, 0, 'i') < 0) {
290                         emp_remque(qp);
291                         free((s_char *)qp);
292                         continue;
293                 }
294                 /* got one interceptor, delete from irv_list and
295                  * add to  int_list.
296                  */
297                 emp_remque(qp);
298                 emp_insque(qp, intlist);
299                 putplane(pp->pln_uid, pp);
300                 icount++;
301         }
302         /* Now, clean out the queue */
303         while (!QEMPTY(irvlist)) {
304           qp = irvlist->q_forw;
305           emp_remque(qp);
306           free((s_char *)qp);
307         }
308         if (icount == 0) {
309                 if(sect.sct_own != 0)
310                 mpr(sect.sct_own, "No %ss launched to intercept.\n", def_name);
311                 return (destroyed);
312         }
313
314         /* attempt to destroy incoming missile */
315
316         while (!QEMPTY(intlist)) {
317           qp = intlist->q_forw;
318           ip = (struct plist *)qp;
319           pp = &ip->plane;
320           pcp = ip->pcp;
321
322           mpr(bombown,"%s %s launched in defense!\n",
323               cname(pp->pln_own), def_name);
324           if (sect.sct_own == pp->pln_own) {
325             mpr(sect.sct_own, "%s launched to intercept %s %s!\n", 
326                 def_name, who, att_name);
327           } else {
328             if (sect.sct_own)
329               mpr(sect.sct_own, "%s launched an %s to intercept the %s %s!\n",
330                   cname(pp->pln_own), def_name, who, att_name);
331             mpr(pp->pln_own, 
332              "%s launched to intercept %s %s arcing towards %s territory!\n",
333                 def_name, who, att_name, cname(sect.sct_own));
334           }
335                 
336           if (!destroyed &&
337               msl_hit(pp, hardtarget, EF_PLANE, news_item, news_item, what, x, y, bombown))  {
338             mpr(bombown, "%s destroyed by %s %s!\n", att_name,
339                 cname(pp->pln_own), def_name);
340             if (sect.sct_own)
341               mpr(sect.sct_own, "%s %s intercepted!\n", who, att_name);
342             if (sect.sct_own != pp->pln_own)
343               mpr(pp->pln_own, "%s %s intercepted!\n", who, att_name);
344             destroyed = 1;
345           }
346           /* zap the missile */
347           makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
348           pp->pln_own = 0;
349           putplane(pp->pln_uid, pp);
350           emp_remque(qp);
351           free((s_char *)qp);
352           if (destroyed)
353             break;
354         }
355         /* Clean out what is left in the list */
356         while (!QEMPTY(intlist)) {
357           qp = intlist->q_forw;
358           emp_remque(qp);
359           free((s_char *)qp);
360         }
361         if (destroyed)
362           return (destroyed);
363         if (icount) {
364           mpr(bombown,"%s made it through %s defenses!\n", att_name, def_name);
365           if (sect.sct_own)
366             mpr(sect.sct_own, "%s made it through %s defenses!\n", att_name,
367                 def_name);
368         }
369         return (destroyed);
370 }
371
372 /* Keep launching missiles on list until mindam damage has been done */
373 int
374 msl_launch_mindam(struct emp_qelem *list, coord x, coord y, int hardtarget, int type, int mindam, s_char *whatp, int victim, int mission)
375 {
376         struct  emp_qelem *qp;
377         struct  emp_qelem *next;
378         struct  plist *plp;
379         int     newdam, dam = 0;
380         int     nukedam = 0;
381         int     news_item;
382         int     snews_item;
383
384         if (type == EF_SHIP) {
385                 news_item = N_SHP_MISS;
386                 snews_item = N_SHP_SMISS;
387         } else if (type == EF_LAND) {
388                 news_item = N_LND_MISS;
389                 snews_item = N_LND_SMISS;
390         } else {
391                 news_item = N_SCT_MISS;
392                 snews_item = N_SCT_SMISS;
393         }
394         
395         for (qp = list->q_back;qp != list && dam < mindam && !nukedam;qp = next) {
396                 next = qp->q_back;
397                 plp = (struct plist *)qp;
398
399                 if (mission_pln_equip(plp, 0, 0, 'p') >= 0) {
400                         if (msl_hit(&plp->plane,
401                                     hardtarget, type, news_item, snews_item,
402                                     whatp, x, y, victim)) {
403                                 newdam = pln_damage(&plp->plane,x,y,'p',&nukedam, 1);
404                                 if (nukedam) {
405                                         if (mission == MI_INTERDICT &&
406                                             type == EF_SECTOR)
407                                                 dam += nukedam;
408                                 } else
409                                         dam += newdam;
410                         } else {
411                             /* Missiles that miss have to hit somewhere! */
412                             newdam = pln_damage(&plp->plane,x,y,'p',&nukedam, 0);
413                             collateral_damage(x, y, newdam, 0);
414                         }
415                         makelost(EF_PLANE, plp->plane.pln_own, plp->plane.pln_uid, plp->plane.pln_x, plp->plane.pln_y);
416                         plp->plane.pln_own = 0;
417                         putplane(plp->plane.pln_uid, &plp->plane);
418                         emp_remque(qp);
419                         free(qp);
420                 }
421         }
422         return dam;
423 }
424