Skip to content

Commit

Permalink
okay this is sort of working now
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Oct 5, 2023
1 parent 2f7e0c1 commit 9eea341
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
18 changes: 10 additions & 8 deletions Scoreboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <iostream>
#include <random>

static std::mt19937 mt(0x31415926);

const uint32_t IdleTimeout = 60;
const uint32_t CloseTimeout = 20;
const uint32_t FindWait = 2; //6;
const uint32_t FindWait = 16;

const float MoveWait = 0.25f;
const float GemWait = 10.0f;
Expand Down Expand Up @@ -113,7 +115,7 @@ void Scoreboard::update(float elapsed) {
player.gatherer = connection;
player.gatherer_state = Player::PendingView;
player.gatherer_at = random_empty();
player.gatherer_cooldown = MoveWait;
player.gatherer_cooldown = MoveWait * (mt() / float(mt.max()) + 0.5f);
} else if (role == 's') {
if (player.seeker) {
disconnect(player.seeker, "Newer seeker connected.");
Expand All @@ -123,7 +125,7 @@ void Scoreboard::update(float elapsed) {
player.seeker = connection;
player.seeker_state = Player::PendingView;
player.seeker_at = random_empty();
player.seeker_cooldown = MoveWait;
player.seeker_cooldown = MoveWait * (mt() / float(mt.max()) + 0.5f);
} else {
disconnect(connection, "Expecting role to be 'g'atherer or 's'eeker, got '" + std::string(&role, 1) + "' instead.");
break;
Expand Down Expand Up @@ -260,8 +262,6 @@ void Scoreboard::update(float elapsed) {
}
}

static std::mt19937 mt(0x31415926);

std::vector< glm::ivec2 > empty;

for (uint32_t y = 0; y < maze.size(); ++y) {
Expand Down Expand Up @@ -315,7 +315,7 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {

static Font const &font = Font::get_shared("Flexi_IBM_VGA_True.ttf");

const glm::uvec2 display_size = glm::uvec2(84, 20);
const glm::uvec2 display_size = glm::uvec2(84, 25);
glm::vec2 char_size = glm::vec2(9.0f * 0.75f, 16.0f);

glm::mat4 world_to_clip;
Expand Down Expand Up @@ -378,6 +378,7 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
for (auto const &fs : find_stars) {
float amt = fs.second;
float amt2 = std::min(1.0f, amt * 2.0f);
amt = std::max(0.0f, amt * 1.5f - 0.5f);
Cell &cell = get_cell(fs.first);
cell.codepoint = 0x263c;
cell.fg = glm::u8vec3(uint8_t(0x88 * amt), uint8_t(0xbb * amt), uint8_t(0x22 * amt2)) + glm::u8vec3(0x44);
Expand Down Expand Up @@ -416,7 +417,8 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
uint32_t sa = a->score();
uint32_t sb = b->score();
if (sa != sb) return sa > sb;
else return a->timestamp < b->timestamp;
else if (a->timestamp != b->timestamp) return a->timestamp < b->timestamp;
else return a->andrewid < b->andrewid;
});

int32_t y = display_size.y - 1;
Expand Down Expand Up @@ -446,6 +448,7 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
auto write_string = [&](std::string string, uint32_t width, glm::u8vec3 fg, glm::u8vec3 bg) {
string = string.substr(0,width); //truncate
while (string.size() < width) string = string + ' '; //pad
assert(string.size() == width);

for (uint32_t i = 0; i < width; ++i) {
write(string[i], fg, bg);
Expand Down Expand Up @@ -629,7 +632,6 @@ void Scoreboard::draw(glm::uvec2 const &drawable_size) {
}

glm::ivec2 Scoreboard::random_empty() const {
static std::mt19937 mt(0xfeedf00d);

std::vector< glm::ivec2 > empty;

Expand Down
23 changes: 23 additions & 0 deletions dist/run-clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

const [server, port, players_txt] = process.argv.slice(2);


const fs = require('fs');
const child_process = require('child_process');

const lines = fs.readFileSync(players_txt, 'utf8').split('\n');

const strats = ['random', 'sw'];

for (const line of lines) {
if (line == '') continue;
const [name, secret, avatar, color] = line.split(/\s+/);

const strat = strats[Math.floor(Math.random() * strats.length)];

//console.log(name + ' ' + secret);
//console.log(secret.substr(3,3) + secret.substr(0,3));
child_process.spawn('node', [
'test-client.js', server, port, name, secret, strat
], {stdio:'inherit'} );
}
8 changes: 4 additions & 4 deletions dist/test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function closeSeeker() {
function buildClient(role, onView, onGem, onClose) {
let client = new net.Socket();
client.connect(parseInt(port), server, function() {
console.log(`[${role}] connected`);
//console.log(`[${role}] connected`);
//send handshake:
const data = `${role}${secret}${name}`;
client.write('H');
Expand Down Expand Up @@ -70,7 +70,7 @@ function buildClient(role, onView, onGem, onClose) {
});

client.on('close', function() {
console.log(`[${role}] server closed connection`);
//console.log(`[${role}] server closed connection`);
onClose();
});

Expand Down Expand Up @@ -184,7 +184,7 @@ function strategize() {
randomGatherer();
}
if (seeker === null) {
randomSeeker(0.1);
randomSeeker(0.02);
}
setTimeout(strategize, 1000);
} else if (strat === 'sw') {
Expand All @@ -201,4 +201,4 @@ function strategize() {
}
}

strategize();
setTimeout(strategize, Math.random() * 1000);
2 changes: 1 addition & 1 deletion maze-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int main(int argc, char **argv) {
if (avatar.size() == 1 && (avatar[0] & 0x80) == 0x00) {
avatar_codepoint = avatar[0];
} else {
static std::regex hex_unicode("U+[0-9a-fA-F]{4}");
static std::regex hex_unicode("U\\+[0-9a-fA-F]{4}");
std::smatch m;
if (!std::regex_match(avatar, m, hex_unicode)) {
std::cerr << "All avatars should be low ascii or U+XXXX hex code, but '" << avatar << "' is not!" << std::endl;
Expand Down

0 comments on commit 9eea341

Please sign in to comment.