/* Destroy by DragonLord */
options results;call time('r')
SS=Sendstring;TR=Transmit;GC=GetChar;Changewhere 'Destroy';vers1='1.11'
SIGNAL ON ERROR;SIGNAL ON SYNTAX;SIGNAL ON IOERR
numeric digits 14;def='[0m';cls='[H[J';p1='[0m[31m';p2='[0m[32m';p3='[0m[33m';p4='[0m[34m';p5='[0m[35m';p6='[0m[36m';p7='[0m[37m';p8='[30m[1m';p9='[31m[1m';pa='[32m[1m';pb='[33m[1m';pc='[34m[1m';pd='[35m[1m';pe='[36m[1 m';pf='[37m[1m';ss'z0'cls''p7
if ~show('L','rexxsupport.library') then do;if addlib('rexxsupport.library',0,-30,0) then nop;else do;tr'Can''t find support library!';exit 10;end;end
getuser 40;idnum=result;getuser 1;handle=result;bbsidentify name;bbsname=result
path='pfiles:Destroy/';path1=path'users/';d_flag=0;stop1=0;path0=path'users';path2=path'maps/';steps=0
do i=1 to 9;line.i='                   ';end i;handle=compress(handle);path3=path'Quests/'
rank.1='Serf';rank.2='Squire';rank.3='Guard';rank.4='Knight';rank.5='Royal Guard';rank.6='Lord';rank.7='Duke';rank.8='Marquis';rank.9='Arch Duke';rank.10='Emporer'

call forbid;f_flag=1
call open(1,path'date',r);date1=readln(1);call close(1)
if date1~=date() then call maint
call permit;f_flag=0

if exists('ram:t/D.'handle) then do;tr'Someone is attacking you. Try again later.';signal good_bye2;end

call forbid;f_flag=1
call open(1,'ram:t/D.'handle,w);call writeln(1,"NO!");call close(1)
call permit;f_flag=0

if exists(path1''handle) then call load_u
else call new_u
tr center('Welcome to Destroy!',78)
call king_check
call main
signal good_bye

king_check:
 if ~exists(path'king.dat') then king='No One'
 else do;call open(1,path'king.dat',r);king=readln(1);call close(1);end
 tr p9'Current King: 'pf''king;call pcr
return

maint:
 call open(1,path'date',w);call writeln(1,date());call close(1)
 ss cls''p9'Running Maintenance... Please hold...'
 users=showdir(path0);num=words(users)
 do ii=1 to num
 han1=word(users,ii)
 call load_ou(han1)
 ou_.5=ou_.6
 ou_.15=100
 ou_.17=ou_.17+1
 maint_flag=1
 call save_ou(han1)
 maint_flag=0
 ss'.'
 end ii
 tr'Complete!'
return

new_u:
 tr'We welcome you, "new" destroyer...'
 query'What name would you like to be known as: ';call gcc(0);if com1~="" then nam=com1
 u_.1=nam;u_.2=1;u_.3=0;u_.4=10;u_.6=rnd(10,20);u_.5=u_.6;u_.7=0;u_.8=rnd(4,6);u_.9=rnd(4,6);u_.10=0;u_.11=0;u_.12=u_.8;u_.13=u_.9;u_.14=0;u_.15=100;u_.16=rnd(60,120);u_.17=16;u_.18='000000000';u_.19='Serf'
 call save_u;call pcr;call read_doc
return

load_u:
 call open(1,path1''handle,r)
 do i=1 to 19;u_.i=readln(1);end i
 call close(1)
 if u_.5<1 then u_.5=u_.6
 if u_.17>u_.16 then call age_death
 call save_u
return

age_death:
 tr'Your age overcomes you, and you pass away.. You are destroyed.'
 age=1
call erase

main:
 do until done=1
  tr cls'  !DESTROY! v'vers1' by DragonLord'
  tr '- Main Menu -'
  tr 'V)iew Stats'
  tr 'A)rena'
  tr 'E)nter Dungeon'
  tr 'S)tore'
  tr 'G)et Information on Quest'
  tr 'L)ist Users'
  tr 'R)ead Docs!'
  tr 'Q)uit'
  ss'Command> ';gc;call gcc(1)
  select
   when com1='V' then call stats
   when com1='A' then call arena
   when com1='E' then call load_dun
   when com1='S' then call store
   when com1='L' then call list_u
   when com1='R' then call read_doc
   when com1='G' then call quest_info
   when com1='Q' then done=1
   otherwise nop
  end
 end
return

quest_info:
 if u_.19=rank.10 then do;tr'You are an 'rank.10' already! You have no quests!';call pcr;return;end
 do i=1 to 9;if gd(u_.18,i)=0 then leave i;end i
 ss cls''pf;sendfile path3'quest.'i
 call pcr
return

read_doc:
 ss cls''pf
 sendfile path'doc.read_me'
 call pcr
return

load_dun:
 key_find=0;d_lvl=1;d_1=35;d_2=11;call st2x(d_lvl);call st2;new_d=1;call st2a
 if key_find=1 then call quest_complete(2)
return

st2x:
 parse arg d_lvl
 ss'Loading map...';call open(1,path2'map'd_lvl'.dat',R)
 do i=1 until eof(1);map.i=readln(1);end;call close(1);tr;d_tot=i-1
 call open(1,path2'map'd_lvl'.inf',R);num1=readln(1);dat1=readln(1);call close(1)
 parse var num1 st_.1','st__.1'|'st_.2','st__.2'|'st_.3','st__.3'|'st_.4','st__.4'|'st_.5','st__.5 /* 1=Up stairs, 2=Down, 3=Weapons, 4=Armor, 5=Messages*/
 do i=1 to st__.5;parse var dat1 x_c1.i','y_c1.i','id_x1.i'|'dat1;end i
 do i=st_.5 to st__.5;mess.num=0;end
 killed=0;hi_step=30;if d_lvl=1 & gd(u_.18,1)=0 then hi_step=10
return

st2:
 ss cls
 tr p2'[1;1H                          ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»'
 do i=1 to 9;if i~=5 then tr'                           º'p7''line.i''p2'º'
 else tr'                           º'p7''overlay('o',line.5,10,1)''p2'º'
 end
 tr'                           ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ¼'
 ff=0
return

st2a:
 donem=0
 do until donem=1
  call load_map1
  ss p1'[12;1H[KV,R'dir.1''dir.2''dir.3''dir.4''dir.5''dir.6',Q,?: 'pf;gc;call gcc(1)
  select
   when (com1='N'|com1='8') & dir.1=',N' then d_1=d_1-1
   when (com1='S'|com1='2') & dir.3=',S' then d_1=d_1+1
   when (com1='E'|com1='6') & dir.2=',E' then d_2=d_2+1
   when (com1='W'|com1='4') & dir.4=',W' then d_2=d_2-1
   when com1='U' & dir.5=',U' then call floor_adv
   when com1='D' & dir.6=',D' then call floor_dec
   when com1='V' then do;call stats;new_d=1;end
   when com1='Q' then signal good_bye
   when (com1='R'|com1='5') then call st2
   when com1='?' then ss pf'[13;1H[KQ)uit,R)edisplay screen,V)iew Stats,N)orth,S)outh,E)ast,W)est,U)p,D)own'
   otherwise nop
  end
  if d_1~=d_1o|d_2~=d_2o then do;call fight_find;if ff=1 then call st2;call load_map1;end
 end
return

floor_adv:
 d_lvl=d_lvl-1;if d_lvl=0 then do;donem=1;return;end
 call st2x(d_lvl);call st2;new_d=1
return

floor_dec:
 d_lvl=d_lvl+1;call st2x(d_lvl);call st2;new_d=1
return

fight_find:
 u_.5=u_.5+1;if u_.5>u_.6 then u_.5=u_.6;steps=steps+1;fight=rnd(1,hi_step)
 if fight<=steps then call fight_find2
return

fight_find2:
 steps=0;lo=d_lvl*25-24;hi=d_lvl*25;lvl=rnd(lo,hi);tr cls'You encounter a level 'lvl' monster!';u_.15=u_.15+1;call fight_m(lvl);ff=1;if m_.1<1 then do;killed=killed+1;if killed>50 & gd(u_.18,1)=0 then call quest_complete(1);end
return

quest_complete:
 parse arg ques;ques1=ques+1
 ss cls''pf;sendfile path3'quest.'ques'.complete';u_.18=overlay(1,u_.18,ques,1);u_.19=rank.ques1;call pcr
return

load_map1:
 if d_1=d_1o & d_2=d_2o & new_d=0 then return;new_d=0
 do i=1 to 6;dir.i='';end
 do j=1 to 9
  xx=d_2+9;yx=d_1-5+j;xy=left(map.yx,xx);line.j=right(xy,19);lin=j+1
  if j~=5 then tr p7'['lin';28H'line.j;else tr'['lin';28H'overlay('o',line.5,10,1)
  li1=d_1+1;li2=d_1-1;li3=d_2+1;li4=d_2-1;li5=d_1;li6=d_2
  if j=4|j=6 then do;lx=left(line.j,10);lx=right(lx,1);if lx=' ' & j=4 then dir.1=',N';if lx=' ' & j=6 then dir.3=',S';end
  if j=5 then do;lx=left(line.j,9);lx=right(lx,1);if lx=' ' then dir.4=',W';lx=left(line.j,11);lx=right(lx,1);if lx=' 'then dir.2=',E';end
 end
 d_1o=d_1;d_2o=d_2
 do i=st_.1 to st__.1;if d_1=x_c1.i & d_2=y_c1.i then dir.5=',U';end i
 do i=st_.2 to st__.2;if d_1=x_c1.i & d_2=y_c1.i then dir.6=',D';end i
 do i=st_.3 to st__.3;if d_1=x_c1.i & d_2=y_c1.i then call find_itm(10);end i
 do i=st_.4 to st__.4;if d_1=x_c1.i & d_2=y_c1.i then call find_itm(11);end i
 do ii=st_.5 to st__.5;if d_1=x_c1.ii & d_2=y_c1.ii then call show_mes;end ii
  tr'[13;1H[K'
  if dir.5=',U' then ss pf'[13;1H[KThere is a flight of stairs going up here!'
  if dir.6=',D' then ss pf'[13;1H[KThere is a flight of stairs going down here!'
return

find_itm:
 parse arg type1;tim=type1+2;tim1=type1-2;type2='Weapon';if type1=11 then type2='Armor'
 lvl=id_x1.i;if u_.type1>=lvl then return
 tr p9'[13;1H[KYou found a Level 'lvl' 'type2'!'
 u_.type1=lvl;u_.tim=u_.type1+u_.tim1;new_d=1;call pcr
return

show_mes:
 if (gd(u_.18,1)=0|gd(u_.18,2)=1) & id_x1.ii=4 & d_lvl=2 then return
 num=id_x1.ii;if mess.num=1 then return;mess.num=1
 ss cls''pf'You find a message that says:'p9
 sendfile path2'mes.'d_lvl'.'num;new_d=1
 if d_lvl=2 & num=4 then key_find=1
 call pcr;call st2
return

stats:
 do i=2 to 14;u_.i=u_.i%1;end i
 tr cls'Name: 'pf''u_.1''p7' ('p9''u_.19''p7')'
 tr ' Lvl: 'pf''right(u_.2,14)''p7' / 'pf''left(u_.7,14)''p7' :GP'
 tr ' Exp: 'pf''right(u_.3,14)''p7' / 'pf''left(u_.4,14)''p7' :Need'
 tr '  HP: 'pf''right(u_.5,14)''p7' / 'pf''left(u_.6,14)''p7' :MAX'
 tr ' Str: 'pf''right(u_.8,14)''p7' / 'pf''left(u_.9,14)''p7' :Def'
 tr '  WL: 'pf''right(u_.10,14)''p7' / 'pf''left(u_.11,14)''p7' :AL'
 tr '  BP: 'pf''right(u_.12,14)''p7' / 'pf''left(u_.13,14)''p7' :BD'
 tr ' Age: 'p9''right(u_.17,14)''p7' / 'pf''left(u_.14,14)''p7' :Deaths'
 call pcr
return

arena:
 done1=0
 do until done1=1
  tr cls'- Arena -'
  tr'M)onster Fight'
  tr'U)ser Fight'
  tr'H)ealing House'
  tr'V)iew Stats'
  tr'Q)uit to Main Menu'
  ss'Command> ';gc;call gcc(1)
  select
   when com1='M' then call m_fight
   when com1='U' then call u_fight
   when com1='H' then call heal
   when com1='V' then call stats
   when com1='Q' then done1=1
   otherwise nop
  end
 end
return

heal:
 need=u_.6-u_.5
 if need>0 then do
  tr cls'You need 'need' HPs to be healed.'
  tr'It costs 1 GP/HP.'
  tr'You have 'u_.7' GPs.'
  if u_.7>0 then do
   Query'Heal how many? ';call gcc(0)
   if com1>need then com1=need
   if com1>u_.7 then com1=u_.7
   if com1<0 then com1=0
   if com1>0 then do
    tr'The cleric heals you.'
    u_.5=u_.5+com1
    u_.7=u_.7-com1
   end
  end
  call pcr
 end
return

m_fight:
 if u_.15<1 then do;tr'No more battles left.';call pcr;return;end
 tr'Battles left: 'u_.15
 lo=1;hi=u_.2%1.2;if hi<1 then hi=1
 query'Which level? (1-'hi'): ';call gcc(0)
 if com1>lo-1 & com1<hi+1 then call fight_m(com1)
return

fight_m:
 parse arg lvl
 if lvl>0 then do;m_.1=lvl*10;m_.4=m_.1;m_.2=2*lvl+rnd(1,2);m_.6=m_.2;m_.3=(1+((lvl-1)*.5))*lvl%1;m_.5=m_.3%1;end
 u_.15=u_.15-1;stop=0
 do until stop=1
  call fight_com
  select
   when com1='F' then call fight_f
   when com1='R' then do;call run_check;if runaway=0 then do;tr'You run away...';if uf_flag=1 then do;ux_gain=0;ug_gain=0;call save_ou;end;call pcr;stop=1;end;else call mon_fi;end
   otherwise nop
  end
 end
 if die=2 then call mon_die
 if die=1 then call you_die
 die=0
return

run_check:
 run=rnd(1,10);runaway=0
 if run>4 then return
 else tr'You attempt to run away but are blocked!';runaway=1
return

save_ou:
 if maint_flag~=1 then do;ou_.5=m_.1;ou_.3=ou_.3+ux_gain;ou_.7=ou_.7+ug_gain;call forbid;f_flag=1;end
 call open(1,path1''han1,w);do i=1 to 19;call writeln(1,ou_.i);end;call close(1)
 if maint_flag~=1 then do;if exists('ram:t/d.'han1) then address command 'delete "ram:t/d.'han1'"';call permit;f_flag=0;end
return

mon_die:
 if lvl>0 then tr'You have defeated the Level 'pf''lvl''p7' Monster!'
 else tr'You have defeated 'ou_.1'!'
 tr'Of experience  you have gained 'pf''m_.3''p7'!'
 tr'Of gold pieces you have gained 'pf''m_.5''p7'!'
 u_.3=u_.3+m_.3;u_.7=u_.7+m_.5
 tr pf'Exp: 'p9''u_.3;tr pf' GP: 'p9''u_.7''p7
 do until u_.3<u_.4|no_gain=1
  if u_.3>=u_.4 then call lvl_up
 end
 if lvl=0 then do;ux_gain=0;ug_gain=-ou_.7;call save_ou;end
 no_gain=0;call pcr
return

need_quest:
 tr'You need to complete quest 'x1' before attaining level 'x1*100'!'
 call pcr
return

lvl_up:
 if u_.2+1//100=0 then do
  x1=u_.2+1%100
  if x1<11 then do
   if gd(u_.18,x1)=0 then do;call need_quest;no_gain=1;return;end
  end
 end
 tr pf'You have advanced a level!'p7
 u_.2=u_.2+1;tr'-=*'p1'Level 'p9''u_.2''p7'*=-';u_.4=(1+((u_.2-1)*.5))*u_.2*20
 hit=rnd(10,20);u_.6=u_.6+hit;u_.5=u_.6
 tr'You have gained 'p9''hit''p7' Hit Points!'
 tr pf'HP: 'u_.5' / 'u_.6' :MAX'p7
 duff=0
 do until duff=1
  ss'Advance S)trength['p9''u_.8''p7'] or D)efense['p9''u_.9''p7']: ';gc;call gcc(1)
  if com1='S' then do;u_.8=u_.8+1;u_.12=u_.12+1;duff=1;end
  if com1='D' then do;u_.9=u_.9+1;u_.13=u_.13+1;duff=1;end
 end
 call save_u
return

you_die:
 tr'You are dead!';u_.14=u_.14+1;if u_.14>u_.2 then call erase
 u_.5=u_.6;ux_gain=u_.2*10%1.2;ug_gain=u_.7;u_.7=0
 if lvl=0 then call save_ou
signal good_bye

erase:
 if age~=1 then tr'Your deaths have exceeded your level. You are destroyed!'
 address command 'delete "'path1''handle'"'
 call forbid;f_flag=1
 if exists('ram:t/d.'handle) then address command 'delete "ram:t/D.'handle'"'
 call permit;f_flag=0
signal good_bye2

fight_com:
 x1=u_.5/u_.6*100;x2=m_.1/m_.4*100
 cl1=p9;if x1>25 then cl1=p1;if x1>50 then cl1=pb;if x1>75 then cl1=pf
 cl2=p9;if x2>25 then cl2=p1;if x2>50 then cl2=pb;if x2>75 then cl2=pf
 ss'<'cl1''u_.5''p7'> <'cl2''m_.1''p7'> F)ight or R)un> ';gc;call gcc(1)
return

fight_f:
 stop=0;hit=rnd((u_.12%1.2)-(m_.6%2),u_.12-(m_.6%2));if hit<0 then hit=0
 if lvl>0 then tr'You swing at the Level 'lvl' Monster and hit for 'hit' points of damage!'
 else tr'You swing at 'ou_.1' and hit for 'hit' points of damage!'
 m_.1=m_.1-hit;if m_.1<1 then do;stop=1;die=2;end
 if stop=0 then call mon_fi
return

mon_fi:
 hit=rnd((m_.2%1.2)-(u_.13%2),m_.2-(u_.13%2));if hit<0 then hit=0
 if lvl>0 then tr'The Level 'lvl' Monster swings at you and hits for 'hit' points of damage!'
 else tr ou_.1' swings at you and hits for 'hit' points of damage!'
 u_.5=u_.5-hit;if u_.5<1 then do;stop=1;die=1;end
return

load_ou:
 parse arg nam0
 call open(1,path1''nam0,r)
 do i=1 to 19;ou_.i=readln(1);end
 call close(1)
return

u_fight:
 if u_.15<1 then do;tr'No more battles left.';call pcr;return;end
 tr'Battles left: 'u_.15;call pcr;stop1=1
 dead_f=1;call list_u;dead_f=0
 query'Enter user number: ';call gcc(0)
 if com1<1|com1>num then return
 han1=word(users,com1);if han1=handle then return;call load_ou(han1)
 if exists('ram:t/d.'han1)|ou_.5<1 then return
 call forbid;f_flag=1
 call open(1,'ram:t/d.'han1,w);call writeln(1,'NO!');call close(1)
 call permit;f_flag=0
 m_.1=ou_.5
 m_.3=ou_.2*10%1.2
 m_.5=ou_.7%1
 m_.2=ou_.12
 m_.6=ou_.13
 m_.4=ou_.6
 uf_flag=1;call fight_m(0);uf_flag=0
return

store:
 done1=0
 do until done1=1
  tr cls'- Store -'
  tr'W)eapon Purchase'
  tr'A)rmor Purchase'
  tr'Q)uit to Main Menu'
  ss'Command> ';gc;call gcc(1)
  select
   when com1='W' then call buy(10)
   when com1='A' then call buy(11)
   when com1='Q' then done1=1
   otherwise nop
  end
 end
return

buy:
 parse arg typ
 if typ=10 then type1='Weapon'
 if typ=11 then type1='Armor'
 x1=u_.typ+1;tim=typ+2
 cost=(1+((x1-1)*.6))*x1*10
 tr'It costs 'cost' GP for a Level 'x1' 'type1'.'
 tr'You have 'u_.7' GPs.'
 if u_.7<cost then do;tr'You do not have enough.';call pcr;return;end
 ss'Purchase? (Y/n) ';gc;call gcc(1)
 if com1='Y' then do;tr'Bought!';u_.7=u_.7-cost;u_.typ=u_.typ+1;u_.tim=u_.tim+1;end
return

list_u:
 tr cls''pf'##) Name_____________________ Level Status Rank________ Playing'
 users=showdir(path0);num=words(users)
 do ii=1 to num
  han1=word(users,ii);call load_ou(han1);on1='Offline';stat='Alive'
  if exists('ram:t/D.'han1) then on1='Playing';if ou_.5<1 then stat='Dead'
  stop1=0;if dead_f=1 & (ou_.5<1|on1="Playing") then stop1=1
  if stop1=0 then tr p7''right(ii,2)''p2') 'p3''left(ou_.1,25)''pc' 'left(ou_.2,5)' 'p5''left(stat,5)'  'p6''left(ou_.19,12)' 'p9''on1''p7
 end ii
 call close(1);if dead_f~=1 then call pcr;stop1=0
return

save_u:
 call open(1,path1''handle,w)
 do i=1 to 19;call writeln(1,u_.i);end i
 call close(1)
return

gd: procedure
 parse arg var,di
 x=left(var,di);x=right(x,1)
return x

rnd: procedure
parse arg lo,hi
x=random(900,999,time('s'))
x=((x//(hi-lo+1))+lo)
return x

pcr:
 ss p7'Press any key to continue!';gc;call gcc(0);ss'[1A[K'
return

gcc:
 arg typ1
 com1=result;GETCARRIER;if result="FALSE" & d_flag=0 then signal good_bye
 if typ1=1 then tr p7''com1
 else tr
RETURN

ERROR:
 tr'Error!';sigl1=sigl
signal enderr

SYNTAX:
 tr'Syntax!';sigl1=sigl
signal enderr

IOERR:
 tr'Ioerr!';sigl1=sigl
signal enderr

enderr:
 if e_flag=1 then signal good_bye
 e_flag=1
 lce='Error Code: 'rc'* 'errortext(rc)', on line 'SIGL1
 tr lce
 call open(1,path'error.txt',w);call writeln(1,lce);call close(1)
 cleareditor;setmailsubj "Error in Destroy"
 loadeditor "pfiles:destroy/error.txt"
 writemail 18;cleareditor
signal good_bye

good_bye:
 tr'Saving and quitting...'
 call save_u
 if exists('ram:t/d.'handle) then address command 'delete "ram:t/D.'handle'"'

good_bye2:
 if f_flag=1 then call permit
 if uf_flag=1 then do;ux_gain=0;ug_gain=0;call save_ou;end
 bufferflush
exit
