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