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