Galactic Bloodshed
victory.cc
Go to the documentation of this file.
1 // Copyright 2014 The Galactic Bloodshed Authors. All rights reserved.
2 // Use of this source code is governed by a license that can be
3 // found in the COPYING file.
4 
5 #include "gb/victory.h"
6 
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10 
11 #include "gb/GB_server.h"
12 #include "gb/buffers.h"
13 #include "gb/races.h"
14 #include "gb/vars.h"
15 
16 static auto constexpr victory_sort(const void *A, const void *B) {
17  const auto *a = (const struct vic *)A;
18  const auto *b = (const struct vic *)B;
19  if (a->no_count) return 1;
20  if (b->no_count) return -1;
21 
22  if (b->rawscore > a->rawscore) return 1;
23  if (b->rawscore < a->rawscore) return -1;
24 
25  // Must be equal
26  return 0;
27 }
28 
29 void victory(const command_t &argv, GameObj &g) {
30  struct vic vic[MAXPLAYERS];
31 
32  /*
33  #ifndef VICTORY
34  g.out << "Victory conditions disabled.\n";
35  return;
36  #endif
37  */
38  int count = (argv.size() > 1) ? std::stoi(argv[1]) : Num_races;
39  if (count > Num_races) count = Num_races;
40 
42 
43  g.out << "----==== PLAYER RANKINGS ====----\n";
44  sprintf(buf, "%-4.4s %-15.15s %8s\n", "No.", "Name", (g.god ? "Score" : ""));
45  notify(g.player, g.governor, buf);
46  for (int i = 0; i < count; i++) {
47  if (g.god)
48  sprintf(buf, "%2d %c [%2d] %-15.15s %5ld %6.2f %3d %s %s\n", i + 1,
49  vic[i].Thing ? 'M' : ' ', vic[i].racenum, vic[i].name,
50  vic[i].rawscore, vic[i].tech, vic[i].IQ,
51  races[vic[i].racenum - 1]->password,
52  races[vic[i].racenum - 1]->governor[0].password);
53  else
54  sprintf(buf, "%2d [%2d] %-15.15s\n", i + 1, vic[i].racenum,
55  vic[i].name);
56  notify(g.player, g.governor, buf);
57  }
58 }
59 
60 void create_victory_list(struct vic vic[MAXPLAYERS]) {
61  Race *vic_races[MAXPLAYERS];
62 
63  for (player_t i = 1; i <= Num_races; i++) {
64  vic_races[i - 1] = races[i - 1];
65  vic[i - 1].no_count = 0;
66  }
67  for (player_t i = 1; i <= Num_races; i++) {
68  vic[i - 1].racenum = i;
69  strcpy(vic[i - 1].name, vic_races[i - 1]->name);
70  vic[i - 1].rawscore = vic_races[i - 1]->victory_score;
71  /* vic[i-1].rawscore = vic_races[i-1]->morale; */
72  vic[i - 1].tech = vic_races[i - 1]->tech;
73  vic[i - 1].Thing = vic_races[i - 1]->Metamorph;
74  vic[i - 1].IQ = vic_races[i - 1]->IQ;
75  if (vic_races[i - 1]->God || vic_races[i - 1]->Guest ||
76  vic_races[i - 1]->dissolved)
77  vic[i - 1].no_count = 1;
78  }
79  qsort(vic, Num_races, sizeof(struct vic), victory_sort);
80 }
void victory(const command_t &argv, GameObj &g)
Definition: victory.cc:29
void create_victory_list(struct vic vic[MAXPLAYERS])
Definition: victory.cc:60
static auto constexpr victory_sort(const void *A, const void *B)
Definition: victory.cc:16
#define MAXPLAYERS
Definition: vars.h:45