2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * unitsub.c: Common subroutines for multiple type of units
29 * Known contributors to this file:
30 * Ron Koenderink, 2007
31 * Markus Armbruster, 2009-2015
41 #include "prototypes.h"
45 unit_nameof(struct empobj *gp)
47 switch (gp->ef_type) {
49 return prship((struct shpstr *)gp);
51 return prplane((struct plnstr *)gp);
53 return prland((struct lndstr *)gp);
55 return prnuke((struct nukstr *)gp);
58 return "The Beast #666";
62 unit_list(struct emp_qelem *unit_list)
65 struct emp_qelem *next;
67 int type, npln, nch, nxl;
72 if (CANT_HAPPEN(QEMPTY(unit_list)))
74 qp = unit_list->q_back;
75 ulp = (struct ulist *)qp;
76 type = ulp->unit.gen.ef_type;
77 if (CANT_HAPPEN(type != EF_LAND && type != EF_SHIP))
81 pr("lnd# land type x,y a eff mil sh gun xl ln mu tech retr\n");
83 pr("shp# ship type x,y fl eff mil sh gun pn he xl ln mob tech\n");
85 for (; qp != unit_list; qp = next) {
87 ulp = (struct ulist *)qp;
88 lnd = &ulp->unit.land;
89 shp = &ulp->unit.ship;
90 unit = &ulp->unit.gen;
91 if (CANT_HAPPEN(type != unit->ef_type))
93 pr("%4d ", unit->uid);
94 pr("%-16.16s ", empobj_chr_name(unit));
95 prxy("%4d,%-4d ", unit->x, unit->y);
96 pr("%1.1s", &unit->group);
97 pr("%4d%%", unit->effic);
98 if (type == EF_LAND) {
99 pr("%4d", lnd->lnd_item[I_MILIT]);
100 pr("%4d", lnd->lnd_item[I_SHELL]);
101 pr("%4d", lnd->lnd_item[I_GUN]);
102 pr("%3d%3d", lnd_nxlight(lnd), lnd_nland(lnd));
104 pr("%4d", shp->shp_item[I_MILIT]);
105 pr("%4d", shp->shp_item[I_SHELL]);
106 pr("%4d", shp->shp_item[I_GUN]);
107 npln = shp_nplane(shp, &nch, &nxl, NULL);
108 pr("%3d%3d%3d", npln - nch - nxl, nch, nxl);
109 pr("%3d", shp_nland(shp));
111 pr("%4d", unit->mobil);
112 pr("%4d", unit->tech);
113 if (type == EF_LAND) {
114 pr("%4d%%", lnd->lnd_retreat);
121 unit_view(struct emp_qelem *list)
124 struct emp_qelem *qp;
125 struct emp_qelem *next;
128 for (qp = list->q_back; qp != list; qp = next) {
130 ulp = (struct ulist *)qp;
131 if (CANT_HAPPEN(!(ef_flags(ulp->unit.gen.ef_type) & EFF_XY)))
133 getsect(ulp->unit.gen.x, ulp->unit.gen.y, §);
134 if (ulp->unit.gen.ef_type == EF_SHIP) {
135 if (mchr[ulp->unit.ship.shp_type].m_flags & M_FOOD)
136 pr("[fert:%d] ", sect.sct_fertil);
137 if (mchr[ulp->unit.ship.shp_type].m_flags & M_OIL)
138 pr("[oil:%d] ", sect.sct_oil);
140 pr("%s @ %s %d%% %s\n", unit_nameof(&ulp->unit.gen),
141 xyas(ulp->unit.gen.x, ulp->unit.gen.y, player->cnum),
142 sect.sct_effic, dchr[sect.sct_type].d_name);
147 unit_rad_map_set(struct emp_qelem *list)
149 struct emp_qelem *qp;
152 for (qp = list->q_back; qp != list; qp = qp->q_back) {
153 unit = &((struct ulist *)qp)->unit.gen;
154 rad_map_set(unit->own, unit->x, unit->y, unit->effic, unit->tech,
155 unit->ef_type == EF_SHIP
156 ? mchr[unit->type].m_vrnge : lchr[unit->type].l_spy);
160 static struct empobj *
161 get_leader(struct emp_qelem *list)
163 return &((struct ulist *)(list->q_back))->unit.gen;
167 switch_leader(struct emp_qelem *list, char *arg)
169 int uid = arg ? atoi(arg) : -1;
171 struct emp_qelem *qp, *save;
177 save = qp = list->q_back;
180 emp_insque(qp, list);
182 ulp = (struct ulist *)qp;
183 if (ulp->unit.gen.uid == uid || uid == -1)
185 } while (list->q_back != save);
189 unit_move_parse(char *cp, char *arg1_default)
193 ac = parse(cp, player->argbuf, player->argp, NULL, NULL, NULL);
194 if (CANT_HAPPEN(ac <= 0)) {
195 player->argp[0] = "";
199 player->argp[1] = arg1_default;
206 unit_move_non_dir(struct emp_qelem *list, char *cp, int *map_shown)
208 struct empobj *leader = get_leader(list);
209 int bmap = 0, stopping;
213 sprintf(leader_str, "%d", leader->uid);
220 cp = unit_move_parse(cp, leader_str);
221 display_region_map(bmap, leader->ef_type, leader->x, leader->y,
222 player->argp[1], player->argp[2]);
226 cp = unit_move_parse(cp, NULL);
227 switch_leader(list, player->argp[1]);
235 if (leader->ef_type == EF_SHIP)
236 stopping = shp_sweep(list, 1, 1, player->cnum);
238 stopping = lnd_sweep(list, 1, 1, player->cnum);
243 cp = unit_move_parse(cp, leader_str);
244 radar(leader->ef_type);
245 player->btused++; /* FIXME use player_coms[].c_cost */
249 cp = unit_move_parse(cp, leader_str);
250 do_look(leader->ef_type);
251 player->btused++; /* FIXME likewise */
254 if (leader->ef_type != EF_SHIP)
256 cp = unit_move_parse(cp, leader_str);
258 player->btused++; /* FIXME likewise */
262 cp = unit_move_parse(cp, NULL);
263 if (!player->argp[1]) {
264 player->argp[1] = leader_str;
265 player->argp[2] = "1";
266 } else if (!player->argp[2]) {
267 player->argp[2] = player->argp[1];
268 player->argp[1] = leader_str;
270 if (leader->ef_type == EF_SHIP)
274 player->btused++; /* FIXME likewise */
289 unit_move_getpath(struct emp_qelem *list, int suppress_map, char *path)
291 struct empobj *leader = get_leader(list);
292 double minmob, maxmob;
293 struct emp_qelem *qp;
299 for (qp = list->q_back; qp != list; qp = qp->q_back) {
300 ulp = (struct ulist *)qp;
301 if (ulp->mobil < minmob)
303 if (ulp->mobil > maxmob)
307 nav_map(leader->x, leader->y,
308 leader->ef_type == EF_SHIP
309 ? !(mchr[leader->type].m_flags & M_SUB) : 1);
310 snprintf(prompt, sizeof(prompt), "<%.1f:%.1f: %s> ",
312 xyas(leader->x, leader->y, player->cnum));
313 return getstring(prompt, path);
317 unit_move_route(struct empobj *unit, char *buf, size_t bufsz)
326 if (CANT_HAPPEN(unit->ef_type != EF_LAND && unit->ef_type != EF_SHIP))
329 if (!sarg_xy(buf, &destx, &desty))
331 if (unit->ef_type == EF_SHIP) {
332 c = path_find(unit->x, unit->y, destx, desty,
333 player->cnum, MOB_SAIL);
334 if (c < 0 || unit->mobil <= 0) {
335 pr("Can't get to '%s' right now.\n",
336 xyas(destx, desty, player->cnum));
340 getsect(unit->x, unit->y, §);
341 mtype = lnd_mobtype((struct lndstr *)unit);
343 * Note: passing sect.sct_own for actor is funny, but works:
344 * its only effect is to confine the search to that nation's
345 * land. It doesn't affect mobility costs. The real actor is
346 * different for marching in allied land, and passing it would
347 * break path finding there.
349 c = path_find(unit->x, unit->y, destx, desty, sect.sct_own, mtype);
351 pr("No owned %s from %s to %s!\n",
352 mtype == MOB_RAIL ? "railway" : "path",
353 xyas(unit->x, unit->y, player->cnum),
354 xyas(destx, desty, player->cnum));
358 len = path_find_route(buf, bufsz, unit->x, unit->y, destx, desty);
359 if (len == 0 || unit->ef_type == EF_LAND) {
361 strcpy(buf + len, "h");
365 pr("Can't handle path to %s, it's too long, sorry\n",
366 xyas(destx, desty, player->cnum));
369 pr("Using path '%s'\n", buf);
374 unit_move(struct emp_qelem *list)
376 struct empobj *leader = get_leader(list);
377 int leader_uid = leader->uid;
378 int type = leader->ef_type;
379 int moved, suppress_map, dir, stopping;
383 unit_rad_map_set(list);
386 type == EF_SHIP ? "Flagship" : "Leader",
387 unit_nameof(leader));
390 if (player->argp[2]) {
391 strcpy(path, player->argp[2]);
392 cp = unit_move_route(leader, path, sizeof(path));
397 moved = suppress_map = 0;
401 * - shp_may_nav() true for all ships
402 * - lnd_may_mar() true for all land units
403 * - leader is up-to-date
404 * Implies all are in the same sector
407 cp = unit_move_getpath(list, suppress_map, path);
409 if (type == EF_SHIP) {
410 shp_nav_stay_behind(list, player->cnum);
411 shp_nav_put(list, player->cnum);
413 lnd_mar_stay_behind(list, player->cnum);
414 lnd_mar_put(list, player->cnum);
418 cp = unit_move_route(leader, path, sizeof(path));
422 } else if ((dir = chkdir(*cp, DIR_STOP, DIR_LAST)) >= 0) {
425 stopping = shp_nav_dir(list, dir, player->cnum)
426 || shp_nav_gauntlet(list, 1, player->cnum);
428 if (!moved && !lnd_abandon_askyn(list)) {
429 lnd_mar_put(list, player->cnum);
432 stopping = lnd_mar_dir(list, dir, player->cnum)
433 || lnd_mar_gauntlet(list, 1, player->cnum);
435 if (dir == DIR_STOP) {
436 CANT_HAPPEN(!QEMPTY(list));
443 cp = unit_move_non_dir(list, cp, &suppress_map);
445 direrr("`%c' to stop", ", `%c' to view", NULL);
446 pr(", `i' to list %s, `f' to change %s,\n",
447 type == EF_SHIP ? "ships" : "units",
448 type == EF_SHIP ? "flagship" : "leader");
449 pr("`r' to radar, %s`l' to look, `M' to map, `B' to bmap,\n",
450 type == EF_SHIP ? "`s' to sonar, " : "");
451 pr("`d' to drop mines, and `m' to minesweep\n");
457 shp_nav_stay_behind(list, player->cnum);
459 lnd_mar_stay_behind(list, player->cnum);
462 pr("No %s left\n", type == EF_SHIP ? "ships" : "lands");
466 leader = get_leader(list);
467 if (leader->uid != leader_uid) {
468 leader_uid = leader->uid;
469 pr("Changing %s to %s\n",
470 leader->ef_type == EF_SHIP ? "flagship" : "leader",
471 unit_nameof(leader));
473 unit_rad_map_set(list);
478 * Teleport @unit to @x,@y.
479 * If @unit's mission op-area is centered on it, keep it centered.
482 unit_teleport(struct empobj *unit, coord x, coord y)
484 if (unit->opx == unit->x && unit->opy == unit->y) {
493 * Update cargo of @carrier for movement or destruction.
494 * If the carrier is destroyed, destroy its cargo (planes, land units,
496 * Else update their location to the carrier's. Any op sectors equal
497 * to location get updated, too.
498 * Return number of units updated.
501 unit_update_cargo(struct empobj *carrier)
505 union empobj_storage obj;
508 for (cargo_type = EF_PLANE; cargo_type <= EF_NUKE; cargo_type++) {
509 snxtitem_cargo(&ni, cargo_type, carrier->ef_type, carrier->uid);
510 while (nxtitem(&ni, &obj)) {
512 unit_teleport(&obj.gen, carrier->x, carrier->y);
514 mpr(obj.gen.own, "%s lost!\n", unit_nameof(&obj.gen));
517 put_empobj(cargo_type, obj.gen.uid, &obj);
525 * Drop cargo of @unit.
526 * Give it to @newown, unless it's zero.
529 unit_drop_cargo(struct empobj *unit, natid newown)
533 union empobj_storage cargo;
535 for (type = EF_PLANE; type <= EF_NUKE; type++) {
536 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
537 while (nxtitem(&ni, &cargo)) {
540 cargo.plane.pln_ship = cargo.plane.pln_land = -1;
543 cargo.land.lnd_ship = cargo.land.lnd_land = -1;
546 cargo.nuke.nuk_plane = -1;
549 mpr(cargo.gen.own, "%s transferred off %s %d to %s\n",
550 unit_nameof(&cargo.gen),
551 ef_nameof(unit->ef_type), unit->uid,
552 xyas(cargo.gen.x, cargo.gen.y, cargo.gen.own));
554 unit_give_away(&cargo.gen, newown, cargo.gen.own);
555 put_empobj(type, cargo.gen.uid, &cargo.gen);
561 * Give @unit and its cargo to @recipient.
562 * No action if @recipient already owns @unit.
563 * If @giver is non-zero, inform @recipient and @giver of the transaction.
564 * Clears mission and group on the units given away.
567 unit_give_away(struct empobj *unit, natid recipient, natid giver)
571 union empobj_storage cargo;
573 if (unit->own == recipient)
577 mpr(unit->own, "%s given to %s\n",
578 unit_nameof(unit), cname(recipient));
579 mpr(recipient, "%s given to you by %s\n",
580 unit_nameof(unit), cname(giver));
583 unit->own = recipient;
584 unit_wipe_orders(unit);
585 put_empobj(unit->ef_type, unit->uid, unit);
587 for (type = EF_PLANE; type <= EF_NUKE; type++) {
588 snxtitem_cargo(&ni, type, unit->ef_type, unit->uid);
589 while (nxtitem(&ni, &cargo))
590 unit_give_away(&cargo.gen, recipient, giver);
595 * Wipe orders and such from @unit.
598 unit_wipe_orders(struct empobj *unit)
605 unit->opx = unit->opy = 0;
609 switch (unit->ef_type) {
611 sp = (struct shpstr *)unit;
613 sp->shp_rpath[0] = 0;
616 pp = (struct plnstr *)unit;
617 pp->pln_range = pln_range_max(pp);
620 lp = (struct lndstr *)unit;
621 lp->lnd_retreat = morale_base;
623 lp->lnd_rpath[0] = 0;