/* This script is supplied with the Mand2000 demo and release */
/* versions and may be freely distributed. */
/* Copyright 1993 Cygnus Software. */

/* This script has been updated for Mand2000 version 2.0, and now creates */
/* animation files directly. */

/* Set the total number of frames to be calculated. */

NumFrames = 35



/*
	This script is for calculating Julia seed movies.  As you drag
the  Julia  seed  across  the  Mandelbrot  set  the  Julia set changes
dramatically,  and  this  script  allows  you  to  easily calculate an
animation  of  these  changes,  thus allowing you to play them back in
real time.

	This  script  takes the julia seed location when you initially
run the script as the start point, and then asks you to select another
point.   It  then smoothly moves from one pointer to the other, saving
the frames out as calculated.  If you ask it to save out full screens,
then the Mandelbrot set with the seed pointer moving across it will be
saved also, showing the cause and effect.

	The  number  of  frames  calculated  can  easily be changed by
adjusting the variable at the top of the script.

	The resulting animation can be easily loaded into DPaint or any
animation player.
	*/

portname = address()	/* Retrieve the current port name. */
/* If the portname does not start with MAND2000 then this script must */
/* have been run with rx, rather than from Mand2000.  Therefore we */
/* need to set the port name.  We do not always set the port name */
/* because it is better to let Mand2000 set it for us, so that */
/* this script can be used with windows other than the one with */
/* port name MAND2000.1. */
if (left(portname, 8) ~= "MAND2000") THEN
	address 'MAND2000.1'

options results

/* Parse out the command option.  This script is called when the */
/* user wants a movie started, when the user wants a movie */
/* aborted and whenever one of the pictures in the sequence is done. */

parse arg command

command = upper(command)	/* Make sure the command is in upper case. */

if (command = START) then
	CALL StartJULIAMovie()
else if (command = STOP) then
	CALL StopJULIAMovie()
else
	CALL ContinueJULIAMovie()

Exit



CleanupStartup:
	/* Turn the julia seed requester off, if that's how it started. */
	if (juliaseedstate = 0) THEN
		REQUESTER JULIASEED OFF
	RETURN 0



StartJULIAMovie:
	GETATTR stem PROJ
	/* Julia fractal types are always odd. */
	if ((PROJ.FRACTALTYPE // 2) ~= 1) THEN DO
		DISPLAYMESSAGE PROMPT "A Julia window must be active when|making these movies.  Please|activate a Julia window and try|again."
		EXIT
		END

	GETATTR stem PROJ
	StartX = PROJ.JuliaX
	StartY = PROJ.JuliaY
	CALL SETCLIP("JuliaMovieStart.X", PROJ.JuliaX)
	CALL SETCLIP("JuliaMovieStart.Y", PROJ.JuliaY)

	DECPAUSE		/* Allow Mand2000 to resume calculating while this script executes. */

	REQUESTER JULIASEED	/* See whether the julia seed requester is up already. */
	juliaseedstate = RESULT
	REQUESTER JULIASEED ON	/* Force it to be up regardless. */

	REQUESTNOTIFY "The current seed location will be|used as the animation start.|Select the end seed location and|click `Continue'."

	GETATTR stem PROJ
	CALL SETCLIP("JuliaMovieEnd.X", PROJ.JuliaX)
	CALL SETCLIP("JuliaMovieEnd.Y", PROJ.JuliaY)

	/* Turn the julia seed requester off, if that's how it started. */
	if (juliaseedstate = 0) THEN
		REQUESTER JULIASEED OFF

	REQUESTSAVEFILE 'title="Julia anim file name."' 'path="RAM:"' 'file="Julia.Anim"'
	filename = result
	if (RC ~= 0) THEN DO
		REQUESTNOTIFY "No filename given."
		RETURN 0
		END

	REQUESTRESPONSE "Would you like full screen|images saved?"
	if (RC = 0) THEN
		OpenScreenAnim filename
	ELSE
		OpenAnim filename

	if (RC ~= 0) THEN DO
		REQUESTNOTIFY "Couldn't open requested file."
		RETURN 0
		END

	/* Put a command in the user menu for stopping the movie creation. */
	menu '"------------------------"'
	menu '"Stop Julia Movie"' JuliaMovie stop

	INCPAUSE		/* Stop calculations again. */
	CALL SETCLIP("Mand2000FrameNum", 1)
	/* Tell Mand2000 to call this script whenever a pictures finishes calculating. */
	EVENTACTION PICTUREDONE JuliaMovie

	SETJULIA StartX StartY
	RETURN 0



StopJULIAMovie:
	/* Tell Mand2000 not to call this script any more. */
	EVENTACTION PICTUREDONE
	/* Remove the `stop Julia movie' menu. */
	CLEARNMENUS 2
	FRAMENUM = GETCLIP("Mand2000FrameNum")
	if (FRAMENUM = "") THEN
		EXIT
	CloseAnim
	CALL SETCLIP("Mand2000FrameNum")
	if (FRAMENUM = 1) THEN
		REQUESTNOTIFY "Zero frames created."
	else if (FRAMENUM = 2) THEN
		REQUESTNOTIFY "One frame created."
	else
		REQUESTNOTIFY "Just created a "FRAMENUM - 1" frame Julia anim.|Use BigAnim or MainView to play it."
	RETURN 0



ContinueJULIAMovie:
	FRAMENUM = GETCLIP("Mand2000FrameNum")
	WriteAnimFrame
	if (RC ~= 0) THEN DO
		REQUESTNOTIFY "Error in saving frame "FRAMENUM".|Aborting animation.|Animation file may be corrupt."
		CALL StopJULIAMovie()
		EXIT
		END

	FRAMENUM = FRAMENUM + 1
	CALL SETCLIP("Mand2000FrameNum", FRAMENUM)

	FRAMENUM = FRAMENUM - 1	/* Subtract off the starting frame number. */
	IF (FRAMENUM > (NumFrames - 1)) THEN DO
		CALL StopJuliaMovie()
		EXIT
		END
	DONERATIO = FRAMENUM / (NumFrames - 1)	/* Calculate doneratio to be between 0.0 and 1.0. */
	StartX = GETCLIP("JuliaMovieStart.X")
	StartY = GETCLIP("JuliaMovieStart.Y")
	EndX = GETCLIP("JuliaMovieEnd.X")
	EndY = GETCLIP("JuliaMovieEnd.Y")

	/* Calculate a new julia seed, part way between start and end. */
	NewX = StartX + (EndX - StartX) * DONERATIO
	NewY = StartY + (EndY - StartY) * DONERATIO

	/* Set a new julia seed. */
	SETJULIA NewX NewY
	RETURN 0
