]> git.pond.sub.org Git - empserver/blob - src/lib/commands/rada.c
8d5a39a2d4fd188c09ae1b249b063b55639b147d
[empserver] / src / lib / commands / rada.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  rada.c: Do radar from a ship/unit/sector
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2006
31  */
32
33 #include <config.h>
34
35 #include "commands.h"
36 #include "unit.h"
37
38 int
39 rada(void)
40 {
41     return radar(EF_SHIP);
42 }
43
44 int
45 lrad(void)
46 {
47     return radar(EF_LAND);
48 }
49
50 int
51 radar(int type)
52 {
53     char *cp;
54     double tf;
55     double tlev;
56     int spy;
57     struct nstr_item ni;
58     struct nstr_sect ns;
59     union empobj_storage item;
60     char buf[1024];
61     char prompt[80];
62
63     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
64         type = EF_SHIP;
65
66     sprintf(prompt, "Radar from (%s # or sector(s)) : ", ef_nameof(type));
67     cp = getstarg(player->argp[1], prompt, buf);
68
69     if (!cp)
70         return RET_SYN;
71     switch (sarg_type(cp)) {
72     case NS_AREA:
73         if (!snxtsct(&ns, cp))
74             return RET_SYN;
75         tlev = getnatp(player->cnum)->nat_level[NAT_TLEV];
76         while (nxtsct(&ns, &item.sect)) {
77             if (item.sect.sct_type != SCT_RADAR)
78                 continue;
79             if (!player->owner)
80                 continue;
81             radmap(item.sect.sct_x, item.sect.sct_y, item.sect.sct_effic,
82                    tlev, 16, 0.0);
83         }
84         break;
85     case NS_LIST:
86     case NS_GROUP:
87         /* assumes a NS_LIST return is a unit no */
88         if (!snxtitem(&ni, type, cp, NULL))
89             return RET_SYN;
90         while (nxtitem(&ni, &item)) {
91             if (!player->owner)
92                 continue;
93             tf = 0.0;
94             if (type == EF_SHIP) {
95                 if (mchr[(int)item.ship.shp_type].m_flags & M_SONAR)
96                     tf = techfact(item.ship.shp_tech, 1.0);
97                 spy = mchr[item.ship.shp_type].m_vrnge;
98             } else {
99                 if (!(lchr[(int)item.land.lnd_type].l_flags & L_RADAR)) {
100                     pr("%s can't use radar!\n", prland(&item.land));
101                     continue;
102                 }
103                 if (item.land.lnd_ship >= 0) {
104                     pr("%s is stowed on ship #%d, and can't use radar!\n",
105                        prland(&item.land), item.land.lnd_ship);
106                     continue;
107                 }
108                 if (item.land.lnd_land >= 0) {
109                     pr("%s is stowed on land #%d, and can't use radar!\n",
110                        prland(&item.land), item.land.lnd_land);
111                     continue;
112                 }
113                 spy = lchr[item.land.lnd_type].l_spy;
114             }
115
116             pr("%s at ", unit_nameof(&item.gen));
117             radmap(item.gen.x, item.gen.y, item.gen.effic,
118                    item.gen.tech, spy, tf);
119         }
120         break;
121     default:
122         pr("Must use a %s or sector specifier\n", ef_nameof(type));
123         return RET_SYN;
124     }
125     return RET_OK;
126 }