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

#include <stdio.h>
#include "Wlib.h"
#include "defs.h"
#include "struct.h"
#include "data.h"

#ifdef GALAXY
static char *classes[NUM_TYPES] = {
	"SC", "DD", "CA", "BB", "AS", "SB", "GA", "  "
};
#else
static char *classes[NUM_TYPES] = {
	"SC", "DD", "CA", "BB", "AS", "SB", "  "
};
#endif /*GALAXY*/

/* isae: For sorting playerlist */
static short teamArray[NUMTEAM+1] = { FED, ROM, KLI, ORI, NOBODY };

/* isae: For number of players/race */
static int teamCount[9];

playerlist()
{
    int i;
    char buf[100];

    /*
    W_ClearWindow(playerw);
    */
    (void) strcpy(buf, "  Type Rank      Name            Kills   Win  Loss  Ratio Offense Defense     DI");
    W_WriteText(playerw, 0, 1, textColor, buf, strlen(buf), W_RegularFont);
    for (i=0; i<MAXPLAYER; i++) {
	updatePlayer[i]=1;
    }
    if (!sortPlayers) playerlist2();
    else sortedPlayerList(); 
}

/* isae: my own attempt to sort the playerlist */
sortedPlayerList()
{
    register int i, k, l;
    char buf[100];
    register struct player *j;
    int kills, losses;
    double ratio;
    float pRating, oRating, dRating, bRating;
    
    if (!W_IsMapped(playerw)) return;
    for (k=0, l=(-1); k<NUMTEAM+1; k++){
      for (i = 0, j = &players[i]; i < MAXPLAYER; i++, j++) {
	if (j->p_team != teamArray[k]) continue;
	l++;
	if (!updatePlayer[j->p_no]) continue;
	updatePlayer[j->p_no]=0;
	if (j->p_status != PALIVE) {
	  /* XFIX */
	  W_ClearArea(playerw, 0, l+2, 83, 1);
	  continue;
	}
	if (j->p_ship.s_type == STARBASE) {
	  kills=j->p_stats.st_sbkills;
	  losses=j->p_stats.st_sblosses;
	} else {
	  kills=j->p_stats.st_kills + j->p_stats.st_tkills;
	  losses=j->p_stats.st_losses + j->p_stats.st_tlosses;
	}
	if (losses==0) {
	  ratio=kills;
	} else {
	  ratio=(double) kills/losses;
	}
	if(!j->p_stats.st_tticks){
	  oRating = 0.0;
	  pRating = 0.0;
	  bRating = 0.0;
	}
	else{
	  oRating = offenseRating(j);
		pRating = planetRating(j);
		bRating = bombingRating(j);
	      }
	dRating = defenseRating(j);
	(void) sprintf(buf, "%c%c %s  %-9.9s %-16.16s%5.2f %5d %5d %6.2lf   %5.2f   %5.2f %8.2f",
		       teamlet[j->p_team],
		       shipnos[j->p_no],
		       classes[j->p_ship.s_type],
		       ranks[j->p_stats.st_rank].name,
		       j->p_name,
		       j->p_kills,
		       kills,
		       losses,
		       ratio,
		       oRating,
		       dRating,
		       (oRating+pRating+bRating)*(j->p_stats.st_tticks/36000.0));
	W_WriteText(playerw, 0, l+2, playerColor(j), buf, strlen(buf),
		    shipFont(j));
      }
    }
    /* isae: fix to clear the rest of the lines -- no more doubles*/
    for (; l<MAXPLAYER; l++)
      W_ClearArea(playerw, 0, l+2, 83, 1);
    showTotalPlayers();
}

playerlist2()
{
    register int i;
    char buf[100];
    register struct player *j;
    int kills, losses;
    double ratio;
    float pRating, oRating, dRating, bRating;

    if (!W_IsMapped(playerw)) return;
    for (i = 0, j = &players[i]; i < MAXPLAYER; i++, j++) {
	if (!updatePlayer[i]) continue;
	updatePlayer[i]=0;
	if (j->p_status != PALIVE) {
	    /* XFIX */
	    W_ClearArea(playerw, 0, i+2, 83, 1);
	    continue;
	}
	if (j->p_ship.s_type == STARBASE) {
	    kills=j->p_stats.st_sbkills;
	    losses=j->p_stats.st_sblosses;
	} else {
	    kills=j->p_stats.st_kills + j->p_stats.st_tkills;
	    losses=j->p_stats.st_losses + j->p_stats.st_tlosses;
	}
	if (losses==0) {
	    ratio=kills;
	} else {
	    ratio=(double) kills/losses;
	}
	if(!j->p_stats.st_tticks){
		oRating = 0.0;
		pRating = 0.0;
		bRating = 0.0;
	}
	else{
		oRating = offenseRating(j);
		pRating = planetRating(j);
		bRating = bombingRating(j);
	}
	dRating = defenseRating(j);
	(void) sprintf(buf, "%c%c %s  %-9.9s %-16.16s%5.2f %5d %5d %6.2lf   %5.2f   %5.2f %8.2f",
	    teamlet[j->p_team],
	    shipnos[j->p_no],
	    classes[j->p_ship.s_type],
	    ranks[j->p_stats.st_rank].name,
	    j->p_name,
	    j->p_kills,
	    kills,
	    losses,
	    ratio,
	    oRating,
	    dRating,
	    (oRating+pRating+bRating)*(j->p_stats.st_tticks/36000.0));
	W_WriteText(playerw, 0, i+2, playerColor(j), buf, strlen(buf),
	    shipFont(j));
    }
    showTotalPlayers();
}

showTotalPlayers()
{
  char buf[80];
  sprintf(buf,"Feds: %d      Roms: %d     Kli: %d     Ori: %d      Independent: %2d", numShips(FED), numShips(ROM), numShips(KLI), numShips(ORI), numShips(NOBODY));
  W_WriteText(playerw, 0, 22, textColor, buf, strlen(buf), W_RegularFont);
}















