/* TicTacToe.thor by Troels Walsted Hansen (some additional stuff by Sigbjørn Skjæret)
** $VER: TicTacToe.thor v1.12 (30.05.95)
**
** Play TicTacToe!
**
** Tons of code ripped from:
** 'TicTacSpot v1.0 © 1993 PSR Software'
*/

TEMPDIR = 'T:'

/* needs THOR and bbsread.library functions */

options results

p = address() || ' ' || show('P',,)
thorport = pos('THOR.',p)

if thorport > 0 then thorport = word(substr(p,thorport),1)
else
do
	say 'No THOR port found!'
	exit 10
end

if ~show('p', 'BBSREAD') then
do
	address command
		"run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
		"WaitForPort BBSREAD"
end

/* start of script */

address(thorport)
SAVEMESSAGE CURRENT FILENAME '"' || TEMPDIR || 'TicTacToe.thor.temp' || '"' NOHEADER NOANSI

if ~Open('Inp',TEMPDIR'TicTacToe.thor.temp','r') then
do
   REQUESTNOTIFY TEXT '"Cannot open temporary file: ' || TEMPDIR || 'TicTacToe.thor.temp"' BT '"_Ok"'
   exit
end

do y=1 to 4
   do x=1 to 3
      POS.x.y=' '
   end
end

newgame = 0

do until pos('* TicTacToe',LINE)>0
   If Eof('Inp') Then Do
      REQUESTNOTIFY TEXT '"Message contains no TicTacToe data!"' BT '"_Ok"'
      REQUESTNOTIFY TEXT '"Start new game?"' BT '"_Yes|_No"'
      if(rc=0 & result = 1) then do
        newgame = 1
        call close('Inp')
        signal PlayMove
      end
      exit
   end
   if exists('Ram:Plop') then exit
   LINE=readln('Inp')
end

call readln('Inp')
LINE.1=readln('Inp')
call readln('Inp')
call readln('Inp')
LINE.2=readln('Inp')
call readln('Inp')
call readln('Inp')
LINE.3=readln('Inp')
call readln('Inp')
call readln('Inp')
xplayer = substr(readln('Inp'), 7)
oplayer = substr(readln('Inp'), 7)

call close('Inp')

do y=1 to 3
  parse var LINE.y POS1' | 'POS2' | 'POS3
  POS1=right(POS1,2)
  POS2=left(POS2,2)
  POS3=left(POS3,2)
  if POS1='/\' then POS.1.y='o'
  if POS1='\/' then POS.1.y='x'
  if POS2='/\' then POS.2.y='o'
  if POS2='\/' then POS.2.y='x'
  if POS3='/\' then POS.3.y='o'
  if POS3='\/' then POS.3.y='x'
end

PlayMove:

	address(thorport)
	CURRENTBBS stem CURRENT
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
		signal exit
	end

	address(bbsread)
	GETBBSDATA bbsname '"'CURRENT.BBSNAME'"' stem BBSDATA
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		signal exit
	end

	if(BBSDATA.USERNAME = '') then
	do
		GETGLOBALDATA stem GLOBALDATA
	        if(rc ~= 0) then
	        do
	        	address(thorport)
	        	REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
	        	signal exit
	        end

		if(GLOBALDATA.USERNAME = '') then signal exit
		else username = GLOBALDATA.USERNAME
	end
	else username = BBSDATA.USERNAME

	if(newgame) then xplayer = username

	select
		when(username = oplayer) then
		do
			PIECE = 'o'
		end

		when(username = xplayer) then
		do
			PIECE = 'x'
		end

		otherwise signal exit
	end

address(thorport)

DONE=0

do until DONE=1
  BOARD=POS.1.1 || '|' || POS.2.1 || '|' || POS.3.1 || '\n' || POS.1.2 || '[' || POS.2.2 || ']' || POS.3.2 || '\n' || POS.1.3 || '|' || POS.2.3 || '|' || POS.3.3
  REQUESTNOTIFY TEXT '"' || 'Top Row:\n' || board || '"' BT '"t_L|t_M|t_R|_Next|_Cancel"'
  if result=0 then exit
  if result=1 then if POS.1.1=' ' then do
    DONE=1
    POS.1.1=PIECE
  end
  if result=2 then if POS.2.1=' ' then do
    DONE=1
    POS.2.1=PIECE
  end
  if result=3 then if POS.3.1=' ' then do
    DONE=1
    POS.3.1=PIECE
  end
  if result=4 then do until DONE=1|result=4
    REQUESTNOTIFY TEXT '"' || 'Middle Row:\n' || board || '"' BT '"m_L|m_M|m_R|_Next|_Cancel"'
    if result=0 then exit
    if result=1 then if POS.1.2=' ' then do
      DONE=1
      POS.1.2=PIECE
    end
    if result=2 then if POS.2.2=' ' then do
      DONE=1
      POS.2.2=PIECE
    end
    if result=3 then if POS.3.2=' ' then do
      DONE=1
      POS.3.2=PIECE
    end
    if result=4 then do until DONE=1|result=4
      REQUESTNOTIFY TEXT '"' || 'Bottom Row:\n' || board || '"' BT '"b_L|b_M|b_R|_Next|_Cancel"'
      if result=0 then exit
      if result=1 then if POS.1.3=' ' then do
        DONE=1
        POS.1.3=PIECE
      end
      if result=2 then if POS.2.3=' ' then do
        DONE=1
        POS.2.3=PIECE
      end
      if result=3 then if POS.3.3=' ' then Do
        DONE=1
        POS.3.3=PIECE
      end
    end
  end
end

if(newgame) then
do
	EVENT.TYPE = 0 /* entermsg */

	address(bbsread)
	GETBBSLIST stem BBSLIST
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit 5
	end

	address(thorport)
	REQUESTLIST instem BBSLIST title '"Select BBS:"' SIZEGADGET
	if(rc ~= 0) then exit
	else MSG.BBSNAME = result

	address(bbsread)
        GETCONFLIST '"'MSG.BBSNAME'"' CONFLIST
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit 5
	end

	address(thorport)

	REQUESTLIST instem CONFLIST title '"Select conf:"' SIZEGADGET
	if(rc ~= 0) then exit
	else EVENT.CONFERENCE = result

	REQUESTSTRING TITLE '"Please enter subject of message:"' BT '"_Ok|_Cancel"' ID '"TicTacToe"' MAXCHARS 100
	EVENT.SUBJECT = result
	if(rc ~= 0 | EVENT.SUBJECT = "") then exit

	do forever
		REQUESTSTRING TITLE '"Please enter the name of your opponent:"' BT '"_Ok|_Cancel "' MAXCHARS 200
		if(rc ~= 0) then signal exit
		EVENT.TONAME = result

		if(upper(EVENT.TONAME) ~= "ALL") then
		do
			address(bbsread)
			SEARCHBRUSER bbsname '"'MSG.BBSNAME'"' stem USERS search '"'EVENT.TONAME'"' name address alias suggestusersstem SUG
			if(rc ~= 0) then signal exit

			if(result > 0) then
			do
				drop LIST.
				drop USERTAGS.

				LIST.COUNT = USERS.COUNT

				do n = 1 to USERS.COUNT
					LIST.n.USERNR = USERS.n.USERNR

					address(bbsread)
					READBRUSER bbsname '"'MSG.BBSNAME'"' usernr USERS.n.USERNR tagsstem USERTAGS
					if(rc ~= 0) then signal exit
					LIST.n = USERTAGS.NAME

					if(symbol("USERTAGS.ADDRESS") = "VAR") then
						LIST.n.ADDRESS = USERTAGS.ADDRESS
				end

				address(thorport)
				REQUESTLIST instem LIST title '"Select user:"'
				if(rc ~= 0) then
				do
					if(rc ~= 5) then REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
					signal exit
				end

				EVENT.TONAME = result

				do n = 1 to LIST.COUNT
					if(LIST.n = EVENT.TONAME) then
						EVENT.TOADDR = LIST.n.ADDRESS
				end
				leave
			end
			else
			do
				if(symbol("SUG.COUNT") = "VAR") then do
					address(thorport)
					REQUESTLIST instem SUG title '"Select user:"'
					if(rc ~= 0) then
					do
						if(rc ~= 5) then REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
						signal exit
					end

					EVENT.TONAME = result

					do n = 1 to SUG.COUNT
						if(SUG.n = EVENT.TONAME) then
							usernumber = SUG.n.USERNR
					end

					drop USERTAGS.

					address(bbsread)
					READBRUSER bbsname '"'MSG.BBSNAME'"' usernr usernumber tagsstem USERTAGS
					if(rc ~= 0) then signal exit

					if(symbol("USERTAGS.ADDRESS") = "VAR") then
						EVENT.TOADDR = USERTAGS.ADDRESS

					leave
				end
				else
				do
					address(thorport)
					REQUESTNOTIFY TEXT '"No matching users found, try again?"' BT '"_Ok|_Cancel"'
					if(rc ~= 0) then signal exit
					if(result = 0) then signal exit
				end
			end
		end
		else signal exit /* doesn't work with ALL */
	end

	oplayer = EVENT.TONAME
end
else
do
	CURRENTMSG stem MSG

	address(bbsread)
	READBRMESSAGE bbsname '"'MSG.BBSNAME'"' confname '"'MSG.CONFNAME'"' msgnr '"'MSG.MSGNR'"' headstem HEAD textstem TEXT

	EVENT.TYPE = 1 /* replymsg */
	EVENT.TONAME = HEAD.FROMNAME
	EVENT.SUBJECT = HEAD.SUBJECT
	EVENT.CONFERENCE = MSG.CONFNAME
	EVENT.REFNR = MSG.MSGNR
	EVENT.REFORGINALNR = HEAD.ORGINALNR
	/*EVENT.REFID = HEAD.REFID*/
	/*EVENT.TOADDR = TEXT.REPLYADDR*/
end

call open('Oup',TEMPDIR'TicTacToe.thor.temp','w')

LINE1=' >  '
LINE2=' >  '
LINE3=' >  '
LINE4=' >  '
LINE5=' >  '
LINE6=' >  '
If POS.1.1='x' Then Do
  LINE1=LINE1||'\/ |'
  LINE2=LINE2||'/\ |'
End
If POS.1.1='o' Then Do
  LINE1=LINE1||'/\ |'
  LINE2=LINE2||'\/ |'
End
If POS.1.1=' ' Then Do
  LINE1=LINE1||'   |'
  LINE2=LINE2||'   |'
End
If POS.2.1='x' Then Do
  LINE1=LINE1||' \/ |'
  LINE2=LINE2||' /\ |'
End
If POS.2.1='o' Then Do
  LINE1=LINE1||' /\ |'
  LINE2=LINE2||' \/ |'
End
If POS.2.1=' ' Then Do
  LINE1=LINE1||'    |'
  LINE2=LINE2||'    |'
End
If POS.3.1='x' Then Do
  LINE1=LINE1||' \/'
  LINE2=LINE2||' /\'
End
If POS.3.1='o' Then Do
  LINE1=LINE1||' /\'
  LINE2=LINE2||' \/'
End

If POS.1.2='x' Then Do
  LINE3=LINE3||'\/ |'
  LINE4=LINE4||'/\ |'
End
If POS.1.2='o' Then Do
  LINE3=LINE3||'/\ |'
  LINE4=LINE4||'\/ |'
End
If POS.1.2=' ' Then Do
  LINE3=LINE3||'   |'
  LINE4=LINE4||'   |'
End
If POS.2.2='x' Then Do
  LINE3=LINE3||' \/ |'
  LINE4=LINE4||' /\ |'
End
If POS.2.2='o' Then Do
  LINE3=LINE3||' /\ |'
  LINE4=LINE4||' \/ |'
End
If POS.2.2=' ' Then Do
  LINE3=LINE3||'    |'
  LINE4=LINE4||'    |'
End
If POS.3.2='x' Then Do
  LINE3=LINE3||' \/'
  LINE4=LINE4||' /\'
End
If POS.3.2='o' Then Do
  LINE3=LINE3||' /\'
  LINE4=LINE4||' \/'
End

If POS.1.3='x' Then Do
  LINE5=LINE5||'\/ |'
  LINE6=LINE6||'/\ |'
End
If POS.1.3='o' Then Do
  LINE5=LINE5||'/\ |'
  LINE6=LINE6||'\/ |'
End
If POS.1.3=' ' Then Do
  LINE5=LINE5||'   |'
  LINE6=LINE6||'   |'
End
If POS.2.3='x' Then Do
  LINE5=LINE5||' \/ |'
  LINE6=LINE6||' /\ |'
End
If POS.2.3='o' Then Do
  LINE5=LINE5||' /\ |'
  LINE6=LINE6||' \/ |'
End
If POS.2.3=' ' Then Do
  LINE5=LINE5||'    |'
  LINE6=LINE6||'    |'
End
If POS.3.3='x' Then Do
  LINE5=LINE5||' \/'
  LINE6=LINE6||' /\'
End
If POS.3.3='o' Then Do
  LINE5=LINE5||' /\'
  LINE6=LINE6||' \/'
End

  call writeln('Oup',' ')
  call writeln('Oup',' > * TicTacToe')
  call writeln('Oup',' >')
  call writeln('Oup',LINE1)
  call writeln('Oup',LINE2)
  call writeln('Oup',' > ----+----+----')
  call writeln('Oup',LINE3)
  call writeln('Oup',LINE4)
  call writeln('Oup',' > ----+----+----')
  call writeln('Oup',LINE5)
  call writeln('Oup',LINE6)
  call writeln('Oup',' >')
  call writeln('Oup',' > X: 'xplayer)
  call writeln('Oup',' > O: 'oplayer)
  call writeln('Oup',' ')

call close('Oup')

address(bbsread)
UNIQUEMSGFILE bbsname '"'MSG.BBSNAME'"' stem UNIQUEFILE
if(rc ~= 0) then
do
	address(thorport)
	REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
	exit
end

address command 'copy >nil: ' || TEMPDIR || 'TicTacToe.thor.temp' || ' TO ' || UNIQUEFILE.NAME
EVENT.MSGFILE = UNIQUEFILE.FILEPART

address(bbsread)
WRITEBREVENT bbsname '"'MSG.BBSNAME'"' event EVENT.TYPE stem EVENT
if(rc ~= 0) then
do
	address(thorport)
	REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
	exit 5
end

address(thorport)
SHOWTEXT TEMPDIR'TicTacToe.thor.temp'

address command 'delete >nil: ' || TEMPDIR || 'TicTacToe.thor.temp'

address(thorport)
REQUESTNOTIFY TEXT '"Edit message?"' BT '"_Yes|_No"'
if(result=1) then STARTEDITOR FILE '"'UNIQUEFILE.NAME'"'

exit

/*
 > * TicTacToe
 >
 >  \/ | /\ | \/
 >  /\ | \/ | /\
 > ----+----+----
 >  \/ | /\ | \/
 >  /\ | \/ | /\
 > ----+----+----
 >  \/ | /\ | \/
 >  /\ | \/ | /\
 >
 > X: Sigbjørn Skjæret
 > O: Troels Walsted_Hansen
*/

signal exit:
	address command 'delete' TEMPDIR'TicTacToe.thor.temp quiet'
	exit
