]> git.pond.sub.org Git - empserver/blob - src/lib/commands/fly.c
New get_planes(), factored out of plane flying commands
[empserver] / src / lib / commands / fly.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  fly.c: fly a plane
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include "commands.h"
38 #include "item.h"
39 #include "path.h"
40 #include "plane.h"
41 #include "ship.h"
42
43 int
44 fly(void)
45 {
46     int mission_flags;
47     coord tx, ty;
48     coord ax, ay;
49     int ap_to_target;
50     struct ichrstr *ip;
51     char flightpath[MAX_PATH_LEN];
52     int cno;
53     struct nstr_item ni_bomb;
54     struct nstr_item ni_esc;
55     struct sctstr target;
56     struct emp_qelem bomb_list;
57     struct emp_qelem esc_list;
58     int wantflags;
59     struct sctstr ap_sect;
60     char buf[1024];
61
62     wantflags = 0;
63     if (get_planes(&ni_bomb, &ni_esc, player->argp[1], player->argp[2]) < 0)
64         return RET_SYN;
65     if (!get_assembly_point(player->argp[3], &ap_sect, buf))
66         return RET_SYN;
67     ax = ap_sect.sct_x;
68     ay = ap_sect.sct_y;
69     if (getpath(flightpath, player->argp[4], ax, ay, 0, 0, P_FLYING) == 0
70         || *flightpath == 0)
71         return RET_SYN;
72     tx = ax;
73     ty = ay;
74     (void)pathtoxy(flightpath, &tx, &ty, fcost);
75     pr("Ending sector is %s\n", xyas(tx, ty, player->cnum));
76     ip = whatitem(player->argp[5], "transport what? ");
77     if (player->aborted)
78         return RET_SYN;
79     getsect(tx, ty, &target);
80
81     cno = -1;
82     if (pln_onewaymission(&target, &cno, &wantflags) < 0)
83         return RET_SYN;
84
85     if (ip && ip->i_uid == I_CIVIL && target.sct_own != target.sct_oldown) {
86         pr("Can't fly civilians into occupied sectors.\n");
87         return RET_FAIL;
88     }
89
90     ap_to_target = strlen(flightpath);
91     if (flightpath[ap_to_target - 1] == 'h')
92         ap_to_target--;
93     pr("range to target is %d\n", ap_to_target);
94     /*
95      * select planes within range
96      */
97     mission_flags = 0;
98     pln_sel(&ni_bomb, &bomb_list, &ap_sect, ap_to_target,
99             1, wantflags, P_M | P_O);
100     if (QEMPTY(&bomb_list)) {
101         pr("No planes could be equipped for the mission.\n");
102         return RET_FAIL;
103     }
104     wantflags |= P_F;
105     wantflags |= P_ESC;
106     pln_sel(&ni_esc, &esc_list, &ap_sect, ap_to_target,
107             1, wantflags, P_M | P_O);
108     if (cno >= 0 && !pln_oneway_to_carrier_ok(&bomb_list, &esc_list, cno)) {
109         pr("Not enough room on ship #%d!\n", cno);
110         return RET_FAIL;
111     }
112     /*
113      * now arm and equip the bombers, transports, whatever.
114      */
115     mission_flags |= P_X;       /* stealth (shhh) */
116     mission_flags |= P_H;       /* gets turned off if not all choppers */
117     mission_flags = pln_arm(&bomb_list, ap_to_target, 't',
118                             ip, 0, mission_flags);
119     if (QEMPTY(&bomb_list)) {
120         pr("No planes could be equipped for the mission.\n");
121         return RET_FAIL;
122     }
123     mission_flags = pln_arm(&esc_list, ap_to_target, 't',
124                             ip, P_ESC | P_F, mission_flags);
125     ac_encounter(&bomb_list, &esc_list, ax, ay,
126                  flightpath, mission_flags, 0);
127     if (QEMPTY(&bomb_list)) {
128         pr("No planes got through fighter defenses\n");
129     } else {
130         pln_dropoff(&bomb_list, ip, tx, ty, cno);
131         pln_newlanding(&bomb_list, tx, ty, cno);
132         pln_newlanding(&esc_list, tx, ty, cno);
133     }
134     pln_put(&bomb_list);
135     pln_put(&esc_list);
136     return RET_OK;
137 }