DECLARE SUB OrderUnit (guy%)
DECLARE FUNCTION GetContract% (eSkill%, eLevel%, MakeID%, pay&, payID%, ipw%)
DECLARE FUNCTION GetMakeID% (eSkill%, eLevel%, index%, avgipw%)
DECLARE SUB NEWGAME ()

DECLARE SUB EmployMenu (city$)
DECLARE SUB Deposit2 (ID%, amt AS LONG, bid%)
DECLARE FUNCTION EmptySlotsB! ()
DECLARE FUNCTION BankTake% (ID!, amt AS ANY)
DECLARE SUB MiniDigit (X!, Y!, t$, C!)
DECLARE SUB Trade (deal, shopno)
DECLARE SUB ShowBank
DECLARE SUB ForgeBar (bar$, UsedWhat$, xpb)
DECLARE SUB AddRock (X, Y, r$, RT)
DECLARE FUNCTION AINPUT$ (prompt$, Init$)
DECLARE FUNCTION EmptySlots! ()
DECLARE FUNCTION EmptySlotsB! ()
DECLARE FUNCTION GetBankNo% (ID!)
DECLARE FUNCTION GetID% (ID$)
DECLARE FUNCTION GetInvNo% (ID!)
DECLARE SUB Mix (mixtype!, idA$, idB$, RtnAmt!)
DECLARE FUNCTION Possess& (ID!)
DECLARE FUNCTION PossessB& (ID!)
DECLARE FUNCTION PossessSubType! (grp!, subg!, DoICare!)

DECLARE SUB AIonAI (U1!, U2!)
DECLARE SUB BloodScreen ()
DECLARE SUB CheckLevels ()
DECLARE SUB Fight (U1!, U2!)
DECLARE SUB FightAI (U1!, U2!)
DECLARE SUB ReadSign (mx!, my!)
DECLARE SUB RefreshInv ()
DECLARE FUNCTION YesNo% (MSGT$, yes$, no$)
'$INCLUDE: 'cdm.bi' 'OBS
' FutureTileEngine
'$INCLUDE: 'future.bi'

SUB AIonAI (U1, U2)
 '-* u1 is attacking. u1 is ai.
 '-* u2 is defending. u2 is also ai.

 strength = unit(U1).Rank + unit(U1).Skill
 Dfc = unit(U2).Rank + unit(U2).Skill
 mag = CINT((strength - Dfc) / 3) + INT(RND * 3)

  unit(U2).HP = unit(U2).HP - mag
  IF unit(U2).HP <= 0 THEN
   unit(U2).HP = 0
   unit(U2).Rank = 0
   unit(U2).X = -2
   unit(U2).Y = -2
   unit(U2).Team = 0
   unit(U2).Name$ = ""
   unit(U1).Mode = 31
  END IF

  ux = unit(U2).X - MapGlobalX(unit(U2).map)
  uy = unit(U2).Y - MapGlobalY(unit(U2).map)
  IF mag > 0 THEN
   Future.PUT ux * 15, uy * 15 + 5, bld()
   'Future.BOX ux * 15, uy * 15 + 1, ux * 15 + 14, uy * 15 + 14, 9
   Future.FILLBOX ux * 15 + 1, uy * 15, ux * 15 + 14, uy * 15 + 1, 4
   Future.FILLBOX ux * 15 + ((unit(U2).HP / unit(U2).THP) * 14), uy * 15, ux * 15, uy * 15 + 1, 2
  END IF
  IF unit(U2).HP <= 0 THEN
   Drawmap currentmap, 1, 0, 0
  END IF
 

END SUB

FUNCTION BankTake% (ID, amt AS LONG)
FOR i = 1 TO 100
 IF BankID(i) = ID THEN
  IF BankAmt(i) >= amt THEN
   BankAmt(i) = BankAmt(i) - amt
   IF BankAmt(i) = 0 THEN BankID(i) = 0
   BankTake% = amt
  ELSE
   BankTake% = 0
  END IF
  EXIT FOR
 END IF
NEXT i
END FUNCTION

SUB BlankUDT
 FOR i = 1 TO maxunits
  unit(i).HP = 0
  unit(i).Rank = -1
  unit(i).X = 0
  unit(i).Y = 0
 NEXT i
END SUB

SUB BloodScreen
 'Future.FILLBOX 0, 0, 640, 7, 4
FOR z = 1 TO 20
 FOR p = 1 TO 5000: NEXT p
 FOR i = 1 TO 255
  OUT &H3C8, i
  r = INP(&H3C9)
  g = INP(&H3C9)
  b = INP(&H3C9)
  'r = r + .1
  g = g - 9
  b = b - 9

 IF limit = 0 THEN
  IF r < 0 THEN r = 0
  IF g < 0 THEN g = 0
  IF b < 0 THEN b = 0
  IF r > 43 THEN r = 43
  IF g > 63 THEN g = 63
  IF b > 63 THEN b = 63
 END IF
  OUT &H3C8, i
  OUT &H3C9, r
  OUT &H3C9, g
  OUT &H3C9, b
 NEXT i
NEXT z

FOR p = 1 TO 15000: NEXT p

FOR z = 1 TO 50
 FOR p = 1 TO 5000: NEXT p
 FOR i = 1 TO 255
  OUT &H3C8, i
  r = INP(&H3C9)
  g = INP(&H3C9)
  b = INP(&H3C9)
  r = r - 1
  g = g - 9
  b = b - 9

 IF limit = 0 THEN
  IF r < 0 THEN r = 0
  IF g < 0 THEN g = 0
  IF b < 0 THEN b = 0
  IF r > 43 THEN r = 43
  IF g > 63 THEN g = 63
  IF b > 63 THEN b = 63
 END IF
  OUT &H3C8, i
  OUT &H3C9, r
  OUT &H3C9, g
  OUT &H3C9, b
 NEXT i
NEXT z

END SUB

SUB CheckLevels
FOR i = 1 TO 12
 startl = Skill(i).Level
 IF Skill(i).XP > xPl(startl + 1).XP THEN
  Skill(i).Level = Skill(i).Level + 1
  Skill(i).amt = Skill(i).amt + 1
 END IF
 diff = Skill(i).Level - startl
 IF diff <> 0 THEN
  MSG "You are now level " + STR$(Skill(i).Level) + " " + RTRIM$(LTRIM$(Skill(i).sname$))
  IF i = 11 THEN
   ' Rise THP and HP 1
   unit(1).HP = unit(1).HP + 1
   unit(1).THP = unit(1).THP + 1
  END IF
 END IF
NEXT i
END SUB

DEFINT A-Z
SUB EmployMenu (city$)
REDIM Types$(12)
REDIM TypesS(12)
REDIM TypesA(12)

REDIM con$(10)
REDIM conT(10)
REDIM conS(10)
REDIM conP(10)

REDIM EmpT(20)
REDIM EmpP(20)
DIM c1 AS LONG, c2 AS LONG
Types$(2) = "Miner": TypesS(1) = 2: TypesA(1) = 30
Types$(3) = "Smith": TypesS(2) = 3: TypesA(2) = 25
Types$(4) = "Chef": TypesS(3) = 4: TypesA(3) = 10
Types$(10) = "Farmer": TypesS(4) = 10: TypesA(4) = 25
Types$(12) = "Craftsman": TypesS(5) = 12: TypesA(5) = 10
NempS = INT(RND * 20) + 1
NconS = INT(RND * 10) + 1

FOR i = 1 TO NempS
 rna = INT(RND * 100) + 1
 sofar = 0
 FOR p = 1 TO 5
  sofar = sofar + TypesA(p)
  IF rna <= sofar THEN net = p: EXIT FOR
 NEXT p
 EmpT(i) = TypesS(net)
 EmpP(i) = 4 + INT(RND * 4)
NEXT i

FOR i = 1 TO NconS
 rna = INT(RND * 100) + 1
 sofar = 0
 FOR p = 1 TO 5
  sofar = sofar + TypesA(p)
  IF rna <= sofar THEN net = p: EXIT FOR
 NEXT p
 conT(i) = TypesS(net)
 conS(i) = INT(RND * 100) + 1
 IF conS(i) < 20 THEN conS(i) = INT(RND * 10) + 20
 conP(i) = (conS(i) * (100 - INT(RND * 14))) * (1 + (market! / 100)) + INT(RND * 120)
 fsn = INT(RND * 20) + 1
 lsn = INT(RND * 20) + 1
 CLOSE : OPEN "data\fname.dat" FOR INPUT AS #1
 k = 0
 DO UNTIL EOF(1)
  k = k + 1
  INPUT #1, n$
  IF k = fsn THEN con$(i) = n$
 LOOP
 CLOSE : OPEN "data\lname.dat" FOR INPUT AS #1
 k = 0
 DO UNTIL EOF(1)
  k = k + 1
  INPUT #1, n$
  IF k = lsn THEN con$(i) = con$(i) + " " + n$
 LOOP
NEXT i
CLOSE

SelRow = 1
rdVE:
Future.MouseOFF
X1 = 33
Y1 = 16
X2 = 510
Y2 = 328
Future.FILLBOX X1, Y1, X2, Y2, 8
Future.BOX X1, Y1, X2, Y2, 7
Future.PRINT X1 + 40, Y1 + 1, " People Available to be Employed", 12, -1

 
 Future.PRINT X1 + 10, Y1 + 15, " Employees                       Contract", 14, -1
 FOR i = 1 TO 20
  'Future.PRINT x1 + 25, y1 + 20 + (i * 17), UCASE$(RTRIM$(wEmployee(i).BWT.TypeN$)), 15, -1
  'Future.PRINT x1 + 225, y1 + 20 + (i * 17), UCASE$(RTRIM$(wContractor(i).Name$)), 15, -1
  IF EmpT(i) > 0 THEN
   IF EmpsORCons = 0 AND SelRow = i THEN
    c1 = 14: c2 = 0
    Future.PRINT X1 + 25, Y1 + 20 + (i * 16), (Types$(EmpT(i))) + " PAY:" + STR$(EmpP(i)), c1, c2
   ELSE
    c1 = 15: c2 = -1
   END IF
   Future.PRINT X1 + 25, Y1 + 20 + (i * 16), (Types$(EmpT(i))) + " PAY:" + STR$(EmpP(i)), c1, c2
  END IF
 NEXT i
 FOR i = 1 TO 10
  IF conT(i) > 0 THEN
   IF EmpsORCons = 1 AND SelRow = i THEN
    c1 = 14: c2 = 0
    Future.PRINT X1 + 225, Y2 - 20, "Lvl:" + STR$(conS(i)) + " " + (Types$(conT(i))) + " Fee:" + STR$(conP(i)) + " GP", 12, 0
   ELSE
    c1 = 15: c2 = -1
   END IF
   Future.PRINT X1 + 225, Y1 + 20 + (i * 16), (Types$(conT(i))) + ": " + con$(i), c1, c2
  END IF
 NEXT i


Future.MouseOn
DO
 Future.UpdateMouse
 mx% = Future.MouseX%
 my% = Future.MouseY%
 IF Future.MouseB% = -1 THEN
  DO: Future.UpdateMouse: LOOP UNTIL Future.MouseB% = 0
 END IF
 jkl$ = INKEY$
 SELECT CASE jkl$
  CASE CHR$(13)
  IF EmpsORCons = 1 THEN
   FOR n = 1 TO 20
    IF wContractor(n).BWT.ID = 0 THEN
     IF Possess(GetID%("gold") + 0) < conP(SelRow) THEN
      MSG "You can't afford the hire fee."
      EXIT FOR
     END IF
     i = n
     wContractor(i).Name$ = con$(SelRow)
     deal = GetContract%(INT(conT(SelRow)), conS(SelRow), MakeID, pay&, payID, ipw)
     IF deal = 1 THEN
      IF Possess(GetID%("gold") + 0) >= conP(SelRow) THEN
       g = GetInvNo%(GetID%("gold") + 0)
       invAmt(g) = invAmt(g) - conP(SelRow)
       RefreshInv
      ELSE
       MSG "You can't afford the hire fee." ' Never happen
       EXIT FOR
      END IF

      wContractor(i).BWT.ID = 1
      wContractor(i).BWT.PayAmt = pay&
      wContractor(i).payID = payID
      wContractor(i).BWT.ipw = ipw
      wContractor(i).BWT.eSkill = conT(SelRow)
      wContractor(i).BWT.city$ = city$
      wContractor(i).BWT.TypeN$ = Types$(conT(SelRow))
      wContractor(i).BWT.MakeID = MakeID
      a = YesNo%(con$(SelRow) + " is making #" + STR$(MakeID) + " at " + city$, "OK", "OK")
     END IF
     EXIT FOR
    END IF
   NEXT n
  END IF
   GOSUB rdVE
  CASE "a"
   i = VAL(AINPUT$("Contractor #:", "1"))
   wContractor(i).Name$ = AINPUT$("Name:", "Hilbo Dang" + STR$(INT(RND * 300) + 300))
   wContractor(i).BWT.ID = 1
   wContractor(i).BWT.PayAmt = 3
   wContractor(i).payID = GetID%("gold")
   wContractor(i).BWT.ipw = 3
   wContractor(i).BWT.city$ = city$
   gi = GetID%(AINPUT$("ID$:", "cpor"))
   wContractor(i).BWT.MakeID = gi
   a = YesNo%("He is making #" + STR$(gi) + " at " + city$, "OK", "OK")
   GOSUB rdVE
  CASE "s"
   i = VAL(AINPUT$("Employee #:", "1"))
   wEmployee(i).Number = wEmployee(i).Number + 1
   wEmployee(i).BWT.ID = 1
   wEmployee(i).BWT.PayAmt = 2
   wEmployee(i).BWT.ipw = 4
   wEmployee(i).BWT.city$ = city$
   gi = GetID%(AINPUT$("ID$:", "bzax"))
   wEmployee(i).BWT.MakeID = gi
   a = YesNo%("He is making #" + STR$(gi) + " at " + city$, "OK", "OK")
   GOSUB rdVE
  CASE CHR$(0) + "H"
   IF EmpsORCons = 0 THEN
    IF SelRow > 1 THEN SelRow = SelRow - 1 ELSE SelRow = NempS
   ELSE
    IF SelRow > 1 THEN SelRow = SelRow - 1 ELSE SelRow = NconS
   END IF
   GOSUB rdVE
  CASE CHR$(0) + "P"
   IF EmpsORCons = 0 THEN
    IF SelRow < NempS THEN SelRow = SelRow + 1 ELSE SelRow = 1
   ELSE
    IF SelRow < NconS THEN SelRow = SelRow + 1 ELSE SelRow = 1
   END IF
   GOSUB rdVE
  CASE CHR$(0) + "K"
   IF EmpsORCons = 0 THEN
    EmpsORCons = 1
    IF SelRow > NconS THEN SelRow = NconS
   ELSE
    EmpsORCons = 0
    IF SelRow > NempS THEN SelRow = NempS
   END IF
   GOSUB rdVE
  CASE CHR$(0) + "M"
   IF EmpsORCons = 0 THEN
    EmpsORCons = 1
    IF SelRow > NconS THEN SelRow = NconS
   ELSE
    EmpsORCons = 0
    IF SelRow > NempS THEN SelRow = NempS
   END IF
   GOSUB rdVE
  CASE CHR$(27)
   EXIT DO
 END SELECT
LOOP

END SUB

DEFSNG A-Z
SUB Fight (U1, U2)
 '-* u1 is attacking. u1 is always the player.
 '-* u2 is defending

  
 monster = unit(U2).Skill
 Dfc = monst(monster).Dfc
 Atk = Skill(5).amt
 strength = Skill(6).amt - INT(Dfc / 4)
 
  SELECT CASE style
   CASE 0     ' aggressive
    Atk = Atk + INT(Skill(5).amt / 4) + 1
   CASE 1     ' strong
    strength = strength + INT(Skill(6).amt / 4) + 1
   CASE 2     ' defensive
    Atk = Atk + INT(Skill(5).amt / 4) + 1
    strength = strength - INT(Skill(6).amt / 6)
  END SELECT

 '==================================
  wep = worn(4).ID' Weapon bonus stuff - makeshift only
  IF wep > 0 THEN strength = strength + obj(wep).Level
  mag = CINT(strength / 5) + INT(RND * 2)


 acc = INT(RND * (Atk * 1.4)) + 1
 IF acc > Dfc THEN
  IF unit(U2).Rank <> 4 THEN
   unit(U2).HP = unit(U2).HP - mag
   IF unit(U2).HP <= 0 THEN
    IF unit(U2).Rank = 17 THEN
     drop = INT(RND * 100)
     IF drop < 60 THEN
      amt = monst(unit(U2).Skill).Atk * 2 + INT(RND * 10)
      Give monst(unit(U2).Skill).Drop1, amt
     ELSE
      Give monst(unit(U2).Skill).Drop2, 1
     END IF
    END IF
    unit(U2).HP = 0
    unit(U2).Mode = 0
    unit(U2).MVT = TIME + 4000
   END IF
  ELSE
   mag = 1
  END IF
  IF mag > 0 THEN
   MSG " You hit a" + STR$(mag)
  ELSE
   MSG " Your attack was blocked"
  END IF


  SELECT CASE style
   CASE 0     ' aggressive                  '-------
    Skill(5).XP = Skill(5).XP + (5 * mag)  '  change this depending on which
    Skill(6).XP = Skill(6).XP + (3 * mag)  '  attack style you're using
                                         '-------
   CASE 1     ' strong
    Skill(5).XP = Skill(5).XP + (3 * mag)  '  change this depending on which
    Skill(6).XP = Skill(6).XP + (5 * mag)  '  attack style you're using
                                         '-------
   CASE 2     ' defensive
    Skill(7).XP = Skill(7).XP + (6 * mag)  '  change this depending on which
    Skill(6).XP = Skill(6).XP + (2 * mag)  '  attack style you're using
                                         '-------
  END SELECT

  ux = unit(U2).X - MapGlobalX(unit(U2).map)
  uy = unit(U2).Y - MapGlobalY(unit(U2).map)
    X! = ux * 15 + 3
    Y! = uy * 15 + 3
  IF mag > 0 THEN
   IF unit(U2).Rank <> 4 THEN
    Future.PUT ux * 15, uy * 15 + 5, bld()
    'Future.BOX ux * 15, uy * 15 + 1, ux * 15 + 14, uy * 15 + 14, 9
    Future.FILLBOX ux * 15 + 1, uy * 15, ux * 15 + 14, uy * 15 + 1, 4
    Future.FILLBOX ux * 15 + ((unit(U2).HP / unit(U2).THP) * 14), uy * 15, ux * 15, uy * 15 + 1, 2
   END IF
  END IF
  IF unit(U2).HP = 0 THEN
   Drawmap currentmap, 1, 0, 0
  END IF

 ELSE
  MSG " Your attack missed "' + STR$(acc) + " Dfc:" + STR$(dfc)
 END IF
 CheckLevels
END SUB

SUB FightAI (U1, U2)
 '-* u1 is attacking. u1 is ai.
 '-* u2 is defending
 monster = unit(U1).Skill
 Dfc = monst(monster).Dfc

 strength = monst(monster).Str
 Atk = monst(monster).Atk
 Dfc = Skill(7).amt
 mag = CINT(strength / 6) + INT(RND * 2)


 IF INT(RND * Atk) + 4 > Dfc THEN
  MSG " You lose " + STR$(mag) + " health."
  unit(U2).HP = unit(U2).HP - mag
  Skill(11).XP = Skill(11).XP + (mag * 7)
  CheckLevels
  Skill(11).amt = Skill(11).amt - mag
  IF unit(U2).HP <= 0 THEN
   unit(U2).HP = 0
   MSG "YOU DIED"
   BloodScreen
   SLEEP
   flag = 4
   MainMenu flag
   IF flag = 2 OR flag = 1 THEN
    NEWGAME
   END IF
  END IF
   
  

  ux = unit(U2).X - MapGlobalX(unit(U2).map)
  uy = unit(U2).Y - MapGlobalY(unit(U2).map)
  IF mag > 0 THEN
   Future.PUT ux * 15, uy * 15 + 5, bld()
   'Future.BOX ux * 15, uy * 15 + 1, ux * 15 + 14, uy * 15 + 14, 9
   Future.FILLBOX ux * 15 + 1, uy * 15, ux * 15 + 14, uy * 15 + 1, 4
   Future.FILLBOX ux * 15 + ((unit(U2).HP / unit(U2).THP) * 14), uy * 15, ux * 15, uy * 15 + 1, 2
  END IF
  IF unit(U2).HP = 0 THEN
   Drawmap currentmap, 1, 0, 0
  END IF
 END IF
 CheckLevels

END SUB

DEFINT A-Z
FUNCTION GetContract% (eSkill, eLevel, MakeID, pay&, payID, ipw)
 payID = GetID%("gold")
 MakeID = GetMakeID%(eSkill, eLevel, 1, avgipw) '/  get first makeID from file
 
 cindex = 1
 ipw = avgipw
rdGMI:
 pay& = ((obj(MakeID).Cost + eSkill) * ipw) + ((INT(RND * 20) / 100) * obj(MakeID).Cost)
 Future.MouseOFF
 X1 = 80
 Y1 = 60
 X2 = 430
 Y2 = 260
 s = 8
 Future.FILLBOX X1 - s, Y1 + s, X2 - s, Y2 + s, 0
 Future.FILLBOX X1, Y1, X2, Y2, 7
 Future.BOX X1, Y1, X2, Y2, 8
 Future.PRINT X1 + 20, Y1 + 1, " Contract:" + RTRIM$(Skill(eSkill).sname$), 10, -1
 Future.PRINT X1 + 20, Y1 + 40, " Pay this man" + STR$(pay&) + " " + RTRIM$(obj(payID).oname$) + " per week", 8, -1
 Future.PRINT X1 + 20, Y1 + 57, " to make " + STR$(ipw) + " " + RTRIM$(obj(MakeID).oname$) + " per week", 8, -1
 Future.PRINT X1 + 20, Y1 + 77, " PAY ", 12, -1
 Future.PRINT X1 + 120, Y1 + 77, " MAKE ", 12, -1
 Future.PRINT X1 + 20, Y2 - 20, " OFFER ", 15, 8
 Future.PRINT X1 + 180, Y2 - 20, " CANCEL ", 15, 8
 Future.XMSarrayPUT X1 + 22, Y1 + 93, icons%, obj(payID).Icon
 Future.XMSarrayPUT X1 + 122, Y1 + 93, icons%, obj(MakeID).Icon
Future.MouseOn
DO
 Future.UpdateMouse
 mx% = Future.MouseX%
 my% = Future.MouseY%
 Future.PRINT 12, 12, "  " + STR$(mx%) + STR$(my%) + "  ", 15, 8
 IF Future.MouseB% = 1 THEN
  DO: Future.UpdateMouse: LOOP UNTIL Future.MouseB% = 0
  IF mx% > X1 + 22 AND mx% < X1 + 54 AND my% > Y1 + 93 AND my% < Y1 + 125 THEN
  END IF
  IF mx% > X1 + 122 AND mx% < X1 + 154 AND my% > Y1 + 93 AND my% < Y1 + 125 THEN
   cindex = cindex + 1
   DO
    MakeID = GetMakeID%(eSkill, eLevel, cindex, avgipw)
    IF MakeID = 0 THEN cindex = 1
   LOOP UNTIL MakeID > 0
   GOSUB rdGMI
  END IF
 END IF
 IF Future.MouseB% = 2 THEN
  DO: Future.UpdateMouse: LOOP UNTIL Future.MouseB% = 0
  IF mx% > X1 + 122 AND mx% < X1 + 154 AND my% > Y1 + 93 AND my% < Y1 + 125 THEN
   cindex = cindex + 10
   DO
    MakeID = GetMakeID%(eSkill, eLevel, cindex, avgipw)
    IF MakeID = 0 THEN cindex = 1
   LOOP UNTIL MakeID > 0
   GOSUB rdGMI
  END IF
 END IF

 jkl$ = INKEY$
 SELECT CASE jkl$
  CASE CHR$(13)
   GetContract% = 1
   EXIT DO
  CASE CHR$(27)
   GetContract% = 0
   EXIT DO
 END SELECT
LOOP
END FUNCTION

FUNCTION GetMakeID% (eSkill, eLevel, index, avgipw)
 FOR i = 1 TO MaxObs
  IF obj(i).Skill = eSkill THEN
   IF obj(i).Level <= eLevel THEN
    cindex = cindex + 1
    IF cindex = index THEN
     avgipw = 4 + INT(RND * 3)
     IF eSkill = 2 THEN
      IF INSTR(obj(i).ID, "pk") <= 0 THEN
       GetMakeID% = i
       EXIT FOR
      ELSE
       index = index + 1
      END IF
     ELSE
       GetMakeID% = i
       EXIT FOR
     END IF
    END IF
   END IF
  END IF
 NEXT i
END FUNCTION

DEFSNG A-Z
SUB LoadObs
 CLOSE
 OPEN "data\objects.dat" FOR INPUT AS #1
 DO UNTIL EOF(1)
  z = z + 1
  INPUT #1, obj(z).ID$, obj(z).oname$, obj(z).Group, obj(z).SubGroup, obj(z).Level, obj(z).XP, obj(z).Skill, obj(z).Icon, obj(z).Cost
 LOOP
 CLOSE
 z = 0
 OPEN "data\xp3.dat" FOR RANDOM AS #1
 DO UNTIL EOF(1)
  z = z + 1
  GET #1, , xPl(z)
 LOOP
 CLOSE
 i = 0
 i = i + 1: Skill(i).sname$ = "Woodcutting"
 i = i + 1: Skill(i).sname$ = "Mining"
 i = i + 1: Skill(i).sname$ = "Smithing"
 i = i + 1: Skill(i).sname$ = "Cooking"
 i = i + 1: Skill(i).sname$ = "Attack"
 i = i + 1: Skill(i).sname$ = "Strength"
 i = i + 1: Skill(i).sname$ = "Defence"
 i = i + 1: Skill(i).sname$ = "Prayer"
 i = i + 1: Skill(i).sname$ = "Magic"
 i = i + 1: Skill(i).sname$ = "Farming"
 i = i + 1: Skill(i).sname$ = "HP"
 i = i + 1: Skill(i).sname$ = "Crafting"
 i = i + 1: Skill(i).sname$ = "<unused1>"
 i = i + 1: Skill(i).sname$ = "<unused2>"
 FOR i = 1 TO 8
  worn(i).Group = 3 ' i dont know why i bothered with these groups
  worn(i).SubGroup = i ' if i dont even use them properly
  worn(i).ID = 0
 NEXT i
END SUB

SUB ModeGuy (i)

ON LOCAL ERROR GOTO 40
 
   

   IF unit(i).Mode = 7 THEN
    I2 = unit(i).tM
    ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)

    '// see if ai is hostile or not... rank 25 is talkable
    IF unit(I2).Rank = 25 THEN
     IF i = selu AND (ca = 1 OR ca = 2) THEN
12
      Says unit(I2).Name$, 1, I2
13
'      unit(i).Mode = 0
      unit(i).tX = unit(i).X
      unit(i).tY = unit(i).Y
     END IF
    ELSE
     IF unit(i).Rank = 1 THEN
           IF (ca = 1 OR ca = 2) AND unit(I2).HP > 0 THEN
            IF unit(i).MVT = TIME THEN
             unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
             IF unit(I2).Rank <> 4 THEN
              unit(I2).Mode = 7
              unit(I2).MVT = TIME + 50
              unit(I2).tM = 1
              unit(I2).tX = unit(i).X: unit(I2).tY = unit(i).Y
             END IF
             IF unit(i).Team <> unit(I2).Team THEN
              Fight i, I2
              unit(i).MVT = TIME + 70'change to weapon's speed
             END IF
            END IF
           END IF
     ELSE
       IF I2 = 1 THEN
        IF unit(i).HP > 0 AND unit(i).Rank <> 4 THEN
           IF (ca = 1 OR ca = 2) THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            IF unit(i).MVT - 1 = TIME THEN
             IF unit(selu).Team = unit(i).Team THEN
              OrderUnit INT(i)
             ELSE
              FightAI i, I2
              unit(i).MVT = TIME + 70
             END IF
            END IF
           ELSE
            unit(i).tX = unit(I2).X: unit(i).tY = unit(I2).Y
           END IF
        END IF
       ELSE
        IF unit(I2).HP > 0 AND unit(i).Rank <> 4 THEN
           IF (ca = 1 OR ca = 2) THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            IF unit(i).MVT - 1 = TIME THEN
             AIonAI i, I2
              unit(I2).Mode = 7
              unit(I2).MVT = TIME + 50
              unit(I2).tM = i
              unit(I2).tX = unit(i).X: unit(I2).tY = unit(i).Y
             unit(i).MVT = TIME + 20
            END IF
           ELSE
            unit(i).MVT = TIME + unit(i).spd + 1
            unit(i).tX = unit(I2).X: unit(i).tY = unit(I2).Y
          END IF
        END IF
       END IF
     END IF
    END IF

   END IF


          IF unit(i).Mode = 30 AND unit(i).Rank > 1 AND unit(i).Rank < 19 THEN
           ' AI scripting move task
           ' Taken care of in MoveGuy
           ' End the task here
           IF unit(i).tX = unit(i).X AND unit(i).tY = unit(i).Y THEN
            unit(i).Mode = 0
           END IF
          END IF

          IF unit(i).Mode = 31 AND unit(i).Rank > 1 THEN
           ' AI gets Agro
            IF unit(i).tM = 0 OR unit(unit(i).tM).HP <= 0 THEN
             ' go after an enemy near you
             bestdist = 27
             FOR I2 = 1 TO maxunits
              IF unit(I2).Team <> unit(i).Team THEN
               ' yes, its an enemy alright
               ndist = Dist(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
               IF ndist < bestdist THEN
                ' he's just over there
                ' i'll put him as my most wanted
                bestdist = ndist
                mstwntd = I2
               END IF
              END IF
             NEXT I2
             IF mstwntd > 0 THEN
              '^ make sure i just picked up a target
              unit(i).tM = mstwntd
              'MSG RTRIM$(unit(i).Name$) + STR$(i) + " found " + RTRIM$(unit(mstwntd).Name$) + STR$(mstwntd) + " and wants him dead"
             END IF
            ELSE
             I2 = unit(i).tM
             IF unit(i).Rank > 1 THEN I2 = unit(i).tM
             unit(i).tX = unit(I2).X: unit(i).tY = unit(I2).Y
             ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
             IF ca = 1 OR ca = 2 THEN unit(i).Mode = 7
            END IF
          END IF


          IF unit(i).Mode = 21 THEN
           I2 = unit(i).tM
           ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
           IF ca = 1 THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            unit(i).Mode = 0
            EmployMenu RTRIM$(unit(I2).Name$)
            Drawmap currentmap, 1, 0, 0
           END IF
          END IF

          IF unit(i).Mode = 11 THEN
           I2 = unit(i).tM
           ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
           IF ca = 1 THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            unit(i).Mode = 0
            ShowBank
           END IF
          END IF
          IF unit(i).Mode = 4 THEN
           I2 = unit(i).tM
           ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
           IF ca = 1 OR ca = 2 THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            unit(i).Mode = 0
            Trade deal, INT(unit(I2).tX)
           END IF
          END IF
          IF unit(i).Mode = 14 THEN
           I2 = unit(i).tM
           ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
           IF ca = 1 THEN
            ob = invID(seli)
            skn = obj(ob).Skill
            skl = obj(ob).Level
            IF Skill(skn).amt >= skl THEN
             unit(i).Mode = 0
             unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
             Mix 0, obj(ob).ID$, STR$(unit(I2).Rank), rtn
            ELSE
             unit(i).Mode = 0
             MSG "You must be level" + STR$(skl) + " " + RTRIM$(LTRIM$(Skill(skn).sname$))
            END IF
           END IF
          END IF
          IF unit(i).Mode = 10 THEN
           I2 = unit(i).tM
           ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(I2).X + 0, unit(I2).Y + 0)
           IF (ca = 1 OR ca = 2) AND unit(I2).HP > 0 THEN
            unit(i).tX = unit(i).X: unit(i).tY = unit(i).Y
            Skill(5).XP = Skill(5).XP + 1
            unit(I2).HP = -1
            unit(I2).MVT = TIME + 3000
            unit(i).Mode = 0
            SELECT CASE unit(I2).Rank
             CASE 20
              MSG "You kill the cow"
              IF EmptySlots > 0 THEN
               Give "rbef", 1
               MSG "...and collect the raw meat"
              END IF
             CASE 22
              MSG "You kill the chicken"
              IF EmptySlots > 0 THEN
               Give "rchk", 1
               MSG "...and collect the raw meat"
              END IF
            END SELECT
            ux = unit(i).X - MapGlobalX(unit(i).map)
            uy = unit(i).Y - MapGlobalY(unit(i).map)
            Drawmap currentmap, 1, 0, 0
            Future.PUT ux * 15, uy * 15 + 5, bld()
           END IF
          END IF


     ca = CheckAdjacent%(unit(i).X + 0, unit(i).Y + 0, unit(i).tX + 0, unit(i).tY + 0)
     IF unit(i).Mode = 16 THEN payfee% = 0
     IF payfee% > 0 AND ca = 1 AND unit(i).Mode > 0 THEN
      gold = GetInvNo%(INT(GetID%("gold")))
      IF invAmt(gold) >= payfee% THEN
       MSG "The king charges you" + STR$(payfee%) + " GP "
       invAmt(gold) = invAmt(gold) - payfee%
       RefreshInv
       payfee% = 0
      ELSE
       MSG "You cannot afford the tax!"
       unit(i).Mode = 0
      END IF
     END IF
     IF unit(i).Mode > 0 THEN
      SELECT CASE unit(i).Rank
       CASE 1
        g$ = CHR$(GROUND(unit(i).tX + 0, unit(i).tY + 0))

'====================
' GROUND TILE MODE ACTION STUFF
'====================
        SELECT CASE unit(i).Mode
         CASE 19 ' pottery
          ' give unfired pot, take clay
          w = GetInvNo%(INT(GetID%("wcly")))
          IF w > 0 THEN
           invID(w) = 0
           invAmt(w) = 0
           Give "ppot", 1
           'Skill(12).XP=Skill(12).XP+4
           MSG "You make the pot out of the clay"
          END IF
          unit(selu).Mode = 0
         CASE 18 ' clay
          IF EmptySlots > 0 THEN
          IF (ca = 1 OR ca = 2) AND g$ <> "q" THEN
           item = PossessSubType(1, 2, 1)
           IF item > 0 THEN
            lbonus = CINT(Skill(2).amt / 10)
            IF lbonus = 0 THEN lbonus = 1
            SELECT CASE g$
             CASE "D"
              ore$ = "try"
             CASE CHR$(165)
              'clay
              IF INT(RND * INT(70 / lbonus)) = 1 THEN
               AddRock unit(i).tX + 1, unit(i).tY + 1, CHR$(165), 700
               Give "clay", 1
               ore$ = "clay"
              ELSE
               ore$ = "try"
              END IF
            END SELECT

            IF ore$ = "" THEN
             MSG "You need to be a higher level to mine that"
              unit(i).Mode = 0
            ELSEIF ore$ <> "try" THEN
             MSG "You manage to mine some " + ore$
             UsedWhat$ = obj(item).ID$
             SetPage 2
             Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("D")
             SetPage Primary%
             UpdateTempMap (unit(i).map)
             IF unit(i).map = currentmap THEN Drawmap currentmap, 1, 0, 0
            END IF
           ELSE
            MSG "You need a pick to mine clay"
             unit(i).Mode = 0
           END IF
          END IF
          ELSE
           MSG "You cannot hold any more clay"
            unit(i).Mode = 0
          END IF

         CASE 17
          IF ca = 1 OR ca = 2 AND g$ = "y" THEN
           IF unit(i).MVT = TIME THEN
            IF Possess(INT(GetID%("fnet"))) > 0 THEN
             IF EmptySlots > 0 THEN
              f = INT(RND * 2)
              IF f = 1 THEN Give "rfsh", 1 ELSE MSG "That one got away"
              unit(i).MVT = TIME + 60
             ELSE
              MSG "Not enough room for more fish"
             END IF
            ELSE
             MSG "You need a fishing net"
            END IF
           END IF
          END IF
         CASE 16
          IF ca = 1 OR ca = 2 AND g$ = "S" THEN
           ReadSign unit(i).tX + 0, unit(i).tY + 0
           unit(i).Mode = 0
          END IF
         CASE 15
          IF ca = 1 AND g$ = "a" THEN
           IF obj(invID(seli)).Group = 5 AND obj(invID(seli)).SubGroup = 4 THEN
            IF Possess(INT(GetID%("hamr"))) > 0 THEN
             ForgeBar obj(invID(seli)).ID$, UsedWhat$, xpb
             Drawmap currentmap, 1, 0, 0
             unit(i).Mode = 0
            ELSE
             MSG "You need a hammer to forge metal"
             unit(i).Mode = 0
            END IF
           ELSE
            MSG "Select a metal bar first"
            unit(i).Mode = 0
           END IF
          END IF
         CASE 12
          IF g$ = "g" AND ca = 1 THEN
           weet = GetInvNo%(INT(GetID%("weet")))
           ppot = GetInvNo%(INT(GetID%("ppot")))
           IF weet > 0 THEN
            IF ppot > 0 THEN
             invID(weet) = 0
             invAmt(weet) = 0
             invID(ppot) = GetID%("flor")
             MSG "You grind the wheat into flour"
             unit(i).Mode = 0
            ELSE
             MSG "You need another pot to put the flour in"
             unit(i).Mode = 0
            END IF
           ELSE
            MSG "You have no wheat"
            unit(i).Mode = 0
           END IF
          END IF
         CASE 13
          IF g$ = "w" AND ca = 1 THEN
           bkt = GetInvNo%(INT(GetID%("bckt")))
           IF bkt > 0 THEN
            invID(bkt) = GetID%("wtbk")
            MSG "You fill the bucket with water"
            unit(i).Mode = 0
           ELSE
            MSG "You have no bucket"
            unit(i).Mode = 0
           END IF
          END IF
         CASE 9
          IF g$ = "j" AND ca = 1 THEN
           IF obj(mru).Group = 5 AND obj(mru).SubGroup = 2 THEN
            SELECT CASE obj(mru).ID$
             CASE "tnor", "cpor"
              Mix 1, "tnor", "cpor", rtn
              IF rtn > 0 THEN
               UsedWhat$ = "bzbr"
              ' MSG "You retrieve a bronze bar"
              END IF
             CASE "inor"
              avsk = Skill(obj(GetID%("irbr")).Skill).amt
              IF obj(GetID%("irbr")).Level <= avsk THEN
              rn = INT(RND * 2)
              iron = GetInvNo%(INT(GetID%("inor")))
              invID(iron) = 0
              invAmt(iron) = 0
              IF rn = 1 THEN
               Give "irbr", 1
               UsedWhat$ = "irbr"
               MSG "You retrieve an iron bar"
              ELSE
               MSG "The ore is too impure "
              END IF
              ELSE
               MSG "You need to be a higher level to make iron"
               unit(i).Mode = 0
              END IF
             CASE "coal"
              avsk = Skill(obj(GetID%("stbr")).Skill).amt
              IF obj(GetID%("stbr")).Level <= avsk THEN
               IF Possess&(GetID%("coal") + 0) < 2 THEN MSG "You need 2 coal": GOTO screwdarest
               iron = GetInvNo%(INT(GetID%("inor")))
               Give "coal", -2
               invID(iron) = 0
               invAmt(iron) = 0
               Give "stbr", 1
               UsedWhat$ = "stbr"
               MSG "You retrieve a steel bar"
              ELSE
               MSG "You need to be a higher level to make steel"
               unit(i).Mode = 0
              END IF
             CASE "mior"
              avsk = Skill(obj(GetID%("mibr")).Skill).amt
              IF obj(GetID%("mibr")).Level <= avsk THEN
               IF Possess&(GetID%("coal") + 0) < 4 THEN MSG "You need 4 coal": GOTO screwdarest
               iron = GetInvNo%(INT(GetID%("mior")))
               Give "coal", -6
               invID(iron) = 0
               invAmt(iron) = 0
               Give "mibr", 1
               UsedWhat$ = "mibr"
               MSG "You retrieve an mithril bar"
              ELSE
               MSG "You need to be a higher level to make mithril"
               unit(i).Mode = 0
              END IF
             CASE "ador"
              avsk = Skill(obj(GetID%("adbr")).Skill).amt
              IF obj(GetID%("adbr")).Level <= avsk THEN
               IF Possess&(GetID%("coal") + 0) < 6 THEN MSG "You need 6 coal": GOTO screwdarest
               iron = GetInvNo%(INT(GetID%("ador")))
               Give "coal", -6
               invID(iron) = 0
               invAmt(iron) = 0
               Give "adbr", 1
               UsedWhat$ = "adbr"
               MSG "You retrieve an adamant bar"
              ELSE
               MSG "You need to be a higher level to make adamant"
               unit(i).Mode = 0
              END IF
             CASE "rnor"
              avsk = Skill(obj(GetID%("rnbr")).Skill).amt
              IF obj(GetID%("rnbr")).Level <= avsk THEN
               IF Possess&(GetID%("coal") + 0) < 8 THEN MSG "You need 8 coal": GOTO screwdarest
               iron = GetInvNo%(INT(GetID%("rnor")))
               Give "coal", -8
               invID(iron) = 0
               invAmt(iron) = 0
               Give "rnbr", 1
               UsedWhat$ = "stbr"
               MSG "You retrieve a rune bar"
              ELSE
               MSG "You need to be a higher level to make rune"
               unit(i).Mode = 0
              END IF
            END SELECT
            unit(i).Mode = 0
           ELSE
            MSG "Select some ore first"
            unit(i).Mode = 0
           END IF
          END IF
         CASE 3
          IF EmptySlots > 0 THEN
          IF ca = 1 AND g$ <> "q" THEN
           item = PossessSubType(1, 2, 1)
           IF item > 0 THEN
            lbonus = CINT(Skill(2).amt / 10)
            IF lbonus = 0 THEN lbonus = 1
            SELECT CASE g$
             CASE "D"
              ore$ = "try"
             CASE "r"
              'copper
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
               AddRock unit(i).tX + 1, unit(i).tY + 1, "r", 700
               Give "cpor", 1
               ore$ = "copper"
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(200)
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
               Give "tnor", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "È", 700
               ore$ = "tin"
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(201)
              IF INT(RND * INT(200 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("inor")).Level THEN
               Give "inor", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "É", 1900
               ore$ = "iron"
               xpb = 1
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(202)
              IF INT(RND * INT(300 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("coal")).Level THEN
               Give "coal", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Ê", 4000
               ore$ = "coal"
               xpb = 3
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(203)
              IF INT(RND * INT(300 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("gdor")).Level THEN
               Give "gdor", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Ë", 4500
               ore$ = "gold"
               xpb = 2
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(204)
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("mior")).Level THEN
               Give "mior", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Ì", 8500
               ore$ = "mithril"
               xpb = 4
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(205)
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("ador")).Level THEN
               Give "ador", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Í", 9500
               ore$ = "adamantite"
               xpb = 6
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(206)
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("rnor")).Level THEN
               Give "rnor", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Î", 11500
               ore$ = "runite"
               xpb = 6
              END IF
              ELSE
               ore$ = "try"
              END IF
             CASE CHR$(207)
              IF INT(RND * INT(100 / lbonus)) = 1 THEN
              IF Skill(2).amt >= obj(GetID%("slor")).Level THEN
               Give "slor", 1
               AddRock unit(i).tX + 1, unit(i).tY + 1, "Ï", 3500
               ore$ = "silver"
               xpb = 1
              END IF
              ELSE
               ore$ = "try"
              END IF
            END SELECT

            IF ore$ = "" THEN
             MSG "You need to be a higher level to mine that"
              unit(i).Mode = 0
            ELSEIF ore$ <> "try" THEN
             MSG "You manage to mine some " + ore$ + " ore"
             UsedWhat$ = obj(item).ID$
             SetPage 2
             Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("D")
             SetPage Primary%
           '  UpdateTempMap (unit(i).map)
             Drawmap currentmap, 2, unit(i).tX + 1, unit(i).tY + 1
            END IF
           ELSE
            MSG "You need a pick to mine ore"
             unit(i).Mode = 0
           END IF
          END IF
          ELSE
           MSG "You cannot hold any ore"
            unit(i).Mode = 0
          END IF

         CASE 6
          IF ca = 2 THEN
           item = GetInvNo%(INT(GetID%("scyt")))
           IF item > 0 THEN
           IF g$ = "F" THEN
           IF EmptySlots > 0 THEN
            Give "weet", 1
            ' farm farm
            SetPage 2
            Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("D")
            SetPage Primary%
            UsedWhat$ = obj(item).ID$
            UpdateTempMap (unit(i).map)
            IF unit(i).map = currentmap THEN Drawmap currentmap, 1, 0, 0
        
            ' find nearest wheat
            max = 100
            FOR X = -5 TO 5
             FOR Y = -5 TO 5
              ux = unit(i).X - X
              uy = unit(i).Y - Y
              d = Dist(ux, uy, unit(i).X + 0, unit(i).Y + 0)
              UsedWhat$ = "scyt"
              g2$ = CHR$(GROUND(ux, uy))
              IF g2$ = "F" AND d < max THEN
               max = d
               uX2 = ux
               uY2 = uy
               found = 1
              END IF
             NEXT Y
            NEXT X
            IF found = 1 THEN
             unit(i).tX = uX2
             unit(i).tY = uY2
             TraceWays uX2, uY2, unit(i).X + 0, unit(i).Y + 0, unit(i).WPT$
            END IF
            IF found = 0 THEN unit(i).Mode = 0 ELSE unit(i).Mode = 6
            ELSE
             MSG "You cannot hold more wheat"
              unit(i).Mode = 0
            END IF
           END IF
            ELSE
             MSG "You need a scythe"
              unit(i).Mode = 0
            END IF
          END IF

         CASE 2
          IF ca = 1 THEN
           item = PossessSubType(1, 1, 1)
           IF item > 0 THEN
           IF g$ = "t" OR g$ = "{" THEN
           IF EmptySlots > 0 THEN
             Give "wood", 1
            ' chop-a 8D
            SetPage 2
            Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("^")
            SetPage Primary%
            UsedWhat$ = obj(item).ID$
            'UpdateTempMap (unit(i).map)
            Drawmap currentmap, 1, unit(i).tX + 1, unit(i).tY + 1
         
            ' find nearest tree
            max = 100
            FOR X = -5 TO 5
             FOR Y = -5 TO 5
              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$ = "t" OR g2$ = "{" AND d < max THEN
               max = d
               uX2 = ux
               uY2 = uy
               found = 1
              END IF
             NEXT Y
            NEXT X
            IF found = 1 THEN
             unit(i).tX = uX2
             unit(i).tY = uY2
             TraceWays uX2, uY2, unit(i).X + 0, unit(i).Y + 0, unit(i).WPT$
            END IF
            IF found = 0 THEN unit(i).Mode = 0 ELSE unit(i).Mode = 2
            ELSE
             MSG "You cannot hold more wood"
              unit(i).Mode = 0
            END IF
           END IF
            ELSE
             MSG "You need an axe that you can use"
              unit(i).Mode = 0
            END IF
          END IF

         CASE 8
          IF ca = 1 THEN
          SELECT CASE g$
           CASE "b"
            SetPage 2
            Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("v")
            SetPage Primary%
            unit(i).Skill = unit(i).Skill + 1
            UpdateTempMap (unit(i).map)
            IF unit(i).map = currentmap THEN Drawmap currentmap, 1, 0, 0
            unit(i).Mode = 0
           CASE "v"
            SetPage 2
            Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC("C")
            SetPage Primary%
            unit(i).Skill = unit(i).Skill + 1
            UpdateTempMap (unit(i).map)
            IF unit(i).map = currentmap THEN Drawmap currentmap, 1, 0, 0
            unit(i).Mode = 0
          END SELECT
          END IF
         CASE 1
          SELECT CASE g$
           CASE "G", "D", "C"
          Build = 0
          IF ca = 1 AND unit(i).Mode = 1 THEN
           SELECT CASE unit(i).Carry$
            CASE "b"
             IF Stone - 20 >= 0 THEN
              Stone = Stone - 20
              Build = 1
             END IF
            CASE "l"
             IF Wood - 20 >= 0 THEN
              Wood = Wood - 20
              Build = 1
             END IF
            CASE "F"
             IF Wood - 20 >= 0 AND Food - 20 >= 0 THEN
              Food = Food - 20
              Build = 1
             END IF
           END SELECT
           IF Build = 1 THEN
            SetPage 2
            Future.PSET unit(i).tX + 1, unit(i).tY + 1, ASC(unit(i).Carry$)
            SetPage Primary%
            unit(i).Skill = unit(i).Skill + 1
            UpdateTempMap (unit(i).map)
            IF unit(i).map = currentmap THEN Drawmap currentmap, 1, 0, 0
            unit(i).Mode = 0
           END IF
          END IF
         END SELECT
        END SELECT 'mode
       END SELECT 'rank
     END IF
screwdarest:
IF UsedWhat$ <> "" THEN
 item = GetID%(UsedWhat$)
 'MSG "Used " + RTRIM$(LTRIM$(obj(item).oname$))
 Skill(obj(item).Skill).XP = Skill(obj(item).Skill).XP + obj(item).XP + xpb
 CheckLevels
END IF
EXIT SUB
40
 SLEEP
 ResetScreen
 PRINT "error in units line "; ERL; "err#"; ERR
 ERROR ERR
 SLEEP
 SYSTEM
 RESUME NEXT

END SUB

SUB OrderUnit (guy%)
MSG "Click where you want the unit to go..."
MSG "Current Mode:" + STR$(unit(guy%).Mode)
DO
 Future.UpdateMouse
 mx% = Future.MouseX%
 my% = Future.MouseY%
 mxs% = INT(mx% / 15) + Xscroll%
 mys% = INT(my% / 15) + Yscroll%
 IF Future.MouseB% = 1 THEN
  DO: LOOP UNTIL Future.MouseB% = 0
   I2 = GuyAtSqr!(mxs% + 0, mys% + 0)
   IF I2 <= 1 THEN
    unit(guy%).tX = mxs%
    unit(guy%).tY = mys%
    unit(guy%).Mode = 2
    unit(guy%).MVT = TIME + 10
    MSG "Moving out"
   ELSE
    unit(guy%).tX = unit(I2).X
    unit(guy%).tY = unit(I2).Y
    unit(guy%).tM = INT(I2)
    unit(guy%).Mode = 31
    unit(guy%).MVT = TIME + 10
    MSG "Attack!" + STR$(unit(guy%).tM)
   END IF
   EXIT DO
 END IF
 jkl$ = INKEY$
 SELECT CASE jkl$
  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 CHR$(27)
   EXIT DO
 END SELECT
LOOP
END SUB

SUB ReadSign (mx, my)
 MSG "started reading"
 CLOSE
 OPEN "data\signs.dat" FOR INPUT AS #1
 DO UNTIL EOF(1)
  INPUT #1, X, Y, title$, stext$
  IF mx - 1 = X AND my - 1 = Y THEN
   MSG title$ + ":" + stext$
   EXIT DO
  END IF
 LOOP
 CLOSE
 MSG STR$(mx) + STR$(my)
 MSG "finished reading"
END SUB

SUB RefreshInv
FOR i = 1 TO 36
 IF invID(i) = 0 THEN invAmt(i) = 0
 IF invAmt(i) = 0 THEN invID(i) = 0
NEXT i
END SUB

SUB Weekly
  ' pay weekly bank interest on cash of 2%
   principal& = PossessB&(GetID%("gold") + 0)
   interest% = CINT(principal& * .02)
   IF interest% > 0 THEN
    Give "gold", INT(interest%)
    MSG "You receive" + STR$(interest%) + " GP interest"
   END IF
  FOR i = 1 TO 20
   IF wContractor(i).BWT.ID > 0 THEN
    pID = wContractor(i).payID
    pAmt& = wContractor(i).BWT.PayAmt
    payment& = PossessB&(pID + 0)
    IF payment& >= pAmt& THEN
     pw = INT(wContractor(i).BWT.ipw)
     'MSG STR$(i) + " gave " + STR$(pw) + " items to bank"
     Deposit2 wContractor(i).BWT.MakeID, pw + 0, bid%
     alt = BankTake(pID, pAmt&)
    ELSE
     MSG "You can't afford to pay contractor #" + STR$(i)
    END IF
   END IF
  NEXT i

  ' Get next week's raw materials for emps.

END SUB

FUNCTION YesNo% (MSGT$, yes$, no$)
X = 200
Y = 200
line$ = MSGT$
GOSUB redraw5
Future.MouseOn
DO
 Future.UpdateMouse
 IF Future.MouseB% > 0 THEN
  DO
   Future.UpdateMouse
  LOOP UNTIL Future.MouseB% = 0
 END IF
 IF Future.MouseY% > Y + 40 AND Future.MouseY% < Y + 56 THEN
  IF Future.MouseX% > X + 20 AND Future.MouseX% < X + 90 THEN
   Future.PRINT X + 20, Y + 40, yes$, 14, 7
   Future.PRINT X + 240, Y + 40, no$, 15, 7
   IF Future.MouseB% = 1 THEN YesNo% = 1: EXIT FUNCTION
  END IF
  IF Future.MouseX% > X + 240 AND Future.MouseX% < X + 330 THEN
   Future.PRINT X + 20, Y + 40, yes$, 15, 7
   Future.PRINT X + 240, Y + 40, no$, 14, 7
   IF Future.MouseB% = 1 THEN YesNo% = 0: EXIT FUNCTION
  END IF
 END IF
 jkl$ = INKEY$
 SELECT CASE jkl$
  CASE CHR$(27)
   YesNo% = 0
   EXIT FUNCTION
  CASE CHR$(13)
   YesNo% = 1
   EXIT FUNCTION
 END SELECT
LOOP
redraw5:
 Future.FILLBOX X, Y, X + 300, Y + 100, 8
 Future.BOX X, Y, X + 300, Y + 100, 7
 Future.PRINT X + 20, Y + 20, MSGT$, 15, -1
 Future.PRINT X + 20, Y + 40, yes$, 15, 7
 Future.PRINT X + 240, Y + 40, no$, 15, 7
RETURN


END FUNCTION

