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