/*
* Slide puzzle core game logic
- * Copyright © 2022 Nick Bowler
+ * Copyright © 2022-2024 Nick Bowler
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
#include <config.h>
#include <limits.h>
#include <string.h>
+#include <unistd.h>
#include <time.h>
-#include <gethrxtime.h>
#include "game.h"
-#define B64(x) ((x) & 0xffffffffffffffff)
+#define B64(x) ((x) & 0xffffffffffffffffull)
/* Rotate val left by n bits. The behaviour is undefined if n is zero. */
static unsigned long long rot_left64(unsigned long long val, int n)
{
unsigned long long z;
- z = B64(*state += 0x9e3779b97f4a7c15);
- z = B64((z ^ (z >> 30)) * 0xbf58476d1ce4e5b9);
- z = B64((z ^ (z >> 27)) * 0x94d049bb133111eb);
+ z = B64(*state += 0x9e3779b97f4a7c15ull);
+ z = B64((z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull);
+ z = B64((z ^ (z >> 27)) * 0x94d049bb133111ebull);
return z ^ (z >> 31);
}
if (!rng_is_seeded()) {
unsigned long long seed;
- seed = time(NULL);
- seed += gethrxtime();
+ /*
+ * Try to get a reasonable initial seed.
+ *
+ * Reasonable in this context means:
+ *
+ * - roughly even distribution of 1/0 bits, and
+ * - unlikely to generate the same seed twice in succession.
+ */
+ game_begin(board);
+
+ seed = time(NULL);
+ seed += board->time_start;
+ seed += (unsigned long long)getpid() << 16;
seed += seed << 32;
game_reseed(seed);
return mask & GOAL_MASK;
}
-void game_begin(struct board *board)
-{
- board->time_start = gethrxtime();
-}
-
-int_fast32_t game_elapsed(struct board *board)
-{
- return (gethrxtime() - board->time_start) / 1000000;
-}
-
int_fast32_t game_finish(struct board *board)
{
int_fast32_t t = game_elapsed(board);