-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,32 @@ | ||
#ifndef GAME_H | ||
#define GAME_H | ||
#ifndef LOGIC_GAME_DOT_H_ | ||
#define LOGIC_GAME_DOT_H_ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
/** | ||
* Definition of columns: | ||
* 0: self generated (unshared) | ||
* 1: self generated, shared with other players (send/receive) | ||
* 2: received from broadcasters scattered around the world | ||
* 3: (same as 2) | ||
* ... | ||
* 7: (same as 2) | ||
*/ | ||
#include <Logic/game_param.h> | ||
|
||
#define GAME_COLUMN_UNSHARED 0 // column index | ||
#define GAME_COLUMN_SHARED_PLAYER 1 // column index | ||
#define GAME_COLUMN_BROADCASTER1 2 // column index | ||
#define GAME_COLUMN_BROADCASTER2 3 // column index | ||
#define GAME_COLUMN_BROADCASTER3 4 // column index | ||
#define GAME_COLUMN_BROADCASTER4 5 // column index | ||
#define GAME_COLUMN_BROADCASTER5 6 // column index | ||
#define GAME_COLUMN_BROADCASTER6 7 // column index | ||
|
||
#define GAME_UPDATE_UNSHARED_PERIOD (10 * 60) // 10 minutes | ||
#define GAME_UPDATE_SHARED_PERIOD (10 * 60) // 10 minutes | ||
|
||
#define GAME_NUM_COLUMNS 8 | ||
#define GAME_NUM_ROWS 16 | ||
#define GAME_DATA_SIZE 8 | ||
|
||
typedef int score_t; | ||
|
||
typedef struct { | ||
uint8_t data[GAME_DATA_SIZE]; | ||
} grid_t; | ||
|
||
typedef struct { | ||
grid_t data[GAME_NUM_COLUMNS][GAME_NUM_ROWS]; | ||
} storage_t; | ||
namespace hitcon { | ||
namespace game { | ||
|
||
// Initialize the game. | ||
// If storage is NULL, this function will generate one. | ||
void game_init(const storage_t *storage); | ||
void game_init(const game_storage_t *storage); | ||
|
||
// Get the storage of the game. Can be used to save the game (persistent | ||
// storage). | ||
storage_t game_get_storage(); | ||
|
||
typedef void (*callback_no_arg_t)(void); | ||
typedef void (*callback_event_data_t)(void *event_data); | ||
void register_callbacks(void (*register_callback_no_arg)( | ||
int period, callback_no_arg_t callback_no_arg), | ||
void (*register_callback_event_data)( | ||
callback_event_data_t callback_event_data)); | ||
game_storage_t& game_get_storage(); | ||
|
||
// This is the data we received from peer. | ||
void game_accept_data(int col, uint8_t* data); | ||
|
||
// Retrieve the data at particular cell. | ||
uint8_t* get_data_cell(int col, int row); | ||
|
||
#endif // GAME_H | ||
// Get a random data cell for sending over IR. The data is written to out_data, while the column is written to out_col. | ||
bool get_random_cell_data_for_ir_transmission(uint8_t* out_data, int* out_col); | ||
|
||
} // namespace game | ||
} // namespace hitcon | ||
|
||
#endif // LOGIC_GAME_DOT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef LOGIC_GAME_PARAM_DOT_H_ | ||
#define LOGIC_GAME_PARAM_DOT_H_ | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
namespace hitcon { | ||
namespace game { | ||
|
||
// We've a grid of cells, each cell has some data. | ||
|
||
constexpr size_t kNumCols = 8; | ||
constexpr size_t kNumRows = 16; | ||
constexpr size_t kDataSize = 8; | ||
|
||
// We do a random generation once every kRandomGenPeriodMs ms. | ||
constexpr size_t kRandomGenPeriodMs = 100; | ||
|
||
typedef int score_t; | ||
|
||
// Represents one cell in the grid. | ||
typedef struct { | ||
uint8_t data[kDataSize]; | ||
} grid_cell_t; | ||
|
||
// game_storage_t is persisted to the persistent storage. | ||
typedef struct { | ||
grid_cell_t cells[kNumCols][kNumRows]; | ||
} game_storage_t; | ||
|
||
// game_cache_t is a temporary structure that contains frequently used data, | ||
// but is not persisted to the storage. | ||
typedef struct { | ||
// The score for each cell is the number of prefix 0 bit in | ||
// sha3_256(col_prefix | cell_data). | ||
// Technically a uint8_t might overflow, but by then we're all screwed so whatever. | ||
uint8_t cell_score_cache[kNumCols][kNumRows]; | ||
|
||
// The score for each column is the sum of all score in the column. | ||
uint32_t col_score_cache[kNumCols]; | ||
|
||
// The total score is the sum of log(col_score), where by log is the natural log. | ||
// This number is stored in Q format fixed number. Q9.22 is used. | ||
uint32_t total_score; | ||
} game_cache_t; | ||
|
||
// These columns may be broadcasted by the IrController. | ||
constexpr int IrAllowedBroadcastCol[] = {0, 2, 3, 4, 7}; | ||
constexpr size_t IrAllowBroadcastCnt = sizeof(IrAllowedBroadcastCol)/sizeof(IrAllowedBroadcastCol[0]); | ||
|
||
// These columns may accept data through internal random generation. | ||
constexpr int InternalGenCol[] = {0, 1, 2}; | ||
constexpr size_t InternalGenColCnt = sizeof(InternalGenCol)/sizeof(InternalGenCol[0]); | ||
|
||
} // namespace game | ||
} // namespace hitcon | ||
|
||
#endif // LOGIC_GAME_PARAM_DOT_H_ |