166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
//----------------------------------------------------------------------------
// The Gameboard
//----------------------------------------------------------------------------
// A 2D array of glyphs to display.
//
// For simplicty we will limit our gameboard size to fit in the (assumed)
// terminal size. One gameboard cell == two terminal character cells. And
// we will fill it with random trees and walls, surrounded by water.
//
// Also, we aren’t being particularly careful to distinguish between the
// gameboard and the game pieces here -- everything is treated the same.
#define GAMEBOARD_WIDTH 38
#define GAMEBOARD_HEIGHT 24
#define GLYPHS(F) \
/* name, likelihood, fg color, bg color, glyph */ \
F(Background, 0, "192;192;192", "0;0;0", " ") \
\
/* player can “walk” on the grass */ \
F(Grass, 0.235690236, "55;159;19", "85;179;49", " ^") \
F(Grass1, 0.117845118, "55;159;19", "85;179;49", "↯ ") \
F(Grass2, 0.353535353, "55;159;19", "85;179;49", " ") \
\
/* player cannot walk through this stuff */ \
F(Wall, 0.038825757, "55;55;55", "93;101;52", ":∶") \
F(Wall1, 0.038825757, "55;55;55", "83;101;52", ".:") \
F(Wall2, 0.038825757, "55;55;55", "103;101;52", "∶:") \
F(Wall3, 0.038825757, "55;55;55", "93;101;52", "·.") \
F(Bush, 0.137626264, "157;242;106", "57;142;6", "🌿") \
\
/* ...nor water, but it is only used to frame the gameboard */ \
F(Water, 0, "55;106;128", "54;167;216", "〰") \
F(Water2, 0, "55;106;128", "54;167;216", " ~") \
F(Water3, 0, "55;106;128", "54;167;216", " ") \
\
/* bit-toggle with grass */ \
F(Start, 0, "255;170;85", "85;179;49", "🦊") \
F(Goal, 0, "255;128;128", "85;179;49", "🫐") \
F(Path, 0, "255;255;192", "85;179;49", "〇")
#define F(NAME,P,F,B,S) Glyph_##NAME,
enum { GLYPHS(F) NUM_GLYPHS };
#undef F
#define F(N,PROBABILITY,F,B,S) PROBABILITY,
const double glyph_probability[NUM_GLYPHS] = { GLYPHS(F) };
#undef F
#define F(N,P,FOREGROUND,B,S) FOREGROUND,
const char * fg_colors[NUM_GLYPHS] = { GLYPHS(F) };
#undef F
#define F(N,P,F,BACKGROUND,S) BACKGROUND,
const char * bg_colors[NUM_GLYPHS] = { GLYPHS(F) };
#undef F
#define F(N,P,F,B,SYMBOL) SYMBOL,
const char * glyphs[NUM_GLYPHS] = { GLYPHS(F) };
#undef F
int * gameboard_data;
int gameboard_width = GAMEBOARD_WIDTH;
int gameboard_height = GAMEBOARD_HEIGHT;
int * gameboard( int x, int y )
{
return gameboard_data + y * gameboard_width + x;
}
void initialize_gameboard( bool is_grass_only )
{
gameboard_data = malloc( sizeof(gameboard_data[0]) * gameboard_width * gameboard_height );
if (!gameboard_data)
failure( "Cannot create gameboard: Dynamic memory allocation failure!" );
// Grass and random WALLS and BUSHES
for (int y = 1; y < gameboard_height - 1; y++)
for (int x = 1; x < gameboard_width - 1; x++)
{
double value = rand() * 1.0 / RAND_MAX;
double sum = 0.0;
if (is_grass_only)
{
*gameboard( x, y ) = Glyph_Grass;
continue;
}
for (int n = Glyph_Grass; n < Glyph_Water; n++)
{
sum += glyph_probability[n];
if (value < sum)
{
*gameboard( x, y ) = n;
break;
}
}
}
// Border it all with WATER
for (int y = 0; y < gameboard_height; y++)
*gameboard( 0, y ) = *gameboard( gameboard_width - 1, y ) = Glyph_Water + (rand() % 3);
for (int x = 0; x < gameboard_width; x++)
*gameboard( x, 0 ) = *gameboard( x, gameboard_height - 1 ) = Glyph_Water + (rand() % 3);
}
static
void draw_glyph( int glyph )
{
printf( "\033[38;2;%s;48;2;%s""m%s", fg_colors[glyph], bg_colors[glyph], glyphs[glyph] );
}
void draw_gameboard( void )
{
goto_xy( 0, 0 );
for (int y = 0; y < gameboard_height; y++)
{
for (int x = 0; x < gameboard_width; x++)
{
draw_glyph( *gameboard( x, y ) );
}
puts("");
}
}
bool is_walkable( int x, int y )
{
if ((x < 1) or (x > gameboard_width-2)) return false;
if ((y < 1) or (y > gameboard_height-2)) return false;
int glyph = *gameboard( x, y );
return ((glyph >= Glyph_Grass) and (glyph <= Glyph_Grass2)) or (glyph == Glyph_Goal);
}
int count_walkable_cells( void )
{
int count = 0;
for (int y = 0; y < gameboard_height; y++)
for (int x = 0; x < gameboard_width; x++)
count += is_walkable( x, y );
return count;
}
bool is_wall( int x, int y )
{
int glyph = *gameboard( x, y );
return (Glyph_Wall <= glyph) and (glyph <= Glyph_Wall3);
}
bool is_walkable_to( XY from, int direction )
{
int x = from.x + directions[direction].x;
int y = from.y + directions[direction].y;
if (!is_walkable( x, y )) return false;
if (!is_diagonal[ direction ]) return true;
return !is_wall( x, from.y ) and !is_wall( from.x, y );
}
void draw_status( const char * message, long seed )
{
char s[50];
sprintf( s, "%ld", seed );
printf( "\033[38;2;55;112;112;48;2;%s""m", bg_colors[Glyph_Background] );
printf( "\r%*s", (int)(gameboard_width * 2 - 6 - strlen(s)), " " );
printf( "seed:%s ", s );
printf( "\r\033[38;2;%s""m %s", fg_colors[Glyph_Background], message );
printf( "\033[0m" );
fflush( stdout );
}
//----------------------------------------------------------------------------
// Priority Queue for XY positions
//----------------------------------------------------------------------------
// Implemented as a min-heap
struct priority_queue
{
struct pq_node
{
int priority;
XY value; // data value ::= XY position
}
* data; // array of data
int size; // array size
};
bool pq_create( struct priority_queue * pq, int capacity )
{
pq->data = malloc( capacity * sizeof(struct pq_node) );
pq->size = 0;
return !! pq->data;
}
void pq_free( struct priority_queue * pq )
{
free( pq->data );
pq->size = 0;
}
static
void pq_sink( struct priority_queue * pq, int index )
{
for (int parent = index;;)
{
// Find the first child of the parent node
int child = (parent * 2) + 1;
if (child >= pq->size)
return;
// Find the child with the minimum priority
if (child+1 < pq->size)
if (pq->data[child+1].priority < pq->data[child].priority)
child += 1;
// Priority for parent must be minimum of (parent, children...)
if (pq->data[parent].priority <= pq->data[child].priority)
return;
// (swap)
struct pq_node node = pq->data[parent];
pq->data[parent] = pq->data[child];
pq->data[child] = node;
// (next)
parent = child;
}
}
void pq_insert( struct priority_queue * pq, XY value, int priority )
{
// Append the new value to the pq data
int child = pq->size ++;
pq->data[child].priority = priority;
pq->data[child].value = value;
// Apply the heap property for each parent of the new value
while (child)
{
int parent = (child - 1) / 2;
pq_sink( pq, parent );
child = parent;
}
}
XY pq_pop( struct priority_queue * pq )
{
XY result = pq->data[0].value; // Result is the min-heap value
pq->data[0] = pq->data[ -- pq->size ]; // Move the last value in the heap data to the min-heap position...
pq_sink( pq, 0 ); // ...then sink it
return result;
}
|