Code snippet: the shuffle bag

This is a moderated forum that collects tutorials, guides, and references for creating Transcendence extensions and scripts.
Post Reply
User avatar
pixelfck
Militia Captain
Militia Captain
Posts: 571
Joined: Tue Aug 11, 2009 8:47 pm
Location: Travelling around in Europe

Because random is sometimes more random than is good for a consistent gameplay experience, I made a tLisp version of the shuffleBag.

Code: Select all

(setq shuffleBagAdd (lambda (theBag theEntry entryCount)
	
	; Add an entry to the shuffleBag list
	; 
	; The following arguments are required
	;   theBag:         The list of entries in the shuffleBag
	;   theEntry:      The entry to add to the list of entries
	;   entryCount:      (Optional) the number of times to add the entry to the list of entries
	;
	; We return a struct with the following elements:
	;   contents:      The list of entries in the shuffleBag
	;   cursor:         The index of where to pick next from
	
	(block (contents i)
	   (setq contents (@ theBag 'contents))
	   (if entryCount
		  (for i 1 entryCount
			 (setq contents (append contents theEntry))
			 )
		  (setq contents (append contents theEntry))
		  )
	   
	   ; return value
	   {   contents: contents
		  cursor: (subtract (count contents) 1)
		  }
	   )
	))
 
 (setq shuffleBagDraw (lambda (theBag) ; NOTE: theBag is modified by reference
	
	; Pick the 'next' entry from the shuffle bag
	; 
	; The following arguments are required
	;   theBag:      The list of entries in the shuffleBag
	;
	; We return a struct with the following elements:
	;   mixed:      The entry that was picked from the shuffleBag list
	
	(block (result contents cursor)
	   (setq contents (@ theBag 'contents))
	   (setq cursor (@ theBag 'cursor))
	   (if (ls cursor 1)
		  (block Nil
			 (setq result (@ contents 0))
			 (setq cursor (subtract (count contents) 1))
			 )
		  (block (pick)
			 (setq pick (random 0 cursor))
			 (setq result (@ contents pick))
			 (set@ contents pick (@ contents cursor))
			 (set@ contents cursor result)
			 (setq cursor (subtract cursor 1))
			 )
		  )
	   (set@ theBag {
		  contents: contents
		  cursor: cursor
		  })
	   result
	   )
	))
 
 (setq shuffleBagSeededDraw (lambda (seed theBag) ; NOTE: theBag is modified by reference
 
	; Pick the 'next' entry from the shuffle bag
	; 
	; The following arguments are required
	;   seed:      The seed to base the 'random' pick
	;   theBag:      The list of entries in the shuffleBag
	;
	; We return a struct with the following elements:
	;   mixed:      The entry that was picked from the shuffleBag list
	
	(block (result contents cursor)
	   (setq contents (@ theBag 'contents))
	   (setq cursor (@ theBag 'cursor))
	   (if (ls cursor 1)
		  (block Nil
			 (setq result (@ contents 0))
			 (setq cursor (subtract (count contents) 1))
			 )
		  (block (pick)
			 (setq pick (seededRandom seed 0 cursor))
			 (setq result (@ contents pick))
			 (set@ contents pick (@ contents cursor))
			 (set@ contents cursor result)
			 (setq cursor (subtract cursor 1))
			 )
		  )
	   (set@ theBag {
		  contents: contents
		  cursor: cursor
		  })
	   result
	   )
	))
Example of usage

Code: Select all

(block (theBag result i n)
   ; Simple usage
   (setq theBag (shuffleBagAdd Nil "X"))
   (setq theBag (shuffleBagAdd theBag "O"))
   
   (for i 0 8
      (setq result (cat result (shuffleBagDraw theBag)))
      )
   (dbgLog result)
   
   ; weighted usage
   (setq theBag (shuffleBagAdd Nil "X" 2))
   (setq theBag (shuffleBagAdd theBag "O" 4))
   
   (setq result "")
   (for i 0 12
      (setq result (cat result (shuffleBagDraw theBag)))
      )
   (dbgLog result)
   )
Enjoy,

Pixelfck
Last edited by pixelfck on Tue Oct 15, 2013 4:33 pm, edited 1 time in total.
Image
Download the Black Market Expansion from Xelerus.de today!
My other mods at xelerus.de
RPC
Fleet Admiral
Fleet Admiral
Posts: 2876
Joined: Thu Feb 03, 2011 5:21 am
Location: Hmm... I'm confused. Anybody have a starmap to the Core?

Sweet, can't wait to try this out in a couple of months...
Tutorial List on the Wiki and Installing Mods
Get on Discord for mod help and general chat
Image
Image
Der Tod ist der zeitlose Frieden und das leben ist der Krieg
Wir müssen wissen — wir werden wissen!
I don't want any sort of copyright on my Transcendence mods. Feel free to take/modify whatever you want.
Post Reply