]> git.pond.sub.org Git - empserver/blob - src/lib/subs/unitsub.c
Simplify unit_put()
[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     struct empobj *unit;
106
107     qp = list->q_back;
108     while (qp != list) {
109         ulp = (struct ulist *)qp;
110         unit = &ulp->unit.gen;
111         if (actor) {
112             mpr(actor, "%s stopped at %s\n", obj_nameof(unit),
113                 xyas(unit->x, unit->y, unit->own));
114             if (unit->ef_type == EF_LAND) {
115                 if (ulp->mobil < -127)
116                     ulp->mobil = -127;
117                 unit->mobil = ulp->mobil;
118             }
119         }
120         if (unit->ef_type == EF_SHIP)
121             unit->mobil = (int)ulp->mobil;
122         put_empobj(unit->ef_type, unit->uid, unit);
123         newqp = qp->q_back;
124         emp_remque(qp);
125         free(qp);
126         qp = newqp;
127     }
128 }
129
130 char *
131 unit_path(int together, struct empobj *unit, char *buf)
132 {
133     coord destx;
134     coord desty;
135     struct sctstr d_sect, sect;
136     char *cp;
137     double dummy;
138     int mtype;
139
140     if (!sarg_xy(buf, &destx, &desty))
141         return 0;
142     if (!together) {
143         pr("Cannot go to a destination sector if not all starting in the same sector\n");
144         return 0;
145     }
146     if (!getsect(destx, desty, &d_sect)) {
147         pr("%d,%d is not a sector\n", destx, desty);
148         return 0;
149     }
150     if (unit->ef_type == EF_SHIP) {
151         cp = BestShipPath(buf, unit->x, unit->y,
152                           d_sect.sct_x, d_sect.sct_y, player->cnum);
153         if (!cp || unit->mobil <= 0) {
154             pr("Can't get to '%s' right now.\n",
155                 xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
156             return 0;
157         }
158     } else {
159         getsect(unit->x, unit->y, &sect);
160         mtype = lnd_mobtype((struct lndstr *)unit);
161         cp = BestLandPath(buf, &sect, &d_sect, &dummy, mtype);
162         if (!cp) {
163             pr("No owned %s from %s to %s!\n",
164                mtype == MOB_RAIL ? "railway" : "path",
165                xyas(unit->x, unit->y, player->cnum),
166                xyas(d_sect.sct_x, d_sect.sct_y, player->cnum));
167             return 0;
168         }
169         pr("Using path '%s'\n", cp);
170     }
171     return cp;
172 }
173
174 void
175 unit_view(struct emp_qelem *list)
176 {
177     struct sctstr sect;
178     struct emp_qelem *qp;
179     struct emp_qelem *next;
180     struct ulist *ulp;
181
182     for (qp = list->q_back; qp != list; qp = next) {
183         next = qp->q_back;
184         ulp = (struct ulist *)qp;
185         getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
186         if (ulp->unit.ef_type == EF_SHIP) {
187             if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
188                 pr("[fert:%d] ", sect.sct_fertil);
189             if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
190                 pr("[oil:%d] ", sect.sct_oil);
191         }
192         pr("%s @ %s %d%% %s\n", obj_nameof(&ulp->unit.gen),
193             xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
194             sect.sct_effic, dchr[sect.sct_type].d_name);
195     }
196 }