Wall Clock Capabilities

104 views
Skip to first unread message

noiv

unread,
Nov 5, 2011, 9:48:34 PM11/5/11
to si...@googlegroups.com
Hello,

I want to use sim.js to simulate simplified economy. The user (invisible hand) can interact with the simulation and should also see what is happening. The entities are constraint by physical parameters while moving from market to market with a given speed and are visible on a map. One goal is to simulate price formation over time show how individual actors on the market use transportation.

I understand this is not exactly what sim.js is build for, however after looking at and debugging the examples, it looks like sim.js has all I need to implement the simulated economy very fast. All missing is a bit flexibility with time.

Basically what I need is running the simulation forever until abort and having e.g. useFacility().done() actually waiting n seconds until it fires. 

In case this demand is not on your list - could you give me a starting point to do the changes?

Many thanks for any input,

-- Torsten

noiv

unread,
Nov 6, 2011, 1:17:32 PM11/6/11
to si...@googlegroups.com
Was easier than thought, sim.js is quite matured!

Sim.prototype.step = function (msecs) {

  var ro, q = this.queue.data;

  // Calls init() on all entities only on first step
  if (this.simTime === 0 ) {
    this.simTime = Date.now();
    this.entities.forEach(function(e){
      e.init();
    })
  }

  // Advance simulation time
  this.simTime += msecs ;

  while (true){

    // nothing to do
    if (q.length <= 0) {break;}

    // check first
    if (q[0].deliverAt <= this.simTime) {

      ro = this.queue.remove();

      // If this event is already cancelled, ignore
      if (ro.cancelled) {continue;}

      // execute callbacks
      ro.deliver();

    } else {
      
      // done for this time
      break;
    }
    
  }

}
Reply all
Reply to author
Forward
0 new messages