]> git.pond.sub.org Git - empserver/blob - src/lib/commands/marc.c
(pr_leader_change, get_leader, switch_leader): Move
[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  *     Ron Koenderink, 2006-2007
34  */
35
36 #include <config.h>
37
38 #include <ctype.h>
39 #include "commands.h"
40 #include "file.h"
41 #include "map.h"
42 #include "path.h"
43 #include "empobj.h"
44 #include "unit.h"
45
46 int
47 march(void)
48 {
49     struct nstr_item ni_land;
50     struct emp_qelem land_list;
51     double minmob, maxmob;
52     int together;
53     char *cp = NULL;
54     int leader_uid;
55     struct empobj *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     leader = get_leader(&land_list);
74     leader_uid = leader->uid;
75     pr("Leader is %s\n", obj_nameof(leader));
76     if (player->argp[2]) {
77         strcpy(buf, player->argp[2]);
78         if (!(cp = lnd_path(together, (struct lndstr *)leader, buf)))
79             cp = player->argp[2];
80     }
81
82     while (!QEMPTY(&land_list)) {
83         char dp[80];
84
85         if (cp == NULL || *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             leader = get_leader(&land_list);
93             if (leader->uid != leader_uid) {
94                 leader_uid = leader->uid;
95                 pr_leader_change(leader);
96                 stopping = 1;
97                 continue;
98             }
99             if (!skip)
100                 nav_map(leader->x, leader->y, 1);
101             else
102                 skip = 0;
103             sprintf(prompt, "<%.1f:%.1f: %s> ", maxmob,
104                     minmob, xyas(leader->x, leader->y, player->cnum));
105             cp = getstring(prompt, buf);
106 /* Just in case any of our lands were shelled while we were at the
107  * prompt, we call lnd_mar() again.
108  */
109             lnd_mar(&land_list, &minmob, &maxmob, &together, player->cnum);
110             if (QEMPTY(&land_list)) {
111                 pr("No lands left\n");
112                 return RET_OK;
113             }
114             leader = get_leader(&land_list);
115             if (leader->uid != leader_uid) {
116                 leader_uid = leader->uid;
117                 pr_leader_change(leader);
118                 stopping = 1;
119                 continue;
120             }
121             if (cp && !(cp = lnd_path(together, (struct lndstr *)leader, buf)))
122                 cp = buf;
123         }
124         if (cp == NULL || *cp == '\0')
125             cp = &dirch[DIR_STOP];
126         dir = chkdir(*cp, DIR_STOP, DIR_LAST);
127         if (dir >= 0) {
128             stopping |=
129                 lnd_mar_one_sector(&land_list, dir, player->cnum, together);
130             cp++;
131             continue;
132         }
133         ac = parse(cp, player->argp, NULL, scanspace, NULL);
134         if (ac <= 1) {
135             sprintf(dp, "%d", leader->uid);
136             player->argp[1] = dp;
137             cp++;
138         } else
139             cp = NULL;
140         bmap_flag = 0;
141         switch (*player->argp[0]) {
142         case 'B':
143             bmap_flag = 'b';
144             /*
145              * fall through
146              */
147         case 'M':
148             do_map(bmap_flag, EF_LAND, player->argp[1], player->argp[2]);
149             skip = 1;
150             continue;
151         case 'f':
152             if (ac <= 1)
153                 switch_leader(&land_list, -1);
154             else
155                 switch_leader(&land_list, atoi(player->argp[1]));
156             leader = get_leader(&land_list);
157             if (leader->uid != leader_uid) {
158                 leader_uid = leader->uid;
159                 pr_leader_change(leader);
160             }
161             continue;
162         case 'i':
163             lnd_list(&land_list);
164             continue;
165         case 'm':
166             lnd_sweep(&land_list, 1, 1, player->cnum);
167             stopping |= lnd_check_mines(&land_list);
168             continue;
169         case 'r':
170             radar(EF_LAND);
171             skip = 1;
172             player->btused++;
173             continue;
174         case 'l':
175             llook();
176             player->btused++;
177             continue;
178         case 'd':
179             if (ac == 2) {
180                 player->argp[2] = player->argp[1];
181                 sprintf(dp, "%d", leader->uid);
182                 player->argp[1] = dp;
183             }
184             landmine();
185             skip = 1;
186             player->btused++;
187             continue;
188         }
189         direrr("`%c' to stop", 0, 0);
190         pr(", `i' to list units, `f' to change leader,\n");
191         pr("`r' to radar, `l' to look, `M' to map, `B' to bmap,\n");
192         pr("`d' to drop mines, and `m' to minesweep\n");
193         stopping = 1;
194     }
195     return RET_OK;
196 }