Author Topic: weird number results in my program.(Fixed, thanks guys!)  (Read 2359 times)

0 Members and 1 Guest are viewing this topic.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
weird number results in my program.(Fixed, thanks guys!)
« on: January 09, 2020, 11:22:56 am »
have some weird number issues in my program, like a number being reduced by 1 then magically going back, or 2 numbers changing by large amounts all the sudden.   the issues of numbers being reduced by 1 then going back occurs in(or seems too) in the sub Drop_Inventory (where I have the prints) and then in sub NPC_TALK or line 617 where drop_inventory is called. the prints are all there for debugging.

FORGOT SOME IINCLUDES, JUST A MINUTE.
Should be good to go now.

some mal-adjusted numbers in the memory routines was placing data in the wrong locations. Caused when I adjusted the Nam string length from 24 to 8 without adjusting the memory location for the 16byte change. Without the help I might never had caught that! Thank you guys over on Discord!


Code: QB64: [Select]
  1. REM'$include:'DW1_Types.bas'
  2. REM'$include:'DW1_Const.bas'
  3. REM'$include:'DW1_Arrays.bas'
  4. REM'$include:'DW1_Layers.bas'
  5. REM'$include:'DW1_timers.bas'
  6. TYPE Player
  7.  Nam AS STRING * 8 '24 'eventually
  8.  Max_HP AS _UNSIGNED _BYTE
  9.  Max_MP AS _UNSIGNED _BYTE
  10.  Pack AS STRING * 32 '_MEM
  11.  Items AS _BYTE 'items in pack
  12.  Armor AS _BYTE
  13.  Weapon AS _BYTE
  14.  Shield AS _BYTE
  15.  Age AS _BYTE 'as player gains experiance levels they age, age negativliy affects stats.
  16.  Strength AS _BYTE
  17.  Agility AS _BYTE
  18.  Luck AS _BYTE
  19.  Level AS _BYTE
  20.  Spells AS INTEGER
  21.  WorldX AS _BYTE 'players location in over world
  22.  WorldY AS _BYTE
  23.  MapX AS _BYTE 'players location in a castle\town or cave\temple
  24.  MapY AS _BYTE
  25.  Map AS _BYTE 'where is player? -1 to 27 (-1 = over world map)
  26.  Facing AS _BYTE
  27.  Has_Princess AS _BYTE 'is player carring Princess?
  28.  Princess_Saved AS _BYTE 'has player saved Princess?
  29.  WX AS _BYTE 'used when player is walking
  30.  WY AS _BYTE
  31.  Keys AS _BYTE 'number of keys player has (6max)
  32.  Herbs AS _BYTE 'how many Herbs player has, different from items in pack
  33.  Has_Stones AS _BYTE 'does player have stones of sunlight?
  34.  Cursed AS _BYTE 'did the player equip a cursed item?
  35.  
  36. TYPE Monsters
  37.  Nam AS STRING * 16
  38.  Strength AS _UNSIGNED _BYTE
  39.  Agility AS _UNSIGNED _BYTE
  40.  Max_Hp AS _UNSIGNED _BYTE
  41.  Max_Mp AS _UNSIGNED _BYTE
  42.  SlpResist AS _UNSIGNED _BYTE
  43.  StpSplResist AS _UNSIGNED _BYTE
  44.  HurtResist AS _UNSIGNED _BYTE
  45.  Evasion AS _UNSIGNED _BYTE
  46.  Attk1 AS _BYTE 'special attack 1 - these are additional options to normal physical attack
  47.  Attk1Prob AS _UNSIGNED _BYTE
  48.  Attk2 AS _BYTE 'special attack 2
  49.  Attk2Prob AS _UNSIGNED _BYTE
  50.  Group AS _BYTE
  51.  
  52. TYPE GameData
  53.  Y AS _BYTE
  54.  X AS _BYTE
  55.  Flag AS _BYTE
  56.  JustLeft AS _BYTE 'if player just left town\castle don't reprocess location
  57.  In_Place AS _BYTE 'player is in a town\castle
  58.  LastMove AS LONG 'timer, if player doesn't press any keys his current stats display
  59.  Menu AS _BYTE 'does player have the menu up?
  60.  Selection AS _BYTE 'what selection did player make from menu
  61.  Message AS INTEGER 'current message displayed via Controls menu
  62.  Input_Wait AS _BYTE 'waiting for player input, after message display
  63.  Action AS _BYTE 'what action is player currently performing?(talk,search,stair,take,door,spell,item)
  64.  Lite_Level AS _BYTE 'how much light is there 0-none, 1-torch 2&3-Radiant
  65.  Display_Status AS _BYTE
  66.  Current_BGM AS _BYTE 'which music is currently in background
  67.  f AS INTEGER 'number of cycles completed perframe(AKA:Frames Per Sec)
  68.  SaveFile AS _BYTE 'Adventure log player is using to save 1-3
  69.  MessageSpeed AS _BYTE 'how fast messages are displayed 1-3
  70.  TextClick AS _BYTE 'Controls the 'typing' sound when displaying a message
  71.  Price AS INTEGER ' price of item used in message processing
  72.  ItemID AS _BYTE 'the item player is trying to buy from shop
  73.  
  74. TYPE Non_Playable_Character
  75.  Sprite_ID AS _BYTE
  76.  X AS _BYTE 'NPCs location
  77.  Y AS _BYTE
  78.  Map AS _BYTE 'Which town\castle\cave do they live in
  79.  Facing AS _BYTE
  80.  Lines AS _BYTE 'What lines do they say
  81.  Flag AS _BYTE 'used if they change what they say over time
  82.  Can_Move AS _BYTE
  83.  Moving AS _BYTE
  84.  MX AS _BYTE 'used to adjust position when NPC is moving
  85.  MY AS _BYTE '""""""""""""""""""""""""""""""""""""""""""
  86.  
  87. TYPE Tiles
  88.  Sprite_ID AS _BYTE 'sprite used on tile
  89.  Is_Town AS _BYTE 'Town ID/Castle ID
  90.  Is_Cave AS _BYTE 'Cave ID
  91.  Is_Stairs AS _BYTE 'Stairs ID
  92.  Territory AS _BYTE 'Monster selection ID, determines which monster are picked for battle
  93.  Is_Exit AS _BYTE 'does tile lead out of castle or town
  94.  Encounter_rate AS _UNSIGNED _BYTE 'odds of engaging a monster
  95.  Has_Special AS _BYTE 'does search find something here or is special battle here
  96.  
  97. TYPE Flags
  98.  Has_Left_Throne_Room AS _BYTE 'set when Player first leave thrown room after starting game.
  99.  Just_Started AS _BYTE 'Set when player first starts game
  100.  Saved_Princess AS _BYTE 'Set after Player has Brought Princess back
  101.  Has_Proof AS _BYTE 'Set when player finds proof of ancestry
  102.  Made_Bridge AS _BYTE 'set after player uses Rainbow Drop and makes Bridge
  103.  YNFlag AS _BYTE 'set if a message dialog needs a Yes or No response
  104.  Dragon_King_Killed AS _BYTE
  105.  
  106. TYPE Stair_Data
  107.  Map AS _BYTE 'where is the stairs
  108.  X AS _BYTE 'stairs position
  109.  Y AS _BYTE
  110.  Direction AS _BYTE 'going up or down
  111.  Link AS _BYTE 'links to which other stairs
  112.  
  113. TYPE Treasure_Chest
  114.  Map AS _BYTE
  115.  X AS _BYTE
  116.  Y AS _BYTE
  117.  Treasure AS _BYTE
  118.  Count AS INTEGER
  119.  
  120. TYPE Items
  121.  Nam AS STRING * 16
  122.  Valu AS INTEGER
  123.  Power AS _BYTE
  124.  
  125. TYPE Menu_Data
  126.  HName AS STRING * 8
  127.  MesSpd AS _BYTE
  128.  CurLvl AS _BYTE
  129.  
  130. TYPE Name_Select
  131.  X AS _BYTE 'selection arrow X
  132.  Y AS _BYTE 'Selection arrow Y
  133.  C AS _BYTE 'Character associated with selection
  134.  
  135. TYPE Place_Data
  136.  Start_X AS _BYTE 'when Entering where do you start.
  137.  Start_Y AS _BYTE '
  138.  Is_Lit AS _BYTE 'is a Torch or Radiant needed to see?
  139.  
  140. TYPE Cords
  141.  X AS _BYTE
  142.  Y AS _BYTE
  143.  
  144. TYPE Messaging
  145.  LineCount AS _BYTE
  146.  LineID AS _UNSIGNED _BYTE
  147.  
  148. CONST TRUE = -1, FALSE = NOT TRUE
  149. CONST DOWN = 0, LEFT = 1, UP = 2, RIGHT = 3
  150. CONST Key_Right = 19712, Key_Left = 19200, Key_Up = 18432, Key_Down = 20480
  151. CONST A_Button = 32, B_Button = 13, Start_Button = 65, Select_Button = 66
  152. CONST MoveDown = 1, MoveLeft = 2, MoveUp = 3, MoveRight = 4
  153. CONST Talking = 1, Stairs = 2, Searching = 3, Spells = 4, Items = 5, Doors = 6, Taking = 7
  154.  
  155. DIM SHARED P AS Player 'Player stats and data
  156. DIM SHARED G AS GameData 'game data
  157. DIM SHARED F AS Flags 'game flags
  158. DIM SHARED I(127) AS Items 'Item data
  159. DIM SHARED M(64) AS Monsters 'Monster stats and data
  160. DIM SHARED L(30) AS _UNSIGNED INTEGER 'level ups
  161. DIM SHARED GF(5) AS SINGLE 'group factor\used in determining escape posibilities
  162. DIM SHARED SA(10) AS Cords 'selection arrow positions
  163. DIM SHARED World(127, 127) AS Tiles 'World data
  164. DIM SHARED Place(27, 45, 45) AS Tiles 'Map data
  165. DIM SHARED PD(-1 TO 27) AS Place_Data 'Where the player starts, if map is prelit
  166. DIM SHARED Stairs(127) AS Stair_Data 'Where all stairs in game lead
  167. DIM SHARED Chest(127) AS Treasure_Chest 'Where each chest is and what it contains
  168. DIM SHARED Shop(16, 10) AS _BYTE
  169.  
  170. DIM SHARED NPC(127) AS Non_Playable_Character 'Data and controls for NPCs
  171. 'DIM SHARED NPCDialog(127, 8) AS NPCMonologue
  172. DIM SHARED Messages(255, 1) AS _UNSIGNED _BYTE '0-line count, 1-LINES() ID
  173. DIM SHARED Script(361) AS STRING 'All the lines spoken in the game by NPCs
  174. DIM SHARED Lines(255, 8) AS INTEGER 'dialog for Messages 0-8 lines of script(9 max)
  175.  
  176. DIM SHARED Layer(32) AS LONG, DWFont AS LONG, Frame%%, SFX(32) AS LONG, BGM(-1 TO 27) AS LONG, Title AS LONG, Frames%, MenuD(3) AS Menu_Data 'internal data
  177. DIM SHARED T(5) AS LONG 'game timers
  178. DIM SHARED LS(63) AS Name_Select 'used for selecting Hero Name
  179. DIM SHARED ClearLayerMaster AS _MEM 'Advanced Clearlayer data
  180.  
  181.  
  182. Layer(0) = _DISPLAY
  183. Layer(1) = _NEWIMAGE(640, 480, 32)
  184. Layer(2) = _NEWIMAGE(255 * 32, 255 * 32, 32) 'world map
  185. Layer(5) = _NEWIMAGE(640, 480, 32) 'temp backup layer for menus
  186. Layer(6) = _NEWIMAGE(352, 128, 32) 'temp layer to scroll text
  187. Layer(7) = _NEWIMAGE(46 * 32, 46 * 32, 32) 'map layer
  188. Layer(8) = _NEWIMAGE(46 * 32, 46 * 32, 32) 'NPC sprite layer
  189. Layer(9) = _NEWIMAGE(640, 480, 32) 'debug info layer
  190. Layer(11) = _NEWIMAGE(640, 480, 32) 'light mask for underground
  191. Layer(12) = _NEWIMAGE(1472, 1472, 32) 'NPC sprite Clear layer
  192. Layer(15) = _NEWIMAGE(640, 480, 32) 'Message handler text print layer
  193. _CLEARCOLOR _RGB32(0, 128, 128), Layer(4)
  194. _CLEARCOLOR _RGB32(0, 0, 0), Layer(12)
  195. ClearLayerMaster = _MEMIMAGE(Layer(12)) '8667136bytes
  196.  
  197. 'Layer(3) = _LOADIMAGE("tilebase2.bmp", 32)
  198. 'Layer(4) = _LOADIMAGE("characters.bmp", 32)
  199. 'Layer(10) = _LOADIMAGE("controller.bmp", 32)
  200. 'Layer(13) = _LOADIMAGE("DragonWarrior1Title.PNG", 32)'title screen
  201. 'Layer(14) = _LOADIMAGE("Shine.bmp", 32)'title shine\flare
  202.  
  203. 'DWFont = _LOADFONT("DW1.ttf", 16, "Monospace")
  204. T(0) = _FREETIMER
  205. T(1) = _FREETIMER
  206. ON TIMER(T(0), .25) Frame_Change
  207. ON TIMER(T(1), 1) FPS
  208.  
  209.  
  210. MFI_Loader "DragonWV1.MFI"
  211. _DELAY .25
  212. _TITLE "'Dragon Warrior I' UniKorn ProDucKions 2019"
  213. INIT 'a bit of pre-run setup
  214. '--------------development data-------------------
  215. G.MessageSpeed = 1
  216. G.TextClick = FALSE
  217. G.SaveFile = 1
  218. MenuD(1).HName = "Newton"
  219. MenuD(1).MesSpd = 1
  220. MenuD(1).CurLvl = 0
  221. '--------------------------------------------------
  222. 'Run_Title_Screen
  223. 'GameStartMenu
  224. IF F.Just_Started THEN Init_Player
  225. Build_Place_Layer Layer(7), P.Map
  226. F.Has_Left_Throne_Room = TRUE
  227. F.Just_Started = FALSE
  228. P.Has_Princess = FALSE 'TRUE
  229. IF P.Map >= 0 THEN L%% = 7: PMX%% = P.MapX: PMY%% = P.MapY ELSE L%% = 2: PMX%% = P.WorldX: PMY%% = P.WorldY
  230. Copy_Map_Layer Layer(L%%), PMX%% + WX%%, PMY%% + WY%%
  231. P.Nam = "Newton"
  232. P.Gold = 200
  233. P.Weapon = 1
  234. Add_To_Inventory 21, 1 'torch
  235. Add_To_Inventory 22, 1 'herb
  236. 'Add_To_Inventory 23, 1 'wings
  237. 'Add_To_Inventory 24, 1 'dragon's scale
  238. Add_To_Inventory 26, 1 'key
  239.  
  240. _FONT DWFont, Layer(5)
  241.  
  242. Draw_Stat_Window Layer(1)
  243. Process_Command Command_Window(Layer(1)), Layer(1)
  244.  
  245. TIMER(T(0)) OFF
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. REM'$include:'DW1_MFILoader.bas'
  254. REM'$include:'CoreFunctions.bas'
  255. REM'$include:'DW1_TitleScreen.bas'
  256. REM'$include:'DW1_BeginNewQuest.bas'
  257. REM'$include:'DW1_RestoreGameFile.bas'
  258. REM'$include:'DW1_GameMenu.bas'
  259. REM'$include:'DW1_Playerstats.bas'
  260. REM'$include:'DW1_InputWindows.bas'
  261. REM'$include:'DW1_Commands.bas'
  262.  
  263.  
  264. SUB NPC_Talk (ID%%)
  265.  SELECT CASE ID%%
  266.   CASE 0 'king
  267.    IF F.Just_Started THEN
  268.     IF F.Has_Left_Throne_Room = FALSE THEN
  269.      Result%% = Message_Handler(Layer(15), 7) 'game intro message
  270.      F.Just_Started = FALSE
  271.     ELSE
  272.      Result%% = Message_Handler(Layer(15), 13) 'restarted game message
  273.     END IF
  274.    ELSEIF F.Has_Left_Throne_Room = FALSE THEN
  275.     Result%% = Message_Handler(Layer(15), 14) 'havent left throne room yet
  276.    ELSEIF P.Has_Princess THEN 'has saved princess
  277.     Result%% = Message_Handler(Layer(15), 15) 'havent left throne room yet
  278.     NPC_Talk 1
  279.     P.Has_Princess = FALSE
  280.     P.Princess_Saved = TRUE
  281.     NPC_Talk 0
  282.    ELSEIF F.Dragon_King_Killed THEN 'has beaten boss
  283.    ELSE
  284.     Result%% = Message_Handler(Layer(15), 8) 'returned to king to save
  285.     IF Yes_NO_Box(20, 5, Layer(1)) THEN 'does player want to save
  286.      Result%% = Message_Handler(Layer(15), 9) 'player saved
  287.     ELSE
  288.      Result%% = Message_Handler(Layer(15), 10) 'does player want to keep playing?
  289.     END IF
  290.     IF Yes_NO_Box(20, 5, Layer(1)) THEN 'does player want to keep playing
  291.      Result%% = Message_Handler(Layer(15), 12) 'keeps playing
  292.     ELSE
  293.      Result%% = Message_Handler(Layer(15), 11) 'quits
  294.      CLOSE: END
  295.     END IF
  296.    END IF
  297.   CASE 1 'Princess
  298.    IF P.Has_Princess THEN
  299.     Result%% = Message_Handler(Layer(15), 21) 'returns princess to throne room
  300.    END IF
  301.   CASE 2 'Guard Roaming throne room
  302.    IF P.Has_Princess OR P.Princess_Saved THEN
  303.     Result%% = Message_Handler(Layer(15), 27) 'Pricess saved
  304.    ELSE
  305.     Result%% = Message_Handler(Layer(15), 24) 'just started
  306.     IF Yes_NO_Box(20, 5, Layer(1)) THEN 'does player know about princess
  307.      Result%% = Message_Handler(Layer(15), 26) 'yes
  308.     ELSE
  309.      Result%% = Message_Handler(Layer(15), 25) 'no, tell player
  310.     END IF
  311.    END IF
  312.   CASE 3 'Left hand Gaurd throne room
  313.    IF F.Has_Left_Throne_Room = FALSE THEN
  314.     Result%% = Message_Handler(Layer(15), 28) '
  315.    ELSE
  316.     Result%% = Message_Handler(Layer(15), 29) '
  317.    END IF
  318.   CASE 4 'right hand gaurd throne room
  319.    IF F.Has_Left_Throne_Room = FALSE THEN
  320.     Result%% = Message_Handler(Layer(15), 30) '
  321.    ELSE
  322.     Result%% = Message_Handler(Layer(15), 31) '
  323.    END IF
  324.   CASE 5 'Gaurd Roaming North of Stairs
  325.    Result%% = Message_Handler(Layer(15), 40) '
  326.   CASE 6 'Key Sales Man
  327.    G.Price = 85
  328.    Result%% = Message_Handler(Layer(15), 49) '
  329.    DO
  330.     IF Yes_NO_Box(20, 5, Layer(1)) THEN 'does player know about princess
  331.      Result%% = Message_Handler(Layer(15), 51) 'yes
  332.      IF P.Keys < 6 THEN
  333.       IF P.Gold >= 85 THEN 'buys a key
  334.        P.Gold = P.Gold - 85
  335.        P.Keys = P.Keys + 1
  336.       ELSE
  337.        Result%% = Message_Handler(Layer(15), 52) 'Player has too little gold
  338.        ExitFlag%% = TRUE
  339.       END IF
  340.       Result%% = Message_Handler(Layer(15), 53) 'Player has too many keys
  341.       ExitFlag%% = TRUE
  342.      ELSE
  343.      END IF
  344.     ELSE
  345.      Result%% = Message_Handler(Layer(15), 50) 'no,
  346.      ExitFlag%% = TRUE
  347.     END IF
  348.    LOOP UNTIL ExitFlag%%
  349.    ExitFlag%% = FALSE
  350.   CASE 7 'Girl by key store
  351.    Result%% = Message_Handler(Layer(15), 42) '
  352.   CASE 8 'Roaming Red Guard
  353.    Result%% = Message_Handler(Layer(15), 41) '
  354.   CASE 9 'Roaming Wizard right of stairs
  355.    Result%% = Message_Handler(Layer(15), 44) '
  356.   CASE 10 'Guard at damage floor room entrance
  357.    Result%% = Message_Handler(Layer(15), 43) '
  358.   CASE 11 'Red Guard in damage floor room
  359.    Result%% = Message_Handler(Layer(15), 45) '
  360.   CASE 12 'Guard north of stairs
  361.    Result%% = Message_Handler(Layer(15), 32) '
  362.   CASE 13 'Guard south of stairs
  363.    Result%% = Message_Handler(Layer(15), 33) '
  364.   CASE 14 'Boy in center
  365.    Result%% = Message_Handler(Layer(15), 35) '
  366.   CASE 15 'boy above treasure room
  367.    Result%% = Message_Handler(Layer(15), 39) '
  368.   CASE 16 'guard in treasure room
  369.    Result%% = Message_Handler(Layer(15), 38) '
  370.   CASE 17 'girl in center area
  371.    Result%% = Message_Handler(Layer(15), 34) '
  372.   CASE 18 'merchant 1 bottom left
  373.    Result%% = Message_Handler(Layer(15), 36) '
  374.   CASE 19 'merchant 2 bottom left
  375.    Result%% = Message_Handler(Layer(15), 37) '
  376.   CASE 20 'Guard standing right of pool
  377.    IF P.Princess_Saved THEN
  378.     Result%% = Message_Handler(Layer(15), 48) '
  379.    ELSE
  380.     Result%% = Message_Handler(Layer(15), 47) '
  381.    END IF
  382.   CASE 21 'Guard Roaming right of pool
  383.    Result%% = Message_Handler(Layer(15), 46) '
  384.   CASE 22 'Wizard Bottom Right
  385.    Result%% = Message_Handler(Layer(15), 54) '
  386.    P.MP = P.Max_MP
  387.   CASE 23 'Guard Left of entrance
  388.    Result%% = Message_Handler(Layer(15), 55) '
  389.   CASE 24 'guard right of entrance
  390.    Result%% = Message_Handler(Layer(15), 56) '
  391.   CASE 25 'Wizard in castle celar
  392.    IF P.Has_Stones THEN
  393.     Result%% = Message_Handler(Layer(15), 58) '
  394.    ELSE
  395.     Result%% = Message_Handler(Layer(15), 57) '
  396.    END IF
  397.    'ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄEnd of Tantegel Castle NPCsÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
  398.   CASE 26 'Red gaurd in top right corner
  399.    Result%% = Message_Handler(Layer(15), 57) '59
  400.   CASE 27 'Armory shop keep
  401.    ExitFlag%% = FALSE
  402.    Result%% = Message_Handler(Layer(15), 60) '
  403.    IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to buy something
  404.     DO
  405.      Result%% = Run_Shop(1, Layer(1))
  406.      IF Result%% < 0 THEN
  407.       ExitFlag%% = TRUE
  408.      ELSE
  409.       G.ItemID = Result%%
  410.       Result%% = Message_Handler(Layer(15), 63) '
  411.       IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to buy it
  412.        IF P.Gold >= I(G.ItemID).Valu THEN 'player has enough gold
  413.         'check and see if player must sell something to buy
  414.         temp%% = G.ItemID 'backup the item player wishes to buy
  415.         IF G.ItemID < 8 AND P.Weapon <> 0 THEN Sell%% = 1: G.ItemID = P.Weapon
  416.         IF G.ItemID > 7 AND G.ItemID < 16 AND P.Armor <> 0 THEN Sell%% = 2: G.ItemID = P.Armor
  417.         IF G.ItemID > 15 AND G.ItemID < 21 AND P.Shield <> 0 THEN Sell%% = 3: G.ItemID = P.Shield
  418.         IF Sell%% THEN
  419.          G.Price = INT(I(G.ItemID).Valu \ 2)
  420.          Result%% = Message_Handler(Layer(15), 67) 'does player wish to sell current equipment
  421.          IF Yes_NO_Box(20, 5, Layer(1)) THEN 'does player wish to sell it
  422.           P.Gold = P.Gold + G.Price
  423.           G.ItemID = temp%% 'restore the item player wants to buy
  424.           SELECT CASE Sell%%
  425.            CASE 1
  426.             P.Weapon = G.ItemID
  427.            CASE 2
  428.             P.Armor = G.ItemID
  429.            CASE 3
  430.             P.Shield = G.ItemID
  431.           END SELECT
  432.  
  433.           P.Gold = P.Gold - I(G.ItemID).Valu
  434.           Draw_Stat_Window Layer(5) 'update the stat window after purchase
  435.           Result%% = Message_Handler(Layer(15), 65) '
  436.          ELSE
  437.           Result%% = Message_Handler(Layer(15), 64) '
  438.          END IF
  439.         ELSE
  440.         END IF
  441.         '--------------------------------------------------
  442.        ELSE 'player lacks enough gold
  443.         Result%% = Message_Handler(Layer(15), 66) '
  444.        END IF
  445.       ELSE
  446.        Result%% = Message_Handler(Layer(15), 64) '
  447.       END IF
  448.       IF Yes_NO_Box(10, 4, Layer(1)) THEN ExitFlag%% = FALSE ELSE ExitFlag%% = TRUE 'does player wish to buy anything else
  449.      END IF
  450.     LOOP UNTIL ExitFlag%%
  451.     Result%% = Message_Handler(Layer(15), 61) 'please come again
  452.    ELSE
  453.     Result%% = Message_Handler(Layer(15), 61) '
  454.    END IF
  455.   CASE 28 'woman standing in front of shop
  456.    Result%% = Message_Handler(Layer(15), 68) '
  457.   CASE 29 'boy at entrace of town
  458.    Result%% = Message_Handler(Layer(15), 69) '
  459.   CASE 30 'boy in room
  460.    Result%% = Message_Handler(Layer(15), 70) '
  461.   CASE 31 'wizard in room, removes curses
  462.    IF P.Cursed THEN
  463.     Result%% = Message_Handler(Layer(15), 72) '
  464.    ELSE
  465.     Result%% = Message_Handler(Layer(15), 71) '
  466.    END IF
  467.   CASE 32 'roaming merchant
  468.    Result%% = Message_Handler(Layer(15), 73) '
  469.   CASE 33 'guard to far right
  470.    Result%% = Message_Handler(Layer(15), 74) '
  471.   CASE 34 'Roaming red guard
  472.    Result%% = Message_Handler(Layer(15), 75) '
  473.   CASE 35 'roaming wizard
  474.    Result%% = Message_Handler(Layer(15), 76) '
  475.   CASE 36 'inn keep
  476.    G.Price = 6
  477.    Result%% = Message_Handler(Layer(15), 77) '
  478.    IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to stay at the inn
  479.     Result%% = Message_Handler(Layer(15), 78) '
  480.     P.HP = P.Max_HP
  481.     P.MP = P.Max_MP
  482.     P.Gold = P.Gold - 6
  483.     _DELAY .33
  484.     Result%% = Message_Handler(Layer(15), 79) '
  485.    ELSE
  486.     Result%% = Message_Handler(Layer(15), 80) '
  487.    END IF
  488.   CASE 37 'Roaming Red guard in Inn
  489.    Result%% = Message_Handler(Layer(15), 81) '
  490.   CASE 38 'Guard in Inn
  491.    Result%% = Message_Handler(Layer(15), 82) '
  492.   CASE 39 'roaming boy
  493.    Result%% = Message_Handler(Layer(15), 83) '
  494.   CASE 40 'roaming girl
  495.    Result%% = Message_Handler(Layer(15), 84) '
  496.   CASE 41 'boy roaming across bridge
  497.    Result%% = Message_Handler(Layer(15), 85) '
  498.   CASE 42 'Boy standing across bridge
  499.    Result%% = Message_Handler(Layer(15), 86) '
  500.   CASE 43 'roaming Red guard
  501.    Result%% = Message_Handler(Layer(15), 87) '
  502.   CASE 44 'Girl selling fairy water
  503.    G.Price = 38
  504.    Result%% = Message_Handler(Layer(15), 88) '
  505.    IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to buy fairy water
  506.     DO
  507.      IF P.Gold >= 38 THEN 'player has enough gold
  508.       IF P.Items < 8 THEN 'player has room in pack
  509.        Result%% = Message_Handler(Layer(15), 90) '
  510.        IF Yes_NO_Box(10, 4, Layer(1)) THEN ExitFlag%% = FALSE ELSE ExitFlag%% = TRUE 'does player wish to buy more
  511.       ELSE 'player is out of room
  512.        Result%% = Message_Handler(Layer(15), 92) '
  513.        ExitFlag%% = TRUE
  514.       END IF
  515.      ELSE 'player lacks enough gold
  516.       Result%% = Message_Handler(Layer(15), 91) '
  517.       ExitFlag%% = TRUE
  518.      END IF
  519.     LOOP UNTIL ExitFlag%%
  520.    ELSE 'player says no
  521.     Result%% = Message_Handler(Layer(15), 89) '
  522.    END IF
  523.   CASE 45 'Tool shop keep
  524.    Result%% = Message_Handler(Layer(15), 93) '
  525.    IF Buy_Sell_Box(10, 4, Layer(1)) THEN 'does player wish to buy
  526.     '--------------player is buying items---------------------------------------
  527.     Result%% = Message_Handler(Layer(15), 99) '
  528.     DO
  529.      Item%% = Run_Shop(2, Layer(1))
  530.      G.ItemID = Item%%
  531.      IF Item%% < 0 THEN
  532.       ExitFlag%% = TRUE
  533.      ELSE
  534.       IF P.Gold > I(Item%%).Valu THEN 'dose player have the gold
  535.        IF P.Items < 8 AND Item%% <> 22 THEN 'does player have room or is player buying herbs
  536.  
  537.         P.Gold = P.Gold - I(Item%%).Valu 'remove the cost from players purse
  538.         Draw_Stat_Window Layer(5) 'update the stat window after purchase
  539.         Add_To_Inventory Item%%, 1 'add the item to players inventory
  540.         Result%% = Message_Handler(Layer(15), 100) 'player buys item
  541.  
  542.        ELSE 'players inventory is full or they are buying herbs
  543.  
  544.         IF Item%% = 22 THEN 'if its a Herb then see if player has less than 6
  545.          IF P.Herbs < 6 THEN 'can player hold more?
  546.           P.Gold = P.Gold - I(Item%%).Valu
  547.           Draw_Stat_Window Layer(5) 'update the stat window after purchase
  548.           P.Herbs = P.Herbs + 1
  549.           Result%% = Message_Handler(Layer(15), 100) 'player buys item
  550.          ELSE 'player has too many herbs
  551.           Result%% = Message_Handler(Layer(15), 102) '
  552.          END IF
  553.         ELSE 'player has too many items
  554.          Result%% = Message_Handler(Layer(15), 103) '
  555.         END IF
  556.        END IF
  557.  
  558.       ELSE 'player hasn't the gold to buy
  559.        Result%% = Message_Handler(Layer(15), 101) '
  560.       END IF
  561.       IF Yes_NO_Box(10, 4, Layer(1)) THEN ExitFlag%% = FALSE ELSE ExitFlag%% = TRUE 'does player wish to buy more
  562.      END IF
  563.     LOOP UNTIL ExitFlag%%
  564.  
  565.    ELSE 'or is player going to sell
  566.     '--------------------------Player is selling Items--------------------------------------
  567.     IF P.Items OR P.Herbs OR P.Keys THEN
  568.      Result%% = Message_Handler(Layer(15), 94) '
  569.      DO
  570.       Item%% = Run_Item(18, 6, Layer(1))
  571.       IF Item%% < 0 THEN 'player canceled
  572.        ExitFlag%% = TRUE
  573.       ELSE
  574.        G.ItemID = Item%%
  575.        G.Price = INT(I(G.ItemID).Valu \ 2)
  576.        Result%% = Message_Handler(Layer(15), 96) '
  577.  
  578.        IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to sell
  579.         P.Gold = P.Gold + G.Price 'pay player
  580.         Draw_Stat_Window Layer(5) 'update the stat window after purchase
  581.  
  582.         IF G.ItemID = 22 THEN 'remove the item from players inventory
  583.          P.Herbs = P.Herbs - 1
  584.         ELSEIF G.ItemID = 26 THEN
  585.          P.Keys = P.Keys - 1
  586.         ELSE
  587.          '         Remove_Inventory G.ItemID, 1
  588.          '         PRINT "starts at"; P.Items
  589.  
  590.  
  591.          Drop_Inventory G.ItemID
  592.          '         PRINT "ends at"; P.Items
  593.          '         END
  594.  
  595.         END IF
  596.         Result%% = Message_Handler(Layer(15), 98) '
  597.        ELSE
  598.         Result%% = Message_Handler(Layer(15), 97) '
  599.        END IF
  600.  
  601.        IF Yes_NO_Box(10, 4, Layer(1)) THEN 'does player wish to sell anything else
  602.         IF P.Items OR P.Herbs OR P.Keys THEN
  603.          ExitFlag%% = FALSE
  604.         ELSE
  605.          Result%% = Message_Handler(Layer(15), 95) 'player has no items to sell
  606.          ExitFlag%% = TRUE
  607.         END IF
  608.        ELSE
  609.         ExitFlag%% = TRUE
  610.        END IF
  611.       END IF
  612.      LOOP UNTIL ExitFlag%%
  613.     ELSE
  614.      Result%% = Message_Handler(Layer(15), 95) 'player has no items to sell
  615.     END IF
  616.    END IF
  617.   CASE 46
  618.   CASE 47
  619.   CASE 48
  620.   CASE 49
  621.   CASE 50
  622.   CASE 51
  623.   CASE 52
  624.   CASE 53
  625.   CASE 54
  626.   CASE 55
  627.   CASE 56
  628.   CASE 57
  629.   CASE 58
  630.   CASE 59
  631.  
  632.  
  633.  
  634. FUNCTION Run_Item%% (X%%, Y%%, L&)
  635.  Result%% = -1
  636.  Selection%% = 1
  637.  _PUTIMAGE , Layer(1), Layer(5)
  638.  IF P.Herbs THEN Count%% = Count%% + 1
  639.  IF P.Keys THEN Count%% = Count%% + 1
  640.  Count%% = Count%% + P.Items
  641.  DO
  642.   _PUTIMAGE , Layer(5), L&
  643.   Draw_Window X%%, Y%%, 11, Count%% * 2 + 1, L&
  644.   i%% = 0
  645.   IF P.Herbs THEN
  646.    _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 1 + (i%% * 2))), "Herbs", L&
  647.    V1$ = LTRIM$(STR$(P.Herbs))
  648.    _PRINTSTRING (16 * (28), 16 * (Y%% + 1 + (i%% * 2))), V1$, L&
  649.    i%% = i%% + 1
  650.   END IF
  651.   IF P.Keys THEN
  652.    _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 1 + (i%% * 2))), "Magic", L&
  653.    _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 2 + (i%% * 2))), " Keys", L&
  654.    V1$ = LTRIM$(STR$(P.Keys))
  655.    _PRINTSTRING (16 * (28), 16 * (Y%% + 1 + (i%% * 2))), V1$, L&
  656.    i%% = i%% + 1
  657.   END IF
  658.  
  659.   Offset%% = Y%% + 1 + (i%% * 2)
  660.  
  661.   FOR j%% = 0 TO P.Items - 1
  662.    IF LEN(RTRIM$(I(Inventory_Item(j%%)).Nam)) < 9 THEN
  663.     V1$ = RTRIM$(I(Inventory_Item(j%%)).Nam)
  664.     _PRINTSTRING (16 * (X%% + 2), 16 * (Offset%% + ((j%%) * 2))), V1$, L&
  665.    ELSE
  666.     V1$ = MID$(I(Inventory_Item(j%%)).Nam, 1, INSTR(I(Inventory_Item(j%%)).Nam, CHR$(32)))
  667.     _PRINTSTRING (16 * (X%% + 2), 16 * (Offset%% + ((j%%) * 2))), V1$, L&
  668.     V2$ = RTRIM$(MID$(I(Inventory_Item(j%%)).Nam, INSTR(I(Inventory_Item(j%%)).Nam, CHR$(32))))
  669.     _PRINTSTRING (16 * (X%% + 2), 16 * (Offset%% + ((j%%) * 2 + 1))), V2$, L&
  670.    END IF
  671.   NEXT j%%
  672.  
  673.   IF Frame%% THEN Display_Selection_Arrow X%% + 1, 7 + ((Selection%% - 1) * 2), L&
  674.   _PUTIMAGE , L&, Layer(0)
  675.    CASE 27
  676.     ExitFlag%% = TRUE
  677.    CASE Key_Up
  678.     Selection%% = Selection%% - 1
  679.     IF Selection%% = 0 THEN Selection%% = 1
  680.    CASE Key_Down
  681.     Selection%% = Selection%% + 1
  682.     IF Selection%% = P.Items + i%% + 1 THEN Selection%% = P.Items + i%%
  683.    CASE A_Button
  684.     ExitFlag%% = TRUE
  685.  
  686.     IF Selection%% = 1 AND P.Herbs THEN
  687.      Result%% = 22
  688.     ELSEIF Selection%% = 1 AND P.Keys > 0 THEN
  689.      Result%% = 26
  690.     ELSEIF Selection%% = 2 AND P.Keys > 0 AND P.Herbs > 0 THEN
  691.      Result%% = 26
  692.     ELSE
  693.      Result%% = Inventory_Item(Selection%% - i%% - 1)
  694.     END IF
  695.  
  696.    CASE B_Button
  697.     ExitFlag%% = TRUE
  698.     Result%% = TRUE 'exit shop routine
  699.   _LIMIT 60
  700.  LOOP UNTIL ExitFlag%%
  701.  Run_Item = Result%%
  702.  
  703.  
  704.  
  705. SUB Process_Command (Opt%%, L&)
  706.  IF Opt%% THEN
  707.   _PUTIMAGE , L&, Layer(5)
  708.   DO
  709.    KBD& = _KEYHIT
  710.    SELECT CASE Opt%%
  711.     CASE 1 'talk
  712.      NPC_Talk 45
  713.      ExitFlag%% = TRUE
  714.     CASE 2 'Status
  715.      Display_Player_Status L&
  716.     CASE 3 'Stairs
  717.      Run_Stairs
  718.     CASE 4 'search
  719.      Result%% = Message_Handler(Layer(15), 2) 'found nothing message
  720.      END
  721.     CASE 5 'spell
  722.      Result%% = Message_Handler(Layer(15), 3) 'no spells yet
  723.      END
  724.     CASE 6 'Items
  725.      '     Result%% = Message_Handler(Layer(15), 4) 'no items yet message
  726.      Result%% = Run_Item(18, 6, Layer(1))
  727.      _PUTIMAGE , Layer(1), Layer(0)
  728.      END
  729.     CASE 7 'Door
  730.      Result%% = Message_Handler(Layer(15), 5) 'no door here message
  731.      END
  732.     CASE 8 'take(chest)
  733.      Result%% = Message_Handler(Layer(15), 6) 'nothing to take message
  734.      END
  735.    END SELECT
  736.    _PUTIMAGE , L&, Layer(0)
  737.    SELECT CASE KBD&
  738.     CASE 27
  739.      ExitFlag%% = TRUE
  740.     CASE A_Button, B_Button
  741.      ExitFlag%% = TRUE
  742.    END SELECT
  743.  
  744.    _LIMIT 60
  745.   LOOP UNTIL ExitFlag%%
  746.  
  747. FUNCTION Run_Shop%% (ID%%, L&)
  748.  X%% = 10: Y%% = 4
  749.  Result%% = FALSE
  750.  Selection%% = 1
  751.  _PUTIMAGE , Layer(1), Layer(5)
  752.  
  753.  SELECT CASE ID%%
  754.   CASE 1 'breconary armory shop
  755.    DO
  756.     _PUTIMAGE , Layer(5), L&
  757.     Draw_Window X%%, Y%%, 20, 13, L&
  758.  
  759.     FOR i%% = 1 TO 6
  760.      IF LEN(RTRIM$(I(Shop(1, i%%)).Nam)) < 9 THEN
  761.       V1$ = RTRIM$(I(Shop(1, i%%)).Nam)
  762.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2))), V1$, L&
  763.      ELSE
  764.       V1$ = MID$(I(Shop(1, i%%)).Nam, 1, INSTR(I(Shop(1, i%%)).Nam, CHR$(32)))
  765.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2))), V1$, L&
  766.       V2$ = RTRIM$(MID$(I(Shop(1, i%%)).Nam, INSTR(I(Shop(1, i%%)).Nam, CHR$(32))))
  767.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2) + 1)), V2$, L&
  768.      END IF
  769.      V1$ = LTRIM$(STR$(I(Shop(1, i%%)).Valu))
  770.      _PRINTSTRING (16 * (30 - LEN(V1$)), 16 * (3 + (i%% * 2))), V1$, L&
  771.     NEXT i%%
  772.     IF Frame%% THEN Display_Selection_Arrow X%% + 1, 3 + (Selection%% * 2), L&
  773.     _PUTIMAGE , L&, Layer(0)
  774.      CASE 27
  775.       ExitFlag%% = TRUE
  776.      CASE Key_Up
  777.       Selection%% = Selection%% - 1
  778.       IF Selection%% = 0 THEN Selection%% = 1
  779.      CASE Key_Down
  780.       Selection%% = Selection%% + 1
  781.       IF Selection%% = 7 THEN Selection%% = 6
  782.      CASE A_Button
  783.       ExitFlag%% = TRUE
  784.       Result%% = Shop(1, Selection%%)
  785.      CASE B_Button
  786.       ExitFlag%% = TRUE
  787.       Result%% = TRUE 'exit shop routine
  788.     END SELECT
  789.     _LIMIT 60
  790.    LOOP UNTIL ExitFlag%%
  791.   CASE 2 'breconary item shop buy
  792.    DO
  793.     _PUTIMAGE , Layer(5), L&
  794.     Draw_Window X%%, Y%%, 20, 7, L&
  795.     FOR i%% = 1 TO 3
  796.      IF LEN(RTRIM$(I(Shop(2, i%%)).Nam)) < 9 THEN
  797.       V1$ = RTRIM$(I(Shop(2, i%%)).Nam)
  798.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2))), V1$, L&
  799.      ELSE
  800.       V1$ = MID$(I(Shop(2, i%%)).Nam, 1, INSTR(I(Shop(2, i%%)).Nam, CHR$(32)))
  801.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2))), V1$, L&
  802.       V2$ = RTRIM$(MID$(I(Shop(2, i%%)).Nam, INSTR(I(Shop(2, i%%)).Nam, CHR$(32))))
  803.       _PRINTSTRING (16 * (13), 16 * (3 + (i%% * 2) + 1)), V2$, L&
  804.      END IF
  805.      V1$ = LTRIM$(STR$(I(Shop(2, i%%)).Valu))
  806.      _PRINTSTRING (16 * (30 - LEN(V1$)), 16 * (3 + (i%% * 2))), V1$, L&
  807.     NEXT i%%
  808.     IF Frame%% THEN Display_Selection_Arrow X%% + 1, 3 + (Selection%% * 2), L&
  809.     _PUTIMAGE , L&, Layer(0)
  810.      CASE 27
  811.       ExitFlag%% = TRUE
  812.      CASE Key_Up
  813.       Selection%% = Selection%% - 1
  814.       IF Selection%% = 0 THEN Selection%% = 1
  815.      CASE Key_Down
  816.       Selection%% = Selection%% + 1
  817.       IF Selection%% = 4 THEN Selection%% = 3
  818.      CASE A_Button
  819.       ExitFlag%% = TRUE
  820.       Result%% = Shop(2, Selection%%)
  821.      CASE B_Button
  822.       ExitFlag%% = TRUE
  823.       Result%% = TRUE 'exit shop routine
  824.     END SELECT
  825.     _LIMIT 60
  826.    LOOP UNTIL ExitFlag%%
  827.   CASE 3 'breconary item shop Sell
  828.  
  829.    DO
  830.     _PUTIMAGE , Layer(5), L&
  831.     Draw_Window X%%, Y%%, 20, P.Items * 2 + 1, L&
  832.     FOR i%% = 0 TO P.Items - 1
  833.      IF LEN(RTRIM$(I(Shop(3, i%%)).Nam)) < 9 THEN
  834.       V1$ = RTRIM$(I(Shop(3, i%%)).Nam)
  835.       _PRINTSTRING (16 * (13), 16 * (3 + ((i%% + 1) * 2))), V1$, L&
  836.      ELSE
  837.       V1$ = MID$(I(Shop(3, i%%)).Nam, 1, INSTR(I(Shop(3, i%%)).Nam, CHR$(32)))
  838.       _PRINTSTRING (16 * (13), 16 * (3 + ((i%% + 1) * 2))), V1$, L&
  839.       V2$ = RTRIM$(MID$(I(Shop(3, i%%)).Nam, INSTR(I(Shop(3, i%%)).Nam, CHR$(32))))
  840.       _PRINTSTRING (16 * (13), 16 * (3 + ((i%% + 1) * 2) + 1)), V2$, L&
  841.      END IF
  842.      V1$ = LTRIM$(STR$(I(Shop(3, i%%)).Valu))
  843.      _PRINTSTRING (16 * (30 - LEN(V1$)), 16 * (3 + ((i%% + 1) * 2))), V1$, L&
  844.     NEXT i%%
  845.     IF Frame%% THEN Display_Selection_Arrow X%% + 1, 3 + (Selection%% * 2), L&
  846.     _PUTIMAGE , L&, Layer(0)
  847.      CASE 27
  848.       ExitFlag%% = TRUE
  849.      CASE Key_Up
  850.       Selection%% = Selection%% - 1
  851.       IF Selection%% = 0 THEN Selection%% = 1
  852.      CASE Key_Down
  853.       Selection%% = Selection%% + 1
  854.       IF Selection%% = P.Items + 1 THEN Selection%% = P.Items
  855.      CASE A_Button
  856.       ExitFlag%% = TRUE
  857.       Result%% = Shop(3, Selection%% - 1)
  858.      CASE B_Button
  859.       ExitFlag%% = TRUE
  860.       Result%% = TRUE 'exit shop routine
  861.     END SELECT
  862.     _LIMIT 60
  863.    LOOP UNTIL ExitFlag%%
  864.  
  865.  Run_Shop = Result%%
  866.  
  867. SUB Run_Stairs
  868.  IF P.Map >= 0 THEN
  869.   IF Place(P.Map, P.MapX, P.MapY).Is_Stairs THEN
  870.    IF NOT F.Has_Left_Throne_Room THEN F.Has_Left_Throne_Room = TRUE 'if player has just started game
  871.  
  872.    IF Stairs(Place(P.Map, P.MapX, P.MapY).Is_Stairs).Direction = 1 THEN _SNDPLAY SFX(1) ELSE _SNDPLAY SFX(2)
  873.    P.Map = Stairs(Place(P.Map, P.MapX, P.MapY).Is_Stairs).Link
  874.    'get location information from the linked stair Record.
  875.    P.MapX = Stairs(P.Map).X
  876.    P.MapY = Stairs(P.Map).Y
  877.    IF Stairs(P.Map).Map = -1 THEN P.WorldX = Stairs(P.Map).X: P.WorldY = Stairs(P.Map).Y
  878.    P.Map = Stairs(P.Map).Map
  879.    IF P.Map >= 0 THEN Build_Place_Layer Layer(7), P.Map
  880.    '   Change_BGM 0
  881.    G.Menu = FALSE
  882.   ELSE
  883.    Result%% = Message_Handler(Layer(15), 0) 'no stairs here message
  884.    END
  885.   END IF
  886.   IF World(P.WorldX, P.WorldY).Is_Stairs THEN
  887.    IF Stairs(World(P.WorldX, P.WorldY).Is_Stairs).Direction = 1 THEN _SNDPLAY SFX(1) ELSE _SNDPLAY SFX(2)
  888.    P.Map = Stairs(World(P.WorldX, P.WorldY).Is_Stairs).Link
  889.    'get location information from the linked stair Record.
  890.    P.MapX = Stairs(P.Map).X
  891.    P.MapY = Stairs(P.Map).Y
  892.    P.Map = Stairs(P.Map).Map
  893.    Build_Place_Layer Layer(7), P.Map
  894.    G.Menu = FALSE
  895.    '  Change_BGM 0
  896.   ELSE
  897.    Result%% = Message_Handler(Layer(15), 0) 'no stairs here message
  898.   END IF
  899.  
  900.  
  901.  
  902. FUNCTION Message_Handler (L&, MSG%)
  903.  STATIC Processed AS STRING, LineCount AS _BYTE, CurrentLine AS _BYTE
  904.  STATIC WLine(10) AS STRING * 22, Slowit AS _BYTE, Displayed AS _BYTE
  905.  TotalLines%% = Messages(MSG%, 1) 'number of lines to display
  906.  CurrentLine = 1
  907.  
  908.  DO
  909.   CMSG% = Lines(Messages(MSG%, 0), DisplayedLines%%) 'Current MeSsaGe line
  910.   IF INSTR(Script(CMSG%), "#") OR INSTR(Script(CMSG%), "&") THEN 'special needs character
  911.    Processed = Process_Message_Coding$(Script(CMSG%)): Message_Length = LEN(Processed)
  912.   ELSE
  913.    Message_Length = LEN(Script(CMSG%))
  914.    Processed = Script(CMSG%)
  915.   END IF
  916.   'check message length word wrap if needed
  917.   IF Message_Length > 22 AND LineCount = 0 THEN LineCount = Process_Word_Wrap(WLine(), Processed)
  918.   IF Message_Length <= 22 THEN LineCount = 1: WLine(1) = Processed
  919.   DO
  920.    Slowit = Slowit + 1 'makes the message display at the set speed
  921.  
  922.    IF Slowit = 2 * G.MessageSpeed THEN Slowit = 0: Current_Char = Current_Char + 1: IF G.TextClick THEN _SNDPLAY SFX(3)
  923.  
  924.    _PRINTSTRING (80, 304 + (16 * (Displayed))), MID$(WLine(CurrentLine), 1, Current_Char), L& 'print message 1 character at a time.
  925.  
  926.    IF Current_Char = LEN(RTRIM$(WLine(CurrentLine))) THEN CurrentLine = CurrentLine + 1: Current_Char = 0: Displayed = Displayed + 1
  927.    IF Displayed = 8 THEN Scroll_Line L&: Displayed = Displayed - 1
  928.  
  929.    IF CurrentLine = LineCount + 1 THEN ExitFlag%% = TRUE
  930.    _PUTIMAGE , Layer(5), Layer(1) 'throw the backup layer in
  931.    Draw_Window 4, 18, 23, 9, Layer(1) 'add message area window
  932.    _PUTIMAGE , L&, Layer(1) 'move text into window
  933.    _PUTIMAGE , Layer(1), Layer(0) 'move that to screen
  934.  
  935.    LOCATE 1, 1: PRINT LineCount; CurrentLine; Displayed
  936.  
  937.    _LIMIT 60
  938.    IF _KEYHIT = 27 THEN ExitFlag%% = TRUE
  939.   LOOP UNTIL ExitFlag%%
  940.  
  941.   ExitFlag%% = FALSE
  942.  
  943.   DisplayedLines%% = DisplayedLines%% + 1
  944.  
  945.   OFFY%% = CurrentLine: CurrentLine = 1: LineCount = 0: Displayed = Displayed + 1
  946.   IF DisplayedLines%% = TotalLines%% THEN 'are we done displaying message? or
  947.    Completed%% = TRUE
  948.   ELSE 'is there more of message to display
  949.    DO
  950.     KBD& = _KEYHIT
  951.     _PUTIMAGE , Layer(5), Layer(1) 'throw the backup layer in
  952.     Draw_Window 4, 18, 23, 9, Layer(1) 'add message area window
  953.     _PUTIMAGE , L&, Layer(1) 'move text into window
  954.     IF Frame%% THEN Display_Waiting_Arrow 15, 18 + Displayed, Layer(1)
  955.     _PUTIMAGE , Layer(1), Layer(0) 'move that to screen
  956.     _LIMIT 60
  957.     IF KBD& = 27 THEN END
  958.    LOOP UNTIL KBD& = A_Button
  959.   END IF
  960.  
  961.  LOOP UNTIL Completed%%
  962.  Processed = "": Message_Length = 0: Result%% = FALSE
  963.  CurrentLine = 0: WLine(1) = "": LineCount = 0
  964.  Message_Handler = Result%%
  965.  
  966. FUNCTION Process_Message_Coding$ (Txt$)
  967.  FOR i~%% = 1 TO LEN(Txt$) 'check message for formating
  968.   IF MID$(Txt$, i~%%, 1) = "#" THEN
  969.    SELECT CASE MID$(Txt$, i~%% + 1, 1)
  970.     CASE "E"
  971.      Result$ = Result$ + RTRIM$(M(battle%%).Nam)
  972.      i~%% = i~%% + 1
  973.     CASE "P"
  974.      Result$ = Result$ + RTRIM$(P.Nam)
  975.      i~%% = i~%% + 1
  976.     CASE "I"
  977.      Result$ = Result$ + RTRIM$(I(G.ItemID).Nam)
  978.      i~%% = i~%% + 1
  979.     CASE "N"
  980.      SELECT CASE MID$(Txt$, i~%% + 2, 1)
  981.       CASE "X"
  982.        Result$ = Result$ + LTRIM$(STR$(EXP_Required))
  983.       CASE "G"
  984.        Result$ = Result$ + LTRIM$(STR$(G.Price)) '+ " "
  985.      END SELECT
  986.      i~%% = i~%% + 2
  987.    END SELECT
  988.   ELSEIF MID$(Txt$, i~%%, 1) = "&" THEN
  989.    SELECT CASE MID$(Txt$, i~%% + 1, 1)
  990.     CASE "Y"
  991.      '     F.YNFlag = TRUE
  992.      Temp$ = MID$(Txt$, i~%% + 2)
  993.      '     G.Yes_Dialog = VAL(MID$(Temp$, 1, INSTR(Temp$, ",") - 0))
  994.      '     G.No_Dialog = VAL(MID$(Temp$, INSTR(Temp$, ",") + 1))
  995.      i~%% = i~%% + 1 + LEN(Temp$)
  996.    END SELECT
  997.   ELSE
  998.    Result$ = Result$ + MID$(Txt$, i~%%, 1)
  999.   END IF
  1000.  NEXT i~%%
  1001.  Process_Message_Coding = Result$
  1002.  
  1003. FUNCTION Process_Word_Wrap (Lines() AS STRING * 22, txt$)
  1004.  Result%% = 1: I%% = 1
  1005.  DO
  1006.   'start on character 22 and work back to a space
  1007.   x%% = 22: Space%% = FALSE
  1008.   DO
  1009.    IF MID$(txt$, x%%, 1) <> CHR$(32) THEN x%% = x%% - 1 ELSE Space%% = TRUE
  1010.    IF x%% = 0 AND I%% = 1 THEN PRINT "ERROR": END
  1011.   LOOP UNTIL Space%%
  1012.   Lines(I%%) = MID$(txt$, 1, x%%)
  1013.   txt$ = MID$(txt$, x%% + 1)
  1014.   I%% = I%% + 1
  1015.   IF LEN(txt$) <= 22 THEN GoodLength%% = TRUE
  1016.  LOOP UNTIL GoodLength%%
  1017.  Lines(I%%) = txt$ 'put the last of the text into a line
  1018.  Result%% = I%%
  1019.  Process_Word_Wrap = Result%%
  1020.  
  1021. SUB Scroll_Line (L&)
  1022.  ClearLayer Layer(6)
  1023.  _PUTIMAGE (0, 0), L&, Layer(6), (80, 320)-STEP(351, 111)
  1024.  _PUTIMAGE (80, 304), Layer(6), L&, (0, 0)-STEP(351, 127)
  1025.  
  1026. FUNCTION EXP_Required~%
  1027.  Result~% = L(P.Level) - P.Exp
  1028.  EXP_Required = Result~%
  1029.  
  1030. SUB Save_Game
  1031.  SELECT CASE G.SaveFile
  1032.   CASE 1
  1033.    OPEN "DW1.SV1" FOR BINARY AS #1
  1034.    PUT #1, , MenuD(1)
  1035.   CASE 2
  1036.    OPEN "DW1.SV2" FOR BINARY AS #1
  1037.    PUT #1, , MenuD(2)
  1038.   CASE 3
  1039.    OPEN "DW1.SV3" FOR BINARY AS #1
  1040.    PUT #1, , MenuD(3)
  1041.  PUT #1, , P
  1042.  PUT #1, , G
  1043.  PUT #1, , F
  1044.  CLOSE #1
  1045.  
  1046.  
  1047. SUB Draw_Window (X%%, Y%%, Xs%%, Ys%%, L&)
  1048.  'top corners of window
  1049.  _PUTIMAGE (X%% * 16, Y%% * 16)-STEP(15, 15), Layer(3), L&, (0, 18)-STEP(7, 7)
  1050.  _PUTIMAGE (X%% * 16 + Xs%% * 16, Y%% * 16)-STEP(15, 15), Layer(3), L&, (8, 18)-STEP(7, 7)
  1051.  'bottom corners of window
  1052.  _PUTIMAGE (X%% * 16, Y%% * 16 + Ys%% * 16)-STEP(15, 15), Layer(3), L&, (16, 18)-STEP(7, 7)
  1053.  _PUTIMAGE (X%% * 16 + Xs%% * 16, Y%% * 16 + Ys%% * 16)-STEP(15, 15), Layer(3), L&, (24, 18)-STEP(7, 7)
  1054.  'top and bottom of window
  1055.  FOR i%% = 1 TO Xs%% - 1
  1056.   _PUTIMAGE (X%% * 16 + i%% * 16, Y%% * 16)-STEP(15, 15), Layer(3), L&, (56, 18)-STEP(7, 7)
  1057.   _PUTIMAGE (X%% * 16 + i%% * 16, Y%% * 16 + Ys%% * 16)-STEP(15, 15), Layer(3), L&, (48, 18)-STEP(7, 7)
  1058.  NEXT i%%
  1059.  'left and right sides of window
  1060.  FOR i%% = 1 TO Ys%% - 1
  1061.   _PUTIMAGE (X%% * 16, Y%% * 16 + 16 * i%%)-STEP(15, 15), Layer(3), L&, (32, 18)-STEP(7, 7)
  1062.   _PUTIMAGE (X%% * 16 + Xs%% * 16, Y%% * 16 + 16 * i%%)-STEP(15, 15), Layer(3), L&, (40, 18)-STEP(7, 7)
  1063.  NEXT i%%
  1064.  'fill in window
  1065.  _PUTIMAGE ((X%% + 1) * 16, (Y%% + 1) * 16)-STEP((Xs%% - 1) * 16, (Ys%% - 1) * 16), Layer(3), L&, (307, 1)-STEP(15, 15)
  1066.  
  1067. SUB ClearLayer (L&)
  1068.  old& = _DEST
  1069.  _DEST L&
  1070.  CLS
  1071.  _DEST old&
  1072.  
  1073. SUB ClearLayerADV (L&)
  1074.  STATIC ML1 AS _MEM
  1075.  ML1 = _MEMIMAGE(L&)
  1076.  _MEMPUT ML1, ML1.OFFSET, _MEMGET(ClearLayerMaster, ClearLayerMaster.OFFSET, STRING * 8667136)
  1077.  
  1078. SUB Display_Selection_Arrow (X%%, Y%%, L&)
  1079.  _PUTIMAGE (X%% * 16, Y%% * 16)-STEP(15, 15), Layer(3), L&, (64, 18)-STEP(7, 7)
  1080.  
  1081. SUB Display_Waiting_Arrow (X%%, Y%%, L&)
  1082.  _PUTIMAGE (X%% * 16, Y%% * 16)-STEP(15, 15), Layer(3), L&, (72, 18)-STEP(7, 7)
  1083.  
  1084. SUB Frame_Change
  1085.  IF Frame%% THEN Frame%% = 0 ELSE Frame%% = 1
  1086.  
  1087. SUB FPS
  1088.  G.f = Frames%
  1089.  Frames% = 0
  1090.  
  1091. SUB INIT
  1092.  _FONT DWFont, Layer(15)
  1093.  _FONT DWFont, Layer(1)
  1094.  TIMER(T(0)) ON
  1095.  G.TextClick = TRUE
  1096.  _SNDVOL BGM(3), .4
  1097.  F.Just_Started = TRUE
  1098.  
  1099. 'mapping routines ------------------------------------------
  1100. SUB Copy_Map_Layer (L&, MX%%, MY%%)
  1101.  IF P.Map >= 0 THEN
  1102.   _PUTIMAGE (16, 16), L&, Layer(1), (MX%% * 32 - 240 + G.X, MY%% * 32 - 208 + G.Y)-STEP(495, 447)
  1103.   _PUTIMAGE (16, 16), L&, Layer(1), (128 + MX%% * 32 - 240 + G.X, 128 + MY%% * 32 - 208 + G.Y)-STEP(495, 447)
  1104.  
  1105. SUB Build_World_Layer (L&)
  1106.  FOR Y~%% = 0 TO 255
  1107.   FOR X~%% = 0 TO 255
  1108.    _PUTIMAGE (32 * X~%%, 32 * Y~%%)-STEP(31, 31), Layer(3), L&, (1 + 17 * 14, 1)-STEP(15, 15)
  1109.  NEXT X~%%, Y~%%
  1110.  FOR Y%% = 0 TO 121
  1111.   FOR X%% = 0 TO 121
  1112.    _PUTIMAGE (128 + 32 * X%%, 128 + 32 * Y%%)-STEP(31, 31), Layer(3), L&, (1 + 17 * World(X%%, Y%%).Sprite_ID, 1)-STEP(15, 15)
  1113.  NEXT X%%, Y%%
  1114.  
  1115. SUB Build_Place_Layer (L&, ID%%)
  1116.  FOR Y%% = 0 TO 45
  1117.   FOR X%% = 0 TO 45
  1118.    _PUTIMAGE (0 + 32 * X%%, 0 + 32 * Y%%)-STEP(31, 31), Layer(3), L&, (1 + 17 * Place(ID%%, X%%, Y%%).Sprite_ID, 1)-STEP(15, 15)
  1119.  NEXT X%%, Y%%
  1120. '-----------------------------------------------------------
  1121. 'Draw Sprite Routines---------------------------------------
  1122. SUB Draw_Player_Sprite (L&)
  1123.  IF P.Weapon AND P.Shield > 16 THEN 'pick which sprite set to use
  1124.   Y1%% = 3 'has a shield and weapon
  1125.  ELSEIF P.Weapon THEN
  1126.   Y1%% = 1 'only has weapon
  1127.  ELSEIF P.Shield > 16 THEN 'No sheild is 16
  1128.   Y1%% = 2 'on has shield
  1129.   Y1%% = 0 'has nothing
  1130.  IF P.Armor > 9 THEN Y1%% = Y1%% + 4 'player has armor
  1131.  
  1132.  IF P.Has_Princess THEN Y1%% = 8 'if player is returning princess over-ride tile set
  1133.  
  1134.  SX% = 3 + (Frame%% * 16) + (P.Facing * 32)
  1135.  SY% = 4 + (Y1%% * 18)
  1136.  _PUTIMAGE (256, 224)-STEP(31, 31), Layer(4), L&, (SX%, SY%)-STEP(15, 15)
  1137.  
  1138. SUB Draw_NPC_Sprites (Count%%, L&)
  1139.  ClearLayerADV L&
  1140.  FOR i%% = 0 TO Count%%
  1141.   IF P.Map = NPC(i%%).Map THEN
  1142.    SX% = 3 + (Frame%% * 16) + (NPC(i%%).Facing * 32)
  1143.    SY% = 4 + (NPC(i%%).Sprite_ID * 18)
  1144.    _PUTIMAGE (NPC(i%%).X * 32 + NPC(i%%).MX, NPC(i%%).Y * 32 + NPC(i%%).MY)-STEP(31, 31), Layer(4), L&, (SX%, SY%)-STEP(15, 15)
  1145.   END IF
  1146.  NEXT i%%
  1147.  
  1148. '-----------------------------------------------------------
  1149.  
  1150. SUB Add_To_Inventory (Item%%, Count%%)
  1151.  STATIC m AS _MEM 'no need to initialize/free it over and over
  1152.  m = _MEM(P) 'Just change where you want to point it to.
  1153.  Pack = m.OFFSET + 28 'æmove to where P .Pack starts at in memory
  1154.  
  1155.  Match%% = FALSE
  1156.  IF Item%% = 22 THEN 'herb
  1157.   P.Herbs = P.Herbs + 1
  1158.  ELSEIF Item%% = 26 THEN 'Key
  1159.   P.Keys = P.Keys + 1
  1160.   _MEMPUT m, Pack + P.Items * 2, Item%% 'POKE
  1161.   _MEMPUT m, Pack + P.Items * 2 + 1, Count%% 'POKE
  1162.   P.Items = P.Items + 1
  1163.  
  1164. FUNCTION Item_Count%% (Item%%)
  1165.  STATIC m AS _MEM 'no need to initialize/free it over and over
  1166.  m = _MEM(P) 'Just change where you want to point it to.
  1167.  Pack = m.OFFSET + 28 'æmove to where P .Pack starts at in memory
  1168.  Result%% = 0
  1169.  Result%% = _MEMGET(m, Pack + Item%% * 2 + 1, _BYTE)
  1170.  Item_Count = Result%%
  1171.  
  1172. FUNCTION Inventory_Item%% (Item%%)
  1173.  STATIC m AS _MEM 'no need to initialize/free it over and over
  1174.  m = _MEM(P) 'Just change where you want to point it to.
  1175.  Pack = m.OFFSET + 28 'æmove to where P .Pack starts at in memory
  1176.  Result%% = 0
  1177.  Result%% = _MEMGET(m, Pack + Item%% * 2, _BYTE)
  1178.  Inventory_Item = Result%%
  1179.  
  1180. SUB Remove_Inventory (Item%%, Count%%)
  1181.  STATIC m AS _MEM 'no need to initialize/free it over and over
  1182.  m = _MEM(P) 'Just change where you want to point it to.
  1183.  Pack = m.OFFSET + 28 'æmove to where P .Pack starts at in memory
  1184.  
  1185.  Match%% = -1
  1186.  FOR i%% = 0 TO 15 'find the item
  1187.   IF Item%% = _MEMGET(m, Pack + i%% * 2, _BYTE) THEN Match%% = i%%: i%% = 30
  1188.  NEXT i%%
  1189.  IF Match%% < 0 THEN
  1190.   'something went horribly wrong!
  1191.   PRINT Item%%: END
  1192.   temp%% = _MEMGET(m, Pack + Match%% * 2 + 1, _BYTE)
  1193.   temp%% = temp%% - Count%%
  1194.  
  1195.   IF temp%% = 0 THEN 'item is out so remove from inventory
  1196.    temp$ = _MEMGET(m, Pack, STRING * 32) 'get entire pack string
  1197.    temp$ = LEFT$(temp$, Match%% * 2) + MID$(temp$, 2 * Match%% + 3)
  1198.    P.Items = P.Items - 1
  1199.  
  1200.    l%% = LEN(temp$)
  1201.    FOR i%% = l%% TO 31 'pad temp$ to full 32 characters
  1202.     temp$ = temp$ + CHR$(0)
  1203.    NEXT i%%
  1204.  
  1205.    _MEMPUT m, Pack, temp$ 'put invetory back
  1206.   ELSE
  1207.    _MEMPUT m, Pack + Match%% * 2 + 1, temp%% 'put adjusted count back
  1208.   END IF
  1209.  
  1210. SUB Drop_Inventory (ID%%)
  1211.  STATIC m AS _MEM 'no need to initialize/free it over and over
  1212.  m = _MEM(P) 'Just change where you want to point it to.
  1213.  Pack = m.OFFSET + 28 'æmove to where P .Pack starts at in memory
  1214.  
  1215.  Match%% = -1
  1216.  FOR i%% = 0 TO 15 'find the item
  1217.   IF ID%% = _MEMGET(m, Pack + i%% * 2, _BYTE) THEN Match%% = i%%: i%% = 30
  1218.  NEXT i%%
  1219.  IF Match%% < 0 THEN
  1220.   'something went horribly wrong!
  1221.   PRINT item%%: END
  1222.   temp$ = _MEMGET(m, Pack, STRING * 32) 'get entire pack string
  1223.   temp$ = LEFT$(temp$, Match%% * 2) + MID$(temp$, 2 * Match%% + 3)
  1224.  
  1225.   PRINT P.Items
  1226.   P.Items = P.Items - 1
  1227.   PRINT P.Items
  1228.  
  1229.   l%% = LEN(temp$)
  1230.   FOR i%% = l%% TO 31 'pad temp$ to full 32 characters
  1231.    temp$ = temp$ + CHR$(0)
  1232.   NEXT i%%
  1233.  
  1234.   _MEMPUT m, Pack, temp$ 'put invetory back
  1235.  
  1236. FUNCTION Player_Battle_Defense~%%
  1237.  'compute primary Defense value
  1238.  Result~%% = Player_Defense
  1239.  'now check for luck bonus good or bad
  1240.  IF INT(RND * 255) <= (P.Luck \ 2) THEN 'if random chance is less than half player luck then give luck adjustment
  1241.   Result~%% = Result~%% + INT(RND * (P.Luck * 2) - (P.Luck - (P.Age * 2)))
  1242.  Player_Battle_Defense = Result~%%
  1243.  
  1244. FUNCTION Player_Defense~%%
  1245.  'compute defense value to display in status window and to use in battle defense function
  1246.  Result~%% = INT(P.Agility / 2) + I(P.Armor).Power + I(P.Shield).Power
  1247.  Player_Defense = Result~%%
  1248.  
  1249. FUNCTION Player_Attack~%%
  1250.  'Compute Attack value to display in status window and to use in battle attack function
  1251.  Result~%% = INT(P.Strength) + I(P.Weapon).Power
  1252.  Player_Attack = Result~%%
  1253.  
  1254. FUNCTION Player_Battle_Attack~%%
  1255.  'get primary attack value
  1256.  Result~%% = Player_Attack
  1257.  C~%% = Chance
  1258.  IF C~%% < (P.Luck \ 2) THEN 'if random chance is less than half player luck then give luck adjustment
  1259.   Result~%% = Result~%% + INT(RND * (P.Luck * 2) - (P.Luck - (P.Age * 2)))
  1260.  ELSEIF C~%% >= (128 - P.Luck / 16) AND C~%% <= (128 + P.Luck / 16) THEN 'critical hit
  1261.   Result~%% = Result~%% + INT(Result~%% * .25) 'add 25% to hit value
  1262.  Player_Battle_Attack = Result~%%
  1263.  
  1264. FUNCTION Enemy_Damage (ID%%)
  1265.  IF Player_Battle_Defense / 2 > M(ID%%).Strength THEN Miss%% = INT(M(ID%%).Strength + 4 / 6) ELSE Miss%% = FALSE
  1266.  Low~%% = (M(ID%%).Strength - Player_Battle_Defense / 2) / 4
  1267.  High~%% = (M(ID%%).Strength - Player_Battle_Defense / 2) / 2
  1268.  
  1269.  IF (Chance < P.Luck / 4) THEN 'player evades attack(-1)
  1270.   Result%% = TRUE
  1271.  ELSEIF Miss%% >= 0 THEN 'not really a miss just a weak hit
  1272.   Result%% = Miss%%
  1273.  ELSE 'normal attack damage
  1274.   Result%% = INT(RND * (High~%% - Low~%%)) + Low~%%
  1275.  Enemy_Damage = Result%%
  1276.  
  1277. FUNCTION Player_Damage (ID%%)
  1278.  IF Chance + P.Luck / 2 > 228 THEN 'critical hit
  1279.   Low~%% = (Player_Battle_Attack / 2)
  1280.   High~%% = (Player_Battle_Attack)
  1281.   Low~%% = (Player_Battle_Attack - M(ID%%).Agility / 2) / 4
  1282.   High~%% = (Player_Battle_Attack - M(ID%%).Agility / 2) / 2
  1283.  Result~%% = INT(RND * (High~%% - Low~%%)) + Low~%%
  1284.  IF Result~%% = 0 THEN
  1285.   IF Chance + (P.Luck / 2) > 127 THEN Result~%% = 1
  1286.  Player_Damage = Result~%%
  1287.  
  1288. FUNCTION Enemy_Flee (ID%%)
  1289.  IF Player_Attack >= M(ID%%).Strength * 2 THEN
  1290.   IF Chance > 192 THEN Result%% = TRUE ELSE Result%% = FALSE
  1291.  Enemy_Flee = Result%%
  1292.  
  1293. FUNCTION Player_Flee (Id%%)
  1294.  IF P.Agility * Chance < M(Id%%).Agility * Chance * GF(M(Id%%).Group) THEN
  1295.   Result%% = FALSE 'player is blocked
  1296.   Result%% = TRUE 'player evades
  1297.  Player_Flee = Result%%
  1298.  
  1299. FUNCTION Battle_Initiative (ID%%)
  1300.  'if false then enemy goes first else player goes first in battle
  1301.  IF P.Agility * Chance < M(ID%%).Agility * Chance * GF(1) THEN
  1302.   Result%% = FALSE 'Monster goes first
  1303.   Result%% = TRUE 'player Goes first
  1304.  Battle_Initiative = Result%%
  1305.  
  1306. FUNCTION Chance~%%
  1307.  Chance = INT(RND * 255)
  1308.  
  1309. SUB MFI_Loader (FN$)
  1310.  DIM Size(64) AS LONG, FOffset(64) AS LONG
  1311.  GET #1, , c~%% 'retrieve number of files
  1312.  FOR I~%% = 1 TO c~%%
  1313.   GET #1, , FOffset(I~%%)
  1314.   GET #1, , Size(I~%%)
  1315.   FOffset&(I~%%) = FOffset&(I~%%) + 1
  1316.  NEXT I~%%
  1317.  Layer(10) = LoadGFX(FOffset(1), Size(1))
  1318.  Layer(3) = LoadGFX(FOffset(2), Size(2))
  1319.  Layer(4) = LoadGFX(FOffset(3), Size(3))
  1320.  Layer(13) = LoadGFX(FOffset(14), Size(14))
  1321.  Layer(14) = LoadGFX(FOffset(15), Size(15))
  1322.  BGM(-1) = LoadSFX(FOffset(4), Size(4))
  1323.  BGM(0) = LoadSFX(FOffset(10), Size(10))
  1324.  BGM(1) = LoadSFX(FOffset(12), Size(12))
  1325.  BGM(3) = LoadSFX(FOffset(13), Size(13))
  1326.  SFX(0) = LoadSFX(FOffset(5), Size(5))
  1327.  SFX(1) = LoadSFX(FOffset(8), Size(8))
  1328.  SFX(2) = LoadSFX(FOffset(9), Size(9))
  1329.  SFX(3) = LoadSFX(FOffset(11), Size(11))
  1330.  Title = LoadSFX(FOffset(16), Size(16))
  1331.  LoadData FOffset(6), Size(6)
  1332.  DWFont = LoadFFX(FOffset(7), Size(7), 16)
  1333.  _CLEARCOLOR _RGB32(0, 0, 0), Layer(14)
  1334.  CLOSE #1
  1335.  IF _FILEEXISTS("temp.dat") THEN KILL "temp.dat"
  1336.  
  1337. FUNCTION LoadGFX& (Foff&, Size&)
  1338.  IF _FILEEXISTS("temp.dat") THEN KILL "temp.dat"
  1339.  OPEN "temp.dat" FOR BINARY AS #3
  1340.  dat$ = SPACE$(Size&)
  1341.  GET #1, Foff&, dat$
  1342.  PUT #3, , dat$
  1343.  CLOSE #3
  1344.  LoadGFX& = _LOADIMAGE("temp.dat", 32)
  1345.  
  1346. FUNCTION LoadFFX& (Foff&, Size&, Fize%%)
  1347.  IF _FILEEXISTS("temp.dat") THEN KILL "temp.dat"
  1348.  OPEN "temp.dat" FOR BINARY AS #3
  1349.  dat$ = SPACE$(Size&)
  1350.  GET #1, Foff&, dat$
  1351.  PUT #3, , dat$
  1352.  CLOSE #3
  1353.  LoadFFX& = _LOADFONT("temp.dat", Fize%%, "monospace")
  1354.  
  1355. FUNCTION LoadSFX& (Foff&, Size&)
  1356.  IF _FILEEXISTS("temp.dat") THEN KILL "temp.dat"
  1357.  OPEN "temp.dat" FOR BINARY AS #3
  1358.  dat$ = SPACE$(Size&)
  1359.  GET #1, Foff&, dat$
  1360.  PUT #3, , dat$
  1361.  CLOSE #3
  1362.  LoadSFX& = _SNDOPEN("temp.dat")
  1363.  
  1364. SUB LoadData (Foff&, Size&)
  1365.  IF _FILEEXISTS("temp.dat") THEN KILL "temp.dat"
  1366.  OPEN "temp.dat" FOR BINARY AS #3
  1367.  dat$ = SPACE$(Size&)
  1368.  GET #1, Foff&, dat$
  1369.  PUT #3, , dat$
  1370.  CLOSE #3
  1371.  
  1372.  F1 = FREEFILE
  1373.  OPEN "temp.dat" FOR BINARY AS #F1
  1374.  GET #F1, , World()
  1375.  GET #F1, , Place()
  1376.  GET #F1, , I()
  1377.  GET #F1, , L()
  1378.  GET #F1, , GF()
  1379.  GET #F1, , M()
  1380.  GET #F1, , Stairs()
  1381.  GET #F1, , PD()
  1382.  GET #F1, , Chest()
  1383.  FOR i% = 1 TO 361
  1384.   GET #F1, , L~%%
  1385.   Script(i%) = SPACE$(L~%%)
  1386.   GET #F1, , Script(i%)
  1387.  NEXT i%
  1388.  GET #F1, , NPC()
  1389.  GET #F1, , Lines()
  1390.  GET #F1, , SA()
  1391.  GET #F1, , LS()
  1392.  GET #F1, , Messages()
  1393.  GET #F1, , Shop()
  1394.  CLOSE #F1
  1395.  
  1396.  
  1397. SUB Level_Up
  1398.  P.Level = P.Level + 1
  1399.  P.Age = P.Age + INT(P.Level * 1.5 - .49)
  1400.  P.Strength = P.Strength + INT(RND * 6) + 1 - INT((-5 + (P.Age - 20)) * (.15 + ((P.Age - 17) / 100)) - .49)
  1401.  P.Agility = P.Agility + INT(RND * 6) + 1 - INT((-8 + (P.Age - 20)) * (.4 + ((P.Age - 17) / 100)) - .49)
  1402.  P.Luck = P.Luck + INT(RND * 3) + INT(0 + (P.Age - 25)) * (.4 + ((P.Age - 17) / 50) + .49)
  1403.  P.Max_HP = P.Max_HP + INT(RND * 10 - (P.Age / 10)) + 1
  1404.  P.Max_MP = P.Max_MP + INT(RND * 5 + (P.Age / 10))
  1405.  Check_Level_Up_Spells
  1406.  
  1407. SUB Check_Level_Up_Spells
  1408.  'give spells to player based on Max_MP
  1409.  'heal-restores 7-18hp
  1410.  IF P.Max_MP >= 4 AND (NOT _READBIT(P.Spells, 0)) THEN P.Spells = _SETBIT(P.Spells, 0)
  1411.  'hurt-hits enemy for 5-12hp
  1412.  IF P.Max_MP >= 10 AND (NOT _READBIT(P.Spells, 1)) THEN P.Spells = _SETBIT(P.Spells, 1)
  1413.  'sleep-puts enemy to sleep for up to 6 rounds
  1414.  IF P.Max_MP >= 20 AND (NOT _READBIT(P.Spells, 2)) THEN P.Spells = _SETBIT(P.Spells, 2)
  1415.  'radiant-generates light in caves radius x3 for 160 steps
  1416.  IF P.Max_MP >= 32 AND (NOT _READBIT(P.Spells, 3)) THEN P.Spells = _SETBIT(P.Spells, 3)
  1417.  'stopspell-prevents enemy from using magic including healing spells
  1418.  IF P.Max_MP >= 48 AND (NOT _READBIT(P.Spells, 4)) THEN P.Spells = _SETBIT(P.Spells, 4)
  1419.  'outside-teleports player out of a cave\temple
  1420.  IF P.Max_MP >= 52 AND (NOT _READBIT(P.Spells, 5)) THEN P.Spells = _SETBIT(P.Spells, 5)
  1421.  'return- teleports player back to Tantegel castle
  1422.  IF P.Max_MP >= 64 AND (NOT _READBIT(P.Spells, 6)) THEN P.Spells = _SETBIT(P.Spells, 6)
  1423.  'repel-stops attacks from monsters weaker than player for 128 steps
  1424.  IF P.Max_MP >= 80 AND (NOT _READBIT(P.Spells, 7)) THEN P.Spells = _SETBIT(P.Spells, 7)
  1425.  'HealMore-restores 70-110hp
  1426.  IF P.Max_MP >= 94 AND (NOT _READBIT(P.Spells, 8)) THEN P.Spells = _SETBIT(P.Spells, 8)
  1427.  'HurtMore-hits enemy for 58-65hp
  1428.  IF P.Max_MP >= 98 AND (NOT _READBIT(P.Spells, 9)) THEN P.Spells = _SETBIT(P.Spells, 9)
  1429.  'Strengthen-adds 1/4 bonus to players attack strength for duration of battle
  1430.  IF P.Max_MP >= 114 AND (NOT _READBIT(P.Spells, 10)) THEN P.Spells = _SETBIT(P.Spells, 10)
  1431.  'Quicken-adds 1/3 bonus to players defensive strength for duration of battle
  1432.  IF P.Max_MP >= 124 AND (NOT _READBIT(P.Spells, 11)) THEN P.Spells = _SETBIT(P.Spells, 11)
  1433.  
  1434. SUB Init_Player
  1435.  '---------------Player starting stats-----------------------
  1436.  P.Max_HP = INT(RND * 8) + 12 'start 12-20hp
  1437.  P.HP = P.Max_HP
  1438.  IF INT(RND * 100) = 95 THEN P.Max_MP = INT(RND * 3) + 4 '1 in 100 chance of starting with magical powers
  1439.  IF P.Max_MP THEN P.Spells = _SETBIT(P.Spells, 0) 'give player HEAL if they start with magic
  1440.  P.MP = P.Max_MP
  1441.  P.Strength = INT(RND * 3) + 2 'determines base attack damage
  1442.  P.Agility = INT(RND * 2) + 2 'determines base defence\evasion
  1443.  P.Luck = INT(RND * 5) 'a "chance" bonus to hits and evasions
  1444.  P.Level = 0 'starting at base; first fight elevates to level 1, no xp gain though
  1445.  P.Age = 17 'level adds to age *1.5 (level 10 adds 15 to age, 20 adds 30, 30 adds 45, max age 72!)
  1446.  ' age affects Strength by subtracting INT((-5 + (Age - 20)) * (.15 + ((Age-17)/100))-.49)  ;  youth gives strength
  1447.  ' age affects Agility by subtracting INT((-8 + (Age - 20)) * (.4 + ((Age-17)/100))-.49)   ;  youth gives great agility, age loses agility quickly
  1448.  ' age affects Luck by adding (0 + (Age - 25)) * (.4 + ((Age-17)/50))  ;  age adds luck
  1449.  P.Weapon = 0 'adds to Attack strength , start with Fists
  1450.  P.Armor = 8 'adds to Defensive strength , start with Loin Cloth
  1451.  P.Shield = 16 'adds to Defensive strength , start with None
  1452.  P.Gold = 0
  1453.  P.Exp = 0
  1454.  P.WorldX = 44
  1455.  P.WorldY = 44
  1456.  P.MapX = 17 'when starting game player always starts here
  1457.  P.MapY = 18
  1458.  P.Map = 1
  1459.  '-----------------------------------------------------------
  1460.  
  1461. SUB Draw_Stat_Window (L&)
  1462.  Draw_Window 2, 4, 7, 11, L&
  1463.  _PUTIMAGE (4 * 16 - 2, 4 * 16)-STEP(4 * 15 + 4, 1 * 15), Layer(3), L&, (205, 35)-STEP(15, 15)
  1464.  _PRINTSTRING (16 * 4, 16 * 4), LEFT$(P.Nam, 4), L&
  1465.  _PRINTSTRING (16 * 3, 16 * 6), "LV", L&
  1466.  _PRINTSTRING (16 * 3, 16 * 8), "HP", L&
  1467.  _PRINTSTRING (16 * 3, 16 * 10), "MP", L&
  1468.  _PRINTSTRING (16 * 3, 16 * 12), "G", L&
  1469.  _PRINTSTRING (16 * 3, 16 * 14), "E", L&
  1470.  _PRINTSTRING (16 * (9 - LEN(STR$(P.Level))), 16 * 6), STR$(P.Level), L&
  1471.  _PRINTSTRING (16 * (9 - LEN(STR$(P.HP))), 16 * 8), (STR$(P.HP)), L&
  1472.  _PRINTSTRING (16 * (9 - LEN(STR$(P.MP))), 16 * 10), STR$(P.MP), L&
  1473.  _PRINTSTRING (16 * (9 - LEN(STR$(P.Gold))), 16 * 12), STR$(P.Gold), L&
  1474.  _PRINTSTRING (16 * (9 - LEN(STR$(P.Exp))), 16 * 14), STR$(P.Exp), L&
  1475.  
  1476. SUB Display_Player_Status (L&)
  1477.  Draw_Window 10, 6, 19, 21, L&
  1478.  _PRINTSTRING (16 * 16, 16 * 7), "NAME:" + RTRIM$(P.Nam), L&
  1479.  _PRINTSTRING (16 * 17, 16 * 9), "STRENGTH:", L&
  1480.  _PRINTSTRING (16 * 18, 16 * 11), "AGILITY:", L&
  1481.  _PRINTSTRING (16 * 15, 16 * 13), "MAXIMUM HP:", L&
  1482.  _PRINTSTRING (16 * 15, 16 * 15), "MAXIMUM MP:", L&
  1483.  _PRINTSTRING (16 * 13, 16 * 17), "ATTACK POWER:", L&
  1484.  _PRINTSTRING (16 * 12, 16 * 19), "DEFENSE POWER:", L&
  1485.  _PRINTSTRING (16 * 13, 16 * 21), "WEAPON:", L&
  1486.  _PRINTSTRING (16 * 14, 16 * 23), "ARMOR:", L&
  1487.  _PRINTSTRING (16 * 13, 16 * 25), "SHIELD:", L&
  1488.  V1$ = LTRIM$(STR$(P.Strength))
  1489.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 9), V1$, L&
  1490.  V1$ = LTRIM$(STR$(P.Agility))
  1491.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 11), V1$, L&
  1492.  V1$ = LTRIM$(STR$(P.Max_HP))
  1493.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 13), V1$, L&
  1494.  V1$ = LTRIM$(STR$(P.Max_MP))
  1495.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 15), V1$, L&
  1496.  V1$ = LTRIM$(STR$(Player_Attack))
  1497.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 17), V1$, L&
  1498.  V1$ = LTRIM$(STR$(Player_Defense))
  1499.  _PRINTSTRING (16 * (26 + (3 - LEN(V1$))), 16 * 19), V1$, L&
  1500.  
  1501.  IF LEN(RTRIM$(I(P.Weapon).Nam)) < 9 THEN
  1502.   V1$ = RTRIM$(I(P.Weapon).Nam)
  1503.   _PRINTSTRING (16 * (20), 16 * 21), V1$, L&
  1504.   V1$ = MID$(I(P.Weapon).Nam, 1, INSTR(I(P.Weapon).Nam, CHR$(32)))
  1505.   _PRINTSTRING (16 * (20), 16 * 21), V1$, L&
  1506.   V2$ = RTRIM$(MID$(I(P.Weapon).Nam, INSTR(I(P.Weapon).Nam, CHR$(32))))
  1507.   _PRINTSTRING (16 * (20), 16 * 22), V2$, L&
  1508.  
  1509.  IF LEN(RTRIM$(I(P.Armor).Nam)) < 9 THEN
  1510.   V1$ = RTRIM$(I(P.Armor).Nam)
  1511.   _PRINTSTRING (16 * (20), 16 * 23), V1$, L&
  1512.   V1$ = MID$(I(P.Armor).Nam, 1, INSTR(I(P.Armor).Nam, CHR$(32)))
  1513.   _PRINTSTRING (16 * (20), 16 * 23), V1$, L&
  1514.   V2$ = RTRIM$(MID$(I(P.Armor).Nam, INSTR(I(P.Armor).Nam, CHR$(32))))
  1515.   _PRINTSTRING (16 * (20), 16 * 24), V2$, L&
  1516.  
  1517.  IF LEN(RTRIM$(I(P.Shield).Nam)) < 9 THEN
  1518.   V1$ = RTRIM$(I(P.Shield).Nam)
  1519.   _PRINTSTRING (16 * (20), 16 * 25), V1$, L&
  1520.   V1$ = MID$(I(P.Shield).Nam, 1, INSTR(I(P.Shield).Nam, CHR$(32)))
  1521.   _PRINTSTRING (16 * (20), 16 * 25), V1$, L&
  1522.   V2$ = RTRIM$(MID$(I(P.Shield).Nam, INSTR(I(P.Shield).Nam, CHR$(32))))
  1523.   _PRINTSTRING (16 * (20), 16 * 26), V2$, L&
  1524.  
  1525.  
  1526. SUB Draw_Command_Window (L&)
  1527.  Draw_Window 12, 2, 15, 9, Layer(1)
  1528.  _PUTIMAGE (16 * 16 - 2, 2 * 16)-STEP(7 * 15 + 4, 1 * 15), Layer(3), L&, (205, 35)-STEP(15, 15)
  1529.  _PRINTSTRING (16 * 16, 16 * 2), "COMMAND", L&
  1530.  _PRINTSTRING (16 * 14, 16 * 4), "TALK", L&
  1531.  _PRINTSTRING (16 * 14, 16 * 6), "STATUS", L&
  1532.  _PRINTSTRING (16 * 14, 16 * 8), "STAIRS", L&
  1533.  _PRINTSTRING (16 * 14, 16 * 10), "SEARCH", L&
  1534.  _PRINTSTRING (16 * 22, 16 * 4), "SPELL", L&
  1535.  _PRINTSTRING (16 * 22, 16 * 6), "ITEM", L&
  1536.  _PRINTSTRING (16 * 22, 16 * 8), "DOOR", L&
  1537.  _PRINTSTRING (16 * 22, 16 * 10), "TAKE", L&
  1538.  
  1539. FUNCTION Yes_NO_Box%% (X%%, Y%%, L&)
  1540.  Result%% = FALSE
  1541.  selection%% = 2
  1542.  _PUTIMAGE , Layer(1), Layer(5)
  1543.  DO
  1544.   _PUTIMAGE , Layer(5), L&
  1545.   Draw_Window X%%, Y%%, 7, 5, L&
  1546.   _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 2)), "YES", L&
  1547.   _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 4)), "NO", L&
  1548.   IF Frame%% THEN Display_Selection_Arrow X%% + 1, Y%% + (selection%% * 2), L&
  1549.   _PUTIMAGE , L&, Layer(0)
  1550.    CASE 27
  1551.     ExitFlag%% = TRUE
  1552.    CASE Key_Up
  1553.     selection%% = selection%% - 1
  1554.     IF selection%% = 0 THEN selection%% = 1
  1555.    CASE Key_Down
  1556.     selection%% = selection%% + 1
  1557.     IF selection%% = 3 THEN selection%% = 2
  1558.    CASE A_Button
  1559.     ExitFlag%% = TRUE
  1560.     IF selection%% = 1 THEN Result%% = TRUE
  1561.    CASE B_Button
  1562.     ExitFlag%% = TRUE
  1563.  
  1564.   _LIMIT 60
  1565.  LOOP UNTIL ExitFlag%%
  1566.  Yes_NO_Box = Result%%
  1567.  
  1568. FUNCTION Buy_Sell_Box%% (X%%, Y%%, L&)
  1569.  Result%% = FALSE
  1570.  selection%% = 1
  1571.  _PUTIMAGE , Layer(1), Layer(5)
  1572.  DO
  1573.   _PUTIMAGE , Layer(5), L&
  1574.   Draw_Window X%%, Y%%, 7, 5, L&
  1575.   _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 2)), "BUY", L&
  1576.   _PRINTSTRING (16 * (X%% + 2), 16 * (Y%% + 4)), "SELL", L&
  1577.   IF Frame%% THEN Display_Selection_Arrow X%% + 1, Y%% + (selection%% * 2), L&
  1578.   _PUTIMAGE , L&, Layer(0)
  1579.    CASE 27
  1580.     ExitFlag%% = TRUE
  1581.    CASE Key_Up
  1582.     selection%% = selection%% - 1
  1583.     IF selection%% = 0 THEN selection%% = 1
  1584.    CASE Key_Down
  1585.     selection%% = selection%% + 1
  1586.     IF selection%% = 3 THEN selection%% = 2
  1587.    CASE A_Button
  1588.     ExitFlag%% = TRUE
  1589.     IF selection%% = 1 THEN Result%% = TRUE
  1590.    CASE B_Button
  1591.     ExitFlag%% = TRUE
  1592.  
  1593.   _LIMIT 60
  1594.  LOOP UNTIL ExitFlag%%
  1595.  Buy_Sell_Box = Result%%
  1596.  
  1597. FUNCTION Command_Window%% (L&)
  1598.  Result%% = FALSE
  1599.  Selection%% = 1
  1600.  DO
  1601.   KBD& = _KEYHIT
  1602.   Draw_Command_Window L&
  1603.   Display_Selection_Arrow SA(Selection%%).X, SA(Selection%%).Y, L&
  1604.   IF KBD& = Key_Up AND Selection%% > 1 AND Selection%% <> 5 THEN Selection%% = Selection%% - 1
  1605.   IF KBD& = Key_Down AND Selection%% < 8 AND Selection%% <> 4 THEN Selection%% = Selection%% + 1
  1606.   IF KBD& = Key_Left AND Selection%% > 4 THEN Selection%% = Selection%% - 4
  1607.   IF KBD& = Key_Right AND Selection%% < 5 THEN Selection%% = Selection%% + 4
  1608.  
  1609.   IF KBD& = B_Button THEN ExitFlag%% = TRUE
  1610.   IF KBD& = A_Button THEN Result%% = Selection%%: ExitFlag%% = TRUE
  1611.   IF KBD& = 27 THEN ExitFlag%% = TRUE
  1612.   _PUTIMAGE , L&, Layer(0)
  1613.   _LIMIT 60
  1614.  LOOP UNTIL ExitFlag%%
  1615.  Command_Window = Result%%
  1616.  
« Last Edit: January 09, 2020, 12:26:11 pm by Cobalt »
Granted after becoming radioactive I only have a half-life!

FellippeHeitor

  • Guest
Re: weird number results in my program.(Fixed, thanks guys!)
« Reply #1 on: January 09, 2020, 01:13:47 pm »
Glad to hear you've figured it out.

For anyone out there not yet taking part in the Discord party, please head to http://discord.qb64.org.

If you're old school and prefer IRC, head over to http://irc.qb64.org, although this one isn't nearly as active as Discord these days.
« Last Edit: January 09, 2020, 01:14:58 pm by FellippeHeitor »