]> git.pond.sub.org Git - empserver/blob - src/lib/subs/unitsub.c
ebc341c573449e42b9591f5f10b5339cc699b70c
[empserver] / src / lib / subs / unitsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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  *  unitsub.c: Common subroutines for multiple type of units
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2007
32  */
33
34 #include <config.h>
35
36 #include "empobj.h"
37 #include "file.h"
38 #include "player.h"
39 #include "prototypes.h"
40 #include "unit.h"
41
42 void
43 unit_list(struct emp_qelem *unit_list)
44 {
45     struct emp_qelem *qp;
46     struct emp_qelem *next;
47     struct ulist *ulp;
48     struct empobj *unit;
49     struct lndstr *lnd;
50     struct shpstr *shp;
51
52     CANT_HAPPEN(QEMPTY(unit_list));
53
54     qp = unit_list->q_back;
55     ulp = (struct ulist *)qp;
56
57     if (ulp->unit.ef_type == EF_LAND)
58         pr("lnd#     land type       x,y    a  eff  sh gun xl  mu tech retr fuel\n");
59     else
60         pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
61
62     for (; qp != unit_list; qp = next) {
63         next = qp->q_back;
64         ulp = (struct ulist *)qp;
65         lnd = &ulp->unit.land;
66         shp = &ulp->unit.ship;
67         unit = &ulp->unit.gen;
68         pr("%4d ", unit->uid);
69         pr("%-16.16s ", emp_obj_chr_name(unit));
70         prxy("%4d,%-4d ", unit->x, unit->y, unit->own);
71         pr("%1.1s", &unit->group);
72         pr("%4d%%", unit->effic);
73         if (unit->ef_type == EF_LAND) {
74             pr("%4d", lnd->lnd_item[I_SHELL]);
75             pr("%4d", lnd->lnd_item[I_GUN]);
76             count_land_planes(lnd);
77             pr("%3d", lnd->lnd_nxlight);
78         } else {
79             pr("%4d", shp->shp_item[I_MILIT]);
80             pr("%4d", shp->shp_item[I_SHELL]);
81             pr("%4d", shp->shp_item[I_GUN]);
82             count_planes(shp);
83             pr("%3d", shp->shp_nplane);
84             pr("%3d", shp->shp_nchoppers);
85             pr("%3d", shp->shp_nxlight);
86             count_units(shp);
87             pr("%3d", shp->shp_nland);
88         }
89         pr("%4d", unit->mobil);
90         pr("%4d", unit->tech);
91         if (unit->ef_type == EF_LAND) {
92             pr("%4d%%", lnd->lnd_retreat);
93             pr("%5d", lnd->lnd_fuel);
94         }
95         pr("\n");
96     }
97 }
98
99 void
100 unit_put(struct emp_qelem *list, natid actor)
101 {
102     struct emp_qelem *qp;
103     struct emp_qelem *newqp;
104     struct ulist *ulp;
105
106     qp = list->q_back;
107     while (qp != list) {
108         ulp = (struct ulist *)qp;
109         if (actor) {
110             mpr(actor, "%s stopped at %s\n", obj_nameof(&ulp->unit.gen),
111                 xyas(ulp->unit.gen.x, ulp->unit.gen.y,
112                      ulp->unit.gen.own));
113             if (ulp->unit.ef_type == EF_LAND) {
114                 if (ulp->mobil < -127)
115                     ulp->mobil = -127;
116                 ulp->unit.land.lnd_mobil = ulp->mobil;
117             }
118         }
119         if (ulp->unit.ef_type == EF_SHIP)
120             ulp->unit.ship.shp_mobil = (int)ulp->mobil;
121         put_empobj(&ulp->unit.gen);
122         newqp = qp->q_back;
123         emp_remque(qp);
124         free(qp);
125         qp = newqp;
126     }
127 }
128
129 char *
130 unit_path(int together, struct empobj *unit, char *buf)
131 {
132     coord destx;
133     coord desty;
134     struct sctstr d_sect, sect;
135     char *cp;
136     double dummy;
137     int mtype;
138
139     if (!sarg_xy(buf, &destx, &desty))
140         return 0;
141     if (!together) {
142         pr("Cannot go to a destination sector if not all starting in the same sector\n");
143         return 0;
144     }
145     if (!getsect(destx, desty, &d_sect)) {
146         pr("%d,%d is not a sector\n", destx, desty);
147         return 0;
148     }
149     if (unit->ef_type == EF_SHIP) {
150         cp = BestShipPath(buf, unit->x, unit->y,
151                           d_sect.sct_x, d_sect.sct_y, player->cnum);
152         if (!cp || unit->mobil <= 0) {
153             pr("Can't get to '%s' right now.\n",
154                 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
155             return 0;
156         }
157     } else {
158         getsect(unit->x, unit->y, &sect);
159         mtype = lnd_mobtype((struct lndstr *)unit);
160         cp = BestLandPath(buf, &sect, &d_sect, &dummy, mtype);
161         if (!cp) {
162             pr("No owned %s from %s to %s!\n",
163                mtype == MOB_RAIL ? "railway" : "path",
164                xyas(unit->x, unit->y, player->cnum),
165                xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
166             return 0;
167         }
168         pr("Using path '%s'\n", cp);
169     }
170     return cp;
171 }
172
173 void
174 unit_view(struct emp_qelem *list)
175 {
176     struct sctstr sect;
177     struct emp_qelem *qp;
178     struct emp_qelem *next;
179     struct ulist *ulp;
180
181     for (qp = list->q_back; qp != list; qp = next) {
182         next = qp->q_back;
183         ulp = (struct ulist *)qp;
184         getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
185         if (ulp->unit.ef_type == EF_SHIP) {
186             if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
187                 pr("[fert:%d] ", sect.sct_fertil);
188             if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
189                 pr("[oil:%d] ", sect.sct_oil);
190         }
191         pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
192             xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
193             sect.sct_effic, dchr[sect.sct_type].d_name);
194     }
195 }