/*
 * dmessage.c 
 *
 * for the client of a socket based protocol.
 */
#include "copyright.h"

#include <stdio.h>
#include <math.h>
#include "Wlib.h"
#include "defs.h"
#include "xsg_defs.h"
#include "struct.h"
#include "localdata.h"

#ifdef SHORT_PACKETS
char *whydeadmess[] = { "", "[quit]", "[photon]", "[phaser]", "[planet]", 
	"[explosion]", "", "", "[ghostbust]", "[genocide]", "", "[plasma]",
	"[detted photon]", "[chain explosion]", "[TEAM]","","[team det]",
	"[team explosion]"};
#define numwhymess 17   /* Wish there was a better way for this - NBT */
#endif


dmessage(mess)
int mess;
{
    register int len;
    W_Color color;

#ifdef RCD
    struct distress dist;
#endif
    
    char message[MSG_LEN];
    unsigned char flags = (unsigned char)messages[mess].m_flags;
    unsigned char from  = (unsigned char)messages[mess].m_from;
    unsigned char recpt = (unsigned char)messages[mess].m_recpt;

#ifdef SHORT_PACKETS
    int templen;
    char *nullstart;
    char tempmsg[150];
#endif

    strcpy(message,messages[mess].m_data);
    color=textColor;

    if (from==255) {
	/* From God */
	color=textColor;
    } else if(from < MAXPLAYER){
	color=playerColor(&(players[from]));
    }
    else
#ifdef NBR
        if (from == 254) 
		from = 255;
	else {	
#else
        {
#endif
	    fprintf(stderr, "Bad 'from' field (%d) for message \"%s\"\n",
	 		from, message);
            return;
	}

#ifdef RCD
        /* kludge for clients that can't handle RCD's */

	if (from < MAXPLAYER) {
          if (flags == (MTEAM | MDISTR | MVALID)) {
                char buf[MSG_LEN];
		flags ^= MDISTR;
                HandleGenDistr(message,from,recpt,&dist);
                makedistress(&dist,buf,distmacro[dist.distype].macro);
                /* note that we do NOT send the F0->FED part
                   so strncat is fine */
                buf[MSG_LEN - strlen(message)] = '\0';
                strcpy(message,buf);
	  }
        }
#endif


    /* Kludge stuff for report kills... 
     */

    message[79] = '\0';		/* make sure the message isn't garbage */
    len = strlen(message);

#ifdef SHORT_PACKETS
    if ((messages[mess].args[0]==DMKILL)||(messages[mess].args[0]==DMBOMB)) {
#else
    if ((strncmp(message, "GOD->ALL", 8)==0 && 
	  (instr(message, "was kill") ||
	  instr(message, "killed by"))) || 
	  (*message!=' ' && instr(message, "We are being attacked"))) {
#endif
	if (!reportKills) return;
	strcpy(tempmsg,message);
#ifdef SHORT_PACKETS
	nullstart = strstr(tempmsg,"(null)");
	if (nullstart!=NULL) {
	    nullstart[0]='\0';
	    strcat(tempmsg,whydeadmess[messages[mess].args[5]]);
	}
#endif
	templen = strlen(tempmsg);
	if (templen>(MSG_LEN-1)) tempmsg[MSG_LEN-1]='\0';
	W_WriteText(reviewWin, 0, 0, color, tempmsg, templen, 0);
	return;
    }
    if (!strncmp(message+5, "GOD", 3) && msgBeep)	/* msg to GOD? */
	W_Beep();

    /* suppress warnings */
    if (!strncmp(message, "WRN->", 5)) return;
    
    W_WriteText(reviewWin, 0, 0, color, message, len, 0);

/*    update_sys_defaults();*/		/* after "rules of game" message */
}


instr(string1, string2)
char *string1, *string2;
{
    char *s;
    int length;

    length=strlen(string2);
    for (s=string1; *s!=0; s++) {
	if (*s == *string2 && strncmp(s, string2, length)==0) return(1);
    }
    return(0);
}

checkMsgs()
{
    while (oldmctl != mctl->mc_current) {
	oldmctl++;
	if (oldmctl>=MAXMESSAGE) oldmctl=0;
	dmessage(oldmctl);
    }
}

