src/lib/subs/trdsub.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  *  trdsub.c: Trade helper functions
00029  * 
00030  *  Known contributors to this file:
00031  *     St Phil, 1989
00032  *     Pat Loney, 1992
00033  *     Steve McClure, 1996
00034  */
00035 
00036 #include <config.h>
00037 
00038 #include "empobj.h"
00039 #include "file.h"
00040 #include "item.h"
00041 #include "land.h"
00042 #include "loan.h"
00043 #include "misc.h"
00044 #include "nat.h"
00045 #include "nsc.h"
00046 #include "nuke.h"
00047 #include "optlist.h"
00048 #include "plane.h"
00049 #include "player.h"
00050 #include "prototypes.h"
00051 #include "sect.h"
00052 #include "ship.h"
00053 #include "trade.h"
00054 #include "xy.h"
00055 
00056 int
00057 trade_check_ok(struct trdstr *tp, union empobj_storage *tgp)
00058 {
00059     return check_trade_ok(tp) && trade_check_item_ok(tgp);
00060 }
00061 
00062 int
00063 trade_check_item_ok(union empobj_storage *tgp)
00064 {
00065     if (tgp->gen.ef_type == EF_LAND)
00066         return check_land_ok(&tgp->land);
00067     if (tgp->gen.ef_type == EF_PLANE)
00068         return check_plane_ok(&tgp->plane);
00069     if (tgp->gen.ef_type == EF_SHIP)
00070         return check_ship_ok(&tgp->ship);
00071     if (tgp->gen.ef_type == EF_NUKE)
00072         return check_nuke_ok(&tgp->nuke);
00073     CANT_REACH();
00074     pr("Trade lot went bad!\n");
00075     return 0;
00076 }
00077 
00078 char *
00079 trade_nameof(struct trdstr *tp, union empobj_storage *tgp)
00080 {
00081     switch (tp->trd_type) {
00082     case EF_NUKE:
00083         return nchr[(int)tgp->nuke.nuk_type].n_name;
00084     case EF_PLANE:
00085         return plchr[(int)tgp->plane.pln_type].pl_name;
00086     case EF_SHIP:
00087         return mchr[(int)tgp->ship.shp_type].m_name;
00088     case EF_LAND:
00089         return lchr[(int)tgp->land.lnd_type].l_name;
00090     }
00091     return "Bad trade type, get help";
00092 }
00093 
00094 /*
00095  * Describe an item up for sale.  "tgp" is a union containing
00096  * the details of the generic item.
00097  * Return 1 on success, 0 on error
00098  */
00099 int
00100 trade_desc(struct trdstr *tp, union empobj_storage *tgp)
00101 {
00102     i_type it;
00103     struct sctstr sect;
00104     struct nukstr *np;
00105     struct shpstr *sp;
00106     struct plnstr *pp;
00107     struct lndstr *lp;
00108     struct nstr_item ni;
00109     struct plnstr plane;
00110     struct lndstr land;
00111 
00112     switch (tp->trd_type) {
00113     case EF_NUKE:
00114         np = &tgp->nuke;
00115         tp->trd_owner = np->nuk_own;
00116         pr("(%3d)  tech %d %d%% %s #%d",
00117            tp->trd_owner, np->nuk_tech, np->nuk_effic,
00118            nchr[(int)np->nuk_type].n_name, tp->trd_unitid);
00119         break;
00120     case EF_SHIP:
00121         sp = &tgp->ship;
00122         tp->trd_owner = sp->shp_own;
00123         pr("(%3d)  tech %d %d%% %s [",
00124            tp->trd_owner, sp->shp_tech, sp->shp_effic, prship(sp));
00125 
00126         for (it = I_NONE + 1; it <= I_MAX; ++it) {
00127             if (sp->shp_item[it])
00128                 pr("%c:%d ", ichr[it].i_mnem, sp->shp_item[it]);
00129         }
00130         pr("] #%d", tp->trd_unitid);
00131         if (opt_SHOWPLANE) {
00132             snxtitem_all(&ni, EF_PLANE);
00133             while (nxtitem(&ni, &plane)) {
00134                 if (plane.pln_ship == sp->shp_uid && plane.pln_own != 0) {
00135                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
00136                        plane.pln_tech,
00137                        plane.pln_effic,
00138                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
00139                     if (plane.pln_nuketype != -1) {
00140                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
00141                     }
00142                 }
00143             }
00144             snxtitem_all(&ni, EF_LAND);
00145             while (nxtitem(&ni, &land)) {
00146                 if (land.lnd_ship == sp->shp_uid && land.lnd_own != 0) {
00147                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
00148                        land.lnd_tech,
00149                        land.lnd_effic,
00150                        lchr[(int)land.lnd_type].l_name, land.lnd_uid);
00151                     if (land.lnd_nxlight) {
00152                         snxtitem_all(&ni, EF_PLANE);
00153                         while (nxtitem(&ni, &plane)) {
00154                             if (plane.pln_land == land.lnd_uid) {
00155                                 pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
00156                                    plane.pln_tech,
00157                                    plane.pln_effic,
00158                                    plchr[(int)plane.pln_type].pl_name,
00159                                    plane.pln_uid);
00160                                 if (plane.pln_nuketype != -1) {
00161                                     pr("(%s)",
00162                                        nchr[(int)plane.pln_nuketype].n_name);
00163                                 }
00164                             }
00165                         }
00166                     }
00167                 }
00168             }
00169         }
00170         getsect(sp->shp_x, sp->shp_y, &sect);
00171         if (sect.sct_type != SCT_WATER)
00172             pr(" in a %s %s",
00173                cname(sect.sct_own), dchr[sect.sct_type].d_name);
00174         else
00175             pr(" at sea");
00176         break;
00177     case EF_LAND:
00178         lp = &tgp->land;
00179         tp->trd_owner = lp->lnd_own;
00180         pr("(%3d)  tech %d %d%% %s [",
00181            tp->trd_owner,
00182            lp->lnd_tech, lp->lnd_effic, lchr[(int)lp->lnd_type].l_name);
00183         for (it = I_NONE + 1; it <= I_MAX; ++it) {
00184             if (lp->lnd_item[it])
00185                 pr("%c:%d ", ichr[it].i_mnem, lp->lnd_item[it]);
00186         }
00187         pr("] #%d", tp->trd_unitid);
00188         if (opt_SHOWPLANE) {
00189             snxtitem_all(&ni, EF_PLANE);
00190             while (nxtitem(&ni, &plane)) {
00191                 if (plane.pln_land == lp->lnd_uid && plane.pln_own != 0) {
00192                     pr("\n\t\t\t\t    tech %3d %3d%% %s #%d",
00193                        plane.pln_tech,
00194                        plane.pln_effic,
00195                        plchr[(int)plane.pln_type].pl_name, plane.pln_uid);
00196                     if (plane.pln_nuketype != -1) {
00197                         pr("(%s)", nchr[(int)plane.pln_nuketype].n_name);
00198                     }
00199                 }
00200             }
00201         }
00202         getsect(lp->lnd_x, lp->lnd_y, &sect);
00203         break;
00204     case EF_PLANE:
00205         pp = &tgp->plane;
00206         tp->trd_owner = pp->pln_own;
00207         pr("(%3d)  tech %d %d%% %s #%d",
00208            tp->trd_owner,
00209            pp->pln_tech,
00210            pp->pln_effic,
00211            plchr[(int)pp->pln_type].pl_name, tp->trd_unitid);
00212         if (pp->pln_nuketype != -1) {
00213             pr("(%s)", nchr[(int)pp->pln_nuketype].n_name);
00214         }
00215         break;
00216     default:
00217         pr("flaky unit type %d", tp->trd_type);
00218         break;
00219     }
00220     return 1;
00221 }
00222 
00223 int
00224 trade_getitem(struct trdstr *tp, union empobj_storage *tgp)
00225 {
00226     if (!ef_read(tp->trd_type, tp->trd_unitid, tgp))
00227         return 0;
00228     return 1;
00229 }
00230 
00231 /*
00232  * Return amount due for LOAN at time PAYTIME.
00233  */
00234 double
00235 loan_owed(struct lonstr *loan, time_t paytime)
00236 {
00237     time_t rtime;               /* regular interest time */
00238     time_t xtime;               /* double interest time */
00239     double rate;
00240     int dur;
00241 
00242     /*
00243      * Split interval paytime - l_lastpay into regular (up to
00244      * l_duedate) and extended (beyond l_duedate) time.
00245      */
00246     rtime = loan->l_duedate - loan->l_lastpay;
00247     xtime = paytime - loan->l_duedate;
00248     if (rtime < 0) {
00249         xtime += rtime;
00250         rtime = 0;
00251     }
00252     if (xtime < 0) {
00253         rtime += xtime;
00254         xtime = 0;
00255     }
00256     if (CANT_HAPPEN(rtime < 0))
00257         rtime = 0;
00258 
00259     dur = loan->l_ldur;
00260     if (CANT_HAPPEN(dur <= 0))
00261         dur = 1;
00262     rate = loan->l_irate / 100.0 / (dur * SECS_PER_DAY);
00263 
00264     return loan->l_amtdue * (1.0 + (rtime + xtime * 2) * rate);
00265 }

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