
'  ___
' /   |
' |
' |
' | ADISMAN
' |
' \---|
'
' This is my game.

DECLARE SUB CHECKWORLD ()
DECLARE SUB LoadMonsters ()
DECLARE SUB NEWGAME ()
DECLARE SUB BlankUDT ()
DECLARE SUB CreateMonster (X!, Y!, Team!, Skill!, HP!, Name$, Mode!)
DECLARE SUB Deposit2 (ID%, amt AS LONG, bid%)
DECLARE SUB Weekly ()
DECLARE SUB SaveUDT (path$, wld!)
DECLARE SUB LoadUDT (path$, wld!)
DECLARE SUB LoadDlg (pname$, lmode)
DECLARE SUB Script (st$, retn!, oldname$, speaker!)
DECLARE SUB MyOwnFade (di!)
DECLARE SUB ChooseClothes ()
DECLARE SUB SyncUpdate ()
DECLARE SUB Lpalette ()
DECLARE FUNCTION rmenu! (menu$(), X!, Y!)
DECLARE SUB INITGAME ()
DECLARE FUNCTION FindSqr! (I!, g3$, g4$, uX2!, uY2!)
DECLARE SUB MainMenu (flag!)
DECLARE SUB ExitGame ()
DECLARE SUB ShowWays ()
DECLARE SUB TraceWays (Tx!, Ty!, ox!, oy!, WPT$)
DECLARE SUB HMapEditKeys ()
DECLARE SUB LoadSpr (pic$, Array!(), cfrom, cto)
DECLARE SUB DrawGuys ()
DECLARE SUB MoveGuys ()
DECLARE SUB Pause ()
DECLARE SUB MINPUT (Text$, Init$)
DECLARE SUB MSG (MSG1$)
DECLARE SUB Drawmap (mapno!, Mode!, tileX!, tileY!)
DECLARE SUB LoadMaps (file$)
DECLARE SUB LoadTiles ()
DECLARE SUB ViewTabs ()

DECLARE FUNCTION GetInvNo% (ID!)
DECLARE FUNCTION PossessSubType! (grp!, subg!, DoICare!)
DECLARE FUNCTION Possess& (ID!)
DECLARE FUNCTION GetID% (ID$)

DECLARE SUB ModeGuy (I!)
DECLARE FUNCTION GetBankNo% (ID AS ANY)
DECLARE SUB ShowBank ()
DECLARE SUB Buyland (P)
DECLARE FUNCTION WhichBar% ()
DECLARE SUB CheckTabs ()
DECLARE SUB ViewSkills ()
DECLARE FUNCTION WhichPlot! (X!, Y!)
DECLARE SUB DrawPlots (n!)
DECLARE SUB LoadPlots ()
DECLARE FUNCTION EmptySlots! ()
DECLARE SUB MiniDigit (X!, Y!, t$, C!)
DECLARE SUB Give (ID$, amt!)
DECLARE SUB LoadObs ()
DECLARE SUB DrawBar ()
DECLARE SUB CreateParticle (X!, Y!, ROT!, M!, o!)
DECLARE FUNCTION UseMenu! (mx!, my!)
DECLARE SUB ClickSqr (mx!, my!)
DECLARE FUNCTION FString$ (Offset!)
DECLARE SUB ViewInventory ()
DECLARE SUB HandleKeys ()
DECLARE SUB DoConsole (a$)


'$INCLUDE: 'cdm.bi' 'CDM
' FutureTileEngine
'$INCLUDE: 'future.bi'

'$DYNAMIC

STACK (STACK / 1.6)
DIM SHARED mapchar(-1 TO 44, -1 TO 28) AS STRING * 1

DIM SHARED WayX(200)
DIM SHARED WayY(200)
DIM SHARED WayL(200)
DIM SHARED lastgive$
DIM SHARED player(90)
'DIM SHARED playerBKG(90)
DIM SHARED pB(90)
DIM SHARED pC(90)
DIM SHARED pD(90)
DIM SHARED pE(90)
DIM SHARED FB(90)
DIM SHARED S1(90)
DIM SHARED bld(90)


maxunits = 100
DIM SHARED unit(maxunits) AS Bunit
DIM SHARED invID(36)
DIM SHARED invAmt(36) AS LONG
DIM SHARED bankID(100)
DIM SHARED bankAmt(100) AS LONG

DIM SHARED wEmployee(20)  AS EmployeeType
DIM SHARED wContractor(20) AS ContractorType
DIM SHARED dedEmp  AS EmployeeType
DIM SHARED dedCon AS ContractorType


maxobs = 155
DIM SHARED obj(maxobs) AS ObjectType
DIM SHARED Skill(14) AS SkillType
DIM SHARED plot(100) AS PlotType
DIM SHARED rockR(50) AS RockType
DIM SHARED xPl(100) AS xPlType
DIM SHARED worn(8) AS WearType
DIM SHARED monst(40) AS MonsterType

'DimShareds
DIM SHARED lbl$(40)
DIM SHARED lblX(40)
DIM SHARED lblY(40)
DIM SHARED lblM(40)
DIM SHARED lblC(40)
DIM SHARED s!(359)
DIM SHARED C!(359)

CONST pi = 3.141592654#
FOR I = 0 TO 359
   s!(I) = SIN(I * (pi / 180))
   C!(I) = COS(I * (pi / 180))
NEXT
RANDOMIZE TIMER
CLOSE
Set640x480 8
'Set800x600 8
SetMaskColor 0

Future.CLS 0

ON ERROR GOTO errordo

IF IsXMSInstalled THEN
 'MSG "XMS installed"

 handle% = AllocateXMS(320480)             '32Xx32Yx20 pix =   20 KB
 Future.DefineXMSArray 32, 32, handle%

 csets% = AllocateXMS(228850)               '15Xx15Yx39  pix =126 KB
 Future.DefineXMSArray 15, 15, csets%
 icons% = AllocateXMS(204800)             '32Xx32Yx200 pix =  200 KB
 Future.DefineXMSArray 32, 32, icons%
 mdgs% = AllocateXMS(323300)                 '5x5x41          = 1 KB
 Future.DefineXMSArray 5, 5, mdgs%
 sprs% = AllocateXMS(327250)                '15x15x23=5250    124 KB
 Future.DefineXMSArray 15, 15, sprs%
 size64x% = AllocateXMS(40970)                '64x64x10   4.8
 Future.DefineXMSArray 64, 64, size64x%


                                                      'TOTAL: 471 KB
ELSE
 MSG "XMS FAILED"
 BEEP
 SLEEP
END IF

Primary% = 0
Secondary% = 1


Future.MouseOff

FOR I = 1 TO 14
 Skill(I).Level = 1
 Skill(I).amt = 1
NEXT I

 world$ = "maps\world 1.bd3"
 tileset$ = "tileset.dat"
 dmaps = 0
 Xscroll% = 0
 Yscroll% = 0
 playerno = 1
 world = 1
 Future.CLS 0
 Lpalette
 LoadTiles
 LoadPlots
 LoadMonsters
 ViewPage Secondary%
 SetPage Secondary%
 Future.CLS 0
 SetPage Primary%
 Future.LoadBMP "data\itr.bmp", 0, 0
ViewPage 0

FOR I = 1 TO 600
 jkl$ = INKEY$
 IF jkl$ <> "" THEN
  IF jkl$ = "1" THEN startmode = 1 ELSE startmode = 0
  EXIT FOR
 END IF
NEXT I

FOR I = 1 TO 20                                        '   :D
 Palette.FadeOut
NEXT I
Future.CLS 0
currentmap = 1

' === startmode: 1= debug 0= normal ====

IF startmode = 0 THEN
   flag = 4
   MainMenu flag
   SELECT CASE flag
    CASE 0
     Drawmap currentmap, 1, 0, 0
     ' ^ this shouldnt happen
     ' if it does... :)
     MSG "menu went all wong. something's skwewed"
    CASE 1
     ' = Load Game =
     Future.CLS 0
     Lpalette
     INITGAME
     Drawmap currentmap, 1, 0, 0
    CASE 2
     Future.CLS 0
     Lpalette
     INITGAME
     Drawmap currentmap, 1, 0, 0
    CASE ELSE
     MSG "what???" + STR$(flag)
   END SELECT
END IF
CLOSE

IF startmode = 1 THEN
Lpalette
  INITGAME
  Deposit2 GetID%("gold"), 200, 0
  CreateGuy 8, 5, 1, 1, 1, 0, 0, 0, 10, 8, "player", 0 'player
  CreateGuy 9, 9, 1, 1, 25, 0, 0, 7, 1, 0, "arnold", 7  ' Arnie
  CreateGuy 35, 32, 1, 1, 25, 0, 0, 6, 1, 0, "young", 6' Lord Young

  CreateGuy 396, 247, 1, 8, 98, 0, 0, 11, 100, 0, "dragon", 0

  CreateGuy 35, 33, 1, 1, 25, 0, 0, 0, 10, 0, "guard", 11
  CreateGuy 30, 32, 1, 1, 25, 0, 0, 0, 14, 0, "soldier", 12
  CreateGuy 30, 30, 1, 1, 25, 0, 0, 0, 14, 0, "soldier", 12

  CreateGuy 211, 6, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 201, 8, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 194, 7, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 188, 5, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 183, 7, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 218, 9, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 217, 3, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19
  CreateGuy 228, 6, -1, 0, 10, 0, 0, 0, 14, 10, "dwarf", 19

  CreateGuy 38, 5, -1, 0, 25, 0, 0, 0, 5, 10, "mugger", 5
  CreateGuy 9, 56, -1, 2, 25, 0, 0, 0, 19, 10, "karn", 4
  CreateGuy 194, 212, -1, 2, 25, 0, 0, 0, 50, 10, "tharg", 4
  CreateGuy 182, 209, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
'  CreateGuy 182, 212, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
'  CreateGuy 189, 209, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
'  CreateGuy 189, 213, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
'  CreateGuy 186, 195, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
'  CreateGuy 189, 195, -1, 2, 25, 0, 0, 0, 6, 10, "orc", 4
  CreateMonster 8, 17, 2, 1, 6, "orc", 0

  CreateGuy 18, 6, -1, 1, 25, 0, 0, 0, 7, 10, "citizen", 10
  CreateGuy 9, 16, -1, 1, 25, 0, 0, 0, 7, 10, "citizen", 9
  CreateGuy 9, 19, -1, 1, 25, 0, 0, 0, 7, 10, "citizen", 8

  CreateGuy 16, 9, 1, 1, 0, 0, 0, 0, 900, 0, "trader", 2    ' trader 0 (e.g uses buy0.dat)
  CreateGuy 16, 1, 1, 1, 0, 1, 0, 0, 900, 0, "trader", 2     ' trader type 1
  CreateGuy 15, 31, 1, 1, 0, 3, 0, 0, 90, 0, "trader", 2    ' trader type 3
  CreateGuy 21, 31, 1, 1, 0, 2, 0, 0, 90, 0, "trader", 2     ' trader type 2
  CreateGuy 87, 119, 1, 1, 0, 4, 0, 0, 9, 0, "trader", 2     ' trader type 4
  CreateGuy 4, 33, 1, 1, 0, 5, 0, 0, 900, 0, "trader", 2     ' trader type 5
  CreateGuy 102, 117, 1, 1, 0, 0, 0, 0, 900, 0, "trader", 2    ' trader type 0
  CreateGuy 15, 8, 1, 1, 0, 99, 0, 0, 900, 0, "Uerdor", 3    ' trader type 99: employment

  CreateGuy 34, 19, 1, 0, 0, 0, 0, 0, 900, 0, "banker", 1
  CreateGuy 100, 111, 1, 0, 0, 0, 0, 0, 900, 0, "banker", 1
  CreateGuy 10, 14, 1, 0, 20, 0, 0, 0, 10, 0, "cow", 0
  CreateGuy 45, 39, 1, 0, 20, 0, 0, 0, 10, 0, "cow", 0
  CreateGuy 43, 35, 1, 0, 20, 0, 0, 0, 10, 0, "cow", 0

  'CreateGuy 7, 12, 1, 0, 19, 1, 0, 0, 700, 0,"",0 ' fire

  CreateGuy 8, 14, 1, 0, 22, 0, 0, 0, 10, 0, "chook", 0   ' chook
  CreateGuy 8, 34, 1, 0, 22, 0, 0, 0, 10, 0, "chook", 0   ' chook
  CreateGuy 8, 36, 1, 0, 22, 0, 0, 0, 10, 0, "chook", 0   ' chook
  CreateGuy 29, 2, 1, 0, 22, 0, 0, 0, 10, 0, "chook", 0   ' chook
  CreateGuy 46, 10, 1, 0, 22, 0, 0, 0, 10, 0, "chook", 0  ' chook

 Give "tdbx", 1
 Give "bzax", 1
 Give "bzpk", 1
 Give "hamr", 1
 Give "scyt", 1
 Give "bckt", 1
 Give "ppot", 1
 Give "fnet", 1
 Give "gold", 300

END IF


MSG "Welcome to Cadisman"
Future.MouseOn
RANDOMIZE TIMER
unit(playerno).MVT = 0
Skill(11).amt = 10
Skill(11).Level = 10
Skill(11).xp = xPl(10).xp
selu = playerno
DIM SHARED FPS AS SINGLE, startT AS LONG

startT = TIMER
DO
10
 TIME = TIME + 1 + gamespeed
 IF bAc > 0 THEN
  bAc = bAc - .004
  IF bAc > 20 THEN
   bAc = 8
   Script "run_player_toopissed", 0, "player", 1
   unit(1).HP = unit(1).HP - INT(unit(1).THP / 4)
   MSG "Don't get too drunk, you'll kill yourself"
   MSG "Go eat some food"
  END IF
  IF TIME >= sobertime THEN sobertime = 0: bAc = 0
 END IF
20
 IF TIME MOD 600 = 0 THEN
  mc% = INT(RND * 7) - 3
  market! = market! + mc%
  ff = FREEFILE
  OPEN "data\market.log" FOR APPEND AS #ff
   WRITE #ff, TIME, market!
  CLOSE ff
 END IF
21
 IF TIME MOD 2500 = 0 THEN 'WEEKLY UPDATER
  Weekly
 END IF

22
 IF TIME = 999999999 THEN
  TIME = 0: YEAR = YEAR + 1: MSG "Year:" + STR$(YEAR + 1540)
  unit(playerno).MVT = 0
  FOR I = 1 TO 100
   IF obj(I).demand > 0 THEN
    obj(I).demand = INT(obj(I).demand / 4)
   END IF
  NEXT I
 END IF
 Future.UpdateMouse
   mx% = Future.MouseX%
   my% = Future.MouseY%
   mxs = INT(mx% / 15) + 1
   mys = INT(my% / 15) + 1

 IF Mapedit = 1 THEN
  IF Future.MouseB% = 2 THEN
   'schar$ = mapchar$(mxs, mys)
    SetPage 2
    schar$ = CHR$(Future.POINT(mxs + Xscroll%, mys + Yscroll%))
    SetPage Primary%

  END IF
  IF Future.MouseB% = 1 THEN
   IF mx% < 600 THEN
    IF schar$ = "" THEN schar$ = "G"
   ' mapchar$(mxs, mys) = schar$
    SetPage 2
    Future.PSET mxs + Xscroll%, mys + Yscroll%, ASC(schar$)
    SetPage Primary%
    Drawmap currentmap, 2, mxs + Xscroll%, mys + Yscroll%
   ELSE
    FOR Y = 1 TO 6
     x2 = 604
     y2 = (Y * 36) + 200
     IF Future.MouseX% > x2 AND Future.MouseX% < x2 + 32 THEN
      IF Future.MouseY% > y2 AND Future.MouseY% < y2 + 32 THEN 'globalY(currentmap + 0), ASC(schar$)
       vuBoxState = 1
       vuBoxNo = Y
       ViewTabs
      END IF
     END IF
    NEXT Y
   END IF' if mx<600
  END IF
  HMapEditKeys

 ELSE
  IF Future.MouseB% = 1 THEN
   DO
    Future.UpdateMouse
   LOOP UNTIL Future.MouseB% = 0
   IF mx% < 600 THEN
    ClickSqr mxs, mys
   ELSE
    FOR Y = 1 TO 6
     x2 = 604
     y2 = (Y * 36) + 200
    IF Future.MouseX% > x2 AND Future.MouseX% < x2 + 32 THEN
     IF Future.MouseY% > y2 AND Future.MouseY% < y2 + 32 THEN 'globalY(currentmap + 0), ASC(schar$)
       vuBoxState = 1
       vuBoxNo = Y
       ViewTabs
     END IF
    END IF
   NEXT Y
   CHECKWORLD
  END IF
27
  END IF
  IF Future.MouseB% = 2 THEN
   DO
    Future.UpdateMouse
   LOOP UNTIL Future.MouseB% = 0
   mx% = Future.MouseX%
   my% = Future.MouseY%
   mxs = INT(mx% / 15) + 1
   mys = INT(my% / 15) + 1
   IF mx% < 600 THEN
    RClickSqr mxs, mys, 0
   END IF
  END IF


  HandleKeys
 END IF

IF Mapedit = 0 THEN
28
 DrawGuys
29
 MoveGuys
30
END IF
 'FOR i = 1 TO Nptcls
 ' RefreshParticle (i)
 'NEXT i
 
 'WAIT &H3DA, 8
 IF TabsState = 1 THEN
   SELECT CASE LastTab
    CASE 3
     ViewSkills
    CASE ELSE
     ViewInventory
   END SELECT
 END IF
LOOP
31
ReSetScreen
END

errordo:
 SLEEP
 ReSetScreen
 PRINT "ERROR in cdm."; STR$(ERR); ERL
 ERROR ERR
 unit(1).Mode = 0
RESUME 10
32

REM $STATIC
SUB Buyland (P)
    IF P = -1 THEN P = WhichPlot(unit(selu).X + 0, unit(selu).Y + 0)
    DrawPlots P
    MSG "This Plot costs" + STR$(plot(P).cost) + " GP. Buy it? (y/n)"
    MSG " "
    MINPUT yn$, ""
    IF yn$ = "y" THEN
     IF Possess(INT(GetID%("gold"))) >= plot(P).cost THEN
      cno = GetInvNo%(INT(GetID%("gold")))
      invAmt(cno) = invAmt(cno) - plot(P).cost
      IF invAmt(cno) = 0 THEN invID(cno) = 0
      plot(P).Owner = playerno
      DrawPlots P
     ELSE
      MSG "You don't have enough money"
     END IF
    END IF
END SUB

SUB ChangeSuit
'=================
SELECT CASE met$
 CASE "bron"
  sprite$ = "bron.tdt"
 CASE "iron"
  sprite$ = "iron.tdt"
 CASE "stee"
  sprite$ = "stee.tdt"
 CASE "mith"
  sprite$ = "mith.tdt"
 CASE "addy"
  sprite$ = "addy.tdt"
 CASE "rune"
  sprite$ = "rune.tdt"
 CASE ELSE
  sprite$ = "player.tdt"
END SELECT
sprite$ = "sprites\" + sprite$
SetPage 1
CLOSE
OPEN sprite$ FOR INPUT AS #1
 DO UNTIL EOF(1)
  INPUT #1, X, Y, C
  Future.PSET X, Y, C
 LOOP
CLOSE
Future.GET 0, 0, 15, 15, player()
SetPage 0
Drawmap currentmap, 1, 0, 0
END SUB

SUB CHECKWORLD
   x2 = 604
   y2 = 90
    IF Future.MouseX% > x2 AND Future.MouseX% < x2 + 32 THEN
     IF Future.MouseY% > y2 AND Future.MouseY% < y2 + 32 THEN
      IF onEdgeMap$ = "1" THEN
       '=============
       'CHANGE WORLD
       '=============
       IF unit(1).Y = 0 THEN
        'world up   N
        nw = world - 10
        ny = 249
        nx = unit(1).X
        nm = currentmap + 90
       END IF
       IF unit(1).Y = 249 THEN
        'world down S
        nw = world + 10
        ny = 0
        nx = unit(1).X
        nm = currentmap - 90
       END IF
       IF unit(1).X = 0 THEN
        'world left W
        nw = world - 1
        ny = unit(1).Y
        nx = 399
        nm = currentmap + 9
       END IF
       IF unit(1).X = 399 THEN
        'world right  E
        nw = world + 1
        ny = unit(1).Y
        nx = 0
        nm = currentmap - 9
       END IF
23
 IF pname$ = "" THEN
  Future.PRINT 20, 5, "SAVE Player before changing worlds...", 14, 0
  LoadDlg pname$, 1
  IF pname$ <> "" THEN SaveGame pname$ ELSE EXIT SUB
 ELSE
  SaveGame pname$
 END IF
24
        IF pname$ <> "" THEN
25       CLOSE : OPEN "player\" + pname$ + "\world" + STR$(nw) + ".bd3" FOR APPEND AS #1
         IF LOF(1) > 0 THEN
          CLOSE
          world = nw       ' player has saved world in player\
                           ' therefore, they also have a udt for that world
          LoadUDT "player\" + pname$ + "\", world
          world$ = "player\" + pname$ + "\world" + STR$(world) + ".bd3"
         ELSE
          world = nw         ' use default map
          world$ = "maps\world" + STR$(world) + ".bd3"
          LoadUDT "maps\", world
          SaveUDT "player\" + pname$ + "\", world'<- copy new udt into player folder
         END IF
         CLOSE
        ELSE
         world = nw         ' use default map (new player) - this wont happen
                            ' because of saving stuff. Just precautionary
         world$ = "maps\world" + STR$(world) + ".bd3"
        END IF
26      INITGAME
        'CreateGuy 99, 99, 1, 1, 1, 0, 0, 0, 10, 8, "player", 0 'player
        Xscroll% = nx - 20
        Yscroll% = ny - 12
        unit(1).X = nx
        unit(1).Y = ny
        unit(1).Rank = 1
        unit(1).THP = Skill(11).Level
        unit(1).HP = Skill(11).amt
        unit(1).Name = "player"
        currentmap = nm
        Drawmap currentmap, 1, 0, 0
      END IF
     END IF
    END IF
END SUB

SUB Choice1 (madeitem)
END SUB

SUB ChooseClothes
 Future.CLS 0
 Future.MouseOff
 IF haircol = 0 THEN
  Y = 60
  FOR I = 1 TO 200
   X = X + 20
   IF X = 600 THEN Y = Y + 20: X = 20
   Future.FILLBOX INT(X), INT(Y), X + 20, Y + 20, I
  NEXT I
  Future.PRINT 20, 10, " Choose your hair colour... ", 15, 0
  Future.PRINT 2, 30, " Choose your colours wisely; you cannnot undo your choice ", 14, 0
  Future.MouseOn
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% <> 0
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% = 0
  mx% = Future.MouseX% - 1
  my% = Future.MouseY% - 1
  haircol = Future.POINT(mx%, my%)
  Future.FILLBOX 40, 400, 80, 440, haircol
  Future.PRINT 20, 10, " Choose your shirt colour...", 15, 0
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% <> 0
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% = 0
  mx% = Future.MouseX% - 1
  my% = Future.MouseY% - 1
  shirtcol = Future.POINT(mx%, my%)
  Future.FILLBOX 80, 400, 120, 440, shirtcol
  Future.PRINT 20, 10, " Choose your pants colour...  ", 15, 0
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% <> 0
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% = 0
  mx% = Future.MouseX% - 1
  my% = Future.MouseY% - 1
  pantscol = Future.POINT(mx%, my%)
  Future.FILLBOX 120, 400, 160, 440, pantscol
 END IF
 CLOSE
 Future.MouseOff
 SetPage Secondary%
 Future.CLS 0
 OPEN "sprites\man1.spr" FOR INPUT AS #1
 DO UNTIL EOF(1)
  INPUT #1, X, Y, C
  SELECT CASE C
   CASE 35
    C = haircol
    IF C = 0 THEN C = 16
   CASE 49
    C = shirtcol
    IF C = 0 THEN C = 16
   CASE 52
    C = pantscol
    IF C = 0 THEN C = 16
   CASE 31
    C = swordcol
   CASE 59
    C = shieldcol
  END SELECT
  Future.PSET X, Y, C
 LOOP
 CLOSE
 Future.GET 0, 0, 15, 15, player()
END SUB

SUB ClickSqr (mx, my)
 IF found = 0 THEN
    IF mx > 40 THEN mx = 40
    IF my > 25 THEN my = 25
    Tx2 = mx - 1
    Ty2 = my - 1
    Tx = (mx - 1) + MapGlobalX(INT(currentmap))
    Ty = (my - 1) + MapGlobalY(INT(currentmap))
    C = Collide(INT(Tx), INT(Ty))
    G$ = CHR$(GROUND(INT(Tx), INT(Ty)))
    IF C = -1 OR G$ = "F" THEN
     RClickSqr INT(Tx2) + 1, INT(Ty2) + 1, 1
    ELSE
     unit(selu).Mode = 0
     TraceWays Tx, Ty, unit(selu).X + 0, unit(selu).Y + 0, unit(selu).WPT$
     unit(selu).Tx = Tx
     unit(selu).Ty = Ty
     IF TIME >= unit(selu).MVT THEN unit(selu).MVT = TIME
    END IF
 END IF
END SUB

SUB CreateGuy (X, Y, map, Team, Rank, Tx, Ty, TM, HP, Spd, Name$, Icon%)
 FOR I = 1 TO maxunits
  IF unit(I).HP = 0 OR unit(I).Rank = -1 THEN
   found = 1
   unit(I).X = X
   unit(I).Y = Y
   unit(I).map = WhichMap(X, Y)
   unit(I).Team = Team
   unit(I).Rank = Rank
   unit(I).Tx = Tx
   unit(I).Ty = Ty
   unit(I).TM = TM
   unit(I).HP = HP
   unit(I).THP = HP
   unit(I).Spd = Spd
   unit(I).Icon = Icon%
   unit(I).Name$ = Name$
   IF Rank > 0 THEN unit(I).Skill = 1
   IF Rank = 99 THEN unit(I).MVT = 1220
   IF Rank = 19 THEN unit(I).MVT = TIME + 15
   IF Name$ = "karn" THEN unit(I).Skill = 6
   SELECT CASE Rank
    CASE 2
     unit(I).Carry$ = "bzdg"
    CASE 3
     unit(I).Carry$ = "bzsw"
   END SELECT
   created = 1
   EXIT FOR
  END IF
 NEXT I

 'IF icon% < 1 THEN MSG "icon is! :" + STR$(icon%)
 IF created = 0 THEN MSG "Error: couldn't spawn " + Name$
END SUB

SUB CreateMonster (X, Y, Team, Skill, HP, Name$, Mode)
 FOR I = 1 TO maxunits
  IF unit(I).HP = 0 OR unit(I).Rank = -1 THEN
   found = 1
   unit(I).X = X
   unit(I).Y = Y
   unit(I).map = WhichMap(X, Y)
   unit(I).Team = Team
   unit(I).Rank = 17
   unit(I).Skill = Skill' monster type
   IF HP = 0 THEN HP = monst(Skill).HP
   unit(I).HP = HP
   unit(I).THP = HP
   unit(I).Spd = monst(Skill).Spd
   unit(I).Icon = monst(Skill).Icon
   unit(I).Name$ = Name$
   unit(I).Mode = Mode
   created = 1
   EXIT FOR
  END IF
 NEXT I

 'IF icon% < 1 THEN MSG "icon is! :" + STR$(icon%)
 IF created = 0 THEN MSG "Error: couldn't spawn " + Name$
END SUB

SUB CreateParticle (X, Y, ROT, M, o)
END SUB

SUB DoConsole (a$)
    SELECT CASE LCASE$(RIGHT$(a$, LEN(a$) - 4))
     CASE "mini"
      MINPUT t$, "Mini:"
      MSG "Press space..."
      MiniDigit 20, 20, t$, 1
      DO: LOOP UNTIL INKEY$ = " "
     CASE "script"
      MINPUT s$, "command:"
      MINPUT n$, "Name$:"
      MINPUT spk$, "Spk#:"
      Script s$, 0, n$, VAL(spk$)
     CASE "qs"
      MINPUT a$, "Qstage:"
      qstage = VAL(a$)
     CASE "talk"
      Says "arnold", 1, 2
     CASE "all4me"
      FOR I = 1 TO 12
       Skill(I).Level = 90
       Skill(I).amt = 90
      NEXT I
      Give "gold", 8000
      Give "rnbr", 6
      unit(1).HP = 90
      unit(1).THP = 90
      unit(1).Spd = 3
     CASE "spd"
      unit(1).Spd = unit(1).Spd - 2
      MSG "New speed is " + STR$(unit(1).Spd)
     CASE "bank"
      ShowBank
     CASE "xp"
      MINPUT s$, "Skill#"
      MINPUT a$, "Lvl"
      Skill(VAL(s$)).Level = VAL(a$)
      Skill(VAL(s$)).amt = VAL(a$)
     CASE "tiles"
      Future.CLS 0
      Drawmap currentmap, 1, 0, 0
     CASE "drop"
      FOR I = 36 TO 1 STEP -1
       IF invID(I) <> 0 THEN
        invID(I) = 0
        invAmt(I) = 0
        EXIT FOR
       END IF
      NEXT I
     CASE "addobj"
      MINPUT ID$, "ID#:"
      MINPUT n$, "Name:"
      MINPUT G$, "Group:"
      MINPUT sg$, "Subgroup:"
      MINPUT l$, "Level:"
      MINPUT xp$, "XP:"
      MINPUT s$, "Skill:"
      MINPUT I$, "Icon:"
      MINPUT C$, "Cost:"
      CLOSE : OPEN "data\objects.dat" FOR APPEND AS #1
       WRITE #1, ID$, n$, VAL(G$), VAL(sg$), VAL(l$), VAL(xp$), VAL(s$), VAL(I$), VAL(C$)
      CLOSE
     CASE "showways"
      ShowWays
     CASE "sys"
      MSG "Ptcls:" + STR$(Nptcls)
      MSG "Time:" + STR$(TIME)
     CASE "move"
      MINPUT a$, ""
      X = VAL(a$)
      MINPUT a$, ""
      Y = VAL(a$)
      M = WhichMap(X, Y)
      MSG STR$(M)
      unit(1).X = X
      unit(1).Y = Y
     CASE "mapedit"
      Mapedit = 1
      MSG "Map editing mode is ON"
     CASE "viewcols"
      sz = 5
      FOR C = 1 TO 255
       Future.FILLBOX 1 + (C * sz), 1, sz, (C * sz) + sz, C
      NEXT C
     CASE "viewcol"
      MINPUT a$, ""
      Future.FILLBOX 1, 1, 40, 40, VAL(a$)
     CASE "exit"
      ReSetScreen
      SYSTEM
     CASE "info"
      MSG "map# " + STR$(currentmap) + "world# " + STR$(world)
      MSG "player# " + STR$(playerno)
      MSG "qs#: " + STR$(qstage)
      MSG "mode#: " + STR$(unit(1).Mode)
      MSG "X" + STR$(unit(selu).X) + " Y" + STR$(unit(selu).Y) + " Map" + STR$(unit(selu).map)
     CASE "snap"
      Future.SNAPSHOT "fte" + STR$(INT(RND * 100) + 100) + ".bmp"
     CASE "warp"

     MSG "Warp unit"
     mpx = MapGlobalX(currentmap + 0)
     mpy = MapGlobalY(currentmap + 0)

 DO
  Future.UpdateMouse
  IF Future.MouseB% = 1 THEN
   DO
    Future.UpdateMouse
   LOOP UNTIL Future.MouseB% = 0
   mx% = Future.MouseX%
   my% = Future.MouseY%
   mxs = INT(mx% / 15) + 1
   mys = INT(my% / 15) + 1
   X1 = mxs + mpx
   Y1 = mys + mpy
   EXIT DO
  END IF
 LOOP
   MSG "done... Unit is at " + STR$(X1) + STR$(Y1)

     unit(1).X = X1
     unit(1).Y = Y1
       
     CASE "world"
      MINPUT M$, ""
      world = VAL(M$)
      world$ = "maps\world" + STR$(world) + ".bd3"
      BlankUDT
      LoadUDT "maps\", world
      LoadMaps world$
      Lpalette
      Drawmap 0, 1, 0, 0
     CASE "give"
      MINPUT ID$, "id"
      MINPUT amt$, "amt:"
      IF obj(GetID%(ID$)).group = 4 THEN
       Give ID$, VAL(amt$)
      ELSE
       FOR I = 1 TO VAL(amt$)
        Give ID$, 1
       NEXT I
      END IF
     CASE "data"
      Future.MouseOff
      ViewPage 1
      SLEEP
      ViewPage 2
      SLEEP
      ViewPage 0
      Future.MouseOn
     CASE "map"
      Future.CLS 16
      FOR mapno = 1 TO 100
        map$ = "maps\temps\map" + LTRIM$(STR$(mapno)) + ".tmp"
        CLOSE
        M = M + 40
        OPEN map$ FOR INPUT AS #1
        X = 0
        DO UNTIL EOF(1)
         X = X + 1
         INPUT #1, line$
         FOR I = 1 TO LEN(line$)
          ch$ = MID$(line$, I, 1)
          Future.PSET (I + M - 40), X + m2, ASC(ch$)
         NEXT I
       LOOP
       CLOSE
       IF RIGHT$(STR$(mapno), 1) = "0" THEN
        M = 0
        m2 = m2 + 25
       END IF
     NEXT mapno
      MSG "Press any key to hide map."
      Pause
      Drawmap currentmap, 1, 0, 0
     CASE "com"
      DO
       MINPUT a$, ""
       IF a$ = "" OR a$ = "quit" THEN EXIT DO
       a$ = "com." + a$
       DoConsole a$
      LOOP
     CASE ELSE
       MSG "Unknown com: " + LCASE$(RIGHT$(a$, LEN(a$) - 4))
    END SELECT
END SUB

SUB DrawBar
 Future.FILLBOX 600, 1, 639, 479, 67
 Future.BOX 600, 1, 639, 479, 164
 Future.FILLBOX 605, 5, 624, 24, 7
 Future.BOX 605, 5, 624, 24, 2

X = 604

 IF onEdgeMap$ = "1" THEN
  nX2 = 604
  ny2 = 90
  Future.XMSArrayPut nX2 + 1, ny2 + 1, icons%, 171
 END IF

 IF seli > 0 THEN
  IF invID(seli) > 0 THEN
   Future.XMSArrayPut 604, 195, icons%, INT(obj(invID(seli)).Icon)
  END IF
 END IF
 FOR Y = 1 TO 6
  x2 = X
  y2 = (Y * 36) + 200
  Future.XMSArrayPut x2 + 1, y2 + 1, icons%, Y + 20
 NEXT Y


END SUB

SUB DrawPlots (n)
 IF n > -1 THEN I = n: only1 = 1: GOSUB onlyone
 FOR I = 1 TO 100
onlyone:
  IF plot(I).Owner <> 0 THEN
   X1 = (plot(I).X - 1 - MapGlobalX(currentmap + 0)) * 15
   Y1 = (plot(I).Y - 1 - MapGlobalY(currentmap + 0)) * 15
   x2 = (plot(I).Xs - MapGlobalX(currentmap + 0)) * 15
   y2 = (plot(I).Ys - MapGlobalY(currentmap + 0)) * 15
   SELECT CASE plot(I).Owner
    CASE -2
     Col = 41
    CASE -1
     Col = 46
    CASE ELSE
     Col = plot(I).Owner
   END SELECT
   Future.BOX X1, Y1, x2, y2, Col
   Future.LINE X1, Y1, x2, y2, Col, -1
   Future.LINE X1, y2, x2, Y1, Col, -1
  END IF
  IF only1 = 1 THEN EXIT SUB
 NEXT I
END SUB

SUB ExitGame
   Future.CLS 0
   DeallocateXMS handle%
   DeallocateXMS icons%
   DeallocateXMS mdgs%
   DeallocateXMS sprs%
   DeallocateXMS csets%
   ReSetScreen
   END
END SUB

FUNCTION FindSqr (I, g3$, g4$, uX2, uY2)
ux = unit(I).X
uy = unit(I).Y
found = 0
IF CHR$(GROUND(ux - 1, uy)) = g3$ THEN uX2 = ux - 1: uY2 = uy: found = 1
IF CHR$(GROUND(ux + 1, uy)) = g3$ AND found = 0 THEN uX2 = ux + 1: uY2 = uy: found = 1
IF CHR$(GROUND(ux, uy - 1)) = g3$ AND found = 0 THEN uX2 = ux: uY2 = uy - 1: found = 1
IF CHR$(GROUND(ux, uy + 1)) = g3$ AND found = 0 THEN uX2 = ux: uY2 = uy + 1: found = 1
 IF found = 0 THEN
  max = 100
  FOR X = -10 TO 10
   FOR Y = -10 TO 10
    ux = unit(I).X - X
    uy = unit(I).Y - Y
    d = Dist(ux, uy, unit(I).X + 0, unit(I).Y + 0)
    g2$ = CHR$(GROUND(ux, uy))
    IF g2$ = g3$ OR g2$ = g4$ AND d < max THEN
    max = d
    uX2 = ux
    uY2 = uy
    found = 1
   END IF
  NEXT Y
 NEXT X
END IF
FindSqr = found
END FUNCTION

FUNCTION FString$ (Offset)
CLOSE
OPEN "data\strings.dat" FOR INPUT AS #1
DO UNTIL EOF(1)
 INPUT #1, n, a$
 IF n = Offset THEN
  FString$ = a$
  CLOSE
  EXIT FUNCTION
 END IF
LOOP
MSG "String not found!!!"
END FUNCTION

SUB HandleKeys
 jkl$ = INKEY$
  SELECT CASE jkl$
   CASE "f"
    FPS = TIME / (TIMER - startT)
    Future.PRINT 20, 20, "Avg. FPS:" + STR$(FPS), 15, 0
   CASE "-"
    gamespeed = gamespeed - .2
   CASE "=", "+"
    gamespeed = gamespeed + .2
   CASE "v"
    ViewPage 2
    Pause
    ViewPage 0
   CASE "b"
    Buyland -1
   CASE "p"
    DrawPlots -1
   CASE " "
    vuBoxState = 1
    vuBoxNo = 1
    ViewTabs
  CASE "h"
   Xscroll% = unit(1).X - 20
   Yscroll% = unit(1).Y - 12
   Drawmap currentmap, 1, 0, 0
  CASE "w"
   Yscroll% = Yscroll% - 1
   Drawmap currentmap, 1, 0, 0
  CASE "s"
   Yscroll% = Yscroll% + 1
   Drawmap currentmap, 1, 0, 0
  CASE "a"
   Xscroll% = Xscroll% - 1
   Drawmap currentmap, 1, 0, 0
  CASE "d"
   Xscroll% = Xscroll% + 1
   Drawmap currentmap, 1, 0, 0
  CASE "e"
   ShowWays
  CASE "l"
   CreateParticle unit(selu).X + 0, unit(selu).Y + 0, 40, 1, selu
  CASE CHR$(27)
   'ExitGame
   MainMenu flag
   Lpalette
   SELECT CASE flag
    CASE 0
     Drawmap currentmap, 1, 0, 0
    CASE 1, 2
     INITGAME
     Drawmap currentmap, 1, 0, 0
     EXIT SUB
    CASE ELSE
     MSG "what???" + STR$(flag)
   END SELECT
  CASE "q"
   Mapedit = 1
  CASE CHR$(0) + "K"
     Xscroll% = Xscroll% - 20
     currentmap = currentmap - 1
     Drawmap currentmap, 1, 0, 0
  CASE CHR$(0) + "M"
      Xscroll% = Xscroll% + 20
      currentmap = currentmap + 1
      playerX = 1
      Drawmap currentmap, 1, 0, 0
  CASE CHR$(0) + "H"
     Yscroll% = Yscroll% - 12
     currentmap = currentmap - 10
     playerY = 12 * 15
     Drawmap currentmap, 1, 0, 0
  CASE CHR$(0) + "P"
     Yscroll% = Yscroll% + 12
     currentmap = currentmap + 10
     playerY = 1
     Drawmap currentmap, 1, 0, 0
  CASE ","
   IF seli = 1 THEN seli = 36
   FOR I = seli - 1 TO 1 STEP -1
    IF invID(I) <> 0 THEN
     seli = I
     'Future.FILLBOX 540, 380, 572, 412, 8
     'Future.XMSArrayPut 540, 380, icons%, INT(obj(invID(i)).icon)
     Future.XMSArrayPut 604, 195, icons%, INT(obj(invID(I)).Icon)
     EXIT FOR
    END IF
   NEXT I
   DrawBar
  CASE "."
   IF seli = 36 THEN seli = 1
   FOR I = seli + 1 TO 36
    IF invID(I) <> 0 THEN
     seli = I
     'Future.FILLBOX 540, 380, 572, 412, 8
     'Future.XMSArrayPut 540, 380, icons%, INT(obj(invID(i)).icon)
     Future.XMSArrayPut 604, 195, icons%, INT(obj(invID(I)).Icon)
     EXIT FOR
    END IF
   NEXT I
   DrawBar
  CASE CHR$(13)
   IF unit(selu).X = 16 AND unit(selu).Y = 9 THEN
    Give "gold", obj(invID(seli)).cost * invAmt(seli)
    invID(seli) = 0
    invAmt(seli) = 0
   END IF
  CASE "i"
   DoConsole "com.info"
  CASE "v"
   ViewSkills
  CASE ";"
   IF mscroll < 19 THEN mscroll = mscroll + 1
   MSG ""
  CASE "'"
   IF mscroll > 0 THEN mscroll = mscroll - 1
   MSG ""
  CASE "`"
   MINPUT a$, ""
   IF INSTR(a$, "com.") = 1 THEN
    DoConsole a$
   END IF
  END SELECT
END SUB

SUB INITGAME
ViewPage Primary%
SetPage Primary%
Future.CLS 0
Future.LoadBMP "data\load.bmp", 0, 0
Future.PRINT 50, 300, "Loading Cadisman...", 15, 0
market! = 0
FOR I = 1 TO 20
 wContractor(I) = dedCon
 IF I <= 10 THEN
  wEmployee(I) = dedEmp
 END IF
NEXT I
Future.PRINT 50, 260, "Loading " + world$, 155, 0
SetPage Secondary%
LoadObs
LoadMaps world$
'LoadSpr "man1.spr", player()
Lpalette
SetPage Primary%
 ChooseClothes
SetPage Secondary%
LoadSpr "iron.tdt", pB(), 0, 0
LoadSpr "eman3.spr", pC(), 0, 0
LoadSpr "man2.spr", pD(), 0, 0
LoadSpr "cplt.spr", pE(), 0, 0
LoadSpr "stone1.spr", S1(), 0, 0
LoadSpr "fire_B.spr", FB(), 0, 0
LoadSpr "blood.spr", bld(), 0, 0
SetPage Primary%
ViewPage Primary%
Drawmap 1, 0, 0, 0
selu = 1
Nptcls = 0

END SUB

SUB LoadLabels
 CLOSE
 FOR I = 1 TO 50
  lbl$(I) = ""
 NEXT I
 OPEN "data\labl" + STR$(world) + ".dat" FOR APPEND AS #1
 CLOSE
 OPEN "data\labl" + STR$(world) + ".dat" FOR INPUT AS #1
 I = 0
 DO UNTIL EOF(1)
  I = I + 1
  INPUT #1, lbl$(I), lblX(I), lblY(I), lblC(I), lblM(I)
 LOOP
 CLOSE
END SUB

SUB LoadMonsters
 CLOSE
 OPEN "data\monster.dat" FOR RANDOM AS #1
 FOR z = 1 TO 40
  GET #1, , monst(z)
 NEXT z
 CLOSE
END SUB

SUB LoadPlots
 CLOSE : OPEN "data\plt" + STR$(world) + ".dat" FOR INPUT AS #1
 DO UNTIL EOF(1)
  z = z + 1
  INPUT #1, plot(z).X, plot(z).Y, plot(z).Xs, plot(z).Ys, plot(z).Owner, plot(z).cost
 LOOP
 CLOSE
END SUB

SUB Lpalette
 
     ff = FREEFILE
     OPEN "sprites\cdm.pal" FOR INPUT AS #ff
     INPUT #ff, R, G, B
     DO UNTIL EOF(ff)
      INPUT #ff, R, G, B
      OUT &H3C8, C
      OUT &H3C9, R
      OUT &H3C9, G
      OUT &H3C9, B
      C = C + 1
     LOOP
     CLOSE #ff

END SUB

SUB MyOwnFade (di)
END SUB

SUB NEWGAME

     Future.CLS 0
     Lpalette
     INITGAME
     Drawmap currentmap, 1, 0, 0

END SUB

SUB RClickSqr (mx, my, Mode)
mmx = MapGlobalX(currentmap + 0)
mmy = MapGlobalY(currentmap + 0)
mx = mx + mmx - 1
my = my + mmy - 1
REDIM menu$(10)
menu$(1) = "Cancel"
gtype = GROUND(mx, my)

'MSG STR$(mx) + STR$(my)

Guy2 = GuyAtSqr(mx, my)
SELECT CASE unit(selu).Rank
CASE 1
  IF Guy2 > 0 AND Guy2 <> selu THEN
   SELECT CASE unit(Guy2).Icon
    CASE 1
     menu$(2) = "Bank"
    CASE 2
     menu$(2) = "Trade"
    CASE 3
     menu$(2) = "Meet"
    CASE ELSE
     IF unit(Guy2).Team = unit(1).Team THEN menu$(2) = "Order" ELSE menu$(2) = "Attack"
     IF unit(Guy2).HP > 0 THEN
     SELECT CASE unit(Guy2).Rank
      CASE 2 TO 4, 30 TO 40
       menu$(2) = "Attack"
      CASE 19
       menu$(2) = "Cook"
      CASE 20
       ' kilkow
       menu$(2) = "Kill"
       menu$(3) = "Milk"
      CASE 22
       ' kill chook
       menu$(2) = "Kill"
      CASE 21
       'sheep (one day)
      CASE 25
       menu$(2) = "Talk"
     END SELECT
     END IF
   END SELECT
  ELSE
   menu$(2) = "Move"
  END IF
  wp = WhichPlot(mx + 1, my + 1)
  owned = plot(wp).Owner
   IF owned = 0 OR owned = -1 OR owned = playerno THEN
  SELECT CASE CHR$(gtype)
   CASE "t", "{", "i"
    menu$(3) = "Chop down"
   CASE "b"
    menu$(3) = "Destroy"
   CASE "v"
    menu$(3) = "Destroy"
   CASE "F"
    menu$(3) = "Harvest"
   CASE CHR$(164)
    menu$(3) = "Shape"
   CASE CHR$(165)
    menu$(3) = "Mine"
   CASE CHR$(166)
    menu$(3) = "Bake"
   CASE "r", CHR$(200) TO CHR$(207)
    menu$(3) = "Mine"
   CASE "q"
    menu$(3) = "Quarry"
   CASE "j"
    menu$(3) = "Smelt"
   CASE "g"
    menu$(3) = "Mill"
   CASE "w"
    menu$(3) = "Fill Bucket"
   CASE "a"
    menu$(3) = "Forge Metal"
   CASE "S"
    menu$(3) = "Read"
   CASE "y"
    menu$(3) = "Net"
  END SELECT
   IF owned = -1 THEN
    ' pay 0.04% fee to king
    payfee% = CINT(plot(wp).cost * .004) + 1
    ' cap fee at 20 GP
    IF payfee% > 20 THEN payfee% = 20
   ELSE
    payfee% = 0
   END IF
   ELSE
    IF owned = -2 THEN
     MSG "That land is for sale for" + STR$(plot(wp).cost) + " GP"
     menu$(3) = "Buy"
    ELSE
     MSG "You do not own that land"
    END IF
   END IF
  FOR I = 1 TO 10
   IF menu$(I) = "" THEN
    IF menu$(I) <> "" THEN I = I + 1
    IF owned = playerno THEN menu$(I) = "Build"
    EXIT FOR
   END IF
  NEXT I

  IF Mode = 0 THEN
   sel = rmenu(menu$(), mx - mmx, my - mmy)  ' <-- RIGHT CLICK MENU
  ELSE
   IF menu$(3) <> "" THEN
    sel = 3
   ELSE
    sel = 2
   END IF
  END IF
  SELECT CASE sel
   CASE 2
    payfee% = 0
    IF Guy2 > 0 THEN
     SELECT CASE unit(Guy2).Icon
      CASE 1
       unit(selu).Mode = 11
      CASE 2
       unit(selu).Mode = 4
      CASE 3
       unit(selu).Mode = 22
      CASE ELSE
       IF menu$(2) = "Order" THEN
        'FRIENDLY DONT SHOOT!
        unit(selu).Mode = 7
       ELSE
        '"Attack"
        unit(selu).Mode = 7
        IF unit(Guy2).Rank >= 20 AND unit(Guy2).Rank < 25 THEN unit(selu).Mode = 10
        IF unit(Guy2).Rank = 19 THEN unit(selu).Mode = 14
       END IF
      END SELECT
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).TM = Guy2
     unit(selu).MVT = TIME
    ELSE
     unit(selu).Mode = 7
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).TM = 0
     unit(selu).MVT = TIME
    END IF
   CASE 3
    IF CHR$(gtype) = "b" OR CHR$(gtype) = "l" OR CHR$(gtype) = "v" THEN unit(selu).Mode = 8
    IF CHR$(gtype) = "t" OR CHR$(gtype) = "{" THEN unit(selu).Mode = 2
    IF CHR$(gtype) = "r" OR (CHR$(gtype) > CHR$(199) AND CHR$(gtype) < CHR$(208)) THEN unit(selu).Mode = 3
    IF CHR$(gtype) = CHR$(164) THEN unit(selu).Mode = 19
    IF CHR$(gtype) = CHR$(165) THEN unit(selu).Mode = 18
    IF CHR$(gtype) = CHR$(166) THEN unit(selu).Mode = 20
    IF CHR$(gtype) = "q" THEN unit(selu).Mode = 5
    IF CHR$(gtype) = "F" THEN unit(selu).Mode = 6
    IF CHR$(gtype) = "j" THEN unit(selu).Mode = 9
    IF CHR$(gtype) = "g" THEN unit(selu).Mode = 12
    IF CHR$(gtype) = "w" THEN unit(selu).Mode = 13
    IF CHR$(gtype) = "a" THEN unit(selu).Mode = 15
    IF CHR$(gtype) = "S" THEN unit(selu).Mode = 16
    IF CHR$(gtype) = "y" THEN unit(selu).Mode = 17
    unit(selu).TM = currentmap
    IF menu$(3) = "Buy" THEN
     Buyland (wp)
     unit(selu).Mode = 0
    END IF
    IF menu$(3) = "Milk" THEN
     unit(selu).Mode = 14
     unit(selu).TM = Guy2
    END IF
    unit(selu).Tx = mx
    unit(selu).Ty = my
    unit(selu).MVT = TIME
  END SELECT

   IF menu$(sel) = "Train" THEN
    unit(selu).Mode = 4
    unit(selu).Tx = unit(Guy2).X
    unit(selu).Ty = unit(Guy2).Y
    unit(selu).TM = Guy2
    unit(selu).MVT = TIME + 30
   END IF
 
  IF menu$(sel) = "Build" THEN
   SELECT CASE UseMenu(mx - mmx, my - mmy)
    CASE 2
     unit(selu).Mode = 1
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).MVT = TIME
     unit(selu).Carry$ = "b"
    CASE 3
     unit(selu).Mode = 1
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).MVT = TIME
     unit(selu).Carry$ = "l"
    CASE 4
     unit(selu).Mode = 1
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).MVT = TIME
     unit(selu).Carry$ = "/"
    CASE 5
     unit(selu).Mode = 1
     unit(selu).Tx = mx
     unit(selu).Ty = my
     unit(selu).MVT = TIME
     unit(selu).Carry$ = "F"
   END SELECT
  END IF
END SELECT

Tx = unit(selu).Tx
Ty = unit(selu).Ty
TraceWays Tx, Ty, unit(selu).X + 0, unit(selu).Y + 0, unit(selu).WPT$

 Future.MouseOff
 Drawmap currentmap, 1, 0, 0
 Future.MouseOn

END SUB

SUB RefreshParticle (P)
END SUB

FUNCTION rmenu (menu$(), X, Y)
 Future.MouseOff
 xp = X * 15
 yp = Y * 15
 FOR I = 1 TO 10
  IF menu$(I) <> "" THEN
   mm = I
   Future.FILLBOX xp, yp + (16 * I), xp + 120, yp + (16 * I) + 16, 8
   Future.BOX xp, yp + (16 * I), xp + 120, yp + (16 * I) + 16, 7
   Future.PRINT xp + 2, yp + (16 * I), menu$(I), 15, -1
  END IF
 NEXT I
 Future.MouseOn
 DO
  Future.UpdateMouse
 LOOP UNTIL Future.MouseB% = 0
 DO
  Future.UpdateMouse
  IF Future.MouseX% > xp AND Future.MouseX% < xp + 120 THEN
   IF Future.MouseY% > yp + 16 AND Future.MouseY% < yp + (16 * mm) + 16 THEN
    ym = INT((Future.MouseY% - yp + 16) / 16) - 1
    IF menu$(ym) <> "" THEN
     rmenu = ym
     FOR I = 1 TO 10
       IF I = ym THEN C = 14 ELSE C = 15
       Future.MouseOff
       Future.PRINT xp + 2, yp + (16 * I), menu$(I), C + 0, -1
       Future.MouseOn
     NEXT I
    END IF
   END IF
  END IF
 LOOP UNTIL Future.MouseB% = 1 OR INKEY$ <> ""
END FUNCTION

FUNCTION UseMenu (mx, my)
REDIM menu$(10)
menu$(1) = "Cancel"
I = 2
iv$ = unit(selu).Carry$
menu$(I) = "Brick Wall": I = I + 1
menu$(I) = "Wood Wall": I = I + 1
menu$(I) = "Wood Floor": I = I + 1
menu$(I) = "Farm": I = I + 1
UseMenu = rmenu(menu$(), mx, my)
END FUNCTION

FUNCTION WhichBar%
 X = 140
 Y = 140
 nb = 8
 Future.FILLBOX X + 1, Y - 18, X + ((nb + 2) * 36), Y + 40, 7
 Future.BOX X + 1, Y - 18, X + ((nb + 2) * 36), Y + 40, 15
 Future.PRINT X + 8, Y - 9, "Choose bar:", 15, -1
 FOR X = 1 TO nb
  x2 = X * 36 + 140
  y2 = Y
  Future.XMSArrayPut x2 + 1, y2 + 1, icons%, X + 35
 NEXT X
 DO
  Future.UpdateMouse
 IF Future.MouseB% = 1 THEN
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% = 0
   FOR X = 1 TO nb
    x2 = X * 36 + 140
    y2 = Y
    IF Future.MouseX% > x2 AND Future.MouseX% < x2 + 32 THEN
     IF Future.MouseY% > y2 AND Future.MouseY% < y2 + 32 THEN
      WhichBar% = X
      Drawmap currentmap, 1, 0, 0
      unit(selu).Mode = 0
      EXIT FUNCTION
     END IF
    END IF
   NEXT X
 END IF
 jkl$ = INKEY$
 SELECT CASE jkl$
  CASE CHR$(27)
   Drawmap currentmap, 1, 0, 0
   WhichBar% = 0
   unit(selu).Mode = 0
   EXIT FUNCTION
 END SELECT
LOOP

END FUNCTION

FUNCTION WhichPlot (X, Y)
 FOR I = 1 TO 100
  X1 = plot(I).X: x2 = plot(I).Xs
  Y1 = plot(I).Y: y2 = plot(I).Ys
  IF plot(I).Owner <> 0 THEN
   IF X <= x2 AND Y <= y2 THEN
    IF X >= X1 AND Y >= Y1 THEN
     WhichPlot = I
     EXIT FOR
    END IF
   END IF
  END IF
 NEXT I
END FUNCTION

