def update(self): self.syst = time.clock() if self.mode == Simulation.init: self.operate() # Closk for run(=op) mode if self.mode == Simulation.op: # Not fast forward: variable dt if not self.ffmode: self.tprev = self.simt self.simt = self.syst - self.syst0 self.simdt = self.simt - self.tprev # Protect against incidental dt's larger than 1 second, # due to window moving, switch to/from full screen, etc. if self.simdt > 1.0: extra = self.simdt-1.0 self.simt = self.simt - extra self.syst0 = self.syst-self.simt # Fast forward: fixed dt until ffstop time, goto pause else: self.simdt = self.fixdt self.simt = self.simt+self.fixdt self.syst0 = self.syst - self.simt if self.ffstop > 0. and self.simt >= self.ffstop: self.ffmode = False self.mode = self.hold # Update simulated clock time self.simtclock = (self.simt + self.deltclock)%onedayinsec # Datalog pre-update (communicate current sim time to loggers) datalog.preupdate(self.simt) # Plugins pre-update plugin.preupdate(self.simt) # For measuring game loop frequency self.dts.append(self.simdt) if len(self.dts) > 20: del self.dts[0] stack.checkfile(self.simt) # Always process stack stack.process() if self.mode == Simulation.op: bs.traf.update(self.simt, self.simdt) # Update metrics self.metric.update() # Update plugins plugin.update(self.simt) # Update loggers datalog.postupdate() # HOLD/Pause mode else: self.syst0 = self.syst-self.simt self.simdt = 0.0 return
def update(self): self.syst = time.clock() if self.mode == Simulation.init: self.operate() # Closk for run(=op) mode if self.mode == Simulation.op: # Not fast forward: variable dt if not self.ffmode: self.tprev = self.simt self.simt = self.syst - self.syst0 self.simdt = self.simt - self.tprev # Protect against incidental dt's larger than 1 second, # due to window moving, switch to/from full screen, etc. if self.simdt > 1.0: extra = self.simdt - 1.0 self.simdt = 1.0 self.simt = self.simt - extra self.syst0 = self.syst - self.simt # Fast forward: fixed dt until ffstop time, goto pause else: self.simdt = self.fixdt self.simt = self.simt + self.fixdt self.syst0 = self.syst - self.simt if self.ffstop > 0. and self.simt >= self.ffstop: self.ffmode = False self.mode = self.hold # Update UTC time self.utc += datetime.timedelta(seconds=self.simdt) # Datalog pre-update (communicate current sim time to loggers) datalog.preupdate(self.simt) # Plugins pre-update plugin.preupdate(self.simt) # For measuring game loop frequency self.dts.append(self.simdt) if len(self.dts) > 20: del self.dts[0] stack.checkfile(self.simt) # Always process stack stack.process() if self.mode == Simulation.op: bs.traf.update(self.simt, self.simdt) # Update metrics self.metric.update() # Update plugins plugin.update(self.simt) # Update loggers datalog.postupdate() # HOLD/Pause mode else: self.syst0 = self.syst - self.simt self.simdt = 0.0 return
def doWork(self): self.syst = int(time.time() * 1000.0) # Send list of stack functions available in this sim to gui at start stackdict = { cmd: val[0][len(cmd) + 1:] for cmd, val in stack.cmddict.items() } manager.sendEvent(StackInitEvent(stackdict)) while self.running: if self.state == Simulation.op: # Plugins pre-update plugin.preupdate(self.simt) # Datalog pre-update (communicate current sim time to loggers) datalog.preupdate(self.simt) # Update screen logic bs.scr.update() # Simulation starts as soon as there is traffic, or pending commands if self.state == Simulation.init: if bs.traf.ntraf > 0 or len(stack.get_scendata()[0]) > 0: self.start() if self.benchdt > 0.0: self.fastforward(self.benchdt) self.bencht = time.time() if self.state == Simulation.op: stack.checkfile(self.simt) # Always update stack stack.process() if self.state == Simulation.op: bs.traf.update(self.simt, self.simdt) # Update metrics # self.metric.update() # Update plugins plugin.update(self.simt) # Update loggers datalog.postupdate() # Update time for the next timestep self.simt += self.simdt # Update clock self.simtclock = (self.deltclock + self.simt) % onedayinsec # Process Qt events manager.processEvents() # When running at a fixed rate, or when in hold/init, increment # system time with sysdt and calculate remainder to sleep if not self.ffmode or not self.state == Simulation.op: self.syst += self.sysdt remainder = self.syst - int(1000.0 * time.time()) if remainder > 0: QThread.msleep(remainder) # If running in fast-time with an end-time that has passed, go back to # real-time running. elif self.ffstop is not None and self.simt >= self.ffstop: # If this fast-time section was part of a benchmark, send # message with benchmark results if self.benchdt > 0.0: bs.scr.echo( 'Benchmark complete: %d samples in %.3f seconds.' % (bs.scr.samplecount, time.time() - self.bencht)) self.benchdt = -1.0 self.pause() else: self.start() # Inform main of our state change if not self.state == self.prevstate: self.sendState() self.prevstate = self.state