]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/rada.c
Rename obj_nameof() to unit_nameof() and move to unitsub.c
[empserver] / src / lib / commands / rada.c
index 4cadf25b73d66045672454bb67aa8c42c6715cba..d21bbebb12e4cc91ba02d71e7fe60a9c9f8559c8 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  rada.c: Do radar from a ship/unit/sector
- * 
+ *
  *  Known contributors to this file:
- *     
+ *     Ron Koenderink, 2006
  */
 
-#include <ctype.h>
-#include "misc.h"
-#include "player.h"
-#include "xy.h"
-#include "var.h"
-#include "ship.h"
-#include "land.h"
-#include "sect.h"
-#include "nsc.h"
-#include "nat.h"
-#include "file.h"
+#include <config.h>
+
 #include "commands.h"
 #include "optlist.h"
+#include "unit.h"
 
 int
 rada(void)
 {
-       s_char  *cp;
-       int     type;
-       double  tf;
-       double  tech;
-       struct  nstr_item ni;
-       struct  nstr_sect ns;
-       struct  shpstr ship;
-       struct  lndstr land;
-       struct  sctstr sect;
-       int     from_unit;
-       s_char  buf[1024];
+    return radar(EF_SHIP);
+}
 
-       from_unit = (**player->argp == 'l');
+int
+lrad(void)
+{
+    return radar(EF_LAND);
+}
 
-       if (!from_unit)
-               cp = getstarg(player->argp[1], "Radar from (ship # or sector(s)) : ", buf);
-       else
-               cp = getstarg(player->argp[1], "Radar from (unit # or sector(s)) : ", buf);
-       if (cp == 0)
-               return RET_SYN;
-       type = sarg_type(cp);
-       if (type == NS_AREA) {
-               if (!snxtsct(&ns, cp))
-                       return RET_SYN;
-               tech = tfact(player->cnum, 8.0);
-               if (tech > ((double)WORLD_Y/4.0))
-                       tech = ((double)WORLD_Y/4.0);
-               if (tech > ((double)WORLD_X/8.0))
-                       tech = ((double)WORLD_X/8.0);
-               while (nxtsct(&ns, &sect)) {
-                       if (sect.sct_type != SCT_RADAR)
-                               continue;
-                       if (!player->owner)
-                               continue;
-                       radmap(sect.sct_x, sect.sct_y, (int)sect.sct_effic,
-                               (int) (tech * 2.0), 0.0);
+int
+radar(int type)
+{
+    char *cp;
+    double tf;
+    double tlev;
+    int spy;
+    struct nstr_item ni;
+    struct nstr_sect ns;
+    union empobj_storage item;
+    char buf[1024];
+    char prompt[80];
+
+    if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
+       type = EF_SHIP;
+
+    sprintf(prompt, "Radar from (%s # or sector(s)) : ", ef_nameof(type));
+    cp = getstarg(player->argp[1], prompt, buf);
+
+    if (!cp)
+       return RET_SYN;
+    switch (sarg_type(cp)) {
+    case NS_AREA:
+       if (!snxtsct(&ns, cp))
+           return RET_SYN;
+       tlev = getnatp(player->cnum)->nat_level[NAT_TLEV];
+       while (nxtsct(&ns, &item.sect)) {
+           if (item.sect.sct_type != SCT_RADAR)
+               continue;
+           if (!player->owner)
+               continue;
+           radmap(item.sect.sct_x, item.sect.sct_y, item.sect.sct_effic,
+                  tlev, 16, 0.0);
+       }
+       break;
+    case NS_LIST:
+    case NS_GROUP:
+       /* assumes a NS_LIST return is a unit no */
+       if (!snxtitem(&ni, type, cp, NULL))
+           return RET_SYN;
+       while (nxtitem(&ni, &item)) {
+           if (!player->owner)
+               continue;
+           tf = 0.0;
+           if (type == EF_SHIP) {
+               if (mchr[(int)item.ship.shp_type].m_flags & M_SONAR)
+                   tf = techfact(item.ship.shp_tech, 1.0);
+               spy = mchr[item.ship.shp_type].m_vrnge;
+           } else {
+               if (!(lchr[(int)item.land.lnd_type].l_flags & L_RADAR)) {
+                   pr("%s can't use radar!\n", prland(&item.land));
+                   continue;
                }
-       } else if (type == NS_LIST || type == NS_GROUP) {
-               if (!from_unit){
-                       /* assumes a NS_LIST return is a shipno */
-                       if (!snxtitem(&ni, EF_SHIP, cp)) {
-                               pr("Specify at least one ship\n");
-                               return RET_SYN;
-                       }
-                       while (nxtitem(&ni, (s_char *)&ship)) {
-                               if (!player->owner)
-                                       continue;
-                               if (mchr[(int)ship.shp_type].m_flags & M_SONAR)
-                                       tf = techfact(ship.shp_tech, 1.0);
-                               else
-                                       tf = 0.0;
-                               pr("%s at ", prship(&ship));
-                               tech = techfact(ship.shp_tech,
-                                       (double) mchr[(int)ship.shp_type].m_vrnge);
-                               if (tech > ((double)WORLD_Y/2.0))
-                                       tech = ((double)WORLD_Y/2.0);
-                               if (tech > ((double)WORLD_X/4.0))
-                                       tech = ((double)WORLD_X/4.0);
-                               radmap(ship.shp_x, ship.shp_y, ship.shp_effic,
-                                       (int) tech, tf);
-                       }
-               }else{
-                       /* from a land unit */
-                       if (!snxtitem(&ni, EF_LAND, cp)) {
-                               pr("Specify at least one unit\n");
-                               return RET_SYN;
-                       }
-                       while (nxtitem(&ni, (s_char *)&land)) {
-                               if (!player->owner)
-                                       continue;
-                               if (!(lchr[(int)land.lnd_type].l_flags & L_RADAR)){
-                                       pr("%s can't use radar!\n",
-                                          prland(&land));
-                                       continue;
-                               }
-                               if (land.lnd_ship >= 0){
-                                       pr("Units on ships can't use radar!\n");
-                                       continue;
-                               }
-                               tf = 0.0;
-                               pr("%s at ", prland(&land));
-                               tech = techfact(land.lnd_tech,
-                                       (double) land.lnd_spy);
-                               if (tech > ((double)WORLD_Y/2.0))
-                                       tech = ((double)WORLD_Y/2.0);
-                               if (tech > ((double)WORLD_X/4.0))
-                                       tech = ((double)WORLD_X/4.0);
-                               radmap(land.lnd_x, land.lnd_y, land.lnd_effic,
-                                       (int) tech, tf);
-                       }
+               if (item.land.lnd_ship >= 0) {
+                   pr("Units on ships can't use radar!\n");
+                   continue;
                }
-       } else {
-               if (!from_unit)
-                       pr("Must use a ship or sector specifier\n");
-               else
-                       pr("Must use a unit or sector specifier\n");
-               return RET_SYN;
+               spy = lchr[item.land.lnd_type].l_spy;
+           }
+
+           pr("%s at ", unit_nameof(&item.gen));
+           radmap(item.gen.x, item.gen.y, item.gen.effic,
+                  item.gen.tech, spy, tf);
        }
-       return RET_OK;
+       break;
+    default:
+       pr("Must use a %s or sector specifier\n", ef_nameof(type));
+       return RET_SYN;
+    }
+    return RET_OK;
 }