[ Jocelyn Ireson-Paine's Home Page | SF plot-generator | SF plot-generator PHP script ]

SF plot generator source

/*  spin.pl  */


/*  
S.F. plot generator, adapted somewhat broadly from 
"The Science Fiction Horror Movie Pocket Computer" by Gahan Wilson,
from "The Year's Best Science Fiction No. 5", edited by
Harry Harrison and Brian Aldiss, Sphere, London, 1972.
*/



/* element( X, Line, Y ):
     Line is a piece of text which makes sense
     to output at stage X in the story. The next
     stage after it will be Y.
*/
element( 0,'Earth', 1) .

element( 0,'Mars', 1) .

element( 0,'Planet 9 of Alpha-Centauri', 1) .

element( 1,'is used as the cue ball in a game of galactic bar-billiards', 2) .

element( 1,'falls toward the Sun', 2) .

element( 1,'falls toward a black hole', 2) .

element( 1,'is struck by a comet', 2) .

element( 1,'is invaded by nasty aliens', 2) .

element( 1,'is taken over by mutant diploid armour plated pterodactyls with ESP and silicon-based DNA', 2) .

element( 1,'is taken over by a time-travelling loony who returns to his youth and', 3) .

element( 3,'changes sex, meets himself, and has children who become', 7) .

element( 3,'kills himself (I said he was a loony)', end) .

element( 7,'yet more of this loony who', 3) .

element( 7,'the whole human race', end) .

element( 2,'and everyone dies', end) .

element( 2,'and almost everyone dies', end) .

element( 2,'and is visited by evil', 4) .

element( 2,'and is visited by good', 4) .

element( 4,aliens, 5) .

element( 4,robots, 5) .

element( 4,'mutant brewers yeast cells', 5) .

element( 5,who, 6) .

element( 5,who, 17) .

element( 6,'also die', end) .

element( 6,'save everyone', 9) .

element( 6,'rewind time to before the disaster', 11) .

element( 6,'copy the lot into a giant Sextium 3000 and edit out the nasty bits', 16) .

element( 9,'and depart', end) .

element( 9,'and give the secret of', 10) .

element( 11,'but then', 0) .

element( 11,'', 9) .

element( 10,'free fusion power', 13) .

element( 10,'free beer', 13) .

element( 10,'Life, the Universe, and Everything', 42) .

element( 10,'Life, the Universe, and Everything', 13) .

element( 10,'eternal life', 13) .

element( 42,'42', 13) .

element( 13,'so everyone gets very bored and tries to forget this by', 15) .

element( 13,'so everyone gets very bored and tries to forget this by', 14) .

element( 13,'so all live happily ever after', end) .

element( 15,'becoming Gods; creating a new Universe wherein', 0) .

element( 14,drinking, end) .

element( 16,'but the system crashes', end) .

element( 16,'but they mis-type "aliens" and take out all the lions instead', end) .

element( 16,'but they run out of credits', end) .

element( 16,'but they run out of budget', end) .

element( 16,'but they run out of CPU time', end) .

element( 16,'but they run out of memory', end) .

element( 16,'but the editor is in Prolog and they know only Basic', end) .

element( 17,'are wiped out by', 18) .

element( 17,'wish only to serve everyone', 21) .

element( 17,'save it and enslave everyone', end) .

element( 17,and, 22) .

element( 17,'are converted by the village priest (who tells them of God) to', 26) .

element( 17,'steal its reserves of', 27) .

element( 18,mumps, 19) .

element( 18,'atom-test radiation', 19) .

element( 18,'the village idiot', 19) .

element( 18,'the beer', 19) .

element( 19,'which then kills off everyone else', end) .

element( 19,'which then turns everyone else into supermen', 20) .

element( 20,'', end) .

element( 20,'', 13) .

element( 21,'(fried)', end) .

element( 21,'(boiled in white sauce)', end) .

element( 21,'', end) .

element( 22,'are stopped by a logician', 23) .

element( 23,'who says "I am lying"', 24) .

element( 23,'who can\'t remember the Paradox of the Liar and gets killed', end) .

element( 24,'and so he must be telling a falsehood', 25) .

element( 25,'so he must be telling the truth', 24) .

element( 26,'good ones', 5) .

element( 27,water, 28) .

element( 27,iron, 28) .

element( 27,'asparagus soup', 28) .

element( 27,beer, 28) .

element( 28,'', end) .

element( 28,and, 22) .


/* spin:
     Spin a story.
*/
spin :-
    spin( 0 ).


/* spin( X ):
     Spin a story starting at stage X.
     If X is 'end', just write 'THE END' 
     and stop.
*/
spin( end ) :-
    write( 'THE END' ), nl.

spin( N ) :-
    findall( [Line,Next], element(N,Line,Next), L ),
    random_element( L, [Line1,Next1] ),                   
    write( Line1 ), nl,
    spin( Next1 ).


/* random_element( List, Elem ):
     Pick a randomly chosen element of
     List into Elem.
*/
random_element( List, Elem ) :-
    length( List, N ),
    N > 0,
    I is random( N ),
    I_ is I + 1,
    nth1( I_, List, Elem ).


/* nth1( N, List, Elem ):
     Unify the Nth element of List with
     Elem.
*/
nth1(1, [Head|_], Head).

nth1(N, [_|Tail], Elem) :-
    M is N-1,
    nth1(M, Tail, Elem).
The main predicate is spin, which builds a plot by randomly choosing elements, defined by predicate element. Try it here. It's running under SWI-Prolog and PHP.

31st October 2008