AB

Runs on the Ancient Brain system have the following global variable defined:

 

 AB;               // instance of class ABClass - basic functionality for Ancient Brain 
 

AB contains the following data and functions. Default values shown.



Global settings

 

AB.clockTick       = 100;    

	// Speed of run: Step every n milliseconds.  
	
	
AB.maxSteps        = 1000;    

	// Length of run: Maximum length of run in steps.  

	
AB.screenshotStep  = 50;   
  
	// For automatic generation of World images.
	// Take screenshot on this step. (All resources should have finished loading.)  

		
AB.drawRunControls = true;			

	// Add run controls (Run/Step/Pause) to the run window  
	


The run

 
   
AB.step;               // the step of the run   

AB.abortRun = false;   // set this to true anytime to end the run   
	


Delayed run, loading screen, splash screen

We often want to delay the start of the run until resources are loaded, or until the user has clicked on a splash screen. Here is how to do it.

A loading screen vanishes automatically when resources load. A splash screen needs user interaction to dismiss.

 

AB.runReady = true;              // Start the run loop immediately   
 
AB.loadingScreen();              // draw a standard loading screen   
AB.loadingHtml ( html );         // define entire HTML of the loading screen  
AB.removeLoading();              // remove loading screen 

AB.newSplash();                  // draw a standard splash screen  
AB.newSplash ( html );           // draw a standard splash screen plus a HTML message 
AB.splashHtml ( html );          // define entire HTML of the splash screen  
AB.splashClick ( fn );           // define function when splash screen clicked    
AB.removeSplash();               // remove splash screen 



Run header

 

AB.showRunHeader();     // Show run header (in case does not show automatically)
AB.hideRunHeader();     // Hide run header (still exists)

// The run header div floats on top of the canvas. 
// Sometimes this interferes with other page elements and need to:

AB.removeRunHeader();   // Remove run header entirely


AB.headerCSS ( css );   // Give run header some css  
  
AB.headerLHS();         // Put run header on LHS   
AB.headerRHS();         // Put run header on RHS 


AB.headerWidth ( w );   // Give run header a fixed width  
  // Useful if writing info every step into (variable width) header. 
  // Can lead to header "flicker" as it keeps changing width.  
  // Solution: Give it fixed width. 
  
  
AB.msg ( html, n );     // Write a message (html) to a numbered location in the run header  
AB.msg ( html );        // Write message to location 1

AB.newDiv ( id );       // Create new div on page with this id (can then set style, content) 



Run functions

 

AB.loadCSS ( file );                 // load CSS styling from this file
AB.loadCSSstring ( string );         // load CSS styling from this string 

AB.onDesktop();                      // return if running on desktop
AB.onMobile();                       // return if running on mobile 

AB.backgroundMusic ( musicfile );    // play background music, draw buttons, return audio object 
AB.standardAudioButtons ( audio );   // draw standard play/pause buttons for this audio 
AB.audioIsPlaying ( audio );         // return if this audio is currently playing 

AB.isScreenshotRun();                // is this a run that generates a screenshot 



Launch buttons

These functions allow you embed in your run a "launch button" to run Worlds, like the launch buttons used on the main Ancient Brain site. The "button" is a small World image, hover makes it zoom, and click starts a run.

These functions return the HTML code needed to make the "button". You then put this HTML code somewhere in the run page.

 

AB.launchWorld ( worldid, worldtitle );                          // returns HTML string 
AB.launchWorldMind ( worldid, worldtitle, mindid, mindtitle );   // returns HTML string



Save and restore data

If you run "logged in" you can save and restore data to and from the server.

 

AB.runloggedin;                 // Boolean. Are we running logged in.  
AB.myuserid;                    // The userid of the run, if running logged in.  

AB.saveData ( object );             
    // Save this object to the server for this user for this World.

AB.queryDataExists ( function ( boolean ) { ... } );  
    // Query if data exists on the server for this user for this World.
    // When it returns, it calls the callback function with boolean (exists or not).

AB.restoreData ( function ( object ) { ... } );      
    // Recover the data saved for this user for this World.
    // When it returns, it calls the callback function with the object. 
					
AB.getAllData ( function ( array ) { ... } );    
    // Get all data saved for all users for this World.
    // When it returns, it calls the callback function with an array. 
    // This is an array of items ( userid, username, object ). 



Websockets

Ancient Brain Worlds come with Websocket support to allow real-time communication between different users running the same World. User actions in one client can make changes in the World on other clients. This allows you to write your own real-time multi-user games. It also allows real-time chat between the users of your World.

 

AB.socketStart();                  // start socket for this World

AB.socketOut ( data );             // send any JS data to all other clients 

// Could check socket has been set up first (socket takes time to set up):
if ( AB.socket )
  if ( AB.socket.connected )
    AB.socketOut ( data );
	 
AB.socketIn = function ( data ) { ... };   
  // Handler for incoming data from other clients.
  // World author must define this function.

AB.socketUserlist = function ( array ) { ... };
  // Keep track of other users in multi-user World.
  // The server triggers this to tell us the latest list of users of this World. 
  // This is called when we connect, and any time another user arrives or leaves.
  // The array is a list of (userid, username) pairs. 
  // World author must define this function (what to do with array) if desired.
	  
 


Generic functions

 

AB.randomFloatAtoB ( A, B );         // return random float between A and B
AB.randomIntAtoB ( A, B );           // return random int between A and B

AB.randomEventProb ( p );            // return boolean should event of this probability happen now
AB.randomBoolean();                  // return random true or false 
AB.randomPick ( a, b );              // return a or b randomly 
AB.randomPick3 ( a, b, c );          // return a, b or c randomly 
AB.randomElementOfArray ( array );   // return random element of this array 

AB.randomColor();                    // return random color from 0x000000 to 0xffffff

AB.distance2D ( x1, y1, x2, y2 );    // return distance between two points on 2D plane 

AB.escapeHTML ( string );            // return string with all HTML chars escaped
AB.escapeHTMLshort ( string );       // just escape < and >