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