/*
 * coup.c
 */
#include "copyright.h"

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include "defs.h"
#include "struct.h"
#include "data.h"

/* throw a coup */

coup()
{
    register int i;
    register struct planet *l;

    if (me->p_kills < 1.0) {
        new_warning(3,"You must have one kill to throw a coup");
	return;
    }
    if (!(me->p_flags & PFORBIT)) {
        new_warning(4,"You must orbit your home planet to throw a coup");
	return;
    }
    for (i = 0, l = &planets[i]; i < MAXPLANETS; i++, l++) {
	if ((l->pl_owner == me->p_team) && (l->pl_armies > 0)) {
            new_warning(5,"You already own a planet!!!");
	    return;
	}
    }
    l = &planets[me->p_planet];

    if ((!(l->pl_flags & PLHOME)) || ((l->pl_flags & ALLTEAM) != me->p_team)) {
        new_warning(6, "You must orbit your home planet to throw a coup");
	return;
    }

    if (l->pl_armies > 4) {
        new_warning(7,"Too many armies on planet to throw a coup");
	return;
    }

    if (l->pl_couptime > 0) {
        new_warning(UNDEF,"Can't throw a coup for %d more minute%s",l->pl_couptime,(l->pl_couptime>1)?"s!":"!");
	return;
    }

    if (l->pl_flags & PLCOUP) { /* Avoid race conditions */
	return;
    }

    /* the cases are now met.  We can have a coup. */

    if (!(l->pl_flags & PLCOUP)) {       /* avoid couping same thing twice */
	l->pl_flags |= PLCOUP;
	me->p_planets++;
	me->p_genoplanets++;
	if (status->tourn) {
#ifdef NEW_CREDIT
	    me->p_stats.st_tplanets+=3;
	    status->planets+=3;
#else
	    me->p_stats.st_tplanets++;
	    status->planets++;
#endif
	} else {
#ifdef NEW_CREDIT
	    me->p_stats.st_planets+=3;
#else
	    me->p_stats.st_planets++;
#endif
	}
    }
}
