]> git.pond.sub.org Git - empserver/blob - src/lib/commands/marc.c
Break inclusion cycle: prototypes.h and commands.h included each
[empserver] / src / lib / commands / marc.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, 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  *  marc.c: March units around
29  * 
30  *  Known contributors to this file:
31  *     Thomas Ruschak
32  *     Ken Stevens, 1995 (rewrite)
33  */
34
35 #include <config.h>
36
37 #include <ctype.h>
38 #include "commands.h"
39 #include "file.h"
40 #include "land.h"
41 #include "map.h"
42 #include "path.h"
43
44 static int set_leader(struct emp_qelem *list, struct lndstr **leaderp);
45 static void switch_leader(struct emp_qelem *list, int land_uid);
46
47 int
48 march(void)
49 {
50     struct nstr_item ni_land;
51     struct emp_qelem land_list;
52     double minmob, maxmob;
53     int together;
54     char *cp = NULL;
55     struct lndstr *lnd = NULL;  /* leader */
56     struct nstr_sect ns;
57     char origin;
58     int dir;
59     int stopping = 0;
60     int skip = 0;
61     char buf[1024];
62     char prompt[128];
63     char scanspace[1024];
64     char bmap_flag;
65     int ac;
66
67     if (!snxtitem(&ni_land, EF_LAND, player->argp[1]))
68         return RET_SYN;
69     lnd_sel(&ni_land, &land_list);
70     lnd_mar(&land_list, &minmob, &maxmob, &together, player->cnum);
71     if (QEMPTY(&land_list)) {
72         pr("No lands\n");
73         return RET_FAIL;
74     }
75     set_leader(&land_list, &lnd);
76     if (player->argp[2]) {
77         strcpy(buf, player->argp[2]);
78         if (!(cp = lnd_path(together, lnd, buf)))
79             cp = player->argp[2];
80     }
81
82     while (!QEMPTY(&land_list)) {
83         char dp[80];
84
85         if (cp == NULL || *cp == '\0' || stopping) {
86             stopping = 0;
87             lnd_mar(&land_list, &minmob, &maxmob, &together, player->cnum);
88             if (QEMPTY(&land_list)) {
89                 pr("No lands left\n");
90                 return RET_OK;
91             }
92             if (set_leader(&land_list, &lnd)) {
93                 stopping = 1;
94                 continue;
95             }
96             if (!skip)
97                 nav_map(lnd->lnd_x, lnd->lnd_y, 1);
98             else
99                 skip = 0;
100             sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
101                     minmob, xyas(lnd->lnd_x, lnd->lnd_y, player->cnum));
102             cp = getstring(prompt, buf);
103 /* Just in case any of our lands were shelled while we were at the
104  * prompt, we call lnd_mar() again.
105  */
106             lnd_mar(&land_list, &minmob, &maxmob, &together, player->cnum);
107             if (QEMPTY(&land_list)) {
108                 pr("No lands left\n");
109                 return RET_OK;
110             }
111             if (set_leader(&land_list, &lnd)) {
112                 stopping = 1;
113                 continue;
114             }
115             if (cp && !(cp = lnd_path(together, lnd, buf)))
116                 cp = buf;
117         }
118         if (cp == NULL || *cp == '\0')
119             cp = &dirch[DIR_STOP];
120         dir = chkdir(*cp, DIR_STOP, DIR_LAST);
121         if (dir >= 0) {
122             stopping |=
123                 lnd_mar_one_sector(&land_list, dir, player->cnum, together);
124             cp++;
125             continue;
126         }
127         ac = parse(cp, player->argp, NULL, scanspace, NULL);
128         if (ac <= 1) {
129             sprintf(dp, "%d", lnd->lnd_uid);
130             player->argp[1] = dp;
131             cp++;
132         } else
133             cp = NULL;
134         bmap_flag = 0;
135         switch (*player->argp[0]) {
136         case 'B':
137             bmap_flag = 'b';
138             /*
139              * fall through
140              */
141         case 'M':
142             unit_map(EF_LAND, atoi(player->argp[1]), &ns, &origin);
143             draw_map(bmap_flag, origin, 0, &ns);
144             skip = 1;
145             break;
146         case 'f':
147             if (ac <= 1)
148                 switch_leader(&land_list, -1);
149             else
150                 switch_leader(&land_list, atoi(player->argp[1]));
151             set_leader(&land_list, &lnd);
152             break;
153         case 'i':
154             lnd_list(&land_list);
155             break;
156         case 'm':
157             lnd_sweep(&land_list, 1, 1, player->cnum);
158             stopping |= lnd_check_mines(&land_list);
159             break;
160         case 'r':
161             player->argp[0] = "lradar";
162             rada();
163             skip = 1;
164             player->btused++;
165             break;
166         case 'l':
167             llook();
168             player->btused++;
169             break;
170         case 'd':
171             if (ac == 2) {
172                 player->argp[2] = player->argp[1];
173                 sprintf(dp, "%d", lnd->lnd_uid);
174                 player->argp[1] = dp;
175             }
176             landmine();
177             skip = 1;
178             player->btused++;
179             break;
180         default:
181             direrr("`%c' to stop", 0, 0);
182             pr(", `i' to list units, `f' to change leader,\n");
183             pr("`r' to radar, `l' to look, `M' to map, `B' to bmap,\n");
184             pr("`d' to drop mines, and `m' to minesweep\n");
185             stopping = 1;
186         }
187     }
188     return RET_OK;
189 }
190
191 static int
192 set_leader(struct emp_qelem *list, struct lndstr **leaderp)
193 {
194     struct llist *llp = (struct llist *)(list->q_back);
195
196     if (!*leaderp)
197         pr("Leader is ");
198     else if ((*leaderp)->lnd_uid != llp->land.lnd_uid)
199         pr("Changing leader to ");
200     else
201         return 0;
202     *leaderp = &llp->land;
203     pr("%s\n", prland(&llp->land));
204     return 1;
205 }
206
207 static void
208 switch_leader(struct emp_qelem *list, int land_uid)
209 {
210     struct emp_qelem *qp, *save;
211     struct llist *llp;
212
213     if (QEMPTY(list))
214         return;
215
216     save = qp = list->q_back;
217     do {
218         emp_remque(qp);
219         emp_insque(qp, list);
220         qp = list->q_back;
221         llp = (struct llist *)qp;
222         if (llp->land.lnd_uid == land_uid || land_uid == -1)
223             break;
224     } while (list->q_back != save);
225 }
226