/*
 *	Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *	SOFTWARE.
 */


#ifndef STORY_H
#define STORY_H


#include "common.h"
#include "portme.h"


#define STORY_STATUS_MASK           0xfe
#define STORY_STATUS_SCORE_TURNS    0
#define STORY_STATUS_HOURS_MINUTES  1

#define STORY_FLAG_V3                            0x01

#define STORY_FLAG_V3_STATUS                     (1 << 1)
#define STORY_FLAG_V3_TWO_DISKS                  (1 << 2)
#define STORY_FLAG_V3_STATUS_LINE_NOT_AVAILABLE  (1 << 4)
#define STORY_FLAG_V3_SCREEN_SPLITTING           (1 << 5)
#define STORY_FLAG_V3_VARIABLE_PITCH_FONT        (1 << 6)

#define STORY_FLAG_V4_COLOR        (1 << 0)
#define STORY_FLAG_V4_PICTURE      (1 << 1)
#define STORY_FLAG_V4_BOLDFACE     (1 << 2)
#define STORY_FLAG_V4_ITALIC       (1 << 3)
#define STORY_FLAG_V4_FIXED_SPACE  (1 << 4)
#define STORY_FLAG_V4_SOUND        (1 << 5)
#define STORY_FLAG_V4_TIMED_INPUT  (1 << 7)

#define STORY_FLAG_2_V1_TRANSCRIPTING        (1 << 0)
#define STORY_FLAG_2_V3_FORCE_FIXED_PITCHED  (1 << 1)
#define STORY_FLAG_2_V6_FORCE_REDRAW         (1 << 2)
#define STORY_FLAG_2_V5_USE_PICTURES         (1 << 3)
#define STORY_FLAG_2_V5_USE_UNDO             (1 << 4)
#define STORY_FLAG_2_V5_USE_MOUSE            (1 << 5)
#define STORY_FLAG_2_V5_USE_COLOR            (1 << 6)
#define STORY_FLAG_2_V5_USE_SOUND            (1 << 7)
#define STORY_FLAG_2_V6_USE_MENUS            (1 << 8)

#define STORY_SCREEN_HEIGHT_INFINITE  255

#define STORY_SCREEN_HEIGHT_LINES      0x20
#define STORY_SCREEN_WIDTH_CHARACTERS  0x21

#define STORY_INTERPRETER_NUMBER_DECSYSTEM_20    1
#define STORY_INTERPRETER_NUMBER_APPLE_IIE       2
#define STORY_INTERPRETER_NUMBER_MACINTOSH       3
#define STORY_INTERPRETER_NUMBER_AMIGA           4
#define STORY_INTERPRETER_NUMBER_ATARI_ST        5
#define STORY_INTERPRETER_NUMBER_IBM_PC          6
#define STORY_INTERPRETER_NUMBER_COMMODORE_128   7
#define STORY_INTERPRETER_NUMBER_COMMODORE_64    8
#define STORY_INTERPRETER_NUMBER_APPLE_IIC       9
#define STORY_INTERPRETER_NUMBER_APPLE_IIGS     10
#define STORY_INTERPRETER_NUMBER_TANDY_COLOR    11

#define STORY_FLAG_3_USE_TRANSPARENCY  (1 << 0)

#define storyVersion()                            portByteGet(0x0)
#define storyFlags()                              portByteGet(STORY_FLAG_V3)
#define storyHightMemory()                        portWordGet(0x4)
#define storyInitialPC()                          portWordGet(0x6)                                       // V1-5
#define storyInitialPackedPC()                    memoryUnpackAddress(portWordGet(0x6), MEMORY_ROUTINE)  // V6
#define storyDictionaryAddress()                  portWordGet(0x8)
#define storyObjectTableAddress()                 portWordGet(0xa)
#define storyGlobalVariableTableAddress()         portWordGet(0xc)
#define storyStaticMemoryBaseAddress()            portWordGet(0xe)
#define storyFlags2()                             portWordGet(0x10)
#define storyAbbreviationTableAddress()           portWordGet(0x18)
#define storyFileSize()                           portWordGet(0x1a)
#define storyChecksum()                           portWordGet(0x1c)
#define storyInterpreterNumber()                  portByteGet(0x1e)
#define storyInterpreterVersion()                 portByteGet(0x1f)
#define storyScreenHeightLines()                  portByteGet(STORY_SCREEN_HEIGHT_LINES)
#define storyScreenWidthCharacters()              portByteGet(STORY_SCREEN_WIDTH_CHARACTERS)
#define storyScreenHeightUnits()                  portWordGet(0x22)
#define storyScreenWidthUnits()                   portWordGet(0x24)
#define storyFontHeightUnits()                    (storyVersion() == 5 ? portByteGet(0x27) ? portByteGet(0x26))  // WTF Infocom?
#define storyFontWidthUnits()                     (storyVersion() == 5 ? portByteGet(0x26) ? portByteGet(0x27))
#define storyRoutinesOffset()                     portWordGet(0x28)
#define storyStringsOffset()                      portWordGet(0x2a)
#define storyDefaultBackgroundColor()             portByteGet(0x2c)
#define storyDefaultForegroundColor()             portByteGet(0x2d)
#define storyTerminatingCharactersTableAddress()  portWordGet(0x2e)
#define storyWidthInPixelsToOutputStream3()       portByteGet(0x30)
#define storyStandardRevisionNumber()             portByteGet(0x32)
#define storyAlphabetTableAddress()               portWordGet(0x34)
#define storyHeaderExtensionTableAddress()        portWordGet(0x36)

#define storyHXWordsInTable()                    portWordGet(storyHeaderExtensionTableAddress())
#define storyHXMouseXClick()                     portWordGet(storyHeaderExtensionTableAddress() + 2)
#define storyHXMouseYClick()                     portWordGet(storyHeaderExtensionTableAddress() + 4)
#define storyHXUnicodeTranslationTableAddress()  portWordGet(storyHeaderExtensionTableAddress() + 6)
#define storyHXFlags3()                          portWordGet(storyHeaderExtensionTableAddress() + 8)
#define storyHXTrueDefaultForegroundColor()      portWordGet(storyHeaderExtensionTableAddress() + 10)
#define storyHXTrueDefaultBackgroundColor()      portWordGet(storyHeaderExtensionTableAddress() + 12)


void     storyChecksumCalculate(void);
uint32_t storyLength(void);
void     storySetup(byte width, byte height);


#endif // STORY_H
