Navigation Menu

Skip to content

Commit

Permalink
Restored hiscore code
Browse files Browse the repository at this point in the history
  • Loading branch information
dzavalishin committed Oct 5, 2021
1 parent daea0db commit 492a781
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/game/Patches.java
Expand Up @@ -76,7 +76,7 @@ public class Patches
public BooleanPatchVariable ai_disable_veh_aircraft = new BooleanPatchVariable(); // disable types for AI
public BooleanPatchVariable ai_disable_veh_ship = new BooleanPatchVariable(); // disable types for AI
public int starting_date; // starting date
public int ending_date; // end of the game (just show highscore)
public int ending_date = 2051; // end of the game (just show highscore)
public int colored_news_date = 1980; // when does newspaper become colored?

public boolean keep_all_autosave; // name the autosave in a different way.
Expand Down
156 changes: 62 additions & 94 deletions src/game/Player.java
Expand Up @@ -4,11 +4,15 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;

import game.Player.PlayerHiScoreComparator;
import game.ai.Ai;
import game.ai.PlayerAiNew;
import game.enums.GameModes;
Expand All @@ -33,6 +37,7 @@
public class Player implements Serializable
{


private static final long serialVersionUID = 1L;

int name_2;
Expand Down Expand Up @@ -1170,7 +1175,7 @@ public static int EndGameGetPerformanceTitleFromValue(int value)


// Save the highscore for the Player
static int SaveHighScoreValue(final Player p)
public static int SaveHighScoreValue(final Player p)
{

HighScore[] hs = Global._highscore_table[GameOptions._opt.diff_level];
Expand All @@ -1191,6 +1196,7 @@ static int SaveHighScoreValue(final Player p)
//memmove(&hs[i + 1], &hs[i], sizeof(HighScore) * (lengthof(_highscore_table[0]) - i - 1));
System.arraycopy(hs, i, hs, i+1, hs.length - i - 1);

/*
Global.SetDParam(0, p.president_name_1);
Global.SetDParam(1, p.president_name_2);
Global.SetDParam(2, p.name_1);
Expand All @@ -1199,125 +1205,66 @@ static int SaveHighScoreValue(final Player p)
hs[i].company = buf;
hs[i].score = score;
hs[i].title = Strings.GetString(EndGameGetPerformanceTitleFromValue(score));
*/
//hs[i].score = score;
hs[i].initFromPlayer(p,score);


return i;
}
}

return -1; // too bad; we did not make it into the top5
}

/* Sort all players given their performance * /
static int HighScoreSorter(final void *a, final void *b)
{
final Player pa = *(final Player* final*)a;
final Player pb = *(final Player* final*)b;
return pb.old_economy[0].performance_history - pa.old_economy[0].performance_history;
} */

/* Save the highscores in a network game when it has ended
//* Save the highscores in a network game when it has ended
//#define LAST_HS_ITEM lengthof(_highscore_table) - 1
static byte SaveHighScoreValueNetwork()
private static final int LAST_HS_ITEM = Global._highscore_table.length - 1;
public static int SaveHighScoreValueNetwork()
{
Player player_sort[MAX_PLAYERS];
size_t count = 0;
byte player = -1;
//Player [] player_sort = new Player[Global.MAX_PLAYERS];
//int count = 0;
int player = -1;

//* Sort all active players with the highest score first
List<Player> player_sort = new ArrayList<>();

/* Sort all active players with the highest score first
for( Player p : _players ) {
if (p.is_active)
if (p.isActive())
player_sort[count++] = p;
}
qsort(player_sort, count, sizeof(player_sort[0]), HighScoreSorter);
{
HighScore hs;
Player* final *p_cur = &player_sort[0];
ubyte i;
memset(_highscore_table[LAST_HS_ITEM], 0, sizeof(_highscore_table[0]));
}*/

//* Copy over Top5 companies
for (i = 0; i < lengthof(_highscore_table[LAST_HS_ITEM]) && i < count; i++) {
char buf[sizeof(_highscore_table[0].company)];
Player.forEach( p -> { if (p.isActive()) player_sort.add(p); } );
Collections.sort(player_sort, new PlayerHiScoreComparator());

hs = &_highscore_table[LAST_HS_ITEM][i];
Global.SetDParam(0, (*p_cur).president_name_1);
Global.SetDParam(1, (*p_cur).president_name_2);
Global.SetDParam(2, (*p_cur).name_1);
Global.SetDParam(3, (*p_cur).name_2);
GetString(buf, Str.STR_HIGHSCORE_NAME); // get manager/company name string
ttd_strlcpy(hs.company, buf, sizeof(buf));
hs.score = (*p_cur).old_economy[0].performance_history;
hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
//* Copy over Top5 companies
//for (int i = 0; i < Global._highscore_table[LAST_HS_ITEM].length && i < count; i++) {
// Player p_cur = player_sort.get(i);
int i = 0;
for(Player p : player_sort)
{
HighScore hs = new HighScore();
hs.initFromPlayer(p,p.old_economy[0].performance_history);

// get the ranking of the local player
if ((*p_cur).index.equals(_local_player))
player = i;
Global._highscore_table[LAST_HS_ITEM][i] = hs;
// get the ranking of the local player
if (p.index.isLocalPlayer())
player = i;

p_cur++;
}
i++;
if( i >= Global._highscore_table[LAST_HS_ITEM].length )
break;
}

// Add top5 players to highscore table
return player;
}
*/
/* */

/* Save HighScore table to file * /
static void SaveToHighScore()
{
FILE *fp = fopen(_highscore_file, "wb");

if (fp != null) {
int i;
HighScore hs;

for (i = 0; i < LAST_HS_ITEM; i++) { // don't save network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
// First character is a command character, so strlen will fail on that
byte length = min(sizeof(hs.company), (hs.company[0] == '\0') ? 0 : strlen(&hs.company[1]) + 1);

fwrite(&length, sizeof(length), 1, fp); // write away string length
fwrite(hs.company, length, 1, fp);
fwrite(&hs.score, sizeof(hs.score), 1, fp);
fwrite("", 2, 1, fp); // placeholder for hs.title, not saved anymore; compatibility
}
}
fclose(fp);
}
}
/* Initialize the highscore table to 0 and if any file exists, load in values * /
static void LoadFromHighScore()
{
FILE *fp = fopen(_highscore_file, "rb");
memset(_highscore_table, 0, sizeof(_highscore_table));
if (fp != null) {
int i;
HighScore hs;
for (i = 0; i < LAST_HS_ITEM; i++) { // don't load network highscores
for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) {
byte length;
fread(&length, sizeof(length), 1, fp);
fread(hs.company, 1, length, fp);
fread(&hs.score, sizeof(hs.score), 1, fp);
fseek(fp, 2, SEEK_CUR); // placeholder for hs.title, not saved anymore; compatibility
hs.title = EndGameGetPerformanceTitleFromValue(hs.score);
}
}
fclose(fp);
}
// Initialize end of game variable (when to show highscore chart)
Global._patches.ending_date = 2051;
}
*/
public void InitialiseEngineReplacement()
{
//EngineID engine;
Expand Down Expand Up @@ -1655,6 +1602,27 @@ public long getEngine_renew_money() {
}


/* Sort all players given their performance * /
static int HighScoreSorter(final void *a, final void *b)
{
final Player pa = *(final Player* final*)a;
final Player pb = *(final Player* final*)b;
return pb.old_economy[0].performance_history - pa.old_economy[0].performance_history;
} */

public static class PlayerHiScoreComparator implements Comparator<Player> {

@Override
public int compare(Player pa, Player pb) {
return pb.old_economy[0].performance_history - pa.old_economy[0].performance_history;
}


}


}



4 changes: 2 additions & 2 deletions src/game/SaveLoad.java
Expand Up @@ -238,8 +238,8 @@ public void exceptionThrown(Exception e) {

static void LoadFromHighScore()
{
// If can't load
//Global._highscore_table = new HighScore[5][5];
// If can't load - clear
Global._highscore_table = new HighScore[5][5];

File fn = new File( Global._path.personal_dir, HISCORE_FILE_NAME );

Expand Down
18 changes: 18 additions & 0 deletions src/game/struct/HighScore.java
@@ -1,8 +1,26 @@
package game.struct;

import game.Global;
import game.Player;
import game.Str;
import game.util.Strings;

public class HighScore
{
public String company = "";
public String title = "";
public int score = 0;

public void initFromPlayer(Player p, int score)
{
Global.SetDParam(0, p.getPresident_name_1());
Global.SetDParam(1, p.getPresident_name_2());
Global.SetDParam(2, p.getName_1());
Global.SetDParam(3, p.getName_2());
company = Strings.GetString(Str.STR_HIGHSCORE_NAME); // get manager/company name string

this.score = score; //p.old_economy[0].performance_history;
title = Strings.GetString( Player.EndGameGetPerformanceTitleFromValue(score) );
}
}

15 changes: 8 additions & 7 deletions src/game/xui/PlayerGui.java
Expand Up @@ -4,6 +4,7 @@

import game.Cmd;
import game.Economy;
import game.GameOptions;
import game.Global;
import game.Hal;
import game.Player;
Expand Down Expand Up @@ -872,7 +873,7 @@ static void HighScoreWndProc(Window w, WindowEvent e)
Global.SetDParam(1, w.window_number + Str.STR_6801_EASY);
Gfx.DrawStringMultiCenter(x + (640 / 2), y + 62, !Global._networking ? Str.STR_0211_TOP_COMPANIES_WHO_REACHED : Str.STR_TOP_COMPANIES_NETWORK_GAME, 500);

/* TODO Draw Highscore peepz */
/* Draw Highscore peepz */
for (int i = 0; i < hs.length; i++) {
Global.SetDParam(0, i + 1);
Gfx.DrawString(x + 40, y + 140 + (i * 55), Str.STR_0212, 0x10);
Expand Down Expand Up @@ -975,17 +976,17 @@ public static void ShowEndGameChart()
w.as_highscore_d().background_img = Sprite.SPR_TYCOON_IMG2_BEGIN;
}

/* TODO In a network game show the endscores of the custom difficulty 'network' which is the last one
* as well as generate a TOP5 of that game, and not an all-time top5.
// * In a network game show the endscores of the custom difficulty 'network'
// * which is the last one as well as generate a TOP5 of that game, and not an all-time top5.
if (Global._networking) {
w.window_number = lengthof(_highscore_table) - 1;
w.as_highscore_d().rank = SaveHighScoreValueNetwork();
w.window_number = Global._highscore_table.length - 1;
w.as_highscore_d().rank = Player.SaveHighScoreValueNetwork();
} else {
// in single player _local player is always valid
final Player p = Player.GetPlayer(Global.gs._local_player);
w.window_number = GameOptions._opt.diff_level;
w.as_highscore_d().rank = SaveHighScoreValue(p);
}*/
w.as_highscore_d().rank = Player.SaveHighScoreValue(p);
}
}
}

Expand Down

0 comments on commit 492a781

Please sign in to comment.