#include "stdafx.h"

#include "UI.h"

#include <stdio.h>

#include "Objects.h"
#include "Dispatch.h"
#include "CSB.h"
#include "Data.h"


extern unsigned int numRandomCalls;
extern i32 VBLMultiplier;

//   TAG00133e
void ClearHeldObjectName(void)
{
  FillRectangle(d.LogicalScreenBase,
                &d.wRectPos624,
                0,
                160);
}

//   TAG004308
void FloorAndCeilingOnly(void)
{
  static i32 count=0;
  if ((VBLMultiplier!=1) && (((count++)%1) != 0)) return;
  Instrumentation(icntFloorAndCeiling);
  ClearMemory(d.pViewportBlack,4144);//space between floor and ceiling
  MemMove(d.pCeilingBitmap, d.pViewportBMP, 3248);//ceiling
  MemMove(d.pFloorBitmap, d.pViewportFloor, 7840);//floor
  d.Word11684=0;
}


//   TAG00788c
void DrawCellF1R1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D4;
  SUMMARIZEROOMDATA roomData;
  //D7W=P1;D6W=P2,D5W=P3;
  SummarizeRoomInfo(&roomData,facing,mapX,mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044fe(d.Word1880,(RectPos *)d.Byte2618);
  else TAG0044fe(d.Word1894,(RectPos *)d.Byte2706);
  goto case01;

case00:
  //ClearViewport(); // xxxxxxxxtra
  TAG00437e(d.pWallBitmaps[9], (RectPos *)d.Byte3010);
  DrawWallDecoration(roomData.decorations[2],11);
  goto exit;

case18:
  if (roomData.decorations[0] != 0) TAG0044fe(d.Word1902, (RectPos *)d.Byte2754);
  else TAG0044fe(d.Word1904,(RectPos *)d.Byte2770);

case16:
  D4W = 65;
  goto tag0079e2;

case17:
  TAG004a1a(roomData.decorations[2],8);
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,8,24);
  TAG00437e(d.pWallBitmaps[5], (RectPos *)d.Byte3194);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1918,0x1080,2,d.Byte3914);
  D4W = 73;
  goto tag007a08;

case02:
  TAG0044fe(roomData.decorations[0]!=0?59:53,(RectPos *)d.Byte2858);
case01:
case05:
  D4W = 65;
tag0079e2:
  TAG004a1a(roomData.decorations[2],8);
  CheckCeilingPit(65,(RectPos *)d.Byte2930,mapX,mapY,1);
tag007a08:
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,8,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3946,(RectPos *)d.Byte3010); /*,roomData);*/
exit:
  return;
}

//*********************************************************
//
//*********************************************************
//   TAG007a4a
void DrawCellF1(i32 facing,i32 mapX,i32 mapY,bool skipDrawing)
{
  dReg D0, D4;
  DB0 *pDB0;
  //i16 *pwA0;
  SUMMARIZEROOMDATA roomData;
  pnt LOCAL_14;
  //D7W = P1; D6W = P2; D5W = P3;
  SummarizeRoomInfo(&roomData,facing,mapX,mapY);//very quick
  d.CellTypeJustAhead = sw(roomData.roomType);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (skipDrawing) goto case01;
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1882, (RectPos *)d.Byte2610);
  else TAG0044b0(d.Word1896, (RectPos *)d.Byte2698);
  goto case01;
case00:
  d.Word10536 = d.Word10534 = d.FacingAlcove = 0;
  if (d.SeeThruWalls != 0)
  {
    TAG022c90(1);
    if (!skipDrawing)
    TAG0088b2(d.pViewportBMP,
              TAG022d38(1),
              (RectPos *)d.Word2538,
              d.uByte2534[0],
              d.uByte2534[2],
              112,
              48,
              -1);
    LOCAL_14 = GetGraphicAddress(41);
    if (!skipDrawing)
    TAG0088b2(LOCAL_14,
              TAG022d38(1),
              (RectPos *)d.Word2538,
              0,
              0,
              48,
              48,
              10);
  };
  //ClearViewport();
  if (!skipDrawing)
  TAG0043c4(d.pWallBitmaps[9], (RectPos *)d.uByte3026);//Draw blocks
  D0W = DrawWallDecoration(roomData.decorations[1],12);//Draws wall decorations
  if (D0W != 0) //if alcove
  {
    //if (!skipDrawing)
    DrawRoomObjects(roomData.rn2,facing,mapX,mapY,6,0);
  };
  if (d.SeeThruWalls != 0)
  {
    TAG0088b2(TAG022d38(1),
              d.pViewportBMP,
              (RectPos *)d.uByte2534, //0x40,0x9f,0x13,0x71
              0,
              0,
              48,
              112,
              9);
    TAG022d5e(1);
    DeleteGraphic((ui16)0x8001);
  };
  goto exit;
case17:
  TAG004a1a(roomData.decorations[2], 7);
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,6,0x218);
  TAG00437e(d.pWallBitmaps[5], (RectPos *)d.Byte3186);
  TAG00437e(d.pWallBitmaps[1], (RectPos *)d.Byte3130);
  TAG00437e(d.Pointer2142, (RectPos *)d.Byte3138);
  //A0 = (pnt)d.misc1052eight[0] + roomData.decorations[1]*4;
  //D0W = (wordGear(A0+2)>>6) & 1;
  //if (D0W != 0) DrawDoorSwitch(1,3);
  pDB0 = GetRecordAddressDB0(roomData.decorations[1]);
  if (pDB0->doorSwitch()) DrawDoorSwitch(1, 3);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1918,0x1080,2,d.Byte3834);
  D4W = 841;
  goto tag007cbe;
case02:
  if (!skipDrawing)
  TAG0044b0(roomData.decorations[0]!=0?60:54, (RectPos *)d.Byte2850);

case01:
case05:
  D4W = 13345;
  TAG004a1a(roomData.decorations[2],7);
  if (!skipDrawing)
  CheckCeilingPit(66,(RectPos *)d.Byte2922,mapX,mapY,0);
tag007cbe:
  //if (!skipDrawing)
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,6,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  if (!skipDrawing)
  TAG004f82(d.Byte3962,(RectPos *)d.uByte3026 /*,roomData*/);
exit:
  return;
}

//   TAG006b20
void DrawCellF3L1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  ROOMTYPE rtD0;
  SUMMARIZEROOMDATA roomData; // array???
//  D7W = P1;
//  D6W = P2;
//  D5W = P3;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  rtD0 = roomData.roomType;
  if (rtD0 != roomType19) goto tag006b7a;
  D0W = roomData.decorations[0];
  if (D0W == 0) goto tag006b68;
  TAG0044b0(d.Word1872, (RectPos *)d.Byte2554);
  goto tag006b76;
tag006b68:
  TAG0044b0(d.Word1886, (RectPos *)d.Byte2642);
tag006b76:
  goto tag006c56;
tag006b7a:
  if (rtD0 != roomSTONE) goto tag006bb6;
  TAG00437e(d.pWallBitmaps[11], (RectPos *)d.Byte3066);
  DrawWallDecoration(roomData.decorations[0],0);
  D0W = DrawWallDecoration(roomData.decorations[1], 2);
  if (D0W == 0) goto tag006bb2;
  D4W = 0;
  goto tag006c66;
tag006bb2:
  goto tag006c9a;
tag006bb6:
  if (rtD0 == roomDOOREDGE) goto tag006bc4;
  if (rtD0 != roomType18) goto tag006bcc;
tag006bc4:
  D4W = 801;
  goto tag006c5a;
tag006bcc:
  if (rtD0 != roomDOORFACING) goto tag006c2a;
  TAG004a1a(roomData.decorations[2], 0);
  DrawRoomObjects(roomData.rn2, facing, mapX, mapY, 1, 0x218);
  TAG00437e(d.pWallBitmaps[4], (RectPos *)d.Byte3082);
  DrawDoor(roomData.decorations[1], roomData.decorations[0], d.Word1910, 984, 0,d.Byte3274);
  D4W = 841;
  goto tag006c66;
tag006c2a:
  if (rtD0 != roomPIT) goto tag006c48;
  D0W = roomData.decorations[0];
  if (D0W != 0) goto tag006c46;
  TAG0044b0(49, (RectPos *)d.Byte2794);
tag006c46:
  goto tag006c56;
tag006c48:
  if (rtD0 == roomTELEPORTER) goto tag006c56;
  if (rtD0 != roomOPEN) goto tag006c7e;
tag006c56:
  D4W = 13345;
tag006c5a:
  TAG004a1a(roomData.decorations[2], 0);
tag006c66:
  DrawRoomObjects(roomData.rn2, facing, mapX, mapY, 1, D4W);
tag006c7e:
  if (roomData.roomType == roomTELEPORTER)
  {
    if (roomData.decorations[0] != 0)
    {
      TAG004f82(d.Byte4002, (RectPos *)d.Byte3066);
    };
  };
tag006c9a: return;
}

//   TAG006ca2
void DrawCellF3R1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  DB0 *pDB0;
  //i16 *pwA0;
  //i32 D4=0, D5, D6, D7;
  SUMMARIZEROOMDATA roomData;
  D4L = 0;
  //D7W = P1;
  //D6W = P2;
  //D5W = P3;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044fe(d.Word1872, (RectPos *)d.Byte2570);
  else TAG0044fe(d.Word1886, (RectPos *)d.Byte2658);
  goto case01;
case00:
  TAG00437e(d.pWallBitmaps[11], (RectPos *)d.Byte3058);
  DrawWallDecoration(roomData.decorations[2], 1);
  D0W = DrawWallDecoration(roomData.decorations[1], 4);
  if (D0W == 0) goto tag006e5e;
  goto tag006e2a;
case16:
case18:
  D4W = 1042;
  goto tag006e1c;
case17:
  TAG004a1a(roomData.decorations[2], 2);
  DrawRoomObjects(roomData.rn2,facing, mapX, mapY,2,296);
  MemMove(d.pWallBitmaps[4], d.Pointer1848, 704);
  TAG004450(d.Pointer1848, d.Byte3090);
  //A0 = (pnt)d.misc1052eight[0] + roomData.decorations[1]*4;
  //D0W = (wordGear(A0+2) >> 6) & 1;
  //if (D0W != 0) DrawDoorSwitch(1, 0);
  pDB0 = GetRecordAddressDB0(roomData.decorations[1]);
  if (pDB0->doorSwitch()) DrawDoorSwitch(1, 0);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1910,984,0,d.Byte3434);
  D4W = 1081;
  goto tag006e2a;
case02:
  if (roomData.decorations[0] == 0) TAG0044fe(49, (RectPos *)d.Byte2810);
case01:
case05:
  D4W = 17170; // 0x4312
tag006e1c:
  TAG004a1a(roomData.decorations[2], 2);
tag006e2a:
  DrawRoomObjects(roomData.rn2,facing, mapX, mapY,2,D4W);
finish:
  if (roomData.roomType == roomTELEPORTER)
  {
    if (roomData.decorations[0] != 0)
             //TAG004f82(d.Byte3994, (RectPos *)d.Byte3058);
             // I replaced the 3994 with 4002 because 3994[3]
             // was zero and cause TAG004f82 to use NULL as a
             // source pointer.  This seems to cause no harm
             // on the ATARI.  In fact, it seems to make no
             // difference what data is used.
             TAG004f82(d.Byte4002, (RectPos *)d.Byte3058);
  };
tag006e5e:
  return;
}

//   TAG006e66
void DrawCellF3(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  DB0 *pDB0;
  SUMMARIZEROOMDATA roomData;
  //void (*fn)(i32, i32, i32) = NULL;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1874, (RectPos *)d.Byte2562);
  else TAG0044b0(d.Word1888, (RectPos *)d.Byte2650);
  goto case01;
case00:
  TAG0043c4(d.pWallBitmaps[11], (RectPos *)d.Byte3074);
  D0W = DrawWallDecoration(roomData.decorations[1], 3);
  if (D0W == 0) goto exit;
  D4W = 0;
  goto tag00709e;
case17:
  TAG004a1a(roomData.decorations[2], 1);
  DrawRoomObjects(roomData.rn2, facing, mapX, mapY, 0, 536);
  TAG00437e(d.pWallBitmaps[3], (RectPos *)d.Byte3098);
  MemMove(d.pWallBitmaps[3], d.Pointer1848, 704);
  TAG004450(d.Pointer1848, d.Byte3106);
  //A0 = (pnt)d.misc1052eight[0] + roomData.decorations[1]*4;
  //D0W = (wordGear(A0+2)>>6)&1;
  //if (D0W != 0) DrawDoorSwitch(1, 1);
  pDB0 = GetRecordAddressDB0(roomData.decorations[1]);
  if (pDB0->doorSwitch()) DrawDoorSwitch(1, 1);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1910,984,0,d.Byte3354);
  D4W = 841;
  goto tag00709e;
case02:
  if (roomData.decorations[0] == 0) TAG0044b0(50, (RectPos *)d.Byte2802);
case05:
  if ((ui16)d.Word1836 < 15) goto case01;
  if (d.Word23228 != 0) goto case01;
  if (d.Long11732 >= 65532) goto tag006fee;
  if (d.Time > 65530) goto tag007002; // 10000 seconds??
tag006fee:
  if ((ui32)(d.Long11732 + 8192) <= (ui32)d.Time) goto case01;
tag007002:
  //D0W = STRandom0_3();
  //if      (D0W == 0) fn = DrawCellF1R1;
  //else if (D0W == 1) fn = DrawCellF1;
  //else if (D0W == 2) fn = DrawCellF1L1;
  //else if (D0W == 3) fn = DrawCellF0R1;
  HopefullyNotNeeded();
  //TAG00907e(fn,
  //          GetGraphicAddress,
  //          538,
  //          d.ClusterCache,
  //          &d.Word23228,
  //          d.Pointer2526,3);
  d.CacheInvalid |= 1;
  d.Word11720 |= 1;
  d.Word1836 -= 10;
  d.Word1840 = 0;
case01:
  D4W = 13345;
  TAG004a1a(roomData.decorations[2], 1);
tag00709e:
  DrawRoomObjects(roomData.rn2, facing, mapX, mapY, 0, D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] != 0)
       TAG004f82(d.Byte4010,(RectPos *)d.Byte3074);
exit:
  return;
}

//   TAG0070d8
void DrawCellF2L1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  SUMMARIZEROOMDATA roomData;
  //D7W = P1;
  //D6W = P2;
  //D5W = P3;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1876, (RectPos *)d.Byte2578);
  else TAG0044b0(d.Word1890, (RectPos *)d.Byte2666);
  goto case01;
case00:
  TAG00437e(d.pWallBitmaps[10], (RectPos *)d.Byte3042);
  DrawWallDecoration(roomData.decorations[0], 5); //wall decorations?
  D0W = DrawWallDecoration(roomData.decorations[1], 7);//wall decorations
  if (D0W == 0) goto exit;
  D4W = 0;
  goto tag00723e;
case18:
  TAG0044b0(d.Word1900, (RectPos *)d.Byte2730);
case16:
  D4W = 0;
  goto tag007230;
case17:
  TAG004a1a(roomData.decorations[2], 3);
  DrawRoomObjects(roomData.rn2,facing, mapX, mapY, 4, 536);
  TAG00437e(d.pWallBitmaps[6], (RectPos *)d.Byte3154);
  DrawDoor(roomData.decorations[1], roomData.decorations[0], d.Word1914, 1952, 1, d.Byte3514);
  D4L = 841;
  goto tag00723e;
case02:
  TAG0044b0(roomData.decorations[0]==0?51:57,(RectPos *)d.Byte2818);
case01:
case05:
  D4W = 13345;
tag007230:
  TAG004a1a(roomData.decorations[2], 3);
tag00723e:
  CheckCeilingPit(63,(RectPos *)d.Byte2890,mapX,mapY,0);
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,4,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3978, (RectPos *)d.Byte3042);
exit:
  return;
}

//   TAG007290
void DrawCellF2R1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  SUMMARIZEROOMDATA roomData;
  //D7W = P1;
  //D6W = P2;
  //D5W = P3;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomOPEN)       goto case01;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  goto finish;
case19:
  if (roomData.decorations[0]!=0) TAG0044fe(d.Word1876,(RectPos *)d.Byte2594);
  else TAG0044fe(d.Word1890, (RectPos *)d.Byte2682);
  goto case01;
case00:
  TAG00437e(d.pWallBitmaps[10], (RectPos *)d.Byte3034);
  DrawWallDecoration(roomData.decorations[2],6);
  D0W = DrawWallDecoration(roomData.decorations[1], 9);
  if (D0W != 0)
  {
    D4W = 0;
    goto tag0074b4;
  };
  if ((ui16)d.Word1836 > 6) goto exit;
  if (d.Word1840 ==0) goto exit;
  if (d.Word23228 != 0) goto exit;
  if ((d.Word11722 & 1) != 0 ) goto tag007354;
  if ((d.CacheInvalid & 1) != 0) goto tag007384;
tag007354:
  if ((d.Word11724 & 1) == 0) goto tag00737a;
  if ((d.Word11722 & 1) == 0) goto tag007372;
  d.Word1842++;
  d.Word11722 &= 0xfffe;
tag007372:
  d.Word11724 &= 0xfffe;
  goto tag00737c;
  goto tag00737c;
tag00737a:
  goto exit;
tag00737c:
  d.Word1836 += 10;
  goto exit;
tag007384:
  d.Word1844 = 555;
  HopefullyNotNeeded();
  //TAG00907e(RandomBool() ? DrawCellF1 : DrawCellF0L1,
  //          GetGraphicAddress,
  //          21,
  //          d.ClusterCache,
  //          (i16 *)&d.Pointer2066, // don't know what to do here
  //          d.Pointer2526,
  //          3);
  d.Word1836 += 5;
  goto exit;
case18:
  TAG0044fe(d.Word1900, (RectPos *)d.Byte2738);
case16:
  D4W = 1073;
  goto tag00748e;
case17:
  TAG004a1a(roomData.decorations[2], 5);
  DrawRoomObjects(roomData.rn2,facing, mapX, mapY, 5, 296);
  TAG00437e(d.pWallBitmaps[6], (RectPos *)d.Byte3170);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1914,1952,1,d.Byte3674);
  D4W = 1081;
  goto tag0074b4;
case02:
  TAG0044fe(roomData.decorations[0]==0?51:57,(RectPos *)d.Byte2834);
case01:
case05:
  D4W = 17170;
tag00748e:
  TAG004a1a(roomData.decorations[2],5);
  CheckCeilingPit(63,(RectPos *)d.Byte2906, mapX, mapY, 1);
tag0074b4:
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,5,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3970, (RectPos *)d.Byte3034);
exit:
  return;
}

//*********************************************************
//
//*********************************************************
//   TAG0074f0
void DrawCellF2(i32 facing,i32 mapX,i32 mapY)
{
  dReg D0, D4;
  DB0 *pDB0;
  //i16 *pwA0;
  SUMMARIZEROOMDATA roomData;
  //D7W = P1; D6W = P2; D5W = P3;
  SummarizeRoomInfo(&roomData,facing,mapX,mapY);
  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1878, (RectPos *)d.Byte2586);
  else TAG0044b0(d.Word1892, (RectPos *)d.Byte2674);
  goto case01;
case00:
  TAG0043c4(d.pWallBitmaps[10], (RectPos *)d.Byte3050);
  D0W = DrawWallDecoration(roomData.decorations[1], 8);
  if (D0W != 0)
  {
    D4W = 0;
    goto tag00768e;
  };
  goto exit;
case17:
  TAG004a1a(roomData.decorations[2], 4);
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,3,536);
  TAG00437e(d.pWallBitmaps[6], (RectPos *)d.Byte3162);
  TAG00437e(d.pWallBitmaps[2], (RectPos *)d.Byte3114); // Door sides appear
  MemMove(d.pWallBitmaps[2], d.Pointer1848, 1560);
  TAG004450(d.Pointer1848, d.Byte3122); // Other side of door.
  pDB0 = GetRecordAddressDB0(roomData.decorations[1]);
  if (pDB0->doorSwitch()) DrawDoorSwitch(1, 2);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1914,1952,1,d.Byte3594);
  //  A door has appeared.
  D4W = 841;
  goto tag00768e;
case02:
  TAG0044b0(roomData.decorations[0]==0?52:58,(RectPos *)d.Byte2826);
case01:
case05:
  D4W = 13345;
  TAG004a1a(roomData.decorations[2], 4);
  CheckCeilingPit(64,(RectPos *)d.Byte2898,mapX,mapY,0);
tag00768e:
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,3,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3986, (RectPos *)d.Byte3050); /*, roomData);*/
exit:
  return;
}

//*********************************************************
//
//*********************************************************
//   TAG0076d0
void DrawCellF1L1(i32 facing,i32 mapX,i32 mapY)
{
  dReg D4;
  SUMMARIZEROOMDATA roomData;
  //D7W = P1; D6W = P2; D5W = P3;
  SummarizeRoomInfo(&roomData,facing, mapX, mapY);
  if ((mapX==1)&&(mapY==1))
  if (TimerTraceActive)
  {
    fprintf(GETFILE(TraceFile),
             "RoomData.decorations[0] = 0x%x\n",
                roomData.decorations[0]);
  };

  if (roomData.roomType == roomType19)     goto case19;
  if (roomData.roomType == roomSTONE)      goto case00;
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomDOORFACING) goto case17;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomOPEN)       goto case01;
  goto finish;
case19:
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1880, (RectPos *)d.Byte2602);
  else TAG0044b0(d.Word1894, (RectPos *)d.Byte2690);
  goto case01;
case00:
  TAG00437e(d.pWallBitmaps[9], (RectPos *)d.Byte3018);
  DrawWallDecoration(roomData.decorations[0], 10);
  goto exit;
case18:
  if (roomData.decorations[0] != 0) TAG0044b0(d.Word1902, (RectPos *)d.Byte2746);
  else TAG0044b0(d.Word1904,(RectPos *)d.Byte2762);
case16:
  D4W = 50;
  goto tag007826;
case17:
  TAG004a1a(roomData.decorations[2],6);
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,7,40);
  TAG00437e(d.pWallBitmaps[5], (RectPos *)d.Byte3178);
  DrawDoor(roomData.decorations[1],roomData.decorations[0],d.Word1918,4224,2,d.Byte3754);
  D4W = 57;
  goto tag00784a;
case02:
TAG0044b0(roomData.decorations[0]!=0?59:53,(RectPos *)d.Byte2842);
case01:
case05:
  D4W = 50;
tag007826:
  TAG004a1a(roomData.decorations[2],6);
  CheckCeilingPit(65,(RectPos *)d.Byte2914,mapX,mapY,0);
tag00784a:
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,7,D4W);
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3954,(RectPos *)d.Byte3018); /*,roomData);*/
exit:
  return;
}

//   TAG007d00
void DrawCellF0L1(i32 facing,i32 mapX,i32 mapY)
{
  //dReg D5,D6,D7;
  SUMMARIZEROOMDATA roomData;
  //D7W=facing; D6W=mapX; D5W=mapY;
#ifdef _VERIFYPPQ
      verifyppq();
#endif
  SummarizeRoomInfo(&roomData,facing,mapX,mapY);
  if (roomData.roomType == roomType18)     goto case18;
  if (roomData.roomType == roomPIT)        goto case02;
  if (roomData.roomType == roomOPEN)       goto case01;
  if (roomData.roomType == roomDOOREDGE)   goto case16;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomSTONE)      goto case00;
  goto finish;
case18:
  TAG0044b0(d.Word1906, (RectPos *)d.Byte2778);
  goto exit;
case02:
  TAG0044b0(roomData.decorations[0] != 0? 61:55,(RectPos *)d.Byte2866);
case01:
case05:
case16:
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,10,2);
  goto finish;
case00:
  TAG00437e(d.pWallBitmaps[8], (RectPos *)d.uByte2994);
  goto exit;
finish:
  CheckCeilingPit(67,(RectPos *)d.Byte2938,mapX,mapY,0);
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3930, (RectPos *)d.uByte2994);
exit:
  return;
}

//   TAG007de8
void DrawCellF0R1(i32 facing,i32 mapX,i32 mapY)
{
    //dReg D5, D6, D7;
    SUMMARIZEROOMDATA roomData;
  //D7W = P1; D6W = P2; D5W = P3;
  SummarizeRoomInfo(&roomData,facing,mapX, mapY);
  if (roomData.roomType == roomType18) goto case18;
  if (roomData.roomType == roomPIT) goto case02;
  if (roomData.roomType ==  roomOPEN) goto case01;
  if (roomData.roomType == roomDOOREDGE) goto case16;
  if (roomData.roomType == roomTELEPORTER) goto case05;
  if (roomData.roomType == roomSTONE) goto case00;
  goto finish;
case18:
  TAG0044fe(d.Word1906,(RectPos *)d.Byte2786);
goto exit;

case02:
  TAG0044fe(roomData.decorations[0]!=0?61:55, (RectPos *)d.Byte2882);

case01:
case05:
case16:
  CheckCeilingPit(67, (RectPos *)d.Byte2954, mapX, mapY, 1);
  DrawRoomObjects(roomData.rn2,facing, mapX, mapY,11,1);
  goto finish;

case00:
  TAG00437e(d.pWallBitmaps[7], (RectPos *)d.Byte2986);
  goto exit;
finish:
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  TAG004f82(d.Byte3922, (RectPos *)d.Byte2986);
exit:
  return;
}

//   TAG007ed2
void DrawLocalCell(i32 facing,i32 mapX,i32 mapY,bool skipDrawing)
{
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  //dReg D5, D6, D7;
  SUMMARIZEROOMDATA roomData;
  //D7W = P1; D6W = P2; D5W = P3;
  SummarizeRoomInfo(&roomData, facing, mapX, mapY);
  if (roomData.roomType == roomDOOREDGE) goto case16;
  if (roomData.roomType == roomType19) goto case19;
  if (roomData.roomType == roomPIT) goto case02;
  goto finish;
case16:
  if (d.SeeThruWalls != 0)
  {
    MemMove(d.pWallBitmaps[0], d.Pointer1848, 1968);
    if (!skipDrawing)
    TAG0088b2(GetGraphicAddress(41),
              d.Pointer1848,
              (RectPos *)d.uByte2542,
              ub(d.Byte3146[0])-d.uByte2534[0], // This isn't right
              0,
              48,
              16,
              9);
    TAG00437e(d.Pointer1848, (RectPos *)d.Byte3146);
  }
  else
  {
    TAG00437e(d.pWallBitmaps[0], (RectPos *)d.Byte3146);
  };
  goto finish;

case19:
  if (roomData.decorations[0] != 0)
  {
    TAG0044b0(d.Word1884, (RectPos *)d.Byte2626);
    TAG0044fe(d.Word1884, (RectPos *)d.Byte2634);
  }
  else
  {
    TAG0044b0(d.Word1898, (RectPos *)d.Byte2714);
    TAG0044fe(d.Word1898, (RectPos *)d.Byte2722);
  };
  goto finish;

case02:
  TAG0044b0(roomData.decorations[0]!=0?62:56,(RectPos *)d.Byte2874);
finish:
  if (!skipDrawing)
  CheckCeilingPit(68,(RectPos *)d.Byte2946,mapX,mapY,0);
  //if (!skipDrawing)
  DrawRoomObjects(roomData.rn2,facing,mapX,mapY,9,0x21);
  if (roomData.roomType != roomTELEPORTER) goto exit;
  if (roomData.decorations[0] == 0) goto exit;
  if (!skipDrawing)
  TAG004f82(d.Byte3938, (RectPos *)d.Byte3002);
exit:
  return;
}

extern i32 DrawViewportCount;

//   TAG00803a
void DrawViewport(i32 facing,i32 x,i32 y)
{
  unsigned int saveRandom;
  unsigned int saveNumRan;
  saveRandom = d.RandomNumber; // Don't upset the game
  saveNumRan = numRandomCalls;
  bool skipDrawing;
  skipDrawing = (VBLMultiplier != 1) && ((d.Time & 0xf) != 0);
#ifdef _VERIFYPPQ
  verifyppq();
#endif
  DrawViewportCount++;
  i32 cellY;
  i32 cellX;
  dReg D0, D4;
  D4W=0;
  if (d.Word11684 != 0)
  {
    FloorAndCeilingOnly();
  };
  d.UseByteCoordinates = 1;
  // Clear all the object buttons.
  memset(d.ViewportObjectButtons[0], 0, 24);
  fillWithByte((pnt)d.ViewportObjectButtons[0],6,ui8(0xff), 4);
  d.Pointer1848=allocateMemory(8880, 0);
  d.UseMirrorImages = (I16)((x + y + facing)&1);
  if (d.UseMirrorImages != 0)
  {
    if (!skipDrawing) Mirror(d.pViewportFloor, 0x0070, 0x0046);
    D4W = 1;//d.BigMemory;
    d.pWallBitmaps[11] = d.Pointer2214;
    d.pWallBitmaps[10] = d.Pointer2218;
    d.pWallBitmaps[9]  = d.Pointer2222;
    d.pWallBitmaps[8]  = d.Pointer2226;
    d.pWallBitmaps[7]  = d.Pointer2230;
  }
  else
  {
    if (!skipDrawing) Mirror(d.pViewportBMP, 0x0070, 0x001d);
  };
  D0W = TAG0097d6(facing, 0x0003, -2, x, y);
  if (D0W==0)
  {
    TAG00437e(d.pWallBitmaps[12], (RectPos *)d.Byte2970);
  };
  D0W=TAG0097d6(facing, 0x0003, 0x0002, x, y);
  if (D0W==0)
  {
    TAG00437e(d.Pointer2086, (RectPos *)d.Byte2978);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing, 4, -1, &cellX, &cellY);
    DrawRoomObjects(FindFirstDrawableObject(cellX,cellY),facing,cellX,cellY,-2,1);
  };
  if (!skipDrawing)
  {
    cellX = x;
    cellY = y;
    UpdateXY(facing, 4, 1, &cellX, &cellY);
    DrawRoomObjects(FindFirstDrawableObject(cellX, cellY), facing,cellX, cellY,-1, 1);
  };
  if (!skipDrawing)
  {
    cellX = x;
    cellY = y;
    UpdateXY(facing,4,0,&cellX,&cellY);
    DrawRoomObjects(FindFirstDrawableObject(cellX,cellY), facing, cellX, cellY, -3, 1);
  };
  if (!skipDrawing)
  {
    cellX = x;
    cellY = y;
    UpdateXY(facing,3,-1,&cellX,&cellY);
    DrawCellF3L1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellY= y;
    cellX=x;
    UpdateXY(facing,3,1,&cellX,&cellY);
    DrawCellF3R1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,3,0,&cellX,&cellY);
    DrawCellF3(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,2,-1,&cellX,&cellY);
    DrawCellF2L1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,2,1,&cellX,&cellY);
    DrawCellF2R1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,2,0,&cellX,&cellY);
    DrawCellF2(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,1,-1,&cellX,&cellY);
    DrawCellF1L1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX=x;
    cellY=y;
    UpdateXY(facing,1,1,&cellX,&cellY);
    DrawCellF1R1(facing,cellX,cellY);
  };
  cellX = x;
  cellY = y;
  UpdateXY(facing,1,0,&cellX,&cellY);
  DrawCellF1(facing,cellX,cellY,skipDrawing);
  if (!skipDrawing)
  {
    cellX = x;
    cellY = y;
    UpdateXY(facing,0,-1,&cellX,&cellY);
    DrawCellF0L1(facing,cellX,cellY);
  };
  if (!skipDrawing)
  {
    cellX = x;
    cellY = y;
    UpdateXY(facing,0,1,&cellX,&cellY);
    DrawCellF0R1(facing,cellX,cellY);
  };
  DrawLocalCell(facing,x,y,skipDrawing);
  if ((D0W=D4W)!=0)
  {
    d.pWallBitmaps[11] = d.Pointer2234;
    d.pWallBitmaps[10] = d.Pointer2238;
    d.pWallBitmaps[9]  = d.Pointer2242;
    d.pWallBitmaps[8]  = d.Pointer2246;
    d.pWallBitmaps[7]  = d.Pointer2250;
  };
  // What was this for??? if (((D0W=d.Word1840)!=0) || true)
  {
  d.Word1840 = 1;
    FreeTempMemory(8880);
  };
  D0W = (I16)((d.partyLevel!=255)?1:0);
  if (!skipDrawing) MarkViewportUpdated(D0W);
  if (d.partyLevel!=255)
  {
    FloorAndCeilingOnly();
  };
  d.RandomNumber = saveRandom;
  numRandomCalls = saveNumRan;
}

//   TAG017c7a
void DrawSmallIcon(OBJECTTYPE icon,i16 x,i16 y)
{ // For example, the eye on the status viewport
  pnt BMP;
  RectPos rect;
  BMP = allocateMemory(128,0);
  rect.w.x1 = x;
  rect.w.x2 = sw(rect.w.x1 + 15);
  rect.w.y1 = y;
  rect.w.y2 = sw(rect.w.y1 + 15);
  GetIconGraphic(icon, BMP);
  BLT2Viewport(BMP, &rect, 8, -1);
  FreeTempMemory(128);
}

//   TAG018836
void DrawHorzBar(i32 x0, i32 y0, i32 width, i32 color)
{
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  RectPos barPos;
  barPos.w.x1 = sw(x0);
  barPos.w.x2 = sw(barPos.w.x1 + width);
  barPos.w.y1 = sw(y0);
  barPos.w.y2 = sw(barPos.w.y1 + 6);
  d.UseByteCoordinates = 0;
  FillRectangle(d.pViewportBMP, &barPos, color, 112);
}

//   TAG018878
void DrawFoodWaterBar(i16 value,i16 y,i16 color)
{
  i32 barColor;
  barColor = color;
  if (value < 0) barColor = 11;   // Yellow
  if (value < -512) barColor = 8; //Red
  value += 1024;
  if (value >= 3072) value = 3071;
  value /= 32;
  DrawHorzBar(115, y+2, value, 0);//Black Background
  DrawHorzBar(113, y, value, barColor);//The bar itself
}

//*********************************************************
//
//*********************************************************
//   TAG0188e4
void DisplayFoodWater(void)
{
  CHARDESC *pcA3;
  pcA3 = &d.CH16482[d.SelectedCharacterOrdinal-1];
  if (TimerTraceActive)
  {
    fprintf(GETFILE(TraceFile),"%06x DisplayFoodWater calling Repack Chest\n",d.Time);
  };
  RepackChest();
  BLT2Viewport(GetGraphicAddress(20), &d.wRectPos926, 72, 8);//Food/water box
  BLT2Viewport(GetGraphicAddress(30), &d.wRectPos950, 24, 12);//"Food"
  BLT2Viewport(GetGraphicAddress(31), &d.wRectPos958, 24, 12);//"Water"
  if (pcA3->uByte42 != 0)
  {
    BLT2Viewport(GetGraphicAddress(32), &d.wRectPos966, 48, 12);
  };
  DrawFoodWaterBar(pcA3->food, 69, 5);
  DrawFoodWaterBar(pcA3->water, 92, 14);
}

//*********************************************************
//
//*********************************************************
RESTARTABLE _TAG0191e8(void)
{//(void)
  RESTARTMAP
    RESTART(1)
  END_RESTARTMAP
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  d.Word23140 = 1;
  d.Word11756 = 1;
  if ((d.MouseSwitches & 2) == 0)
  {
    d.Word23140 = 0;
    d.Word11756 = 0;
    d.Word11758 = 0;
    RETURN;
  };
  TAG01953a();
  STHideCursor();
  VBLDelay(_1_,8); // 8 vertical blanks
  DrawSmallIcon(obj_Special_e, 12, 13);//Eye looking towards skill scores
  if (d.EmptyHanded != 0)
  {
    TAG019036();
  }
  else
  {
    ClearHeldObjectName();
    ASSERT(RememberToPutObjectInHand == -1);
    TAG0183ea(d.objectInHand, 1);
  };
  MarkViewportUpdated(0);
  RETURN;
}
