warehouse/warehouse/main.c
2020-07-29 20:12:04 -05:00

672 lines
16 KiB
C

/*
* Warehouse for JoeyLib - A Sokoban Clone
* Copyright (C) 2020 Scott Duensing <scott@kangaroopunch.com>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stddclmr.h>
#define JOEY_MAIN
#include "joey.h"
#ifdef JOEY_IIGS
segment "warehouse";
#endif
#define MAX_WIDTH 20
#define MAX_HEIGHT 12
#define TILE_NOTHING 0
#define TILE_WALL 1
#define TILE_FLOOR 2
#define TILE_GOAL 3
#define TILE_CRATE 4
#define TILE_PLAYER 5
#define TILE_PLAYER_ON_GOAL 6
#define TILE_CRATE_ON_GOAL 7
#define TILE_COUNT 8
typedef struct PuzzleS {
byte offsetX;
byte offsetY;
byte width;
byte height;
byte puzzle[MAX_WIDTH][MAX_HEIGHT];
} PuzzleT;
typedef struct SaveS {
jint16 lastPuzzle;
jint16 solvedSize;
byte *solved;
} SaveT;
typedef struct CoordS {
byte x;
byte y;
} CoordT;
static jlImgT *fontI = NULL;
static jlImgT *tilesI = NULL;
static jint16 puzzleCount;
static jint32 *puzzleIndex = NULL;
static PuzzleT puzzle;
static SaveT saveGame;
static byte puzzleNow[MAX_WIDTH][MAX_HEIGHT];
static byte puzzleBefore[MAX_WIDTH][MAX_HEIGHT];
static byte avatarX;
static byte avatarY;
static byte avatarXLast;
static byte avatarYLast;
static byte crateCount;
static byte cratesOnTarget;
static char puzzleChars[] = { "_# .$@+*" };
static CoordT tileLookup[TILE_COUNT] = {
{ 10 * 8, 0 },
{ 0 * 8, 0 },
{ 2 * 8, 0 },
{ 4 * 8, 0 },
{ 6 * 8, 0 },
{ 8 * 8, 0 },
{ 8 * 8, 0 },
{ 6 * 8, 0 }
};
void drawAvatar(void);
byte drawMenu(char *menu[], byte width, byte *height, byte *offsetX, byte *offsetY);
void drawPuzzle(void);
void forceFullRedraw(void);
void loadPuzzle(jint16 number);
bool menu(void);
void moveCrate(byte sx, byte sy, byte dx, byte dy);
void play(void);
void printPuzzle(char *message, byte which[MAX_WIDTH][MAX_HEIGHT]);
void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...);
void redraw(void);
void showPalette(void);
void title(void);
void drawAvatar(void) {
jint16 x = 0; // Screen (tile) coordinates.
jint16 y = 0;
CoordT t;
x = ((avatarX << 1) + puzzle.offsetX) << 3;
y = ((avatarY << 1) + puzzle.offsetY) << 3;
t = tileLookup[TILE_PLAYER];
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x, t.y, x, y );
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x + 8, t.y, x + 8, y );
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x, t.y + 8, x, y + 8);
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x + 8, t.y + 8, x + 8, y + 8);
}
byte drawMenu(char *menu[], byte width, byte *height, byte *offsetX, byte *offsetY) {
jint16 count = 0;
jint16 lx = ((byte)221 % 40) << 3; // Left-hand block ASCII
jint16 ly = ((byte)221 / 40) << 3;
jint16 rx = ((byte)222 % 40) << 3; // Right-hand block ASCII
jint16 ry = ((byte)222 / 40) << 3;
jint16 tx = ((byte)223 % 40) << 3; // Top-half block ASCII
jint16 ty = ((byte)223 / 40) << 3;
jint16 bx = ((byte)220 % 40) << 3; // Bottom-half block ASCII
jint16 by = ((byte)220 / 40) << 3;
jint16 x1;
jint16 y1;
jint16 x2;
jint16 y2;
jint16 y3;
// 222 223 221
// 222 220 221
// Calculate height and offsets from menu count.
while (menu[count]) {
count++;
}
*height = 13 + count;
*offsetX = 20 - width / 2;
*offsetY = 11 - *height / 2;
// Clear area behind menu. Slightly higher and lower to add borders to the non-bordered ASCII characters.
jlDrawBoxFilled(*offsetX * 8, *offsetY * 8 - 4, (*offsetX + width + 1) * 8, (*offsetY + *height) * 8 + 4);
// Draw sides.
x1 = *offsetX * 8;
x2 = (*offsetX + width) * 8;
for (y1=(*offsetY * 8); y1<(*offsetY + *height) * 8; y1+=8) {
jlDrawBlit8x8(jlImgSurfaceGet(fontI), rx, ry, x1, y1);
jlDrawBlit8x8(jlImgSurfaceGet(fontI), lx, ly, x2, y1);
}
// Draw horizontal lines.
y1 = *offsetY * 8;
y2 = (*offsetY + 4) * 8;
y3 = (*offsetY + *height - 1) * 8;
for (x1=(*offsetX + 1) * 8; x1<(*offsetX + width) * 8; x1+=8) {
jlDrawBlit8x8(jlImgSurfaceGet(fontI), tx, ty, x1, y1);
jlDrawBlit8x8(jlImgSurfaceGet(fontI), bx, by, x1, y2);
jlDrawBlit8x8(jlImgSurfaceGet(fontI), bx, by, x1, y3);
}
printAt(fontI, 16, *offsetY + 2, "Main Menu");
// Draw menu.
x1 = *offsetX + 5;
y1 = *offsetY + 7;
count = 0;
while (menu[count]) {
printAt(fontI, x1, y1, menu[count]);
y1+=2;
count++;
}
// Returns number of menu items.
return count;
}
void drawPuzzle(void) {
byte bx = 0; // Board coordinates.
byte by = 0;
jint16 x = 0; // Screen (tile) coordinates.
jint16 y = 0;
CoordT t;
for (by=0; by<puzzle.height; by++) {
y = ((by << 1) + puzzle.offsetY) << 3;
for (bx=0; bx<puzzle.width; bx++) {
// Is this tile "dirty" on the display?
if (puzzleNow[bx][by] != puzzleBefore[bx][by]) {
puzzleBefore[bx][by] = puzzleNow[bx][by];
x = ((bx << 1) + puzzle.offsetX) << 3;
t = tileLookup[puzzleNow[bx][by]];
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x, t.y, x, y );
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x + 8, t.y, x + 8, y );
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x, t.y + 8, x, y + 8);
jlDrawBlit8x8(jlImgSurfaceGet(tilesI), t.x + 8, t.y + 8, x + 8, y + 8);
}
}
}
}
void forceFullRedraw(void) {
// Clear "displayed" board so it has to redraw.
memset(&puzzleBefore, TILE_NOTHING, sizeof(byte) * MAX_WIDTH * MAX_HEIGHT);
}
void loadPuzzle(jint16 number) {
FILE *in = NULL;
byte x = 0;
byte y = 0;
// NOTE: We don't use fgetc() because it's borked in ORCA/C.
in = fopen("data/puzzles.dat", "rb");
if (!in) jlUtilDie("Unable to open puzzle database!");
crateCount = 0;
cratesOnTarget = 0;
// Skip to requested puzzle.
fseek(in, puzzleIndex[number - 1], SEEK_SET);
// Load width of puzzle
fread(&puzzle.width, sizeof(byte), 1, in);
// Load height of puzzle
fread(&puzzle.height, sizeof(byte), 1, in);
// Load the puzzle itself
for (y=0; y<puzzle.height; y++) {
for (x=0; x<puzzle.width; x+=2) {
fread(&puzzle.puzzle[x][y], sizeof(byte), 1, in);
// Split single byte into two tiles.
puzzle.puzzle[x + 1][y] = puzzle.puzzle[x][y] & 0x0f;
puzzle.puzzle[x][y] = puzzle.puzzle[x][y] >> 4;
// Is this our start location? If so, replace with proper tile and remember where we are.
if (puzzle.puzzle[x][y] == TILE_PLAYER) {
puzzle.puzzle[x][y] = TILE_FLOOR;
avatarX = x;
avatarY = y;
}
if (puzzle.puzzle[x][y] == TILE_PLAYER_ON_GOAL) {
puzzle.puzzle[x][y] = TILE_GOAL;
avatarX = x;
avatarY = y;
}
if (puzzle.puzzle[x + 1][y] == TILE_PLAYER) {
puzzle.puzzle[x + 1][y] = TILE_FLOOR;
avatarX = x + 1;
avatarY = y;
}
if (puzzle.puzzle[x + 1][y] == TILE_PLAYER_ON_GOAL) {
puzzle.puzzle[x + 1][y] = TILE_GOAL;
avatarX = x + 1;
avatarY = y;
}
// Get crate tallys.
if ((puzzle.puzzle[x][y] == TILE_CRATE) || (puzzle.puzzle[x + 1][y] == TILE_CRATE)) {
crateCount++;
}
if ((puzzle.puzzle[x][y] == TILE_CRATE_ON_GOAL) || (puzzle.puzzle[x + 1][y] == TILE_CRATE_ON_GOAL)) {
crateCount++;
cratesOnTarget++;
}
}
}
fclose(in);
// Center puzzle on display.
puzzle.offsetX = (10 - puzzle.width / 2) * 2;
puzzle.offsetY = (6 - puzzle.height / 2) * 2;
// Make copy for our rendering array.
memcpy(&puzzleNow, puzzle.puzzle, sizeof(byte) * MAX_WIDTH * MAX_HEIGHT);
forceFullRedraw();
}
bool menu(void) {
char *menu[] = { "About", "How to Play", "Select Level", "Reset Level", "Exit", 0 };
byte count = 0;
byte height;
byte offsetX;
byte offsetY;
byte inMenuYOffset = 7;
byte key;
jint16 rx = ((byte)175 % 40) << 3; // Right Arrows ASCII
jint16 ry = ((byte)175 / 40) << 3;
jint16 sx = ((byte)32 % 40) << 3; // Space ASCII
jint16 sy = ((byte)32 / 40) << 3;
jint16 xpos;
jint16 ypos;
jint16 selected;
jint16 last;
jint16 lastY;
bool inMenu = true;
bool keepPlayingn = true;
count = drawMenu(menu, 20, &height, &offsetX, &offsetY);
inMenuYOffset += offsetY;
xpos = (offsetX + 3) * 8;
ypos = inMenuYOffset * 8;
lastY = ypos;
selected = 0;
last = 1;
while (inMenu && !jlUtilMustExit()) {
if (jlKeyPressed()) {
while (jlKeyPressed()) {
key = jlKeyRead();
}
switch (key) {
case 27:
inMenu = false;
forceFullRedraw();
jlDrawClear();
break;
case 13:
switch (selected) {
case 4: // Exit
inMenu = false;
keepPlayingn = false;
break;
}
break;
case 'I':
case 'i':
if (selected > 0) {
selected--;
ypos -= 16;
} else {
selected = count - 1;
ypos = (inMenuYOffset + (count - 1) * 2) * 8;
}
break;
case 'M':
case 'm':
if (selected < count - 1) {
selected++;
ypos += 16;
} else {
selected = 0;
ypos = inMenuYOffset * 8;
}
break;
}
}
if (selected != last) {
jlDrawBlit8x8(jlImgSurfaceGet(fontI), sx, sy, xpos, lastY);
jlDrawBlit8x8(jlImgSurfaceGet(fontI), rx, ry, xpos, ypos);
jlDisplayPresent();
lastY = ypos;
last = selected;
}
}
// Return 'false' to exit the game.
return keepPlayingn;
}
void moveCrate(byte sx, byte sy, byte dx, byte dy) {
if (puzzleNow[sx][sy] == TILE_CRATE) {
puzzleNow[sx][sy] = TILE_FLOOR;
} else {
puzzleNow[sx][sy] = TILE_GOAL;
cratesOnTarget--;
}
if (puzzleNow[dx][dy] == TILE_FLOOR) {
puzzleNow[dx][dy] = TILE_CRATE;
} else {
puzzleNow[dx][dy] = TILE_CRATE_ON_GOAL;
cratesOnTarget++;
}
}
void play(void) {
jint16 current = 1;
jint16 last = 0;
char key = 0;
bool playing = true;
byte tile = 0;
// Force tile palette
jlImgDisplay(tilesI);
// Show menu on entry
playing = menu();
while (playing && !jlUtilMustExit()) {
if (jlKeyPressed()) {
while (jlKeyPressed()) {
key = jlKeyRead();
}
avatarXLast = avatarX;
avatarYLast = avatarY;
switch (key) {
case 27:
playing = menu();
if (playing) redraw();
break;
case 'Q':
case 'q':
current--;
if (current < 1) {
current = puzzleCount;
}
break;
case 'W':
case 'w':
current++;
if (current >= puzzleCount) {
current = 1;
}
break;
case 'I':
case 'i':
// Can we move up?
tile = puzzleNow[avatarX][avatarY - 1];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarY--;
}
// Can we push up?
if ((tile == TILE_CRATE) || (tile == TILE_CRATE_ON_GOAL)) {
tile = puzzleNow[avatarX][avatarY - 2];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarY--;
moveCrate(avatarX, avatarY, avatarX, avatarY - 1);
}
}
break;
case 'J':
case 'j':
// Can we move left?
tile = puzzleNow[avatarX - 1][avatarY];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarX--;
}
// Can we push left?
if ((tile == TILE_CRATE) || (tile == TILE_CRATE_ON_GOAL)) {
tile = puzzleNow[avatarX - 2][avatarY];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarX--;
moveCrate(avatarX, avatarY, avatarX - 1, avatarY);
}
}
break;
case 'K':
case 'k':
// Can we move right?
tile = puzzleNow[avatarX + 1][avatarY];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarX++;
}
// Can we push right?
if ((tile == TILE_CRATE) || (tile == TILE_CRATE_ON_GOAL)) {
tile = puzzleNow[avatarX + 2][avatarY];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarX++;
moveCrate(avatarX, avatarY, avatarX + 1, avatarY);
}
}
break;
case 'M':
case 'm':
// Can we move down?
tile = puzzleNow[avatarX][avatarY + 1];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarY++;
}
// Can we push down?
if ((tile == TILE_CRATE) || (tile == TILE_CRATE_ON_GOAL)) {
tile = puzzleNow[avatarX][avatarY + 2];
if ((tile == TILE_FLOOR) || (tile == TILE_GOAL)) {
avatarY++;
moveCrate(avatarX, avatarY, avatarX, avatarY + 1);
}
}
break;
}
}
// Load new level?
if (last != current) {
last = current;
loadPuzzle(current);
avatarXLast = -1;
avatarYLast = -1;
jlDrawClear();
printAt(fontI, 0, 24, "%d puzzles. Showing #%d", puzzleCount, current);
}
// Redraw?
if ((avatarX != avatarXLast) || (avatarY != avatarYLast)) {
puzzleBefore[avatarXLast][avatarYLast] = TILE_NOTHING;
redraw();
}
}
}
void printPuzzle(char *message, byte which[MAX_WIDTH][MAX_HEIGHT]) {
byte bx = 0; // Board coordinates.
byte by = 0;
printf("%s\n", message);
for (by=0; by<puzzle.height; by++) {
for (bx=0; bx<puzzle.width; bx++) {
printf("%c", puzzleChars[which[bx][by]]);
}
printf("\n");
}
}
// (Slow) Font Printing
__attribute__((__format__ (__printf__, 4, 0)))
void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...) {
jint16 x;
jint16 y;
jint16 tx;
jint16 ty;
jint16 counter;
char msg[40]; // Very short messages (screen width). Be careful!
va_list va;
va_start(va, what);
vsprintf(msg, what, va);
va_end(va);
tx = cx << 3;
ty = cy << 3;
for (counter=0; counter<(int)strlen(msg); counter++) {
x = ((byte)msg[counter] % 40) << 3;
y = ((byte)msg[counter] / 40) << 3;
jlDrawBlit8x8(jlImgSurfaceGet(font), x, y, tx, ty);
tx += 8;
}
}
void redraw(void) {
//printPuzzle("Now", puzzleNow);
//printPuzzle("Before", puzzleBefore);
drawPuzzle();
drawAvatar();
printAt(fontI, 30, 24, "%d/%d ", cratesOnTarget, crateCount);
jlDisplayPresent();
}
void showPalette(void) {
byte x;
for (x=0; x<16; x++) {
jlDrawColorSet(x);
jlDrawBoxFilled(x * 20, 179, x * 20 + 19, 199);
}
}
void title(void) {
char *images[] = { "kanga", "backgrnd", 0 };
byte count = 0;
jlDrawColorSet(0);
jlDisplayBorder(BORDER_BLACK);
jlDrawClear();
while (images[count] && !jlUtilMustExit()) {
// For splash screens, reuse tilesI to save memory.
jlImgLoad(tilesI, images[count]);
jlImgDisplay(tilesI);
//showPalette();
jlDisplayPresent();
jlUtilSleep(20);
jlDrawClear();
jlDisplayPresent();
jlUtilSleep(3);
count++;
}
}
int main(void) {
FILE *in = NULL;
jlUtilStartup("Warehouse");
// Get something on the screen as quickly as possible.
//title();
// Load the rest of our data.
jlImgLoad(tilesI, "tiles");
if (!tilesI) jlUtilDie("Unable to load tiles!");
jlImgLoad(fontI, "font");
if (!fontI) jlUtilDie("Unable to load font!");
// Load the index.
in = fopen("data/index.dat", "rb");
if (!in) jlUtilDie("Unable to open puzzle index!");
// How many puzzles?
fread(&puzzleCount, sizeof(jint16), 1, in);
puzzleIndex = (jint32 *)jlMalloc(sizeof(jint32) * puzzleCount);
fread(puzzleIndex, sizeof(jint32), puzzleCount, in);
fclose(in);
// Create bit array of possible games for save file.
saveGame.solvedSize = sizeof(byte) * (size_t)((float)(puzzleCount + 0.5f) / 8.0f);
saveGame.solved = (byte *)jlMalloc(saveGame.solvedSize);
// Does this user have a save file?
in = fopen("data/save.dat", "rb");
if (in) {
// Load save file.
fread(&puzzleCount, sizeof(jint16), 1, in);
fread(&saveGame.lastPuzzle, sizeof(jint16), 1, in);
fread(&saveGame.solvedSize, sizeof(jint16), 1, in);
fread(saveGame.solved, saveGame.solvedSize, 1, in);
fclose(in);
} else {
// No save. Prime the pump.
saveGame.lastPuzzle = 1;
memset(saveGame.solved, 0, saveGame.solvedSize);
}
play();
jlFree(saveGame.solved);
jlFree(puzzleIndex);
jlImgFree(tilesI);
jlImgFree(fontI);
jlUtilShutdown();
}