REM > !LevelDes.!RunImage

LIBRARY"<Obey$Dir>.^.!HeeHeeHee.GameLib"

ON ERROR MODE 12: PROCGameLib_Error
MODE 27
*ScreenLoad <Obey$Dir>.Sprites
OFF

DIM DroidX%(100), DroidY%(100)

VDU 28, 1, 58, 78, 53
SYS"Wimp_TextColour", 0+128
SYS"Wimp_TextColour", 7
CLS

VDU 29, 35; 128;
VDU 24, 0; 0; 1200; 800;
SYS"Wimp_SetColour", 7+128
CLG

SYS"OS_Byte", 4, 1

*Pointer

MOUSE RECTANGLE 0, 0, 1200, 800

 REPEAT
  CLS
  PRINT"Choose an option..."'
  PRINT"1  Create new level"
  PRINT"2  Load a level"
  PRINT"3  Quit"
  REPEAT: a% = GET - 48: UNTIL (a% > 0) AND (a% < 5)

   CASE a% OF
    WHEN 1: CLS
            INPUT"Name of level: "Level$
            INPUT"Width of level: "Wide%
            INPUT"Height of level: "High%
            PROCCreate
    WHEN 2: CLS
            INPUT"Level name: "Level$
            PROCLoad
    WHEN 3: SYS"Wimp_CommandWindow", -1: END
   ENDCASE
 UNTIL 0

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCCreate
LOCAL Tile%()

f% = OPENIN("<Obey$Dir>.^.!HeeHeeHee.Levels."+Level$+".Data")
 IF f% <> 0 THEN
  CLOSE#f%
  VDU7
  PRINT'"A level called "+Level$+"‘ already exists!"
  IF INKEY(200)
  ENDPROC
 ENDIF

DIM Tile%(Wide% - 1, High% - 1)
Tile%() = 0

Droids% = 0

PROCEdit(Tile%())
ENDPROC

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCLoad
LOCAL Tile%()

File% = OPENIN("<Obey$Dir>.^.!HeeHeeHee.Levels."+Level$+".Data")
 IF File% = 0 THEN
  VDU7
  PRINT'"Could not load the level "+Level$+"‘!"
  IF INKEY(200)
  ENDPROC
 ENDIF
PRINT'"Loading..."

Wide% = VAL(GET$#File%)
High% = VAL(GET$#File%)
DIM Tile%(Wide%, High%)

X% = 0
Y% = High% - 1

 REPEAT
  Tile% = BGET#File%

   CASE Tile% OF
    WHEN 10    : Y% -= 1: X% = 0
    WHEN ASC" ": X% += 1
    WHEN ASC"P": Tile%(X%, Y%) += 1: X% += 1
    WHEN ASC"L": Tile%(X%, Y%) += 2: X% += 1
    WHEN ASC"W": Tile%(X%, Y%) += 4: X% += 1
    WHEN ASC"*": Tile%(X%, Y%) += 8: X% += 1
    WHEN ASC"S": Tile%(X%, Y%) += 16
    WHEN ASC"F": Tile%(X%, Y%) += 32
   ENDCASE

 UNTIL Y% = -1
CLOSE#File%

File% = OPENIN("<Obey$Dir>.^.!HeeHeeHee.Levels."+Level$+".Droids")
 IF File% = 0 THEN
  VDU7
  PRINT'"Could not load the level "+Level$+"‘!"
  IF INKEY(200)
  ENDPROC
 ENDIF

Droids% = 0
 WHILE NOT EOF#File%
  Droids% += 1
  DroidX%(Droids%) = VAL(GET$#File%)
  DroidY%(Droids%) = High% - VAL(GET$#File%)
 ENDWHILE

CLOSE#File%

PROCEdit(Tile%())
ENDPROC

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCSave(Tile%())
OSCLI"CDir <Obey$Dir>.^.!HeeHeeHee.Levels."+Level$
File% = OPENOUT("<Obey$Dir>.^.!HeeHeeHee.Levels."+Level$+".Data")
 IF File% = 0 THEN
  VDU7
  PRINT'"Could not save the level "+Level$+"‘"
  IF INKEY(200)
  ENDPROC
 ENDIF
PRINT''"Saving..."

BPUT#File%, STR$Wide%
BPUT#File%, STR$High%

 FOR Y% = High% - 1 TO 0 STEP -1
   FOR X% = 0 TO Wide% - 1

    Tile% = Tile%(X%, Y%)

    IF Tile% AND 1 THEN BPUT#File%, "P";
    IF Tile% AND 2 THEN BPUT#File%, "L";
    IF Tile% AND 4 THEN BPUT#File%, "W";
    IF Tile% AND 8 THEN BPUT#File%, "*";
    IF Tile% AND 16 THEN BPUT#File%, "S";
    IF Tile% AND 32 THEN BPUT#File%, "F";
    IF (Tile% AND %1111) = 0 THEN BPUT#File%, " ";

   NEXT
  BPUT#File%, ""
 NEXT
CLOSE#File%

File% = OPENOUT("<Obey$Dir>.^.!HeeHeeHee.Levels."+Level$+".Droids")
 IF File% = 0 THEN
  VDU7
  PRINT'"Could not save the level "+Level$+"‘"
  IF INKEY(200)
  ENDPROC
 ENDIF

 IF Droids% THEN
   FOR Droid% = 1 TO Droids%
    BPUT#File%, STR$(DroidX%(Droid%))
    BPUT#File%, STR$(High% - DroidY%(Droid%))
   NEXT
 ENDIF
CLOSE#File%

ENDPROC

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCEdit(Tile%())
XOff% = 0
YOff% = 0
REPEAT
CLG
VDU 29, 35 - (XOff% * 16); 128 - (YOff% * 16);
 FOR Y% = YOff% + 50 TO YOff% STEP -1
   FOR X% = XOff% TO XOff% + 75
    IF (X% < Wide%) AND (Y% < High%) THEN PROCTile(X%, Y%)
   NEXT
 NEXT
CLS
PRINT"Choose item to place by pressing key and click left mouse to place it"
PRINT"Click right button to clear square"
PRINT  "P  Platform             L  Ladder               W  Walls (blue)"
PRINT  "R  Deadly star (red)    S  Start (yellow)       F  Finish (green)"
PRINT  "X  Save & Exit          D  Droids"
PRINT  "arrows  scroll position";
Type% = 1
Scroll% = FALSE
Finish% = FALSE
 REPEAT
  a$ = INKEY$(0)
   CASE a$ OF
    WHEN "P","p": Type% = 1
    WHEN "L","l": Type% = 2
    WHEN "W","w": Type% = 4
    WHEN "R","r": Type% = 8
    WHEN "S","s": Type% = 16
    WHEN "F","f": Type% = 32
    WHEN "X","x": Finish% = TRUE
    WHEN "D","d": PROCDroids
                  Scroll% = TRUE
    WHEN "ˆ":  IF XOff% = 0 THEN
                VDU7
               ELSE
                XOff% -= 25
                Scroll% = TRUE
               ENDIF
    WHEN "‰":  IF XOff% + 75 > Wide% THEN
                VDU7
               ELSE
                XOff% += 25
                Scroll% = TRUE
               ENDIF
    WHEN "Š":  IF YOff% = 0 THEN
                VDU7
               ELSE
                YOff% -= 25
                Scroll% = TRUE
               ENDIF
    WHEN "‹":  IF YOff% + 50 > High% THEN
                VDU7
               ELSE
                YOff% += 25
                Scroll% = TRUE
               ENDIF
   ENDCASE
  MOUSE x%, y%, z%
  x% = x% / 16
  y% = y% / 16
   IF z% > 0 THEN
     IF (x% > (Wide% - 1)) OR (y% > (High% - 1)) THEN
      VDU7
      z% = 0
     ENDIF
   ENDIF
   IF z% = 4 THEN
     IF Type% <= 8 THEN
      Tile%(x%, y%) = Type%
     ELSE
      Tile%(x%, y%) = Tile%(x%, y%) OR Type%
     ENDIF
    PROCTile(x%, y%)
   ENDIF
   IF z% = 1 THEN
    Tile%(x%, y%) = 0
    PROCTile(x%, y%)
   ENDIF
 UNTIL Finish% OR Scroll%
UNTIL Finish%

PROCSave(Tile%())
ENDPROC

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCTile(x%, y%)
Tile% = Tile%(x%, y%)
IF Tile% = 0 THEN RECTANGLE FILL x%*16, y%*16, 14
IF Tile% AND  1 THEN OSCLI"SChoose P": PLOT &ED, x%*16, y%*16
IF Tile% AND  2 THEN OSCLI"SChoose L": PLOT &ED, x%*16, y%*16
IF Tile% AND  4 THEN OSCLI"SChoose W": PLOT &ED, x%*16, y%*16
IF Tile% AND  8 THEN OSCLI"SChoose *": PLOT &ED, x%*16+4, y%*16+4
IF Tile% AND 16 THEN OSCLI"SChoose S": PLOT &ED, x%*16+4, y%*16+4
IF Tile% AND 32 THEN OSCLI"SChoose F": PLOT &ED, x%*16+4, y%*16+4
ENDPROC

REM ˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜˜

DEF PROCDroids
OSCLI"SChoose D"
 IF Droids% > 0 THEN
   FOR Droid% = 1 TO Droids%
    PLOT &ED, DroidX%(Droid%)*16, DroidY%(Droid%)*16
   NEXT
 ENDIF
CLS
PRINT"Click left button to add a droid"
PRINT"Click right button to remove a droid"
PRINT"Press F when finished"
 REPEAT
   CASE INKEY$(0) OF
    WHEN "F","f": ENDPROC
   ENDCASE
  MOUSE x%,y%,z%
  x% = x% / 16
  y% = y% / 16
   IF z% > 0 THEN
     IF (x% > (Wide% - 1)) OR (y% > (High% - 1)) THEN
      VDU7
      z% = 0
     ENDIF
   ENDIF
   CASE z% OF
    WHEN 4:  IF Droids% = 100 THEN
              VDU7
              PRINT"Maximum number of droids reached"
              IF INKEY(200)
             ELSE
              Droids% += 1
              DroidX%(Droids%) = x%
              DroidY%(Droids%) = y%
              OSCLI"SChoose D"
              PLOT &ED, DroidX%(Droids%)*16, DroidY%(Droids%)*16
             ENDIF
            PRINT"Droid added at ";x%;", ";y%
            IF INKEY(100)
    WHEN 1: IF Droids% <> 0 THEN
            Droid% = 0
             REPEAT
              Droid% += 1
             UNTIL ((DroidX%(Droid%) = x%) AND (DroidY%(Droid%) = y%)) OR (Droid% >= Droids%)
             IF ((DroidX%(Droid%) = x%) AND (DroidY%(Droid%) = y%)) THEN
              PROCTile(x%, y%)
               FOR d% = Droid% TO Droids%-1
                DroidX%(d%) = DroidX%(d%+1)
                DroidY%(d%) = DroidY%(d%+1)
               NEXT
              Droids% -= 1
              PRINT"Droid removed"
             ELSE
              VDU7
             ENDIF
            ENDIF
   ENDCASE
 UNTIL 0
ENDPROC