Summary of Simulator Classes ############################ .. wordcount:: .. vim:filetype=rst spell: To help you design your simulator, here are the example header files I used in building the demonstration code you have seen in class. Highway Class ************* The highway provides the logic that makes the demo run. Vehicles were designed like the pool balls that inspired this demo code. After discussing the design in class last week, this design is not that great. But it is a start. .. literalinclude:: sim/Highway.h :linenos: :caption: include/Highway.h If you look at the routines provided here, you see methods that are doing the checking to see if a car needs to modify its behavior. For example, the ``check_safe`` method determines if a car could change lanes when it is told to move. Figuring this out involves looking to see if any other car is in the way of a possible lane change. In this simple simulation, the lane changes happen instantly. The ``checkCollisions`` method is right out of the pool ball simulation, and is used to alter the speed of the offending vehicle. Vehicles ******** Vehicles in the demo are limited gadgets. Basically, they store a bit of data used to decide how they move. .. literalinclude:: sim/Vehicle.h :linenos: :caption: include/Vehicle.h Here, the ``move`` method is the most important. The vehicle will only move away from the current lane if it is "safe". You need to think about this to decide what should happen. (It depends on which way you need to go.) The logic needed here is up to the car, not the highway. The idea is for the highway to provide enough information to the car for it to decide the next move. Demo Main Code ************** Here is the original demo main logic used to run the simulation. This went together pretty quickly so I could show it in class. It needs refactoring a bit, which we will see next. .. literalinclude:: sim/main1.cpp :linenos: :caption: src/main.cpp This is pretty ugly, since it combines application logic and graphics engine logic. A better design would isolate those two aspects of the design. Main Refactored *************** Pulling the graphics logic out of ``main``, and placing it in its own file is what is needed here. Here is the new ``main`` code. .. literalinclude:: sim/main.cpp :linenos: :caption: src/main.cpp That is a lot easier. It also sets us up ot run simulations that do not use graphics at all. We would bypass the call to ``sim_main`` and use only console output to see what happens. Simulator Graphics ****************** The heart of the visual demo is the graphics library. I put all of the startup code in a separate file to clean up ``main``. Here is the header file for the new graphics module: .. literalinclude:: sim/SimGraphics.h :linenos: :caption: include/SimGraphics.h Here is a start on the Graphics Code. In this code I have added two key commands to make running the simulator more interesting. The ``b`` command stops the action, allowing you to study the current situation. While the simulation is paused, you can press the ``s`` key to single step it one cycle. I wanted to get this running before I add crash logic to the demo. That will happen soon (I hope!) .. literalinclude:: sim/SimGraphics.cpp :linenos: :caption: lib/SimGraphics.cpp Feel free to use this code, either directly (of course, you need to provide the implementation logic), or as a model for your own code. If you get stuck, see me for guidance. I may let you peek at my code to move you forward.