]> git.pond.sub.org Git - empserver/blob - src/lib/commands/marc.c
40bb48b3301cd5fd28598d672fd7a02723eb09af
[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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 "misc.h"
39 #include "player.h"
40 #include "land.h"
41 #include "xy.h"
42 #include "nsc.h"
43 #include "path.h"
44 #include "file.h"
45 #include "map.h"
46 #include "commands.h"
47
48 static int set_leader(struct emp_qelem *list, struct lndstr **leaderp);
49
50 int
51 march(void)
52 {
53     struct nstr_item ni_land;
54     struct emp_qelem land_list;
55     double minmob, maxmob;
56     int together;
57     s_char *cp = 0;
58     struct lndstr *lnd = 0;     /* leader */
59     struct nstr_sect ns;
60     s_char origin;
61     int dir;
62     int stopping = 0;
63     int skip = 0;
64     s_char buf[1024];
65     s_char prompt[128];
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         s_char *bp, dp[80];
84
85         if (cp == 0 || *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         }
116         if (cp == 0 || *cp == '\0')
117             cp = &dirch[DIR_STOP];
118         if (*cp == 'M' ||
119             *cp == 'B' || *cp == 'f' || *cp == 'i' || *cp == 'm') {
120             ++cp;
121             if (cp[-1] == 'M') {
122                 unit_map(EF_LAND, lnd->lnd_uid, &ns, &origin);
123                 draw_map(0, origin, 0, &ns);
124                 skip = 1;
125             } else if (cp[-1] == 'B') {
126                 unit_map(EF_LAND, lnd->lnd_uid, &ns, &origin);
127                 draw_map('b', origin, 0, &ns);
128                 skip = 1;
129             } else if (cp[-1] == 'f') {
130                 struct emp_qelem *qp;
131                 qp = land_list.q_back;
132                 emp_remque(land_list.q_back);
133                 emp_insque(qp, &land_list);
134                 set_leader(&land_list, &lnd);
135             } else if (cp[-1] == 'i') {
136                 lnd_list(&land_list);
137             } else {
138                 lnd_sweep(&land_list, 1, 1, player->cnum);
139                 stopping |= lnd_check_mines(&land_list);
140             }
141             continue;
142         } else if (*cp == 'r' || *cp == 'l') {
143             bp = ++cp;
144             while ((*bp != ' ') && (*bp))
145                 bp++;
146             while ((*bp == ' ') && (*bp))
147                 bp++;
148             if ((bp != (s_char *)0) && (*bp))
149                 player->argp[1] = bp;
150             else {
151                 sprintf(dp, "%d", lnd->lnd_uid);
152                 player->argp[1] = dp;
153             }
154             if (cp[-1] == 'r') {
155                 player->argp[0] = "lradar";
156                 rada();
157                 skip = 1;
158             } else
159                 llook();
160             *cp = 0;
161             player->btused++;
162             continue;
163         } else {
164             dir = chkdir(*cp++, DIR_STOP, DIR_LAST);
165             if (dir < 0) {
166                 if (NULL != (cp = lnd_path(together, lnd, buf)))
167                     continue;
168                 direrr("`%c' to stop", 0, 0);
169                 pr(", `i' to list units, `f' to change leader,\n");
170                 pr("`r' to radar, `l' to look, `M' to map, `B' to bmap,\n");
171                 pr("and `m' to minesweep\n");
172                 stopping = 1;
173                 continue;
174             }
175         }
176         stopping |=
177             lnd_mar_one_sector(&land_list, dir, player->cnum, together);
178     }
179     return RET_OK;
180 }
181
182 static int
183 set_leader(struct emp_qelem *list, struct lndstr **leaderp)
184 {
185     struct llist *llp = (struct llist *)(list->q_back);
186
187     if (!*leaderp)
188         pr("Leader is ");
189     else if ((*leaderp)->lnd_uid != llp->land.lnd_uid)
190         pr("Changing leader to ");
191     else
192         return 0;
193     *leaderp = &llp->land;
194     pr("%s\n", prland(&llp->land));
195     return 1;
196 }