#ifndef lint
static char *RCSid = "$Header: /users/empire/EMP/empmain/COMS/RCS/demo.c,v 1.4 89/06/09 03:33:07 empire Exp $";
#endif /* not lint */

/*
 * demo.c
 *
 * de-mobilize "n" military in a given sector.
 * You get to pay $5/mil for "GI Bill", and the
 * sector must have at least 60% efficiency.
 *
 * Dave Pare, 1986
 */

#include "misc.h"
#include "xy.h"
#include "deity.h"
#include "var.h"
#include "sect.h"
#include "nat.h"
#include "nsc.h"
#include "file.h"

/*
 * format: demo <SECTS> number
 *
 */
demo()
{
	extern	char *argp[];
	struct	natstr *natp;
	struct	nstr_sect nstr;
	struct	sctstr sect;
	int	mil_demob;
	int	mil;
	int	civ;
	int	deltamil;
	int	number;
	char	*p;
	int	reserves;

	natp = getnatp(cnum);
	if (!snxtsct(&nstr, argp[1]))
		return RET_SYN;
	if ((p = getstarg(argp[2], "Number to de-mobilize : ")) == 0)
		return RET_SYN;
	number = atoi(p);
	if (!(p = getstarg(argp[3], "New civilians on active reserve? (y/n) ")))
		return RET_SYN;
	if (*p != 'y' && *p != 'n')
		return RET_SYN;
	mil_demob = 0;
	reserves = 0;
	while (nxtsct(&nstr, &sect)) {
		if (!owner || sect.sct_effic < 60)
			continue;
		if ((mil = getvar(V_MILIT, (char *)&sect, EF_SECTOR)) == 0)
			continue;
		if (sect.sct_own != sect.sct_oldown)
			continue;
		civ = getvar(V_CIVIL, (char *)&sect, EF_SECTOR);
		if (number < 0){
			if ((deltamil = mil + number) <= 0)
				continue;
		}
		else if ((deltamil = min(mil, number)) <= 0)
			continue;
		civ += deltamil;
		mil -= deltamil;
		mil_demob += deltamil;
		dolcost += (double) (deltamil * 5);
		pr(fmt("%d demobilized in %s (%d mil left)\n",
			deltamil, xyas(sect.sct_x, sect.sct_y, cnum), mil));
		if (*p == 'y')
			reserves += deltamil;
		putvar(V_MILIT, mil, (char *)&sect, EF_SECTOR);
		putvar(V_CIVIL, civ, (char *)&sect, EF_SECTOR);
		putsect(&sect);
	}
	if (!mil_demob) {
		pr("No eligible sectors/military for demobilization\n");
		return RET_FAIL;
	}
	pr(fmt("Total new civilians : %d\n", mil_demob));
	if (*p == 'y')
		pr(fmt("Military reserve stands at %d (up %d)\n",
			natp->nat_reserve + reserves, reserves));
	NAT_DELTA(nat_reserve, cnum, reserves);
	return RET_OK;
}
