src/lib/commands/cede.c

Go to the documentation of this file.
00001 /*
00002  *  Empire - A multi-player, client/server Internet based war game.
00003  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
00004  *                           Ken Stevens, Steve McClure
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  *  ---
00021  *
00022  *  See files README, COPYING and CREDITS in the root of the source
00023  *  tree for related information and legal notices.  It is expected
00024  *  that future projects/authors will amend these files as needed.
00025  *
00026  *  ---
00027  *
00028  *  cede.c: Give a sector to a neighbor
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare
00032  *     Thomas Ruschak
00033  */
00034 
00035 #include <config.h>
00036 
00037 #include "commands.h"
00038 #include "land.h"
00039 #include "lost.h"
00040 #include "nuke.h"
00041 #include "path.h"
00042 #include "plane.h"
00043 #include "ship.h"
00044 
00045 static void cede_hdr(void);
00046 static int cede_sect(struct nstr_sect *, natid);
00047 static int cede_ship(struct nstr_item *, natid);
00048 static void grab_sect(struct sctstr *, natid);
00049 static void grab_ship(struct shpstr *, natid);
00050 
00051 int
00052 cede(void)
00053 {
00054     static int sct_or_shp[] = { EF_SECTOR, EF_SHIP, EF_BAD };
00055     natid to;
00056     int n;
00057     int is_sector = 0, is_ship = 0;
00058     char *p;
00059     struct nstr_sect ns;
00060     struct nstr_item ni;
00061     struct natstr *natp;
00062     char buf[1024];
00063 
00064     if ((p = getstarg(player->argp[1], "Cede what? ", buf)) == 0)
00065         return RET_SYN;
00066     if (snxtsct(&ns, p))
00067         is_sector = 1;
00068     if (snxtitem(&ni, EF_SHIP, p))
00069         is_ship = 1;
00070     if (!is_sector && !is_ship)
00071         return RET_SYN;
00072     if ((n = natarg(player->argp[2], "to which country? ")) < 0)
00073         return RET_SYN;
00074     if (is_sector && is_ship) {
00075         int type;
00076 
00077         if ((p =
00078              getstarg(player->argp[3], "Cede sectors or ships (se, sh)? ",
00079                       buf)) == 0)
00080             return RET_FAIL;
00081         if (strlen(p) > 4)
00082             p[2] = 0;
00083         type = ef_byname_from(p, sct_or_shp);
00084         if (type < 0) {
00085             pr("Please type 'se' or 'sh'!\n");
00086             return RET_FAIL;
00087         }
00088     }
00089 
00090 
00091     if (n == player->cnum) {
00092         pr("Giving something to yourself?\n");
00093         return RET_FAIL;
00094     }
00095     natp = getnatp(n);
00096     if (natp->nat_stat != STAT_ACTIVE) {
00097         pr("You can only give to normal countries...\n");
00098         return RET_FAIL;
00099     }
00100     if (getrel(natp, player->cnum) < FRIENDLY) {
00101         pr("You can only cede to a country that is friendly towards you...\n");
00102         return RET_FAIL;
00103     }
00104     to = n;
00105 
00106     if (is_sector)
00107         return cede_sect(&ns, to);
00108     else
00109         return cede_ship(&ni, to);
00110 }
00111 
00112 static int
00113 cede_sect(struct nstr_sect *ns, natid to)
00114 {
00115     struct sctstr sect, osect;
00116     int nsect;
00117     int n, bad;
00118     int off_x, off_y;
00119     struct nstr_item ni;
00120     struct shpstr ship;
00121 
00122     prdate();
00123     nsect = 0;
00124     while (nxtsct(ns, &sect)) {
00125         if (!player->owner)
00126             continue;
00127         if (sect.sct_mobil == 0) {
00128             pr("%s has no mobility and cannot be ceded\n",
00129                xyas(sect.sct_x, sect.sct_y, player->cnum));
00130             continue;
00131         }
00132 
00133         bad = 1;
00134         for (n = 1; n <= 6; n++) {      /* Directions */
00135             off_x = sect.sct_x + diroff[n][0];
00136             off_y = sect.sct_y + diroff[n][1];
00137 
00138             if (!getsect(off_x, off_y, &osect))
00139                 continue;
00140             if ((osect.sct_own == to) && (osect.sct_mobil))
00141                 bad = 0;
00142             if ((osect.sct_own == to) &&
00143                 has_units_with_mob(osect.sct_x, osect.sct_y, to))
00144                 bad = 0;
00145         }
00146         snxtitem_all(&ni, EF_SHIP);
00147         while (nxtitem(&ni, &ship)) {
00148             if ((ship.shp_own == to) &&
00149                 ((ship.shp_x == sect.sct_x) && (ship.shp_y == sect.sct_y)))
00150                 bad = 0;
00151         }
00152         if (bad) {
00153             pr("%s has no sector with mobility adjacent to or ship in %s!\n",
00154                cname(to), xyas(sect.sct_x, sect.sct_y, player->cnum));
00155             continue;
00156         }
00157 
00158         if (nsect++ == 0)
00159             cede_hdr();
00160 
00161         grab_sect(&sect, to);
00162         putsect(&sect);
00163         pr("  %s %d%% ceded\n", xyas(sect.sct_x, sect.sct_y, player->cnum),
00164            (int)sect.sct_effic);
00165         wu(0, (natid)to, "%s ceded to you by %s\n",
00166            xyas(sect.sct_x, sect.sct_y, to), cname(player->cnum));
00167     }
00168     pr("%d sector%s\n", nsect, splur(nsect));
00169     return RET_OK;
00170 }
00171 
00172 static void
00173 cede_hdr(void)
00174 {
00175     if (player->god)
00176         pr("own ");
00177     pr("  sect eff\n");
00178 }
00179 
00180 
00181 
00182 static void
00183 grab_sect(struct sctstr *sp, natid to)
00184 {
00185     struct plnstr *pp;
00186     struct lndstr *lp;
00187     struct nukstr *np;
00188     struct nstr_item ni;
00189     struct plnstr p;
00190     struct lndstr l;
00191     struct nukstr nuk;
00192 
00193     /* Wipe all the distribution info */
00194     memset(sp->sct_dist, 0, sizeof(sp->sct_dist));
00195     memset(sp->sct_del, 0, sizeof(sp->sct_del));
00196     sp->sct_dist_x = sp->sct_x;
00197     sp->sct_dist_y = sp->sct_y;
00198 
00199     pp = &p;
00200     snxtitem_xy(&ni, EF_PLANE, sp->sct_x, sp->sct_y);
00201     while (nxtitem(&ni, pp)) {
00202         if (pp->pln_own == 0)
00203             continue;
00204         if (pp->pln_ship >= 0)
00205             continue;
00206         if (pp->pln_own != player->cnum)
00207             continue;
00208         if (pp->pln_flags & PLN_LAUNCHED)
00209             continue;
00210 
00211         wu(0, to, "\t%s ceded to you by %s\n",
00212            prplane(pp), cname(player->cnum));
00213         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
00214         pp->pln_own = to;
00215         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
00216                     pp->pln_x, pp->pln_y);
00217         pp->pln_mobil = 0;
00218         pp->pln_mission = 0;
00219         putplane(pp->pln_uid, pp);
00220     }
00221 
00222     np = &nuk;
00223     snxtitem_xy(&ni, EF_NUKE, sp->sct_x, sp->sct_y);
00224     while (nxtitem(&ni, np)) {
00225         if (np->nuk_own == 0)
00226             continue;
00227 
00228         wu(0, to, "\t%s ceded to you by %s\n",
00229            prnuke(np), cname(player->cnum));
00230         makelost(EF_NUKE, np->nuk_own, np->nuk_uid, np->nuk_x, np->nuk_y);
00231         np->nuk_own = to;
00232         makenotlost(EF_NUKE, np->nuk_own, np->nuk_uid,
00233                     np->nuk_x, np->nuk_y);
00234         putnuke(ni.cur, np);
00235     }
00236 
00237     lp = &l;
00238     snxtitem_xy(&ni, EF_LAND, sp->sct_x, sp->sct_y);
00239     while (nxtitem(&ni, lp)) {
00240         if (lp->lnd_own == 0)
00241             continue;
00242         if (lp->lnd_ship == 0)
00243             continue;
00244         if (lp->lnd_own != player->cnum)
00245             continue;
00246 
00247         wu(0, to, "\t%s ceded to you by %s\n",
00248            prland(lp), cname(player->cnum));
00249         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
00250         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
00251         lp->lnd_own = to;
00252         lp->lnd_mobil = 0;
00253         lp->lnd_mission = 0;
00254         putland(ni.cur, lp);
00255     }
00256 
00257     sp->sct_avail = 0;
00258 
00259     if (sp->sct_oldown == to) {
00260         sp->sct_che = 0;        /* FIXME where do these guys go? */
00261         sp->sct_che_target = 0;
00262         sp->sct_loyal = 0;
00263     }
00264 
00265     if (sp->sct_oldown == to)
00266         sp->sct_loyal = 0;
00267     else                        /* people don't like being given away */
00268         sp->sct_loyal = 50;
00269 
00270     sp->sct_dist_x = sp->sct_x;
00271     sp->sct_dist_y = sp->sct_y;
00272     makelost(EF_SECTOR, sp->sct_own, 0, sp->sct_x, sp->sct_y);
00273     makenotlost(EF_SECTOR, to, 0, sp->sct_x, sp->sct_y);
00274     if (sp->sct_oldown == sp->sct_own)
00275         sp->sct_oldown = to;
00276     sp->sct_own = to;
00277     sp->sct_mobil = 0;
00278 }
00279 
00280 static int
00281 cede_ship(struct nstr_item *ni, natid to)
00282 {
00283     struct shpstr ship;
00284     struct shpstr tship;
00285     struct sctstr sect;
00286     struct nstr_item tni;
00287     int nships = 0;
00288     int bad = 0;
00289 
00290     while (nxtitem(ni, &ship)) {
00291 
00292         if (!player->owner || ship.shp_own == 0)
00293             continue;
00294 
00295         bad = 1;
00296         snxtitem_xy(&tni, EF_SHIP, ship.shp_x, ship.shp_y);
00297         while (nxtitem(&tni, &tship) && bad)
00298             if (tship.shp_own == to)
00299                 bad = 0;
00300 
00301         getsect(ship.shp_x, ship.shp_y, &sect);
00302         if (bad && (sect.sct_own != to)) {
00303             pr("%s isn't in a %s sector, and is not with a %s ship!\n",
00304                prship(&ship), cname(to), cname(to));
00305             continue;
00306         }
00307         grab_ship(&ship, to);
00308         putship(ship.shp_uid, &ship);
00309         nships++;
00310         wu(0, to, "%s ceded to you by %s\n",
00311            prship(&ship), cname(player->cnum));
00312         pr("%s ceded to %s\n", prship(&ship), cname(to));
00313     }
00314     pr("    %d ship%s\n", nships, splur(nships));
00315 
00316     return RET_OK;
00317 }
00318 
00319 static void
00320 grab_ship(struct shpstr *sp, natid to)
00321 {
00322     struct plnstr *pp;
00323     struct lndstr *lp;
00324     struct nstr_item ni;
00325     struct plnstr p;
00326     struct lndstr l;
00327 
00328     pp = &p;
00329     snxtitem_xy(&ni, EF_PLANE, sp->shp_x, sp->shp_y);
00330     while (nxtitem(&ni, pp)) {
00331         if (pp->pln_own == 0)
00332             continue;
00333         if (pp->pln_flags & PLN_LAUNCHED)
00334             continue;
00335         if (pp->pln_ship != sp->shp_uid)
00336             continue;
00337         if (pp->pln_own != player->cnum)
00338             continue;
00339 
00340         wu(0, to, "\t%s ceded to you by %s\n",
00341            prplane(pp), cname(player->cnum));
00342         makelost(EF_PLANE, pp->pln_own, pp->pln_uid, pp->pln_x, pp->pln_y);
00343         pp->pln_own = to;
00344         makenotlost(EF_PLANE, pp->pln_own, pp->pln_uid,
00345                     pp->pln_x, pp->pln_y);
00346         pp->pln_mobil = 0;
00347         pp->pln_mission = 0;
00348         putplane(pp->pln_uid, pp);
00349     }
00350 
00351     lp = &l;
00352     snxtitem_xy(&ni, EF_LAND, sp->shp_x, sp->shp_y);
00353     while (nxtitem(&ni, lp)) {
00354         if (lp->lnd_own == 0)
00355             continue;
00356         if (lp->lnd_ship != sp->shp_uid)
00357             continue;
00358         if (lp->lnd_own != player->cnum)
00359             continue;
00360 
00361         wu(0, to, "\t%s ceded to you by %s\n",
00362            prland(lp), cname(player->cnum));
00363         makelost(EF_LAND, lp->lnd_own, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
00364         makenotlost(EF_LAND, to, lp->lnd_uid, lp->lnd_x, lp->lnd_y);
00365         lp->lnd_own = to;
00366         lp->lnd_mobil = 0;
00367         lp->lnd_mission = 0;
00368         putland(ni.cur, lp);
00369     }
00370 
00371     makelost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
00372     sp->shp_own = to;
00373     makenotlost(EF_SHIP, sp->shp_own, sp->shp_uid, sp->shp_x, sp->shp_y);
00374 }

Generated on Fri Mar 28 11:01:11 2008 for empserver by  doxygen 1.5.2