'    It would be a good Idea To Make This Available.
'                    Please Upload.
'
'       Rpg Engine V1.0 Coded By Shockwave 2002.
'This program is the property of the projects forum at
'WWW.YABASIC.CO.UK and may not be adapted or modified by
'anybody else. (C) 2002 Shockwave & www.Yabasic.co.uk
'=========================================================
gosub overall_setup:rem                  SET UP EVERYTHING
'=========================================================
'                - -START OF GAME LOOP- -
'=========================================================

gosub drawborder:rem           DRAW STATIC SCREEN ELEMENTS
gosub newlocation:rem                DRAW COMPLETE DISPLAY

label main
   setdrawbuf dw:rem                 SELECT LOGICAL SCREEN
   dw=1-dw :rem                      TOGGLE SCREEN POINTER
   setdispbuf dw:rem            DISPLAY OLD LOGICAL SCREEN
   gosub dialoguebox:rem            CLEAR THE OLD DIALOGUE
   gosub control:rem   INPUT, SCROLLING, COLLISION+DISPLAY
   gosub player:rem   DRAW PLAYER AND DO A LIMITED REFRESH

goto main
'=========================================================
'                  - -END OF GAME LOOP- -
'=========================================================
exit
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'              Clear Out the Dialogue Box.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label dialoguebox
   setrgb 1,255,255,255:rem                   SELECT WHITE
   fill rect 20,450 to 620,510:rem             DRAW BORDER
   setrgb 1,0,0,0:rem                         SELECT BLACK
   fill rect 21,451 to 619,509:rem               ERASE BOX
'=========================================================
'              DISCARD BELOW WHEN WORKING:
'=========================================================
   setrgb 1,155,155,255
   text 320,470,"TREAT THIS PART AS A NORMAL DOUBLE BUFFERED SCREEN!","cc"
   text 322,490,"RPG V1.0 (C) SHOCKWAVE + RPG PROJECT FORUM WORK IN PROGRESS","cc"
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'             DRAW THE PLAYER'S GRAPHIC
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

label player
 gosub refresh: rem           REFRESH BLOCKS AROUND PLAYER
 setrgb 1,255,255,255:rem                      DRAW PLAYER
 fill rect plx-10,ply-10 to plx+10,ply+10
setrgb 1,0,0,0
 
 if d=1 text plx-6,ply+4,"L"
 if d=2 text plx-6,ply+4,"R"
 if d=3 text plx-6,ply+4,"D"
 if d=4 text plx-6,ply+4,"U"

return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'        UPDATE BLOCKS THAT PLAYER IS STANDING ON
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label refresh
   xw=(plx-21) :rem                          CENTRAL BLOCK
   yw=(ply-21)
   gosub refr
   xw=((plx-100)-21) :rem                       LEFT BLOCK
   yw=(ply-21)
   gosub refr
   xw=((plx+100)-21) :rem                      RIGHT BLOCK
   yw=(ply-21)
   gosub refr
   xw=(plx-21) :rem                             DOWN BLOCK
   yw=((ply+100)-21)
   gosub refr
   xw=(plx-21) :rem                               UP BLOCK
   yw=((ply-100)-21)
   gosub refr
   xw=((plx-100)-21) :rem                 ABOVE LEFT BLOCK
   yw=((ply-100)-21)
   gosub refr
   xw=((plx+100)-21) :rem                ABOVE RIGHT BLOCK
   yw=((ply-100)-21)
   gosub refr
   xw=((plx-100)-21) :rem                 BELOW LEFT BLOCK
   yw=((ply+100)-21)
   gosub refr
   xw=((plx+100)-21) :rem                BELOW RIGHT BLOCK
   yw=((ply+100)-21)
   gosub refr
return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'      This Sub Refreshes The Block At xw,xt, yw,yt
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label refr
if xw<0 or xw>599 return:rem      IF X AT EDGE DONT BOTHER
if yw<0 or yw>399 return:rem      IF Y AT EDGE DONT BOTHER
   xw=int(xw/100)+1:rem                    CALCULATE MAP X
   yw=int(yw/100)+1 rem                    CALCULATE MAP Y
   xt=((xw-1)*100)+21:rem                CALCULATE BLOCK X
   yt=((yw-1)*100)+21:rem                CALCULATE BLOCK Y
   gosub blocks:rem                         DRAW THE BLOCK
return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'            Move Player And Scroll The Map.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label control
xo=plx:rem             STORE PLAYER X IN CASE OF COLLISION
yo=ply:rem             STORE PLAYER Y IN CASE OF COLLISION
  c=peek("port1"):rem                          READ PORT 1
  if and (c,32)<>0 plx=plx+2:rem                MOVE RIGHT
  if and (c,64)<>0 ply=ply+2 :rem               MOVE DOWN
  if and (c,16)<>0 ply=ply-2:rem                MOVE UP
  if and (c,128)<>0 plx=plx-2 :rem              MOVE LEFT
 if plx>xo d=2:rem                  SET DIRECTION TO RIGHT
 if plx<xo d=1:rem                   SET DIRECTION TO LEFT
 if ply>yo d=3:rem                   SET DIRECTION TO DOWN
 if ply<yo d=4:rem                     SET DIRECTION TO UP
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'  Scenery Collisions: (any block 10+can't be walked on)
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   xw=plx-21
   yw=ply-21
   xw=int(xw/100)+1
   yw=int(yw/100)+1
   if map(xw+pgx,yw+pgy)>=10 then plx=xo:ply=yo:fi
'%%%%%%%%%%%%%%%%%%%%
'%%% SCROLL RIGHT %%%
'%%%%%%%%%%%%%%%%%%%%
if plx>610 then
 if (pgx+1)/6 < dmx-1 then
 plx=31
 pgx=pgx+6
 gosub newlocation
 fi
fi
'%%%%%%%%%%%%%%%%%%%%
'%%% SCROLL LEFT  %%%
'%%%%%%%%%%%%%%%%%%%%
if plx<31 then
 if pgx > 0 then
 plx=610
 pgx=pgx-6
 gosub newlocation
 fi
fi
'%%%%%%%%%%%%%%%%%%%
'%%% SCROLL DOWN %%%
'%%%%%%%%%%%%%%%%%%%
if ply>410 then
 if (pgy+1)/4 < dmy-1 then
 ply=31
 pgy=pgy+4
 gosub newlocation
 fi
fi
'%%%%%%%%%%%%%%%%%%%
'%%% SCROLL   UP %%%
'%%%%%%%%%%%%%%%%%%%
if ply<31 then
 if pgy>0 then
 ply=410
 pgy=pgy-4
 gosub newlocation
 fi
fi
 if ply<31 ply=31:rem              MINIMUM PERMITTED Y POS
 if ply>409 ply=409:rem            MAXIMUM PERMITTED Y POS
 if plx>609 plx=609:rem            MAXIMUM PERMITTED X POS
 if plx<31 plx=31:rem              MINIMUM PERMITTED X POS
return

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'            Completely Refresh Current Page
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label newlocation
for a=1 to 2:rem       UPDATE LOGICAL AND PHYSICAL SCREENS
setdrawbuf dw:rem                    SELECT LOGICAL SCREEN
dw=1-dw:rem                          TOGGLE SCREEN POINTER
setdispbuf dw:rem               DISPLAY OLD LOGICAL SCREEN
   setrgb 1,0,0,0:rem           CLEAR LOCATION DESCRIPTION
   fill rect 100,430 to 520,445:rem           BOX AND THEN
   setrgb 1,155,155,155:rem          WRITE NEW DESCRIPTION
   text 318,440,location$((pgx/6)+1,(pgy/4)+1),"cc"
   setrgb 1,255,255,255
   text 319,441,location$((pgx/6)+1,(pgy/4)+1),"cc"
   setrgb 1,0,0,0
   text 320,442,location$((pgx/6)+1,(pgy/4)+1),"cc"

xt=21:rem                           X BLOCK DRAWING OFFSET
yt=21:rem                           Y BLOCK DRAWING OFFSET
for y=1 to 4:rem                             4 BLOCKS TALL
 for x=1 to 6:rem                            6 BLOCKS WIDE
xw=x:rem          MAKE X COMPATIBLE WITH BLOCK DRAWING SUB
yw=y:rem          MAKE Y COMPATIBLE WITH BLOCK DRAWING SUB
   gosub blocks:rem                     DRAW THE NEW BLOCK
   xt=xt+100 :rem                   MOVE ACCROSS ONE BLOCK
 next x
 xt=21:rem                            RESET X BLOCK OFFSET
 yt=yt+100:rem                             MOVE DOWN A ROW
next y
next a
return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'          This Subroutine Draws Scenery Blocks
' They must be 100*100 in size and start at xt,yt and end
' at xt+100,yt+100, I reccommend using triangles and 
' rectangles to create them although small circles should
' be okay without harming overall speed I suppose.
' Anything up to and including number 10 the player can
' walk over, above it the player collides with it.
'
'Format:
'if map (xw+pgx,yw+pgy) = (block number) then
' Your gfx here
'fi
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label blocks
'=========================================================
'                  WALKABLE BLOCK TYPES:
'=========================================================
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%% grass1 (horizontal stripe) %%
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if map(xw+pgx,yw+pgy)=1 then
   setrgb 1,10,40,10
   fill rect xt,yt to xt+100,yt+100
   setrgb 1,20,50,20
   fill rect xt,yt to xt+100,yt+25
   fill rect xt,yt+50 to xt+100,yt+75
fi
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'%% grass2 (diamond pattern) %%
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if map(xw+pgx,yw+pgy)=2 then
   setrgb 1,10,40,10
   fill rect xt,yt to xt+100,yt+100
   setrgb 1,20,50,20
   fill triangle xt,yt to xt+50,yt to xt,yt+50
   fill triangle xt+50,yt to xt+100,yt to xt+100,yt+50
   fill triangle xt,yt+50 to xt,yt+100 to xt+50,yt+100
   fill triangle xt+100,yt+50 to xt+100,yt+100 to xt+50,yt+100
fi
'%%%%%%%%%%%%%%%%%%%%%%%
'%% Horizontal bridge %%
'%%%%%%%%%%%%%%%%%%%%%%%
if map(xw+pgx,yw+pgy)=3 then
   setrgb 1,60,50,50
   fill rect xt,yt to xt+100,yt+100
   setrgb 1,70,50,50
   fill rect xt+20,yt to xt+80,yt+100
   setrgb 1,80,50,50
   fill rect xt+40,yt to xt+60,yt+100
fi
'%%%%%%%%%%%%%%%%%%%%%
'%% Vertical bridge %%
'%%%%%%%%%%%%%%%%%%%%%
if map(xw+pgx,yw+pgy)=4 then
   setrgb 1,60,50,50
   fill rect xt,yt to xt+100,yt+100
   setrgb 1,70,50,50
   fill rect xt,yt+20 to xt+100,yt+80
   setrgb 1,80,50,50
   fill rect xt,yt+40 to xt+100,yt+60
fi
'%%%%%%%%%%
'%% Path %%
'%%%%%%%%%%
if map(xw+pgx,yw+pgy)=5 then
   setrgb 1,65,65,60
   fill rect xt,yt to xt+100,yt+100
fi
'=========================================================
'                Non Walkable Block Types:
'=========================================================
'%%%%%%%%%%%
'%% River %%
'%%%%%%%%%%%
if map(xw+pgx,yw+pgy)=10 then
   setrgb 1,15,15,50
   fill rect xt,yt to xt+100,yt+100
fi

return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'Draws All Fixed Items Of The Screen Display On Both The
'Logical And Physical Screens.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label drawborder
for a=1 to 2:rem             LOOP TWICE TO DO BOTH SCREENS
      setdrawbuf dw:rem                    SET DRAW SCREEN
      dw=1-dw:rem                    TOGGLE SCREEN POINTER
setrgb 1,180,110,130:rem          RGB VALUE FOR GTRI POINT
setrgb 2,130,170,110:rem          RGB VALUE FOR GTRI POINT
   setrgb 3,20,30,60:rem          RGB VALUE FOR GTRI POINT
   gtriangle 0,0 to 640,512 to 0,512:rem   BACKGROUND COLS
   gtriangle 0,0 to 640,512 to 640,0:rem   BACKGROUND COLS
   setrgb 1,155,155,255:rem         RGB COLOUR FOR BORDERS
   fill rect 13,13 to 628,427:rem             DRAW BORDERS
   fill rect 98,428 to 522,447
   setrgb 1,0,0,0:rem           SET TO BLACK FOR PLAY AREA
   fill rect 15,15 to 626,425:rem          CLEAR PLAY AREA
   fill rect 102,432 to 518,443

next a
return
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                  Overall Setup For RPG
' Contains Map, Array Definitions, Dialog, Vars And Data.
' This is called at the very beginning of the program.
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
label overall_setup

open window 640,512:rem               OPEN GRAPHICS SCREEN
d=1:rem             DIRECTION (1=left 2=right 4=up 3=down)
plx=120:rem          PLAYER X START AS A PHYSICAL SCRN POS
ply=256:rem          PLAYER Y START AS A PHYSICAL SCRN POS
dmx=3 : rem                          MAP IS 3 SCREENS WIDE
dmy=5 : rem                          MAP IS 5 SCREENS TALL
dim map((dmx*6),(dmy*4)):rem         STORAGE SPACE FOR MAP
dim location$(dmx,dmy):rem   STORAGE SPACE FOR SCRN TITLES
pgx=0 :rem                          MAP DISPLAY LOCATION X
pgy=0 :rem                          MAP DISPLAY LOCATION Y

'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'                      Read In Data:
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 for y=1 to dmy*4:rem             < THESE NESTED LOOPS 
  for x=1 to dmx*6:rem              READ THE MAP DATA INTO
    read map(x,y):rem               THE map() ARRAY WHICH
  next x:rem                        IS TWO DIMENSIONAL.
 next y:rem                       < END OF LOOP CYCLE.
for y=1 to dmy:rem                < AND THESE NESTED LOOPS
 for x=1 to dmx:rem                 READ THE LOCATION 
   read location$(x,y):rem          NAMES INTO THE 2 DIM
 next x:rem                         ARRAY "location$()"
next y:rem                        < END OF LOOP CYCLE.
return
'=========================================================
'              DATA STATEMENTS START HERE:
'=========================================================
'Block types. Add Short descriptions next to numbers. 
'Up to 10 you can walk on, 11>40 you collide with.
'Also below these you'll find the descriptions map so
'that you can give each screen a title (required).

'1:grss1 5:path   9:blank 13:blank 17:blank 21: etc..
'2:grss2 6:blank 10:river 14:blank 18:blank 22: etc..
'3:brdgh 7:blank 11:blank 15:blank 19:blank 23: etc..
'4:brdgv 8:blank 12:blank 16:blank 20:blank 24: etc..
'=========================================================
'                  THE SCENERY MAP:
'=========================================================
REM  LOCATION 1:   LOCATION 2:    LOCATION 3:
data 10,10,10,2,2,2,  2,2, 2, 2,2,  2,   1,1,10,1, 1, 1
data 5, 5, 3, 5,5,5,  5,5, 5, 5,10, 2,   1,1,3, 1, 1, 1
data 2, 5,10, 2,2,2,  1,10,10,4,10, 2,   1,1,10,10,10,10
data 2, 5,10, 2,2,2,  1,1, 1, 5,2,  2,   1,1,1, 1, 1, 1   

REM  LOCATION 4:   LOCATION 5:    LOCATION 6:
data 1,5,10,1,1,1,    1,1,1,5,2,2,   1,1,1,1,1,1
data 1,5,10,1,1,1,    1,1,1,5,5,5,   5,5,5,5,5,5
data 1,5, 3,5,5,5,    5,5,5,5,2,2,   2,2,2,2,2,2
data 1,5,10,1,1,1,    1,1,1,5,2,2,   2,2,2,2,2,2   

REM  LOCATION 7:   LOCATION 8:    LOCATION 9:
data 1,5,10,1,1, 1,  2, 2, 2,5,2,2,  1,1,1,1,1,1
data 1,5,10,1,1, 1,  2, 2, 2,5,2,2,  1,1,1,1,1,1
data 1,5,10,4,10,10, 10,10,2,5,2,2,  1,1,1,1,1,1
data 1,5,1, 1,1, 1,  2, 10,2,5,2,2,  1,1,1,1,1,1   

REM  LOCATION 10:  LOCATION 11:   LOCATION 12:
data 1,5,1,1,1,1,  2,10,1,5,2,2,   2,2,2,2,2,2
data 1,5,1,1,1,1,  5,3 ,5,5,2,2,   2,2,2,2,2,2
data 1,5,1,1,1,1,  2,10,2,5,1,2,   2,2,2,2,2,2
data 1,5,1,1,1,1,  2,10,2,5,1,2,   2,2,2,2,2,2   

REM  LOCATION 13:  LOCATION 14:   LOCATION 15:
data 1,5,1,1,1,1,  2,10,2, 5, 2, 2,   1,1,1,1,1,1
data 1,5,1,1,1,1,  2,10,10,5, 5, 5,   1,1,1,1,1,1
data 5,5,5,5,5,5,  5,5, 10,4 ,10,5,   1,1,1,1,1,1
data 1,1,1,1,1,1,  1,5, 3, 5, 10,5,   5,5,5,5,5,5   

'=========================================================
'            The Location Description Map :
'=========================================================
data "THE PADDOCK","THE KISSING BRIDGE","THE MILL"
data "THE FARMYARD","DESERTED COTTAGE","PEDDLERS WAGGON"
data "THE FARMHOUSE","THE MINESHAFT","OLD HERMITS HUT"
data "THE TRADERS ROUTE","HUNTERS LODGE","LEAFY GLADE"
data "THE ANCIENT WALL","WEST VILLAGE","EAST VILLAGE"



