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

#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <ctype.h>
#include <varargs.h>
#include "defs.h"
#include "struct.h"
#include "data.h"

/* new code, sends bouncemsg to bounceto from GOD 4/17/92 TC */
/* Moved to smessage and changed to varargs 8/2/93 NBT */
/*VARARGS*/
bounce(bounceto, va_alist)
int bounceto;
va_dcl
{
    char buf[15];
    va_list args;
    va_start(args);

    sprintf(buf, "GOD->%2s",players[bounceto].p_mapchars);

    do_message(bounceto, MINDIV, buf, 255, args);
    va_end(args);
}

/*VARARGS*/
pmessage(recip, group, address, va_alist)
int recip;
int group;
char *address;
va_dcl
{
    /* Message from God */
    va_list args;
    va_start(args);
    do_message(recip, group, address, 255, args);
    va_end(args);
}

/*VARARGS*/
pmessage2(recip, group, address, from,va_alist)
int recip;
int group;
char *address;
u_char from;
va_dcl
{
    va_list args;
    va_start(args);
    do_message(recip,group,address,from,args);
    va_end(args);
}

/*VARARGS*/
do_message(recip, group, address, from, args)
int recip;
int group;
char *address;
u_char from;
va_list args;
{
    char *fmt;
    struct message *cur;
    char temp[150];
    char temp2[150];
    int mesgnum;

    fmt = va_arg(args, char *);

    if (strlen(fmt) == 0) {
	ERROR(1,("ntserv: bogus pmessage call!!\n"));
	return;
    }
    vsprintf(temp,fmt,args);

    sprintf(temp2,"%-9s %s",address,temp);
    temp2[MSG_LEN - 1]='\0';      /* added 3/8/93  NBT */

    if ((mesgnum = ++(mctl->mc_current)) >= MAXMESSAGE) {
	mctl->mc_current = 0;
	mesgnum = 0;
    }
    cur = &messages[mesgnum];
    cur->m_flags = group;
    cur->m_no = mesgnum;
    cur->m_recpt = recip;
    cur->m_from = from;

    strcpy(cur->m_data,temp2);

#ifdef SHORT_PACKETS
    message_flag(cur,address);
#endif
    cur->m_flags |= MVALID;

    if ((cur->m_flags & MINDIV) && (cur->m_recpt == me->p_no) && 
	(cur->m_from == me->p_no))
	check_command(cur);
}
