Something odd going on with the puzzle database.

This commit is contained in:
Scott Duensing 2020-07-27 19:41:23 -05:00
parent 2654b709ab
commit 9d962fa564
13 changed files with 486 additions and 9 deletions

2
.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
*.xcf filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text

9
.gitignore vendored
View file

@ -1,3 +1,10 @@
build-* build-*-Debug
*.dat *.dat
*~ *~
*.user
*.dis
*.lnk
*.map
*.stn
*.img
*.sym

BIN
Project Icon.jpg (Stored with Git LFS) Normal file

Binary file not shown.

View file

@ -1,3 +1,25 @@
/*
* 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 <stdio.h>
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
@ -8,14 +30,16 @@
#define MAX_LINE 8192 #define MAX_LINE 8192
static char puzzleChars[] = { "_# .$@+*" };
int isLegal(char c) { int isLegal(char c) {
int x; int x;
char legal[] = { "_# .$@+*" };
if (c == '-') c = ' '; if (c == '-') c = ' ';
for (x=0; x<(int)strlen(legal); x++) { for (x=0; x<(int)strlen(puzzleChars); x++) {
if (legal[x] == c) { if (puzzleChars[x] == c) {
return x + 1; return x + 1;
} }
} }
@ -29,6 +53,8 @@ int main(int argc, char *argv[]) {
char line[MAX_LINE]; // Ooo! Look! Potential buffer overflow! char line[MAX_LINE]; // Ooo! Look! Potential buffer overflow!
char height; char height;
unsigned char width; unsigned char width;
unsigned char b1;
unsigned char b2;
int16_t x; int16_t x;
int16_t y; int16_t y;
int16_t count = 0; int16_t count = 0;
@ -64,12 +90,19 @@ int main(int argc, char *argv[]) {
width = 0; width = 0;
while (fgets(line, MAX_LINE, in)) { while (fgets(line, MAX_LINE, in)) {
// Is there a newline to remove?
if ((strlen(line) > 0) && (line[strlen(line) - 1] == '\n')) {
line[strlen(line) - 1] = 0;
}
// Is there anything on this line? // Is there anything on this line?
if (strlen(line) > 0) { if (strlen(line) > 0) {
// Is this a puzzle line? // Is this a puzzle line?
if (isLegal(line[0]) > 0) { if (isLegal(line[0]) > 0) {
// Got it. Read this line. // Got it. Read this line.
if (width < strlen(line)) width = strlen(line); if (width < strlen(line)) {
width = strlen(line);
printf("Width %d\n", width);
}
height++; height++;
for (x=0; x<width; x++) { for (x=0; x<width; x++) {
puzzle[x][(int)height] = line[x]; puzzle[x][(int)height] = line[x];
@ -78,26 +111,36 @@ int main(int argc, char *argv[]) {
// Not a puzzle line. Were we currently reading a puzzle? // Not a puzzle line. Were we currently reading a puzzle?
if (height >= 0) { if (height >= 0) {
// Yep! Output it! // Yep! Output it!
height++; // Make it 1 based ('width' has been 1 based the whole time)
if ((width & 1) == 1) { if ((width & 1) == 1) {
// Width must be even - fill new column with zeros. // Width must be even - fill new column with 1, which will become zero below.
for (y=0; y<height; y++) puzzle[width][y] = 0; for (y=0; y<height; y++) {
puzzle[width][y] = 1;
}
width++; width++;
printf("Increased width to %d\n", width);
} }
fputc(width, out); fputc(width, out);
fputc(height + 1, out); fputc(height, out);
bytes += 2; bytes += 2;
// Write out the puzzle data, two tiles to a byte. // Write out the puzzle data, two tiles to a byte.
printf("%dx%d\n", width, height);
for (y=0; y<height; y++) { for (y=0; y<height; y++) {
for (x=0; x<width; x+=2) { for (x=0; x<width; x+=2) {
fputc((isLegal(puzzle[x][y]) << 4) + isLegal(puzzle[x + 1][y]), out); b1 = isLegal(puzzle[x][y]) - 1;
b2 = isLegal(puzzle[x + 1][y]) - 1;
printf("%c%c", puzzleChars[b1], puzzleChars[b2]);
fputc((b1 << 4) + b2, out);
bytes++; bytes++;
} }
printf("\n");
} }
// Reset for next pass. // Reset for next pass.
count++; count++;
width = 0; width = 0;
height = -1; height = -1;
printf("Puzzle %d imported, %d total bytes written.\n", count, bytes); printf("Puzzle %d imported, %d total bytes written.\n", count, bytes);
break;
} }
} }
} }

73
warehouse/.gitignore vendored Normal file
View file

@ -0,0 +1,73 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

10
warehouse/build-IIgs.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/bash -e
PROJECT=warehouse
DATA=(font.img font.stn kanga.img tiles.img ../puzzles.dat)
#SOURCE=(*.c *.h)
SOURCE=()
. ${JOEY}/joeylib/scripts/build-IIgs.helper.sh
buildIIgs $1

BIN
warehouse/font.xcf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
warehouse/kanga.xcf (Stored with Git LFS) Normal file

Binary file not shown.

212
warehouse/main.c Normal file
View file

@ -0,0 +1,212 @@
/*
* 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_CRATE_ON_GOAL 6
#define TILE_PLAYER_ON_GOAL 7
typedef struct PuzzleS {
byte width;
byte height;
byte puzzle[MAX_WIDTH][MAX_HEIGHT];
} PuzzleT;
typedef struct SaveS {
jint16 lastPuzzle;
jint16 solvedSize;
byte *solved;
} SaveT;
static jlImgT *fontI = NULL;
static jlImgT *tilesI = NULL;
static jint16 puzzleCount = 0;
static PuzzleT puzzle;
static SaveT saveGame;
static char puzzleChars[] = { "_# .$@+*" };
void loadPuzzle(jint16 number);
void printAt(jlImgT *font, jint16 cx, jint16 cy, const char *what, ...);
void loadPuzzle(jint16 number) {
FILE *in = NULL;
jint16 count = 0;
byte x = 0;
byte y = 0;
in = fopen("data/puzzles.dat", "rb");
if (!in) jlUtilDie("Unable to open puzzle database!");
// Skip over puzzle count bytes.
fread(&puzzleCount, sizeof(jint16), 1, in);
// Iterate until we find the puzzle we need.
while (count < number) {
// Load width of puzzle
puzzle.width = fgetc(in);
// Load height of puzzle
puzzle.height = fgetc(in);
// Load the puzzle itself
for (y=0; y<puzzle.height; y++) {
for (x=0; x<puzzle.width; x+=2) {
puzzle.puzzle[x][y] = fgetc(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;
}
}
// Count it.
count++;
}
fclose(in);
//***DEBUG***
printf("Puzzles found: %d\n", puzzleCount);
printf("Puzzle %d - %dx%d\n", count, puzzle.width, puzzle.height);
for (y=0; y<puzzle.height; y++) {
for (x=0; x<puzzle.width; x++) {
printf("%c", puzzleChars[puzzle.puzzle[x][y]]);
}
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 * 8;
ty = cy * 8;
for (counter=0; counter<(int)strlen(msg); counter++) {
x = (msg[counter] % 40) * 8;
y = (msg[counter] / 40) * 8;
jlDrawBlit8x8(jlImgSurfaceGet(font), x, y, tx, ty);
tx += 8;
}
}
int main(void) {
FILE *in = NULL;
jlUtilStartup("Warehouse");
// Get something on the screen as quickly as possible.
jlDrawColorSet(0);
jlDisplayBorder(BORDER_BLACK);
jlDrawClear();
// For splash screens, reuse tilesI to save memory.
jlImgLoad(tilesI, "kanga");
if (!tilesI) jlUtilDie("Unable to load kanga!");
// jlImgDisplay(tilesI);
// jlDisplayPresent();
// 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!");
// How many puzzles?
in = fopen("data/puzzles.dat", "rb");
if (!in) jlUtilDie("Unable to open puzzle database!");
fread(&puzzleCount, sizeof(jint16), 1, 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);
}
loadPuzzle(1);
jlDrawClear();
jlDrawColorSet(15);
printAt(fontI, 0, 0, "%d puzzles found", puzzleCount);
jlDisplayPresent();
jlKeyWaitForAny();
jlFree(saveGame.solved);
jlImgFree(tilesI);
jlImgFree(fontI);
jlUtilShutdown();
}

9
warehouse/postlink.sh Executable file
View file

@ -0,0 +1,9 @@
#!/bin/bash
GAME=$1
export JOEY=$2
mkdir -p data
cp -f "${GAME}"/../puzzles.dat data/.
cp -f "${GAME}"/*.img data/.
cp -f "${GAME}"/*.stn data/.

95
warehouse/stddclmr.h Normal file
View file

@ -0,0 +1,95 @@
#ifndef STDDCLMR_H
#define STDDCLMR_H
/*
Action figures sold separately. Add toner. All models over 18 years of age.
All rights reserved. Allow four to six weeks for delivery. An equal
opportunity employer. Any resemblance to actual persons, living or dead, is
unintentional and purely coincidental. Apply only to affected area. Approved
for veterans. As seen on TV. At participating locations only. Avoid contact
with mucous membranes. Avoid contact with skin. Avoid extreme temperatures
and store in a cool dry place. Batteries not included. Be sure each item is
properly endorsed. Beware of dog. Booths for two or more. Breaking seal
constitutes acceptance of agreement. Call toll free number before digging.
Caveat emptor. Check here if tax deductible. Close cover before striking
Colors may fade. Contains a substantial amount of non-tobacco ingredients.
Contents may settle during shipment. Contestants have been briefed on some
questions before the show. Copyright 1995 Joker's Wild. Disclaimer does
not cover hurricane, lightning, tornado, tsunami, volcanic eruption,
earthquake, flood, and other Acts of God, misuse, neglect, unauthorized
repair, damage from improper installation, broken antenna or marred cabinet,
incorrect line voltage, missing or altered serial numbers, sonic boom
vibrations, electromagnetic radiation from nuclear blasts, customer
adjustments that are not covered in the joke list, and incidents owing to
airplane crash, ship sinking, motor vehicle accidents, leaky roof, broken
glass, falling rocks, mud slides, forest fire, flying projectiles, or
dropping the item. Do not bend, fold, mutilate, or spindle. Do not place
near flammable or magnetic source. Do not puncture, incinerate, or store
above 120 degrees Fahrenheit. Do not stamp. Use other side for additional
listings. Do not use while operating a motor vehicle or heavy equipment. Do
not write below this line. Documents are provided "as is" without any
warranties expressed or implied. Don't quote me on anything. Don't quote me
on that. Driver does not carry cash. Drop in any mailbox. Edited for
television. Employees and their families are not eligible. Falling rock.
First pull up, then pull down. Flames redirected to /dev/null. For a
limited time only. For external use only. For off-road use only. For office
use only. For recreational use only. Do not disturb. Freshest if eaten
before date on carton. Hand wash only, tumble dry on low heat. If a rash,
redness, irritation, or swelling develops, discontinue use. If condition
persists, consult your physician. If defects are discovered, do not attempt
to fix them yourself, but return to an authorized service center. If
ingested, do not induce vomiting, if symptoms persist, consult a doctor.
Keep away from open flames and avoid inhaling fumes. Keep away from
sunlight, pets, and small children. Keep cool; process promptly. Limit
one-per-family please. Limited time offer, call now to ensure prompt
delivery. List at least two alternate dates. List each check separately by
bank number. List was current at time of printing. Lost ticket pays maximum
rate. May be too intense for some viewers. Must be 18 to enter. No Canadian
coins. No alcohol, dogs or horses. No anchovies unless otherwise specified.
No animals were harmed in the production of these documents. No money down.
No other warranty expressed or implied. No passes accepted for this
engagement. No postage necessary if mailed in the United States. No
preservatives added. No purchase necessary. No salt, MSG, artificial color
or flavor added. No shoes, no shirt, no service, no kidding. No solicitors.
No substitutions allowed. No transfers issued until the bus comes to a
complete stop. No user-serviceable parts inside. Not affiliated with the
American Red Cross. Not liable for damages due to use or misuse. Not
recommended for children. Not responsible for direct, indirect, incidental
or consequential damages resulting from any defect, error or failure to
perform. Not the Beatles. Objects in mirror may be closer than they appear.
One size fits all. Many suitcases look alike. Other copyright laws for
specific entries apply wherever noted. Other restrictions may apply. Package
sold by weight, not volume. Parental advisory - explicit lyrics. Penalty for
private use. Place stamp here. Please remain seated until the ride has come
to a complete stop. Possible penalties for early withdrawal. Post office will
not deliver without postage. Postage will be paid by addressee. Prerecorded
for this time zone. Price does not include taxes. Processed at location
stamped in code at top of carton. Quantities are limited while supplies last.
Read at your own risk. Record additional transactions on back of previous
stub. Replace with same type. Reproduction strictly prohibited. Restaurant
package, not for resale. Return to sender, no forwarding order on file,
unable to forward. Safety goggles may be required during use. Sanitized for
your protection. Sealed for your protection, do not use if the safety seal is
broken. See label for sequence. Shading within a garment may occur. Sign here
without admitting guilt. Simulated picture. Slightly enlarged to show detail.
Slightly higher west of the Rockies. Slippery when wet. Smoking these may be
hazardous to your health. Some assembly required. Some equipment shown is
optional. Some of the trademarks mentioned in this product appear for
identification purposes only. Subject to FCC approval. Subject to change
without notice. Substantial penalty for early withdrawal. Text may contain
material some readers may find objectionable, parental guidance is advised.
Text used in these documents is made from 100% recycled electrons and magnetic
particles. These documents do not reflect the thoughts or opinions of either
myself, my company, my friends, or my rabbit. This is not an offer to sell
securities. This offer is void where prohibited, taxed, or otherwise
restricted. This product is meant for educational purposes only. Times
approximate. Unix is a registered trademark of AT&T. Use only as directed. Use
only in a well-ventilated are. User assumes full liabilities. Void where
prohibited. We have sent the forms which seem right for you. You must be
present to win. You need not be present to win. Your canceled check is your
receipt. Your mileage may vary. I didn't do it. You can't prove anything.
This supersedes all previous notices.
*/
#endif // STDDCLMR_H

BIN
warehouse/tiles.xcf (Stored with Git LFS) Normal file

Binary file not shown.

14
warehouse/warehouse.pro Normal file
View file

@ -0,0 +1,14 @@
JOEY = /home/scott/joey
include($$JOEY/dist/joey.pri)
HEADERS += \
stddclmr.h
SOURCES += \
main.c
OTHER_FILES += \
build-IIgs.sh \
postlink.sh
linux:QMAKE_POST_LINK = $$PWD/postlink.sh "$$PWD" "$$JOEY"