src/lib/subs/unitsub.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  unitsub.c: Common subroutines for multiple type of units
00029  * 
00030  *  Known contributors to this file:
00031  *     Ron Koenderink, 2007
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "empobj.h"
00037 #include "file.h"
00038 #include "player.h"
00039 #include "prototypes.h"
00040 #include "unit.h"
00041 
00042 void
00043 unit_list(struct emp_qelem *unit_list)
00044 {
00045     struct emp_qelem *qp;
00046     struct emp_qelem *next;
00047     struct ulist *ulp;
00048     struct empobj *unit;
00049     struct lndstr *lnd;
00050     struct shpstr *shp;
00051 
00052     CANT_HAPPEN(QEMPTY(unit_list));
00053 
00054     qp = unit_list->q_back;
00055     ulp = (struct ulist *)qp;
00056 
00057     if (ulp->unit.ef_type == EF_LAND)
00058         pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
00059     else
00060         pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
00061 
00062     for (; qp != unit_list; qp = next) {
00063         next = qp->q_back;
00064         ulp = (struct ulist *)qp;
00065         lnd = &ulp->unit.land;
00066         shp = &ulp->unit.ship;
00067         unit = &ulp->unit.gen;
00068         pr("%4d ", unit->uid);
00069         pr("%-16.16s ", emp_obj_chr_name(unit));
00070         prxy("%4d,%-4d ", unit->x, unit->y, unit->own);
00071         pr("%1.1s", &unit->group);
00072         pr("%4d%%", unit->effic);
00073         if (unit->ef_type == EF_LAND) {
00074             pr("%4d", lnd->lnd_item[I_SHELL]);
00075             pr("%4d", lnd->lnd_item[I_GUN]);
00076             count_land_planes(lnd);
00077             pr("%3d", lnd->lnd_nxlight);
00078         } else {
00079             pr("%4d", shp->shp_item[I_MILIT]);
00080             pr("%4d", shp->shp_item[I_SHELL]);
00081             pr("%4d", shp->shp_item[I_GUN]);
00082             count_planes(shp);
00083             pr("%3d", shp->shp_nplane);
00084             pr("%3d", shp->shp_nchoppers);
00085             pr("%3d", shp->shp_nxlight);
00086             count_units(shp);
00087             pr("%3d", shp->shp_nland);
00088         }
00089         pr("%4d", unit->mobil);
00090         pr("%4d", unit->tech);
00091         if (unit->ef_type == EF_LAND) {
00092             pr("%4d%%", lnd->lnd_retreat);
00093             pr("%5d", lnd->lnd_fuel);
00094         }
00095         pr("\n");
00096     }
00097 }
00098 
00099 void
00100 unit_put(struct emp_qelem *list, natid actor)
00101 {
00102     struct emp_qelem *qp;
00103     struct emp_qelem *newqp;
00104     struct ulist *ulp;
00105 
00106     qp = list->q_back;
00107     while (qp != list) {
00108         ulp = (struct ulist *)qp;
00109         if (actor) {
00110             mpr(actor, "%s stopped at %s\n", obj_nameof(&ulp->unit.gen),
00111                 xyas(ulp->unit.gen.x, ulp->unit.gen.y,
00112                      ulp->unit.gen.own));
00113             if (ulp->unit.ef_type == EF_LAND) {
00114                 if (ulp->mobil < -127)
00115                     ulp->mobil = -127;
00116                 ulp->unit.land.lnd_mobil = ulp->mobil;
00117             }
00118         }
00119         if (ulp->unit.ef_type == EF_SHIP)
00120             ulp->unit.ship.shp_mobil = (int)ulp->mobil;
00121         put_empobj(&ulp->unit.gen);
00122         newqp = qp->q_back;
00123         emp_remque(qp);
00124         free(qp);
00125         qp = newqp;
00126     }
00127 }
00128 
00129 char *
00130 unit_path(int together, struct empobj *unit, char *buf)
00131 {
00132     coord destx;
00133     coord desty;
00134     struct sctstr d_sect, sect;
00135     char *cp;
00136     double dummy;
00137     int mtype;
00138 
00139     if (!sarg_xy(buf, &destx, &desty))
00140         return 0;
00141     if (!together) {
00142         pr("Cannot go to a destination sector if not all starting in the same sector\n");
00143         return 0;
00144     }
00145     if (!getsect(destx, desty, &d_sect)) {
00146         pr("%d,%d is not a sector\n", destx, desty);
00147         return 0;
00148     }
00149     if (unit->ef_type == EF_SHIP) {
00150         cp = BestShipPath(buf, unit->x, unit->y,
00151                           d_sect.sct_x, d_sect.sct_y, player->cnum);
00152         if (!cp || unit->mobil <= 0) {
00153             pr("Can't get to '%s' right now.\n",
00154                 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
00155             return 0;
00156         }
00157     } else {
00158         getsect(unit->x, unit->y, &sect);
00159         mtype = lnd_mobtype((struct lndstr *)unit);
00160         cp = BestLandPath(buf, &sect, &d_sect, &dummy, mtype);
00161         if (!cp) {
00162             pr("No owned %s from %s to %s!\n",
00163                mtype == MOB_RAIL ? "railway" : "path",
00164                xyas(unit->x, unit->y, player->cnum),
00165                xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
00166             return 0;
00167         }
00168         pr("Using path '%s'\n", cp);
00169     }
00170     return cp;
00171 }
00172 
00173 void
00174 unit_view(struct emp_qelem *list)
00175 {
00176     struct sctstr sect;
00177     struct emp_qelem *qp;
00178     struct emp_qelem *next;
00179     struct ulist *ulp;
00180 
00181     for (qp = list->q_back; qp != list; qp = next) {
00182         next = qp->q_back;
00183         ulp = (struct ulist *)qp;
00184         getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
00185         if (ulp->unit.ef_type == EF_SHIP) {
00186             if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
00187                 pr("[fert:%d] ", sect.sct_fertil);
00188             if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
00189                 pr("[oil:%d] ", sect.sct_oil);
00190         }
00191         pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
00192             xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
00193             sect.sct_effic, dchr[sect.sct_type].d_name);
00194     }
00195 }

Generated on Fri Mar 28 11:01:16 2008 for empserver by  doxygen 1.5.2