Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

robbase.h

Go to the documentation of this file.
00001 /*
00002  * robbase.h
00003  * 
00004  * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
00005  * and Martin Trautmann (martintrautmann@gmx.de) 
00006  * 
00007  * This file may be distributed and/or modified under the terms of the 
00008  * GNU General Public License version 2 as published by the Free Software 
00009  * Foundation. 
00010  * 
00011  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013  * 
00014  */
00015 
00020 #ifndef __RT_ROBBASE__
00021 #define __RT_ROBBASE__
00022 
00023 #include <rtcollect.h>
00024 #include <rtstring.h>
00025 #include <rtlist.h>
00026 #include <rtmap.h>
00027 
00028 #include "robvars.h"
00029 #include "robglob.h"
00030 
00031 //using namespace lrt;
00032 
00033 namespace rt {
00034 
00035 // forward declares
00036 class Program;
00037 class ErrorHandler;
00038 class Bot;
00039 class Task;
00040 class Bank;
00041 class Instr;
00042 class SimSupervisor;
00043 class ExecSupervisor;
00044 class ProgramLoader;
00045 
00047 class Simulation {
00048 public:
00049         const ErrorHandler* errHandler;
00050         const Globals& glob; 
00051         rint vars[simVarLength];        
00052         rint fieldWidth, fieldHeight;
00053 
00054         lrt::Array<Bot*> field; 
00055         lrt::List<Bot*> bots;   
00056         unsigned long curCycle; 
00057 
00058         Simulation(const Globals& glob, const ErrorHandler*);
00059         ~Simulation();
00060         
00061         Program* run(); 
00062         rint getAllNumBots(); 
00063         void addProgram(Program* program); 
00064         int getNumPrograms();                   
00065         Program* getProgram(int index); 
00066         void addSupervisor(SimSupervisor* supervis); 
00067         void addExecSupervisor(ExecSupervisor* es); 
00068         
00070         void getPosBefore(rint& x, rint& y, rint dir);
00071 private:
00072         lrt::Vector<Program*> programs;
00073         lrt::List<SimSupervisor*> supervisors;
00074         lrt::List<ExecSupervisor*> execSupervisors;
00075         bool started;
00076 
00077         void processBots(); 
00078 
00079         friend class Task; // needs access to execSupervisors
00080 };
00081 
00084 class Program {
00085 public:
00086         struct HeaderField {
00087                 HeaderField();
00088                 HeaderField(const lrt::String& name, const lrt::String& value, HeaderType type);
00089                 lrt::String name;
00090                 lrt::String value;
00091                 HeaderType type;
00092         };
00093 
00094         static lrt::Vector<ProgramLoader*> exampleLoaders;
00095         static ProgramLoader* getLoader(const lrt::String& file); 
00096         static Program* create(const lrt::String& file, const Globals& glob, int globalNum);
00097 
00098         Simulation* sim;
00099         
00100         rint vars[progVarLength];
00102         const lrt::Vector<Bank*>& getBanks();
00103         lrt::StringMap<HeaderField> headers;
00104         int programNum; 
00105         int globalNum; 
00106         Bank* emptyBank; 
00107 
00108         lrt::String print();
00109 
00111         Program(ProgramLoader* loader);
00114         void init(rint num, Simulation* sim); 
00116         ProgramLoader* getLoader();
00118         ~Program();
00119 
00120 private:
00121         lrt::Vector<Bank*> banks;
00122         ProgramLoader* loader;
00123 };
00124 
00126 class ErrorHandler {
00127 public:
00129         virtual void handleBotError(const Bot* affectedBot, ExecReturnType type) const = 0;
00131         virtual void handleLoadError(const lrt::String& affectedFile, const lrt::String& message) const = 0;
00133         virtual void handleSystemError(int num, const lrt::String& message) const = 0;
00137         virtual void handleWarning(const lrt::String& message) const {};
00138 };
00139 
00141 class Bot {
00142 public:
00143         Program* owner;
00144 
00145         rint vars[botVarLength];  
00146         rint* stdVars;                  
00147         lrt::Array<Bank*> banks;
00148         lrt::Vector<Task*> tasks;
00149 
00150         rint curTask;
00151 
00152         int creationCycle; 
00153         int deactCycle; 
00154 
00156         Bot(rint instrSet, rint numBanks, rint mobile,
00157                 rint posX, rint posY, rint dir, rint generation, 
00158                 Program* owner);
00160         ~Bot();
00161 
00162         ExecReturnType exec();
00163 };
00164 
00167 class Task {
00168 public:
00169         Bot* bot;
00170         Simulation* sim; 
00171 
00172         rint vars[taskVarLength];
00173         unsigned long instrStarted; 
00174         Instr* curInstr;
00175         bool seized;
00176 
00180         Task(Bot* bot, Task* creator, rint dir);
00182         ~Task();
00183 
00184         ExecReturnType exec();
00185         ExecReturnType reboot();
00186 
00187 
00190         Bot* getRefBot();
00193         void getRefBotPos(rint& x, rint& y);
00194 };
00195 
00198 class Bank {
00199 public:
00200         Program* owner;
00201 
00202         lrt::String name;
00203         lrt::Vector<Instr*> instr;
00204         bool isSecret;
00205 
00206         lrt::String print();
00207 
00208         Bank(Program* const owner, const lrt::String& name);
00210         ~Bank();
00211         
00212 };
00213 
00215 class Instr {
00216 public:
00217 
00218         struct Op {
00219                 bool isRemote;
00220                 OpType type;
00221                 rint ptr;
00222         };
00223 
00224         virtual ~Instr();
00225 
00230         virtual ExecReturnType exec(Task* task) = 0;
00238         virtual rint getNumCycles(Task* task);
00242         virtual TimingMode getTimingMode();
00243 
00245         virtual lrt::String getName() = 0;
00249         virtual lrt::String print(int instrPos = 0);
00250 
00251 protected:
00253         Instr(int numParams);
00255         Op* ops;
00257         int numOps;
00259         rint cycles;
00264         rint getValue(Task* task, int opNum, bool absolute = false);
00267         void setValue(Task* task, int opNum, rint value);
00269         void initOps(const lrt::Array<Op>& ops);
00271         lrt::String printOp(int opNum, int instrPos);
00273         lrt::String printOps(int instrPos);
00274 };
00275 
00276 
00279 class SimSupervisor {
00280 public:
00281         struct GameState {
00282                 GameEventType event;
00283                 rint affectedProgram;
00284         };
00285 
00286         virtual ~SimSupervisor() {}
00287 
00292         virtual SimSuperPosition getPreferredPosition();
00295         virtual unsigned long getStepCycles() = 0;
00299         virtual void initSim(Simulation* const curSim);
00303         virtual GameState exec(Simulation* const curSim) = 0;
00309         virtual void exitSim(Simulation* const curSim, const GameState& simResult);
00310 
00311 protected:
00312         SimSupervisor() {}
00313 };
00314 
00317 class ExecSupervisor {
00318 public:
00322         virtual void initSim(Simulation* const curSim);
00328         virtual void onExec(Instr* instr, Bank* bank, Task* task) = 0;
00334         virtual void exitSim(Simulation* const curSim);
00335 
00336         virtual ~ExecSupervisor() {}
00337 
00338 protected:
00339         ExecSupervisor() {}
00340 };
00341 
00343 class ProgramLoader {
00344 public:
00345         virtual ProgramLoader* create(const lrt::String& filename, const Globals& glob) = 0;
00346         virtual ~ProgramLoader();
00347 
00349         lrt::StringMap<Program::HeaderField>* getHeaders();
00351         lrt::Vector<Bank*>* getBanks();
00352 
00353         virtual bool canLoad(const lrt::String& filename) = 0;
00354 
00356         virtual lrt::String getFormatName() = 0;
00357 
00360         lrt::String getFileName() { return filename; }
00361 
00362         const ErrorHandler* getErrorHandler(); 
00363         void setErrorHandler(const ErrorHandler* handler);
00364 
00365         Program* owner;
00366 protected:
00367         struct LoadReturnType {
00368                 bool success;
00369                 lrt::String message;
00370         };
00371 
00372         enum LoadState {
00373                 lsError = -1,                   
00374                 lsNotLoaded = 0,                
00375                 lsHeadersLoaded = 1,    
00376                 lsLoaded = 2                    
00377         };
00378 
00379         LoadState state;
00380         lrt::Vector<Bank*>* banks;
00381         lrt::StringMap<Program::HeaderField>* headers;
00382         const Globals& glob;
00383         const ErrorHandler* handler; 
00384 
00385         lrt::String filename; 
00386         
00387         ProgramLoader(const Globals& glob, const ErrorHandler* handler, const lrt::String& filename);
00388         virtual LoadReturnType loadHeaders() = 0;
00389         virtual LoadReturnType load() = 0;
00391         bool isLanguageVersionSupported(const lrt::String& language);
00392 };
00393 
00394 
00395 
00396 } // namespace
00397 
00398 #endif
00399 

This is the RoboTour API documentation version 0.1 • Copyright (C) by Florian Fischer