]> git.pond.sub.org Git - empserver/blob - src/lib/commands/marc.c
Update copyright notice.
[empserver] / src / lib / commands / marc.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, 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 empobj *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     leader = get_leader(&land_list);
73     leader_uid = leader->uid;
74     pr("Leader is %s\n", obj_nameof(leader));
75     if (player->argp[2]) {
76         strcpy(buf, player->argp[2]);
77         if (!(cp = lnd_path(together, (struct lndstr *)leader, 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             leader = get_leader(&land_list);
92             if (leader->uid != leader_uid) {
93                 leader_uid = leader->uid;
94                 pr_leader_change(leader);
95                 stopping = 1;
96                 continue;
97             }
98             if (!skip)
99                 nav_map(leader->x, leader->y, 1);
100             else
101                 skip = 0;
102             sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
103                     minmob, xyas(leader->x, leader->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             leader = get_leader(&land_list);
114             if (leader->uid != leader_uid) {
115                 leader_uid = leader->uid;
116                 pr_leader_change(leader);
117                 stopping = 1;
118                 continue;
119             }
120             if (cp && !(cp = lnd_path(together, (struct lndstr *)leader, 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", leader->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             continue;
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             leader = get_leader(&land_list);
156             if (leader->uid != leader_uid) {
157                 leader_uid = leader->uid;
158                 pr_leader_change(leader);
159             }
160             continue;
161         case 'i':
162             lnd_list(&land_list);
163             continue;
164         case 'm':
165             lnd_sweep(&land_list, 1, 1, player->cnum);
166             stopping |= lnd_check_mines(&land_list);
167             continue;
168         case 'r':
169             radar(EF_LAND);
170             skip = 1;
171             player->btused++;
172             continue;
173         case 'l':
174             llook();
175             player->btused++;
176             continue;
177         case 'd':
178             if (ac == 2) {
179                 player->argp[2] = player->argp[1];
180                 sprintf(dp, "%d", leader->uid);
181                 player->argp[1] = dp;
182             }
183             landmine();
184             skip = 1;
185             player->btused++;
186             continue;
187         }
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     return RET_OK;
195 }
196
197 void
198 pr_leader_change(struct empobj *leader)
199 {
200     pr("Changing %s to %s\n",
201         leader->ef_type == EF_SHIP ? "flagship" : "leader",
202         obj_nameof(leader));
203 }
204
205 struct empobj *
206 get_leader(struct emp_qelem *list)
207 {
208     return &((struct ulist *)(list->q_back))->unit.gen;
209 }
210
211 void
212 switch_leader(struct emp_qelem *list, int uid)
213 {
214     struct emp_qelem *qp, *save;
215     struct ulist *ulp;
216
217     if (QEMPTY(list))
218         return;
219
220     save = qp = list->q_back;
221     do {
222         emp_remque(qp);
223         emp_insque(qp, list);
224         qp = list->q_back;
225         ulp = (struct ulist *)qp;
226         if (ulp->unit.gen.uid == uid || uid == -1)
227             break;
228     } while (list->q_back != save);
229 }
230