]> git.pond.sub.org Git - empserver/blob - src/lib/subs/unitsub.c
Update copyright notice
[empserver] / src / lib / subs / unitsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  unitsub.c: Common subroutines for multiple type of units
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2007
31  *     Markus Armbruster, 2009-2014
32  */
33
34 #include <config.h>
35
36 #include "file.h"
37 #include "path.h"
38 #include "player.h"
39 #include "optlist.h"
40 #include "prototypes.h"
41 #include "unit.h"
42
43 char *
44 unit_nameof(struct empobj *gp)
45 {
46     switch (gp->ef_type) {
47     case EF_SHIP:
48         return prship((struct shpstr *)gp);
49     case EF_PLANE:
50         return prplane((struct plnstr *)gp);
51     case EF_LAND:
52         return prland((struct lndstr *)gp);
53     case EF_NUKE:
54         return prnuke((struct nukstr *)gp);
55     }
56     CANT_REACH();
57     return "The Beast #666";
58 }
59
60 void
61 unit_list(struct emp_qelem *unit_list)
62 {
63     struct emp_qelem *qp;
64     struct emp_qelem *next;
65     struct ulist *ulp;
66     int type, npln, nch, nxl;
67     struct empobj *unit;
68     struct lndstr *lnd;
69     struct shpstr *shp;
70
71     if (CANT_HAPPEN(QEMPTY(unit_list)))
72         return;
73     qp = unit_list->q_back;
74     ulp = (struct ulist *)qp;
75     type = ulp->unit.gen.ef_type;
76     if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
77         return;
78
79     if (type == EF_LAND)
80         pr("lnd#     land type       x,y    a  eff mil  sh gun xl ln  mu tech retr\n");
81     else
82         pr("shp#     ship type       x,y   fl  eff mil  sh gun pn he xl ln mob tech\n");
83
84     for (; qp != unit_list; qp = next) {
85         next = qp->q_back;
86         ulp = (struct ulist *)qp;
87         lnd = &ulp->unit.land;
88         shp = &ulp->unit.ship;
89         unit = &ulp->unit.gen;
90         if (CANT_HAPPEN(type != unit->ef_type))
91             continue;
92         pr("%4d ", unit->uid);
93         pr("%-16.16s ", empobj_chr_name(unit));
94         prxy("%4d,%-4d ", unit->x, unit->y);
95         pr("%1.1s", &unit->group);
96         pr("%4d%%", unit->effic);
97         if (type == EF_LAND) {
98             pr("%4d", lnd->lnd_item[I_MILIT]);
99             pr("%4d", lnd->lnd_item[I_SHELL]);
100             pr("%4d", lnd->lnd_item[I_GUN]);
101             pr("%3d%3d", lnd_nxlight(lnd), lnd_nland(lnd));
102         } else {
103             pr("%4d", shp->shp_item[I_MILIT]);
104             pr("%4d", shp->shp_item[I_SHELL]);
105             pr("%4d", shp->shp_item[I_GUN]);
106             npln = shp_nplane(shp, &nch, &nxl, NULL);
107             pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
108             pr("%3d", shp_nland(shp));
109         }
110         pr("%4d", unit->mobil);
111         pr("%4d", unit->tech);
112         if (type == EF_LAND) {
113             pr("%4d%%", lnd->lnd_retreat);
114         }
115         pr("\n");
116     }
117 }
118
119 char *
120 unit_path(struct empobj *unit, char *buf, size_t bufsz)
121 {
122     coord destx;
123     coord desty;
124     struct sctstr sect;
125     size_t len;
126     double c;
127     int mtype;
128
129     if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
130         return NULL;
131
132     if (!sarg_xy(buf, &destx, &desty))
133         return buf;
134     if (unit->ef_type == EF_SHIP) {
135         c = path_find(unit->x, unit->y, destx, desty,
136                       player->cnum, MOB_SAIL);
137         if (c < 0 || unit->mobil <= 0) {
138             pr("Can't get to '%s' right now.\n",
139                xyas(destx, desty, player->cnum));
140             return NULL;
141         }
142     } else {
143         getsect(unit->x, unit->y, &sect);
144         mtype = lnd_mobtype((struct lndstr *)unit);
145         /*
146          * Note: passing sect.sct_own for actor is funny, but works:
147          * its only effect is to confine the search to that nation's
148          * land.  It doesn't affect mobility costs.  The real actor is
149          * different for marching in allied land, and passing it would
150          * break path finding there.
151          */
152         c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
153         if (c < 0) {
154             pr("No owned %s from %s to %s!\n",
155                mtype == MOB_RAIL ? "railway" : "path",
156                xyas(unit->x, unit->y, player->cnum),
157                xyas(destx, desty, player->cnum));
158             return NULL;
159         }
160     }
161     len = path_find_route(buf, bufsz, unit->x, unit->y, destx, desty);
162     if (len == 0 || unit->ef_type == EF_LAND) {
163         if (len + 1 < bufsz)
164             strcpy(buf + len, "h");
165         len++;
166     }
167     if (len >= bufsz) {
168         pr("Can't handle path to %s, it's too long, sorry\n",
169            xyas(destx, desty, player->cnum));
170         return NULL;
171     }
172     pr("Using path '%s'\n", buf);
173     return buf;
174 }
175
176 void
177 unit_view(struct emp_qelem *list)
178 {
179     struct sctstr sect;
180     struct emp_qelem *qp;
181     struct emp_qelem *next;
182     struct ulist *ulp;
183
184     for (qp = list->q_back; qp != list; qp = next) {
185         next = qp->q_back;
186         ulp = (struct ulist *)qp;
187         if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
188             continue;
189         getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
190         if (ulp->unit.gen.ef_type == EF_SHIP) {
191             if (mchr[ulp->unit.ship.shp_type].m_flags & M_FOOD)
192                 pr("[fert:%d] ", sect.sct_fertil);
193             if (mchr[ulp->unit.ship.shp_type].m_flags & M_OIL)
194                 pr("[oil:%d] ", sect.sct_oil);
195         }
196         pr("%s @ %s %d%% %s\n", unit_nameof(&ulp->unit.gen),
197            xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
198            sect.sct_effic, dchr[sect.sct_type].d_name);
199     }
200 }
201
202 /*
203  * Teleport UNIT to X,Y.
204  * If UNIT's mission op-area is centered on it, keep it centered.
205  */
206 void
207 unit_teleport(struct empobj *unit, coord x, coord y)
208 {
209     if (unit->opx == unit->x && unit->opy == unit->y) {
210         unit->opx = x;
211         unit->opy = y;
212     }
213     unit->x = x;
214     unit->y = y;
215 }
216
217 /*
218  * Update cargo of CARRIER for movement or destruction.
219  * If the carrier is destroyed, destroy its cargo (planes, land units,
220  * nukes).
221  * Else update their location to the carrier's.  Any op sectors equal
222  * to location get updated, too.
223  * Return number of units updated.
224  */
225 int
226 unit_update_cargo(struct empobj *carrier)
227 {
228     int cargo_type;
229     struct nstr_item ni;
230     union empobj_storage obj;
231     int n = 0;
232
233     for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
234         snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
235         while (nxtitem(&ni, &obj)) {
236             if (carrier->own)
237                 unit_teleport(&obj.gen, carrier->x, carrier->y);
238             else {
239                 mpr(obj.gen.own, "%s lost!\n", unit_nameof(&obj.gen));
240                 obj.gen.effic = 0;
241             }
242             put_empobj(cargo_type, obj.gen.uid, &obj);
243             n++;
244         }
245     }
246     return n;
247 }
248
249 /*
250  * Drop cargo of UNIT.
251  * Give it to NEWOWN, unless it's zero.
252  */
253 void
254 unit_drop_cargo(struct empobj *unit, natid newown)
255 {
256     int type;
257     struct nstr_item ni;
258     union empobj_storage cargo;
259
260     for (type = EF_PLANE; type <= EF_NUKE; type++) {
261         snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
262         while (nxtitem(&ni, &cargo)) {
263             switch (type) {
264             case EF_PLANE:
265                 cargo.plane.pln_ship = cargo.plane.pln_land = -1;
266                 break;
267             case EF_LAND:
268                 cargo.land.lnd_ship = cargo.land.lnd_land = -1;
269                 break;
270             case EF_NUKE:
271                 cargo.nuke.nuk_plane = -1;
272                 break;
273             }
274             mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
275                 unit_nameof(&cargo.gen),
276                 ef_nameof(unit->ef_type), unit->uid,
277                 xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
278             if (newown)
279                 unit_give_away(&cargo.gen, newown, cargo.gen.own);
280             put_empobj(type, cargo.gen.uid, &cargo.gen);
281         }
282     }
283 }
284
285 /*
286  * Give UNIT and its cargo to RECIPIENT.
287  * No action if RECIPIENT already owns UNIT.
288  * If GIVER is non-zero, inform RECIPIENT and GIVER of the transaction.
289  * Clears mission and group on the units given away.
290  */
291 void
292 unit_give_away(struct empobj *unit, natid recipient, natid giver)
293 {
294     int type;
295     struct nstr_item ni;
296     union empobj_storage cargo;
297
298     if (unit->own == recipient)
299         return;
300
301     if (giver) {
302         mpr(unit->own, "%s given to %s\n",
303             unit_nameof(unit), cname(recipient));
304         mpr(recipient, "%s given to you by %s\n",
305             unit_nameof(unit), cname(giver));
306     }
307
308     unit->own = recipient;
309     unit_wipe_orders(unit);
310     put_empobj(unit->ef_type, unit->uid, unit);
311
312     for (type = EF_PLANE; type <= EF_NUKE; type++) {
313         snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
314         while (nxtitem(&ni, &cargo))
315             unit_give_away(&cargo.gen, recipient, giver);
316     }
317 }
318
319 /*
320  * Wipe orders and such from UNIT.
321  */
322 void
323 unit_wipe_orders(struct empobj *unit)
324 {
325     struct shpstr *sp;
326     struct plnstr *pp;
327     struct lndstr *lp;
328
329     unit->group = 0;
330     unit->opx = unit->opy = 0;
331     unit->mission = 0;
332     unit->radius = 0;
333
334     switch (unit->ef_type) {
335     case EF_SHIP:
336         sp = (struct shpstr *)unit;
337         sp->shp_rflags = 0;
338         sp->shp_rpath[0] = 0;
339         break;
340     case EF_PLANE:
341         pp = (struct plnstr *)unit;
342         pp->pln_range = pln_range_max(pp);
343         break;
344     case EF_LAND:
345         lp = (struct lndstr *)unit;
346         lp->lnd_retreat = morale_base;
347         lp->lnd_rflags = 0;
348         lp->lnd_rpath[0] = 0;
349         break;
350     case EF_NUKE:
351         break;
352     default:
353         CANT_REACH();
354     }
355 }