]> git.pond.sub.org Git - empserver/blob - src/lib/subs/unitsub.c
Oops when stuck cargo snaps to new ship, plane or land unit
[empserver] / src / lib / subs / unitsub.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, 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-2011
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  sh gun xl  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_SHELL]);
99             pr("%4d", lnd->lnd_item[I_GUN]);
100             pr("%3d", lnd_nxlight(lnd));
101         } else {
102             pr("%4d", shp->shp_item[I_MILIT]);
103             pr("%4d", shp->shp_item[I_SHELL]);
104             pr("%4d", shp->shp_item[I_GUN]);
105             npln = shp_nplane(shp, &nch, &nxl, NULL);
106             pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
107             pr("%3d", shp_nland(shp));
108         }
109         pr("%4d", unit->mobil);
110         pr("%4d", unit->tech);
111         if (type == EF_LAND) {
112             pr("%4d%%", lnd->lnd_retreat);
113         }
114         pr("\n");
115     }
116 }
117
118 void
119 unit_put(struct emp_qelem *list, natid actor)
120 {
121     struct emp_qelem *qp;
122     struct emp_qelem *newqp;
123     struct ulist *ulp;
124     struct empobj *unit;
125
126     qp = list->q_back;
127     while (qp != list) {
128         ulp = (struct ulist *)qp;
129         unit = &ulp->unit.gen;
130         if (CANT_HAPPEN(unit->ef_type != EF_LAND
131                         && unit->ef_type != EF_SHIP))
132             continue;
133         if (actor) {
134             mpr(actor, "%s stopped at %s\n", unit_nameof(unit),
135                 xyas(unit->x, unit->y, actor));
136             if (unit->ef_type == EF_LAND) {
137                 if (ulp->mobil < -127)
138                     ulp->mobil = -127;
139                 unit->mobil = ulp->mobil;
140             }
141         }
142         if (unit->ef_type == EF_SHIP)
143             unit->mobil = (int)ulp->mobil;
144         put_empobj(unit->ef_type, unit->uid, unit);
145         newqp = qp->q_back;
146         emp_remque(qp);
147         free(qp);
148         qp = newqp;
149     }
150 }
151
152 char *
153 unit_path(int together, struct empobj *unit, char *buf)
154 {
155     coord destx;
156     coord desty;
157     struct sctstr sect;
158     size_t len;
159     double c;
160     int mtype;
161
162     if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
163         return NULL;
164
165     if (!sarg_xy(buf, &destx, &desty))
166         return buf;
167     if (!together) {
168         pr("Cannot go to a destination sector if not all starting in the same sector\n");
169         return NULL;
170     }
171     if (unit->ef_type == EF_SHIP) {
172         c = path_find(unit->x, unit->y, destx, desty,
173                       player->cnum, MOB_SAIL);
174         if (c < 0 || unit->mobil <= 0) {
175             pr("Can't get to '%s' right now.\n",
176                xyas(destx, desty, player->cnum));
177             return NULL;
178         }
179     } else {
180         getsect(unit->x, unit->y, &sect);
181         mtype = lnd_mobtype((struct lndstr *)unit);
182         /*
183          * Note: passing sect.sct_own for actor is funny, but works:
184          * its only effect is to confine the search to that nation's
185          * land.  It doesn't affect mobility costs.  The real actor is
186          * different for marching in allied land, and passing it would
187          * break path finding there.
188          */
189         c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
190         if (c < 0) {
191             pr("No owned %s from %s to %s!\n",
192                mtype == MOB_RAIL ? "railway" : "path",
193                xyas(unit->x, unit->y, player->cnum),
194                xyas(destx, desty, player->cnum));
195             return NULL;
196         }
197     }
198     len = path_find_route(buf, sizeof(buf), unit->x, unit->y, destx, desty);
199     if (len == 0 || unit->ef_type == EF_LAND) {
200         if (len + 1 < sizeof(buf))
201             strcpy(buf + len, "h");
202         len++;
203     }
204     if (len >= sizeof(buf)) {
205         pr("Can't handle path to %s, it's too long, sorry\n",
206            xyas(destx, desty, player->cnum));
207         return NULL;
208     }
209     if (unit->ef_type == EF_LAND)
210         pr("Using path '%s'\n", buf);
211     return buf;
212 }
213
214 void
215 unit_view(struct emp_qelem *list)
216 {
217     struct sctstr sect;
218     struct emp_qelem *qp;
219     struct emp_qelem *next;
220     struct ulist *ulp;
221
222     for (qp = list->q_back; qp != list; qp = next) {
223         next = qp->q_back;
224         ulp = (struct ulist *)qp;
225         if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
226             continue;
227         getsect(ulp->unit.gen.x, ulp->unit.gen.y, &sect);
228         if (ulp->unit.gen.ef_type == EF_SHIP) {
229             if (((struct mchrstr *)ulp->chrp)->m_flags & M_FOOD)
230                 pr("[fert:%d] ", sect.sct_fertil);
231             if (((struct mchrstr *)ulp->chrp)->m_flags & M_OIL)
232                 pr("[oil:%d] ", sect.sct_oil);
233         }
234         pr("%s @ %s %d%% %s\n", unit_nameof(&ulp->unit.gen),
235            xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
236            sect.sct_effic, dchr[sect.sct_type].d_name);
237     }
238 }
239
240 /*
241  * Update cargo of CARRIER for movement or destruction.
242  * If the carrier is destroyed, destroy its cargo (planes, land units,
243  * nukes).
244  * Else update their location to the carrier's.  Any op sectors equal
245  * to location get updated, too.
246  * Return number of units updated.
247  */
248 int
249 unit_update_cargo(struct empobj *carrier)
250 {
251     int cargo_type;
252     struct nstr_item ni;
253     union empobj_storage obj;
254     int n = 0;
255
256     for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
257         snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
258         while (nxtitem(&ni, &obj)) {
259             if (!carrier->own) {
260                 mpr(obj.gen.own, "%s lost!\n", unit_nameof(&obj.gen));
261                 obj.gen.effic = 0;
262             } else {
263                 /* mission op-area centered on the obj travels with it */
264                 if (obj.gen.opx == obj.gen.x && obj.gen.opy == obj.gen.y) {
265                     obj.gen.opx = carrier->x;
266                     obj.gen.opy = carrier->y;
267                 }
268                 obj.gen.x = carrier->x;
269                 obj.gen.y = carrier->y;
270             }
271             put_empobj(cargo_type, obj.gen.uid, &obj);
272             n++;
273         }
274     }
275     return n;
276 }
277
278 /*
279  * Drop cargo of UNIT.
280  * Give it to NEWOWN, unless it's zero.
281  */
282 void
283 unit_drop_cargo(struct empobj *unit, natid newown)
284 {
285     int type;
286     struct nstr_item ni;
287     union empobj_storage cargo;
288
289     for (type = EF_PLANE; type <= EF_NUKE; type++) {
290         snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
291         while (nxtitem(&ni, &cargo)) {
292             switch (type) {
293             case EF_PLANE:
294                 cargo.plane.pln_ship = cargo.plane.pln_land = -1;
295                 break;
296             case EF_LAND:
297                 cargo.land.lnd_ship = cargo.land.lnd_land = -1;
298                 break;
299             case EF_NUKE:
300                 cargo.nuke.nuk_plane = -1;
301                 break;
302             }
303             mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
304                 unit_nameof(&cargo.gen),
305                 ef_nameof(unit->ef_type), unit->uid,
306                 xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
307             if (newown)
308                 unit_give_away(&cargo.gen, newown, cargo.gen.own);
309             put_empobj(type, cargo.gen.uid, &cargo.gen);
310         }
311     }
312 }
313
314 /*
315  * Give UNIT and its cargo to RECIPIENT.
316  * No action if RECIPIENT already owns UNIT.
317  * If GIVER is non-zero, inform RECIPIENT and GIVER of the transaction.
318  * Clears mission and group on the units given away.
319  */
320 void
321 unit_give_away(struct empobj *unit, natid recipient, natid giver)
322 {
323     int type;
324     struct nstr_item ni;
325     union empobj_storage cargo;
326
327     if (unit->own == recipient)
328         return;
329
330     if (giver) {
331         mpr(unit->own, "%s given to %s\n",
332             unit_nameof(unit), cname(recipient));
333         mpr(recipient, "%s given to you by %s\n",
334             unit_nameof(unit), cname(giver));
335     }
336
337     unit->own = recipient;
338     unit_wipe_orders(unit);
339     put_empobj(unit->ef_type, unit->uid, unit);
340
341     for (type = EF_PLANE; type <= EF_NUKE; type++) {
342         snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
343         while (nxtitem(&ni, &cargo))
344             unit_give_away(&cargo.gen, recipient, giver);
345     }
346 }
347
348 /*
349  * Wipe orders and such from UNIT.
350  */
351 void
352 unit_wipe_orders(struct empobj *unit)
353 {
354     struct shpstr *sp;
355     struct plnstr *pp;
356     struct lndstr *lp;
357     int i;
358
359     unit->group = 0;
360     unit->opx = unit->opy = 0;
361     unit->mission = 0;
362     unit->radius = 0;
363
364     switch (unit->ef_type) {
365     case EF_SHIP:
366         sp = (struct shpstr *)unit;
367         sp->shp_destx[0] = sp->shp_desty[0] = 0;
368         sp->shp_destx[1] = sp->shp_desty[1] = 0;
369         for (i = 0; i < TMAX; ++i) {
370             sp->shp_tstart[i] = I_NONE;
371             sp->shp_tend[i] = I_NONE;
372             sp->shp_lstart[i] = 0;
373             sp->shp_lend[i] = 0;
374         }
375         sp->shp_autonav = 0;
376         sp->shp_mobquota = 0;
377         sp->shp_path[0] = 0;
378         sp->shp_follow = sp->shp_uid;
379         sp->shp_rflags = 0;
380         sp->shp_rpath[0] = 0;
381         break;
382     case EF_PLANE:
383         pp = (struct plnstr *)unit;
384         pp->pln_range = pln_range_max(pp);
385         break;
386     case EF_LAND:
387         lp = (struct lndstr *)unit;
388         lp->lnd_retreat = morale_base;
389         lp->lnd_rflags = 0;
390         lp->lnd_rpath[0] = 0;
391         break;
392     case EF_NUKE:
393         break;
394     default:
395         CANT_REACH();
396     }
397 }