/*
 * setres.c
 *
 * David Muir Sharnoff
 *
 * This command NEEDS TO BE REDONE.
 * Karl Hagen
 *
 */

#include <stdio.h>
#include <ctype.h>
#include "misc.h"
#include "player.h"
#include "var.h"
#include "xy.h"
#include "sect.h"
#include "nat.h"
#include "news.h"
#include "nsc.h"
#include "item.h"
#include "file.h"


static resnoise();

/*
 * format: setres resource <amt>  <sect>
 */
setres()
{
	struct	sctstr sect;
	s_char	*what;
	int	amt;
	s_char	*p;
	struct	nstr_sect nstr;
	s_char	buf[1024];

	if ((what = getstarg(player->argp[1],
		"Set What (iron, gold, oil, uranium, fertility)? ", buf)) == 0) 
		return RET_SYN;
	switch (what[0]) {
	case 'i':
		if (!snxtsct(&nstr, player->argp[2]))
                 	return RET_SYN;
		while (nxtsct(&nstr,&sect) > 0) {
			if( !(p = getstarg(player->argp[3], "What value : ", buf)) || 
			    (*p == '\0'))
				return RET_SYN;
			amt = atoi(p);
			if (amt > 100)
		 		amt = 100;
			if (amt < 0)
				amt = 0;
			if (sect.sct_own != 0)
				resnoise(&sect,1,"Iron ore content",
					(int) sect.sct_min, amt);
			sect.sct_min = (u_char) amt;
			putsect(&sect);
			}
		break;
	case 'g':
		if (!snxtsct(&nstr, player->argp[2]))
                 	return RET_SYN;
		while (nxtsct(&nstr,&sect) > 0) {
			if( !(p = getstarg(player->argp[3], "What value : ", buf)) || 
			    (*p == '\0'))
				return RET_SYN;
			amt = atoi(p);
			if (amt > 100)
		 		amt = 100;
			if (amt < 0)
				amt = 0;
			if (sect.sct_own != 0) 
				resnoise(&sect,1,"Gold content",
					(int) sect.sct_gmin, amt);
			sect.sct_gmin = (u_char) amt;
			putsect(&sect);
			}
		break;
	case 'o':
		if (!snxtsct(&nstr, player->argp[2]))
			return RET_SYN;
		while (nxtsct(&nstr,&sect) > 0) {
			if( !(p = getstarg(player->argp[3], "What value : ", buf)) || 
			    (*p == '\0'))
				return RET_SYN;
			amt = atoi(p);
			if (amt > 100)
		 		amt = 100;
			if (amt < 0)
				amt = 0;
			if (sect.sct_own != 0)
				resnoise(&sect,1,"Oil content",
					(int) sect.sct_oil, amt);
			sect.sct_oil = (u_char) amt;
			putsect(&sect);
			}
		break;
	case 'f':
		if (!snxtsct(&nstr, player->argp[2]))
			return RET_SYN;
		while (nxtsct(&nstr,&sect) > 0) {
			if( !(p = getstarg(player->argp[3], "What value : ", buf)) || 
			    (*p == '\0'))
				return RET_SYN;
			amt = atoi(p);
			if (amt > 100)
		 		amt = 100;
			if (amt < 0)
				amt = 0;
			if (sect.sct_own != 0)
				resnoise(&sect,1,"Fertility content",
					(int) sect.sct_fertil, amt);
			sect.sct_fertil = (u_char) amt;
			putsect(&sect);
			}
		break;
	case 'u':
		if (!snxtsct(&nstr, player->argp[2]))
			return RET_SYN;
		while (nxtsct(&nstr,&sect) > 0) {
			if( !(p = getstarg(player->argp[3], "What value : ", buf)) || 
			    (*p == '\0'))
				return RET_SYN;
			amt = atoi(p);
			if (amt > 100)
		 		amt = 100;
			if (amt < 0)
				amt = 0;
			if (sect.sct_own != 0) 
				resnoise(&sect,1,"Uranium content",
					(int) sect.sct_uran, amt);
			sect.sct_uran = (u_char) amt;
			putsect(&sect);
			}
		break;
	default:
		pr("huh?\n");
		return RET_SYN;
	}
	return RET_OK;
}

static
resbenefit(who, good) 
	natid	who;
	int	good;
{
#ifdef QUIET
	return;
#endif

	if (good) {
		if (who)
			nreport(player->cnum, N_AIDS, who, 1);
	} else {
		if (who)
			nreport(player->cnum, N_HURTS, who, 1);
	}
}

static
resnoise(sptr, public_amt, name, old, new)
	struct	sctstr *sptr;
	int	public_amt;
	s_char	*name;
	int	old, new;
{
	s_char	p[100];

	pr("%s of %s changed from %d to %d%\n",
		name, xyas(sptr->sct_x, sptr->sct_y, player->cnum), old, new);
	if (public_amt) 
		(void) sprintf(p, "changed from %d to %d", old, new);
	else
		(void) sprintf(p, "%s", old<new ? "increased":"decreased");
#ifndef QUIET
	if (sptr->sct_own)
		wu(0, sptr->sct_own,
			"%s in %s was %s by an act of %s\n",
			    name, xyas(sptr->sct_x, sptr->sct_y, sptr->sct_own),
			    p, cname(player->cnum));
	resbenefit(sptr->sct_own, (old < new));
#endif
}
