src/lib/update/deliver.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  *  deliver.c: Deliver commodities to neighboring sector
00029  * 
00030  *  Known contributors to this file:
00031  *  
00032  */
00033 
00034 #include <config.h>
00035 
00036 #include "item.h"
00037 #include "path.h"
00038 #include "plague.h"
00039 #include "update.h"
00040 
00041 #define DELIVER_BONUS 4.0
00042 
00043 static int
00044 deliver(struct sctstr *from, struct ichrstr *ip, int dir,
00045         int thresh, int amt_src, int plague, i_packing packing)
00046 {
00047     struct sctstr *to;
00048     i_type vtype;                       /* item vartype */
00049     int amt_moved;
00050     int amt_dst;
00051     int mobility;
00052     double mcost;
00053     struct dchrstr *dp;
00054     int n;
00055 
00056     if (dir <= 0 || dir > DIR_UL)
00057         return 0;
00058     if (amt_src <= 0)
00059         return 0;
00060     if ((amt_moved = amt_src - thresh) <= 0)
00061         return 0;
00062     /*
00063      * make sure delivery looks ok.  Check where its going,
00064      * where its coming from, and see if there is more than
00065      * the threshold amount
00066      */
00067     if (!military_control(from))
00068         return 0;
00069     to = getsectp(from->sct_x + diroff[dir][0],
00070                   from->sct_y + diroff[dir][1]);
00071     if (to->sct_own != from->sct_own) {
00072         wu(0, from->sct_own, "%s delivery walkout at %s\n",
00073            ip->i_name, ownxy(from));
00074         return 0;
00075     }
00076     dp = &dchr[from->sct_type];
00077     vtype = ip->i_uid;
00078     mobility = from->sct_mobil / 2;
00079     if (vtype == I_CIVIL) {
00080         if (from->sct_own != from->sct_oldown) {
00081             wu(0, from->sct_own,
00082                "The conquered populace in %s refuses to relocate!\n",
00083                ownxy(from));
00084             return 0;
00085         }
00086         if (to->sct_own != to->sct_oldown) {
00087             wu(0, from->sct_own,
00088                "Citizens in %s refuse to relocate!\n", ownxy(from));
00089             return 0;
00090         }
00091     }
00092     /*
00093      * disallow delivery into prohibited sectors.
00094      * calculate unit movement cost; decrease amount if
00095      * there isn't enough mobility.
00096      */
00097     mcost = sector_mcost(to, MOB_MOVE) * ip->i_lbs / ip->i_pkg[packing];
00098     mcost /= DELIVER_BONUS;
00099 
00100     if (mobility < mcost * amt_moved) {
00101         /* XXX can mcost be == 0? */
00102         amt_moved = (int)(mobility / mcost);
00103         if (amt_moved <= 0)
00104             return 0;
00105     }
00106     amt_dst = to->sct_item[vtype];
00107     if (amt_moved > ITEM_MAX - amt_dst) {
00108         /* delivery backlog */
00109         amt_moved = ITEM_MAX - amt_dst;
00110     }
00111     to->sct_item[vtype] = amt_moved + amt_dst;
00112     /* deliver the plague too! */
00113     if (plague == PLG_INFECT && to->sct_pstage == PLG_HEALTHY)
00114         to->sct_pstage = PLG_EXPOSED;
00115     n = from->sct_mobil - (int)(mcost * amt_moved);
00116     if (n < 0)
00117         n = 0;
00118     from->sct_mobil = n;
00119     return amt_moved;
00120 }
00121 
00122 void
00123 dodeliver(struct sctstr *sp)
00124 {
00125     i_type i;
00126     int thresh;
00127     int dir;
00128     int plague;
00129     i_packing packing;
00130     int n;
00131 
00132     if (sp->sct_mobil <= 0)
00133         return;
00134     plague = sp->sct_pstage;
00135     packing = sp->sct_effic >= 60 ? dchr[sp->sct_type].d_pkg : IPKG;
00136     for (i = I_NONE + 1; i <= I_MAX; i++) {
00137         if (sp->sct_del[i] == 0)
00138             continue;
00139         thresh = sp->sct_del[i] & ~0x7;
00140         dir = sp->sct_del[i] & 0x7;
00141         n = deliver(sp, &ichr[i], dir, thresh, sp->sct_item[i],
00142                     plague, packing);
00143         if (n > 0) {
00144             sp->sct_item[i] -= n;
00145             if (sp->sct_mobil <= 0)
00146                 break;
00147         }
00148     }
00149 }

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