home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume10 / xtrek / part02 / scorelist.c < prev   
C/C++ Source or Header  |  1990-10-23  |  5KB  |  174 lines

  1. static char sccsid[] = "@(#)scorelist.c    3.1";
  2. /* Copyright (c) 1986     Chris Guthrie */
  3.  
  4.  
  5. #include <X11/Xlib.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <signal.h>
  9. #include "defs.h"
  10. #include "data.h"
  11. #include "bitmaps.h"
  12.  
  13. struct score {
  14.     int    s_no;
  15.     char    *p_name;
  16.     double    st_kills;
  17.     double    st_maxkills;
  18.     int    st_losses;
  19.     double    skill;
  20.     int    st_torps;
  21.     int    st_phasers;
  22.     int    st_planets;
  23.     int    st_armsbomb;
  24. } player_scores[MAXPLAYER];
  25.  
  26. score_cmp(s1, s2)
  27. register struct score    *s1, *s2;
  28. {
  29.     if (s1->skill < s2->skill)
  30.         return(1);
  31.     if (s1->skill > s2->skill)
  32.         return(-1);
  33.     if (s1->st_kills < s2->st_kills)
  34.         return(1);
  35.     if (s1->st_kills > s2->st_kills)
  36.         return(-1);
  37.     if (s1->st_maxkills < s2->st_maxkills)
  38.         return(1);
  39.     if (s1->st_maxkills > s2->st_maxkills)
  40.         return(-1);
  41.     return(0);
  42. }
  43.  
  44. scorelist(p, fid)
  45. register struct player    *p;
  46. int            fid;
  47. {
  48.     register int i, t;
  49.     register int k = 0;
  50.     char buf[BUFSIZ];
  51.     register struct player *j;
  52.     struct player team;
  53.     int active_team;
  54.     int    ps_index;
  55.     struct score    *psp;
  56.  
  57.     strcpy(buf, " # Name              Kills MaxK  Killed Skill    Torps Phasers Planets Bombed");
  58.     if (p) {
  59.         p->p_infomapped = 1;
  60.         p->infow = XCreateWindow(p->display, p->mapw, 10, 10, 77 * fontWidth(p->dfont),
  61.         (MAXPLAYER + NUMTEAM + 4) * fontHeight(p->dfont), 2, DefaultDepth(p->display, p->screen), InputOutput,
  62.         (Visual *) CopyFromParent, 0L, (XSetWindowAttributes *) 0);
  63.     /*    (MAXPLAYER + NUMTEAM + 4) * fontHeight(dfont), 2, p->foreTile, p->backTile);*/
  64.         XSetWindowBackground(p->display, p->infow, p->backColor);
  65.         XMapWindow(p->display, p->infow);
  66.         XDrawImageString(p->display, p->infow, p->dfgc, 0, fontHeight(p->dfont), buf,
  67.            strlen(buf));
  68.         k = 2;
  69.     } else {
  70.     write(fid, buf, strlen(buf));
  71.     write(fid, "\n", 1);
  72.     }
  73.  
  74.     ps_index = 0;
  75.     for (i = 0, j = &players[i]; i < MAXPLAYER; i++, j++) {
  76.     if (j->p_status == PFREE)
  77.         continue;
  78.     psp = &player_scores[ps_index++];
  79.     psp->s_no = j->p_ship->s_no;
  80.     psp->p_name = j->p_name;
  81.     psp->st_kills = j->p_stats.st_kills;
  82.     psp->st_maxkills = j->p_stats.st_maxkills;
  83.     psp->st_losses = j->p_stats.st_losses;
  84.     psp->skill = (j->p_stats.st_losses == 0) ? 
  85.         (j->p_stats.st_kills * 2) : j->p_stats.st_kills / j->p_stats.st_losses;
  86.     psp->st_torps = j->p_stats.st_torps;
  87.     psp->st_phasers = j->p_stats.st_phasers;
  88.     psp->st_planets = j->p_stats.st_planets;
  89.     psp->st_armsbomb = j->p_stats.st_armsbomb;
  90.     }
  91.     qsort(player_scores, ps_index, sizeof (struct score), score_cmp);
  92.  
  93.     for (i = 0, psp = &player_scores[0]; i < ps_index; i++, psp++) {
  94.     sprintf(buf, " %1x %-16.16s %6.2f %5.2f %6d %5.2f %7d %7d %7d %6d",
  95.         psp->s_no,
  96.         psp->p_name,
  97.         psp->st_kills,
  98.         psp->st_maxkills,
  99.         psp->st_losses,
  100.         psp->skill,
  101.         psp->st_torps,
  102.         psp->st_phasers,
  103.         psp->st_planets,
  104.         psp->st_armsbomb
  105.         );
  106.     if (!p) {
  107.         write(fid, buf, strlen(buf));
  108.         write(fid, "\n", 1);
  109.     } else {
  110.         XDrawImageString(p->display, p->infow, p->dfgc, 0, fontHeight(p->dfont) * k++, buf, strlen(buf));
  111.     }
  112.     }
  113.  
  114.     k++;    /* leave a blank line between players and teams */
  115.     if (!p)
  116.     write(fid, "\n", 1);
  117.  
  118.     for (t = 0; t < NUMTEAM; t++) {
  119.     team.p_no = t;
  120.     if (t == FED)
  121.         strcpy(team.p_name, "Federation");
  122.     else if (t == ROM)
  123.         strcpy(team.p_name, "Romulan");
  124.     else if (t == ORI)
  125.         strcpy(team.p_name, "Orion");
  126.     else if (t == KLI)
  127.         strcpy(team.p_name, "Klingon");
  128.     team.p_stats.st_kills = 0;
  129.     team.p_stats.st_maxkills = 0;
  130.     team.p_stats.st_losses = 0;
  131.     team.p_stats.st_torps = 0;
  132.     team.p_stats.st_phasers = 0;
  133.     team.p_stats.st_planets = 0;
  134.     team.p_stats.st_armsbomb = 0;
  135.     active_team = 0;
  136.     for (i = 0, j = &players[0]; i < MAXPLAYER; i++, j++) {
  137.         if (j->p_status == PFREE || j->p_ship->s_team != t)
  138.         continue;
  139.         if (j->p_copilot)
  140.         continue;
  141.         active_team = 1;
  142.         team.p_stats.st_kills    += j->p_ship->s_stats.st_kills;   
  143.         if (team.p_stats.st_maxkills < j->p_stats.st_maxkills)
  144.           team.p_stats.st_maxkills = j->p_stats.st_maxkills;
  145.         team.p_stats.st_losses   += j->p_ship->s_stats.st_losses;  
  146.         team.p_stats.st_torps    += j->p_ship->s_stats.st_torps;   
  147.         team.p_stats.st_phasers  += j->p_ship->s_stats.st_phasers; 
  148.         team.p_stats.st_planets  += j->p_ship->s_stats.st_planets; 
  149.         team.p_stats.st_armsbomb += j->p_ship->s_stats.st_armsbomb;
  150.     }
  151.     if (active_team) {
  152.       sprintf(buf, " %1x %-16.16s %6.2f %5.2f %6d %5.2f %7d %7d %7d %6d",
  153.         team.p_no,
  154.         team.p_name,
  155.         team.p_stats.st_kills,
  156.         team.p_stats.st_maxkills,
  157.         team.p_stats.st_losses,
  158.         (team.p_stats.st_losses == 0) ? 
  159.             (team.p_stats.st_kills * 2) : team.p_stats.st_kills / team.p_stats.st_losses,
  160.         team.p_stats.st_torps,
  161.         team.p_stats.st_phasers,
  162.         team.p_stats.st_planets,
  163.         team.p_stats.st_armsbomb
  164.         );
  165.       if (!p) {
  166.         write(fid, buf, strlen(buf));
  167.         write(fid, "\n", 1);
  168.       } else {
  169.         XDrawImageString(p->display, p->infow, p->dfgc, 0, fontHeight(p->dfont) * k++, buf, strlen(buf));
  170.       }
  171.     }
  172.     }
  173. }
  174.