Skip to content

Commit

Permalink
https://github.com/xtreme8000/BetterSpades/issues/136
Browse files Browse the repository at this point in the history
  • Loading branch information
forked-from-1kasper committed Sep 16, 2023
1 parent 1e73ce0 commit ab8580a
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 94 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Start the client e.g. with the following inside the `dist/` directory:
```
Or connect directly to localhost:
```
./betterspades -aos://16777343:32887
./betterspades --server aos://16777343:32887
```

#### macOS
Expand Down
2 changes: 2 additions & 0 deletions include/BetterSpades/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ struct config_setting {
void (*label_callback)(char* buffer, size_t length, int value, size_t index);
};

extern char * config_filepath;

extern List config_settings;

void config_register_key(int internal, int def, const char* name, int toggle, const char* display,
Expand Down
2 changes: 2 additions & 0 deletions include/BetterSpades/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ int network_update(void);
int network_status(void);
void network_init(void);

void network_join_game(unsigned char team, unsigned char weapon);

void read_PacketMapChunk(void * data, int len);
void read_PacketChatMessage(void * data, int len);
void read_PacketBlockAction(void * data, int len);
Expand Down
33 changes: 17 additions & 16 deletions include/BetterSpades/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@
#include <BetterSpades/aabb.h>
#include <BetterSpades/network.h>

#define PLAYERS_MAX 256 // just because 32 players are not enough
#define TEAM_1 0
#define TEAM_2 1
#define PLAYERS_MAX 256 // just because 32 players are not enough
#define TEAM_1 0
#define TEAM_2 1
#define TEAM_SPECTATOR 255

typedef struct {
float x, y, z;
} Position;

typedef struct {
float x, y, z;
} Orientation;

typedef struct {
float x, y, z;
} Velocity;

typedef struct {
char name[11];
unsigned char red, green, blue;
Expand Down Expand Up @@ -58,9 +70,10 @@ extern unsigned char local_player_ammo, local_player_ammo_reserved;
extern unsigned char local_player_respawn_time;
extern float local_player_death_time;
extern unsigned char local_player_respawn_cnt_last;
extern unsigned char local_player_newteam;
extern unsigned char local_player_lasttool;

extern int default_team, default_gun;

extern float local_player_last_damage_timer;
extern float local_player_last_damage_x;
extern float local_player_last_damage_y;
Expand Down Expand Up @@ -94,18 +107,6 @@ typedef struct {
bool player_intersection_exists(Hit * s);
int player_intersection_choose(Hit * s, float * distance);

typedef struct {
float x, y, z;
} Position;

typedef struct {
float x, y, z;
} Orientation;

typedef struct {
float x, y, z;
} Velocity;

typedef struct {
char name[17];
Position pos;
Expand Down
7 changes: 5 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#include <ini.h>

char * config_filepath = "config.ini";

struct RENDER_OPTIONS settings, settings_tmp;

List config_keys, config_settings, config_file, config_keybind;
Expand Down Expand Up @@ -96,7 +98,7 @@ void config_save() {
config_seti("controls", e->name, e->def);
}

void * f = file_open("config.ini", "w");
void * f = file_open(config_filepath, "w");
if (f) {
char last_section[32] = {0};
for (int k = 0; k < list_size(&config_file); k++) {
Expand Down Expand Up @@ -368,7 +370,8 @@ void config_reload() {

list_sort(&config_keys, config_key_cmp);

char * s = file_load("config.ini");
char * s = file_load(config_filepath);

if (s) {
ini_parse_string(s, config_read_key, NULL);
free(s);
Expand Down
70 changes: 28 additions & 42 deletions src/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,14 @@ static void hud_ingame_render3D() {
}

if (screen_current == SCREEN_GUN_SELECT) {
int team = network_logged_in ? players[local_player_id].team : local_player_newteam;

matrix_identity(matrix_model);
matrix_translate(matrix_model, -1.5F, -1.25F, -3.25F);
matrix_rotate(matrix_model, window_time() * 90.0F, 0.0F, 1.0F, 0.0F);
matrix_translate(matrix_model, (model_semi.xpiv - model_semi.xsiz / 2.0F) * model_semi.scale,
(model_semi.zpiv - model_semi.zsiz / 2.0F) * model_semi.scale,
(model_semi.ypiv - model_semi.ysiz / 2.0F) * model_semi.scale);
matrix_upload();
kv6_render(&model_semi, team);
kv6_render(&model_semi, TEAM_SPECTATOR);

matrix_identity(matrix_model);
matrix_translate(matrix_model, 0.0F, -1.25F, -3.25F);
Expand All @@ -276,7 +274,7 @@ static void hud_ingame_render3D() {
(model_smg.zpiv - model_smg.zsiz / 2.0F) * model_smg.scale,
(model_smg.ypiv - model_smg.ysiz / 2.0F) * model_smg.scale);
matrix_upload();
kv6_render(&model_smg, team);
kv6_render(&model_smg, TEAM_SPECTATOR);

matrix_identity(matrix_model);
matrix_translate(matrix_model, 1.5F, -1.25F, -3.25F);
Expand All @@ -285,7 +283,7 @@ static void hud_ingame_render3D() {
(model_shotgun.zpiv - model_shotgun.zsiz / 2.0F) * model_shotgun.scale,
(model_shotgun.ypiv - model_shotgun.ysiz / 2.0F) * model_shotgun.scale);
matrix_upload();
kv6_render(&model_shotgun, team);
kv6_render(&model_shotgun, TEAM_SPECTATOR);
}

struct kv6_t * rotating_model = NULL;
Expand Down Expand Up @@ -1697,43 +1695,39 @@ static void hud_ingame_keyboard(int key, int action, int mods, int internal) {
}
}

static int new_team = -1;

if (screen_current == SCREEN_TEAM_SELECT) {
int new_team = 256;
switch (key) {
case WINDOW_KEY_SELECT1: new_team = TEAM_1; break;
case WINDOW_KEY_SELECT2: new_team = TEAM_2; break;
case WINDOW_KEY_SELECT1: new_team = TEAM_1; break;
case WINDOW_KEY_SELECT2: new_team = TEAM_2; break;
case WINDOW_KEY_SELECT3: new_team = TEAM_SPECTATOR; break;
default: new_team = -1; break;
}
if (new_team <= 255) {

if (new_team >= 0) {
if (network_logged_in) {
struct PacketChangeTeam p;
p.player_id = local_player_id;
p.team = new_team;
p.team = new_team;

network_send(PACKET_CHANGETEAM_ID, &p, sizeof(p));
screen_current = SCREEN_NONE;
return;
} else {
local_player_newteam = new_team;
if (new_team == TEAM_SPECTATOR) {
struct PacketExistingPlayer login;
login.player_id = local_player_id;
login.team = local_player_newteam;
login.weapon = WEAPON_RIFLE;
login.held_item = TOOL_GUN;
login.kills = 0;
login.blue = players[local_player_id].block.b;
login.green = players[local_player_id].block.g;
login.red = players[local_player_id].block.r;

encodeMagic(login.name, settings.name, strlen(settings.name), sizeof(login.name));
network_send(PACKET_EXISTINGPLAYER_ID, &login, sizeof(login) - sizeof(login.name) + strlen(login.name) + 1);
network_join_game(new_team, WEAPON_RIFLE);
screen_current = SCREEN_NONE;
} else if (default_gun >= 0) {
network_join_game(new_team, default_gun);
screen_current = SCREEN_NONE;
} else {
screen_current = SCREEN_GUN_SELECT;
}
return;
}
}

if ((key == WINDOW_KEY_CHANGETEAM || key == WINDOW_KEY_ESCAPE)
&& (!network_connected || (network_connected && network_logged_in))) {
screen_current = SCREEN_NONE;
Expand All @@ -1742,35 +1736,27 @@ static void hud_ingame_keyboard(int key, int action, int mods, int internal) {
}

if (screen_current == SCREEN_GUN_SELECT) {
int new_gun = 255;
int new_gun = -1;
switch (key) {
case WINDOW_KEY_SELECT1: new_gun = WEAPON_RIFLE; break;
case WINDOW_KEY_SELECT2: new_gun = WEAPON_SMG; break;
case WINDOW_KEY_SELECT1: new_gun = WEAPON_RIFLE; break;
case WINDOW_KEY_SELECT2: new_gun = WEAPON_SMG; break;
case WINDOW_KEY_SELECT3: new_gun = WEAPON_SHOTGUN; break;
default: new_gun = -1; break;
}
if (new_gun < 255) {

if (new_gun >= 0) {
if (network_logged_in) {
struct PacketChangeWeapon p;
p.player_id = local_player_id;
p.weapon = new_gun;
p.weapon = new_gun;

network_send(PACKET_CHANGEWEAPON_ID, &p, sizeof(p));
} else {
struct PacketExistingPlayer login;
login.player_id = local_player_id;
login.team = local_player_newteam;
login.weapon = new_gun;
login.held_item = TOOL_GUN;
login.kills = 0;
login.blue = players[local_player_id].block.b;
login.green = players[local_player_id].block.g;
login.red = players[local_player_id].block.r;
strcpy(login.name, settings.name);
network_send(PACKET_EXISTINGPLAYER_ID, &login,
sizeof(login) - sizeof(login.name) + strlen(settings.name) + 1);
}
} else network_join_game(new_team >= 0 ? new_team : default_team, new_gun);

screen_current = SCREEN_NONE;
return;
}

if ((key == WINDOW_KEY_CHANGEWEAPON || key == WINDOW_KEY_ESCAPE)
&& (!network_connected || (network_connected && network_logged_in))) {
screen_current = SCREEN_NONE;
Expand Down
49 changes: 36 additions & 13 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <BetterSpades/matrix.h>
#include <BetterSpades/texture.h>
#include <BetterSpades/chunk.h>
#include <BetterSpades/unicode.h>
#include <BetterSpades/main.h>

int fps = 0;
Expand Down Expand Up @@ -735,6 +736,8 @@ void idle(double dt) {
rpc_update();
}

#define MATCH(x, y) if (!strcmp((x), (y)))

int main(int argc, char ** argv) {
settings.opengl14 = 1;
settings.color_correction = 0;
Expand Down Expand Up @@ -783,6 +786,32 @@ int main(int argc, char ** argv) {

log_info("TigerSpades " BETTERSPADES_VERSION);

char * default_server = NULL;

for (size_t i = 1; i < argc; i++) {
MATCH(argv[i], "--help") {
log_info("Usage: %s --server aos://<ip>:<port> --team <team> --weapon <weapon> --config <file>", argv[0]);
} else MATCH(argv[i], "--server") {
if (argc <= ++i) log_error("The “--server” option requires an argument.");
else default_server = argv[i];
} else MATCH(argv[i], "--team") {
if (argc <= ++i) log_error("The “--team” option requires an argument.");
else MATCH(argv[i], "1") default_team = TEAM_1;
else MATCH(argv[i], "2") default_team = TEAM_2;
else MATCH(argv[i], "3") default_team = TEAM_SPECTATOR;
else log_error("Unknown team (expected 1, 2, or 3).");
} else MATCH(argv[i], "--weapon") {
if (argc <= ++i) log_error("The “--weapon” option requires an argument.");
else MATCH(argv[i], "rifle") default_gun = WEAPON_RIFLE;
else MATCH(argv[i], "smg") default_gun = WEAPON_SMG;
else MATCH(argv[i], "shotgun") default_gun = WEAPON_SHOTGUN;
else log_error("Unknown weapon name (expected rifle, smg, or shotgun).");
} else MATCH(argv[i], "--config") {
if (argc <= ++i) log_error("The “--config” option requires an argument.");
else config_filepath = argv[i];
}
}

config_reload();

window_init(&argc, argv);
Expand All @@ -792,9 +821,9 @@ int main(int argc, char ** argv) {
log_error("Could not load extended OpenGL functions!");
#endif

log_info("Vendor: %s", glGetString(GL_VENDOR));
log_info("Vendor: %s", glGetString(GL_VENDOR));
log_info("Renderer: %s", glGetString(GL_RENDERER));
log_info("Version: %s", glGetString(GL_VERSION));
log_info("Version: %s", glGetString(GL_VERSION));

if (settings.multisamples > 0) {
glEnable(GL_MULTISAMPLE);
Expand All @@ -803,26 +832,20 @@ int main(int argc, char ** argv) {

while (glGetError() != GL_NO_ERROR);

init();
atexit(deinit);
init(); atexit(deinit);

if (settings.vsync < 2)
window_swapping(settings.vsync);

if (settings.vsync > 1)
window_swapping(0);

if (argc > 1) {
if (!strcmp(argv[1], "--help")) {
log_info("Usage: client [server browser]");
log_info(" client -aos://<ip>:<port> [custom address]");
exit(0);
}

if (!network_connect_string(argv[1] + 1)) {
if (default_server != NULL) {
if (!network_connect_string(default_server)) {
log_error("Error: Connection failed (use --help for instructions)");
exit(1);
} else {
log_info("Connection to %s successful", argv[1] + 1);
log_info("Connection to %s successful", default_server);
hud_change(&hud_ingame);
}
}
Expand Down
Loading

0 comments on commit ab8580a

Please sign in to comment.