#ifndef lint
static char *RCSid = "$Header: /usr/brule/guest/empire/empire/emprcs/lib/commands/sell.c,v 2.0 1995/01/25 19:23:25 empire Exp $";
#endif

/*
 * sell.c
 *
 * engage in inter-nation trading of commodities.
 * Place commodities on-sale at the deity trading post.
 *
 * Dave Pare, 1986
 */

#include "misc.h"
#include "player.h"
#include "xy.h"
#include "file.h"
#include "var.h"
#include "sect.h"
#include "item.h"
#include "nsc.h"
#include "nat.h"
#include "nuke.h"
#include "plane.h"
#include "ship.h"
#include "land.h"
#include "trade.h"
#include <math.h>		/* bailey@math-cs.kent.edu */

/*
 * format: sell <COMMODITY> <SECTS> <NUMBER> <PRICE>
 *   where NUMBER represents either the number to reach
 *   or, if negative, the abs number to try and get from
 *   each sector.
 */
sell()
{
	struct sctstr	sect;
	struct nstr_sect nstr;
	struct ichrstr	*ip;
	int     number_set;
	int     number_sub;
	int     product;
	int     totalcom;
	int     amt;
	int     com;
	s_char	*p;
	float   price;
	s_char	buf[1024];

	if ((ip = whatitem(player->argp[1], "Commodity to sell : ")) == 0)
		return RET_SYN;
	if (ip->i_sell == 0) {
		pr("You can't sell %s\n", ip->i_name);
		return RET_FAIL;
	}
	product = ip - ichr;
	if (!snxtsct(&nstr, player->argp[2]))
		return RET_SYN;
	number_set = 0;
	number_sub = 0;
	if ((p = getstarg(player->argp[3], "amount :  ", buf)) == 0 || *p == 0)
		return RET_SYN;
	number_set = atoi(p);
	if (number_set < 0) {
		number_sub = -number_set;
		number_set = 0;
	}
	if ((p = getstarg(player->argp[4], "price : ", buf)) == 0 || *p == 0)
		return RET_SYN;
	price = atof(p);
	if (price <= 0.0) {
		pr("No sale.\n");
		return RET_FAIL;
	}
	if ((price == HUGE) || (price > 1000000.0)) /* Inf can cause overflow */
		price = 1000000.0;		  /* bailey@math-cs.kent.edu */
	totalcom = 0;
	while (nxtsct(&nstr, &sect) > 0) {
		if (!player->owner)
			continue;
		/*
		 * military control necessary to sell
		 * goodies in occupied territory.
		 */
		if (sect.sct_oldown != player->cnum) {
			int     tot_mil=0;
			struct  nstr_item ni;
			struct  lndstr land;
			snxtitem_xy(&ni, EF_LAND, sect.sct_x, sect.sct_y);
			while (nxtitem(&ni, (s_char *)&land)){
				if (land.lnd_own == player->cnum)
					tot_mil  += total_mil(&land);
			}
			if ((getvar(V_MILIT,(s_char *)&sect,EF_SECTOR)+tot_mil)
				*10 < getvar(V_CIVIL,(s_char *)&sect,EF_SECTOR))
				continue;
		}
		if (((amt = getvar(ip->i_vtype, (s_char *)&sect,EF_SECTOR))) == 0)
			continue;
		if (number_sub)
			com = min(number_sub, amt);
		else
			com = amt - number_set;
		if (com <= 0)
			continue;
		totalcom += com;
		amt -= com;
		pr("Sold %d %s at %s (%d left)\n", com, ip->i_name,
		       xyas(sect.sct_x, sect.sct_y, player->cnum), amt);
		putvar(ip->i_vtype, amt, (s_char *)&sect, EF_SECTOR);
		putsect(&sect);
	}
	if (totalcom > 0) {
		player->dolcost += (double) (totalcom * ip->i_lbs / 10);
		if (!commlock()) {
			pr("Unable to lock commodity file; get help!\n");
			return RET_SYS;
		}
		if (commread() < 0) {
			(void) communlock();
			pr("Unable to read commodity file; get help!\n");
			return RET_SYS;
		}
		commset(player->cnum, product, totalcom);
		commprice(product, &price);
		if (commwrite() < 0) {
			(void) communlock();
			pr("Unable to write commodity file; get help!\n");
			return RET_SYS;
		}
		(void) communlock();
		pr("Total %s put on the market : %d\n", ip->i_name,
		       totalcom);
		pr("item unit cost : %8.2f\n", price);
		return RET_OK;
	}
	else {
		pr("No eligible %s for sale\n", ip->i_name);
		return RET_FAIL;
	}
}
