src/lib/commands/sell.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  *  sell.c: Sell commodities to other nations.
00029  * 
00030  *  Known contributors to this file:
00031  *     Dave Pare, 1986
00032  *     Jeff Bailey
00033  *     Pat Loney, 1992
00034  *     Steve McClure, 1996
00035  */
00036 
00037 #include <config.h>
00038 
00039 #include "commands.h"
00040 #include "commodity.h"
00041 #include "item.h"
00042 #include "land.h"
00043 #include "optlist.h"
00044 #include "plane.h"
00045 #include "ship.h"
00046 
00047 /*
00048  * format: sell <COMMODITY> <SECTS> <NUMBER> <PRICE>
00049  *   where NUMBER represents either the number to reach
00050  *   or, if negative, the abs number to try and get from
00051  *   each sector.
00052  */
00053 int
00054 sell(void)
00055 {
00056     struct sctstr sect;
00057     struct ichrstr *ip;
00058     struct comstr comm;
00059     int number_set;
00060     int number_sub;
00061     int totalcom;
00062     int amt;
00063     int com;
00064     char *p;
00065     float price;
00066     time_t now;
00067     int ii = 0;
00068     coord x, y;
00069     char buf[1024];
00070 
00071     if (!opt_MARKET) {
00072         pr("The market is disabled.\n");
00073         return RET_FAIL;
00074     }
00075     check_market();
00076     check_trade();
00077     if (!(ip = whatitem(player->argp[1], "Commodity you want to sell: ")))
00078         return RET_SYN;
00079     if (ip->i_sell == 0) {
00080         pr("You can't sell %s\n", ip->i_name);
00081         return RET_FAIL;
00082     }
00083     if (!(p = getstarg(player->argp[2], "Sector to sell from: ", buf)))
00084         return RET_SYN;
00085     if (!sarg_xy(p, &x, &y))
00086         return RET_SYN;
00087     if (!getsect(x, y, &sect))
00088         pr("Could not access that sector.\n");
00089     if ((sect.sct_type != SCT_HARBR && sect.sct_type != SCT_WAREH) ||
00090         !player->owner) {
00091         pr("That sector cannot sell goods.\n");
00092         return RET_FAIL;
00093     }
00094     if (sect.sct_effic < 60) {
00095         pr("Sectors need to be >= 60%% efficient to sell goods.\n");
00096         return RET_FAIL;
00097     }
00098     if (sect.sct_mobil <= 0) {
00099         pr("Sectors need at least 1 mobility to sell goods.\n");
00100         return RET_FAIL;
00101     }
00102     number_sub = 0;
00103     if ((p = getstarg(player->argp[3], "Quantity: ", buf)) == 0 || *p == 0)
00104         return RET_SYN;
00105     if (!check_sect_ok(&sect))
00106         return RET_FAIL;
00107     number_set = atoi(p);
00108     if ((p = getstarg(player->argp[4], "Price per unit: ", buf)) == 0 ||
00109         *p == 0)
00110         return RET_SYN;
00111     if (!check_sect_ok(&sect))
00112         return RET_FAIL;
00113     price = atof(p);
00114     if (price <= 0.0) {
00115         pr("No sale.\n");
00116         return RET_FAIL;
00117     }
00118     if (price > 1000.0)         /* Inf can cause overflow */
00119         price = 1000.0;         /* bailey@math-cs.kent.edu */
00120     totalcom = 0;
00121     if (!military_control(&sect)) {
00122         pr("Military control required to sell goods.\n");
00123         return RET_FAIL;
00124     }
00125     if ((amt = sect.sct_item[ip->i_uid]) == 0) {
00126         pr("You don't have any %s to sell there.\n", ip->i_name);
00127         return RET_FAIL;
00128     }
00129     if (number_set >= 0)
00130         com = MIN(number_set, amt);
00131     else
00132         com = amt + number_set;
00133     if (com <= 0)
00134         return RET_SYN;
00135     totalcom += com;
00136     amt -= com;
00137     pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
00138        xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
00139     sect.sct_item[ip->i_uid] = amt;
00140     putsect(&sect);
00141     if (totalcom > 0) {
00142         for (ii = 0; getcomm(ii, &comm); ii++) {
00143             if (comm.com_owner == 0)
00144                 break;
00145         }
00146         if (getcomm(ii, &comm) == 0)
00147             ef_extend(EF_COMM, 1);
00148         (void)time(&now);
00149         comm.com_type = ip->i_uid;
00150         comm.com_owner = player->cnum;
00151         comm.com_price = price;
00152         comm.com_maxbidder = player->cnum;
00153         comm.com_markettime = now;
00154         comm.com_amount = totalcom;
00155         comm.com_x = 0;
00156         comm.com_y = 0;
00157         comm.sell_x = sect.sct_x;
00158         comm.sell_y = sect.sct_y;
00159         comm.com_uid = ii;
00160         if (!putcomm(ii, &comm)) {
00161             pr("Problems with the commodities file, call the Deity\n");
00162             return RET_FAIL;
00163         }
00164     } else {
00165         pr("No eligible %s for sale\n", ip->i_name);
00166         return RET_FAIL;
00167     }
00168     return RET_OK;
00169 }

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