Преглед изворни кода

implemented most variables

Zoadian пре 10 година
родитељ
комит
bf1b804aca
3 измењених фајлова са 201 додато и 65 уклоњено
  1. 32 31
      README.md
  2. 4 4
      playfield.cpp
  3. 165 30
      simulator.h

+ 32 - 31
README.md

@@ -52,37 +52,38 @@ A programming game based on RoboCom.
 
 
 # Variables
-| Local Variable   | mutability | Description                                                                                                        |
-|------------------|------------|--------------------------------------------------------------------------------------------------------------------|
-| #1 .. #20        | mutable    | The twenty all-purpose standard integers. They cannot be accessed by remote programs                               |
-| #Pub             | mutable    | Public variable which is accessible from all programs on the field                                                 |
-| #Team            | mutable    | Public variable which is accessible from all own programs on the field                                             |
-| #Active          | mutable    | Determines if a current program is active (positive values) or not (zero or negative)                              |
-
-| Remote Variable | mutability  | Description                                                                                                        |
-|-----------------|-------------|--------------------------------------------------------------------------------------------------------------------|
-| %Active         | mutable     | Determines if a remote program is active (positive values) or not (zero or negative)                               |
-| %Banks          | immutable   | The number of banks which the remote program has                                                                   |
-| %InstrSet       | immutable   | The instruction set which the remote program has                                                                   |
-| %Mobile         | immutable   | Tells if the remote program is mobile                                                                              |
-| %Age            | immutable   | The remote program's age in cycles                                                                                 |
-| %Tasks          | immutable   | The number of tasks in the remote program, incl. sleeping or seized tasks                                          |
-
-| Constants       | mutability  | Description                                                                                                        |
-|-----------------|-------------|--------------------------------------------------------------------------------------------------------------------|
-| $Banks          | immutable   | The number of banks which the current program has                                                                  |
-| $InstrSet       | immutable   | The instruction set which the current program has                                                                  |
-| $Mobile         | immutable   | Tells if the current program is mobile                                                                             |
-| $Age            | immutable   | The current program's age in cycles                                                                                |
-| $Tasks          | immutable   | The number of tasks in the current program, incl. sleeping or seized tasks                                         |
-| $Own            | immutable   | The number of friendly programs on the board                                                                       |
-| $Others         | immutable   | The number of enemy programs on the board                                                                          |
-| $Fields         | immutable   | The size of the board                                                                                              |
-| $Generation     | immutable   | The generation of the current program                                                                              |
-| $ID             | immutable   | Identifier which is equal for each program of the same team<br> and initialized to random when the game is started |
-| $InstrPos       | immutable   | The number of the current instruction (in its bank)                                                                |
-| $Time           | immutable   | The game time in cycles                                                                                            |
-| $Timeout        | immutable   | The automatic simulation timeout in cycles                                                                         |
+
+| Local Variable  | mutability | Description                                                                                                        |
+|-----------------|------------|--------------------------------------------------------------------------------------------------------------------|
+| #1 .. #20       | mutable    | The twenty all-purpose standard integers. They cannot be accessed by remote programs                               |
+| #Active         | mutable    | Determines if a current program is active (positive values) or not (zero or negative)                              |
+| $Banks          | immutable  | The number of banks which the current program has                                                                  |
+| $InstrSet       | immutable  | The instruction set which the current program has                                                                  |
+| $Mobile         | immutable  | Tells if the current program is mobile                                                                             |
+| $Age            | immutable  | The current program's age in cycles                                                                                |
+| $Tasks          | immutable  | The number of tasks in the current program, incl. sleeping or seized tasks                                         |
+| $Generation     | immutable  | The generation of the current program                                                                              |
+| $ID             | immutable  | Identifier which is equal for each program of the same team<br> and initialized to random when the game is started |
+
+| Remote Variable | mutability | Description                                                                                                        |
+|-----------------|------------|--------------------------------------------------------------------------------------------------------------------|
+| %Active         | mutable    | Determines if a remote program is active (positive values) or not (zero or negative)                               |
+| %Banks          | immutable  | The number of banks which the remote program has                                                                   |
+| %InstrSet       | immutable  | The instruction set which the remote program has                                                                   |
+| %Mobile         | immutable  | Tells if the remote program is mobile                                                                              |
+| %Age            | immutable  | The remote program's age in cycles                                                                                 |
+| %Tasks          | immutable  | The number of tasks in the remote program, incl. sleeping or seized tasks                                          |
+| %Generation     | immutable  | The generation of the remote program                                                                               |
+
+| Global Variable | mutability | Description                                                                                                        |
+|-----------------|------------|--------------------------------------------------------------------------------------------------------------------|
+| #Pub            | mutable    | Public variable which is accessible from all programs on the field                                                 |
+| #Team           | mutable    | Public variable which is accessible from all own programs on the field                                             |
+| $Own            | immutable  | The number of friendly programs on the board                                                                       |
+| $Others         | immutable  | The number of enemy programs on the board                                                                          |
+| $Fields         | immutable  | The size of the board                                                                                              |
+| $Time           | immutable  | The game time in cycles                                                                                            |
+| $Timeout        | immutable  | The automatic simulation timeout in cycles                                                                         |
 
 
 # Execution Model

+ 4 - 4
playfield.cpp

@@ -30,11 +30,11 @@ Playfield::Playfield(Simulator& simulator, QWidget *parent)
 void Playfield::paintEvent(QPaintEvent* event) {
     QPainter painter(this);
 
-    int w = qMin(width(), height()) / simulator.size;
-    int h = qMin(width(), height()) / simulator.size;
+    int w = qMin(width(), height()) / FIELDS_XY;
+    int h = qMin(width(), height()) / FIELDS_XY;
 
-    for(int y = 0; y < simulator.size; ++y) {
-        for(int x = 0; x < simulator.size; ++x) {
+    for(int y = 0; y < FIELDS_XY; ++y) {
+        for(int x = 0; x < FIELDS_XY; ++x) {
             int xx = x * w;
             int yy = y * w;
             painter.setPen(Qt::black);

+ 165 - 30
simulator.h

@@ -8,6 +8,11 @@
 #include <stdint.h>
 using namespace std;
 
+enum {
+    CYCLE_TIMEOUT = 100000,
+    FIELDS_XY = 20,
+};
+
 struct Position {
     size_t x = 0;
     size_t y = 0;
@@ -96,30 +101,50 @@ enum Params : uint16_t {
 };
 
 enum Variables {
-    LocalVar_0 = 0,
-    LocalVar_20 = 19,
-    PubVar,
-    TeamVar,
-    LocalActiveVar,
-    RemoteActiveVar,
+    Local_0,
+    Local_1,
+    Local_2,
+    Local_3,
+    Local_4,
+    Local_5,
+    Local_6,
+    Local_7,
+    Local_8,
+    Local_9,
+    Local_10,
+    Local_11,
+    Local_12,
+    Local_13,
+    Local_14,
+    Local_15,
+    Local_16,
+    Local_17,
+    Local_18,
+    Local_19,
+    LocalActive,
     LocalBanks,
-    RemoteBanks,
     LocalInstrSet,
-    RemoteInstrSet,
     LocalMobile,
-    RemoteMobile,
     LocalAge,
-    RemoteAge,
-    Own,
-    Others,
-    Fields,
-    LocalGeneration,
-    TeamId,
-    InstrPos,
-    Time,
-    Timeout,
     LocalTasks,
+    LocalGeneration,
+    LocalId,
+
+    RemoteActive,
+    RemoteBanks,
+    RemoteInstrSet,
+    RemoteMobile,
+    RemoteAge,
     RemoteTasks,
+    RemoteGeneration,
+
+    GlobalPub,
+    GlobalTeam,
+    GlobalOwn,
+    GlobalOthers,
+    GlobalFields,
+    GlobalTime,
+    GlobalTimeout,
 };
 
 enum Error {
@@ -170,6 +195,11 @@ struct Task {
 };
 
 struct Program {
+    int active = 0;
+    int instructionSet = 0;
+    int mobile = 0;
+    int creationCycle = 0;
+    int generation = 0;
     Error error = NoError;
     QColor color;
     Position position;
@@ -193,11 +223,9 @@ struct Field {
 struct Simulator {
     size_t cycle = 0;
     vector<Program> programs;
-    size_t size = 0;
 
     Simulator() {
-        size = 20;
-        programs.reserve(size * size);
+        programs.reserve(FIELDS_XY * FIELDS_XY);
 
 //        std::srand(std::time(0));
         std::srand(0);
@@ -205,10 +233,10 @@ struct Simulator {
 
     Position calcPosition(Position position, Direction direction, int32_t distance) {
         switch(direction) {
-        case Right: return Position{(position.x + distance) % size, position.y};
-        case Down: return Position{position.x, (position.y + distance) % size};
-        case Left: return Position{(position.x - distance) % size, position.y};
-        case Up: return Position{position.x, (position.y - distance) % size};
+        case Right: return Position{(position.x + distance) % FIELDS_XY, position.y};
+        case Down: return Position{position.x, (position.y + distance) % FIELDS_XY};
+        case Left: return Position{(position.x - distance) % FIELDS_XY, position.y};
+        case Up: return Position{position.x, (position.y - distance) % FIELDS_XY};
         }
     }
 
@@ -286,12 +314,119 @@ struct Simulator {
                 int32_t* c = &c_safe;
 
                 auto decode = [&](int*& v) {
-                    if(*v >= VAR_0 && *v <= VAR_20) {
-                        v = &program.vars[*v];
-                        return;
-                    }
                     switch(*v) {
-                        case 21: break;
+                        case Local_0:  v = &program.vars[0]; break;
+                        case Local_1:  v = &program.vars[1]; break;
+                        case Local_2:  v = &program.vars[2]; break;
+                        case Local_3:  v = &program.vars[3]; break;
+                        case Local_4:  v = &program.vars[4]; break;
+                        case Local_5:  v = &program.vars[5]; break;
+                        case Local_6:  v = &program.vars[6]; break;
+                        case Local_7:  v = &program.vars[7]; break;
+                        case Local_8:  v = &program.vars[8]; break;
+                        case Local_9:  v = &program.vars[9]; break;
+                        case Local_10: v = &program.vars[10]; break;
+                        case Local_11: v = &program.vars[11]; break;
+                        case Local_12: v = &program.vars[12]; break;
+                        case Local_13: v = &program.vars[13]; break;
+                        case Local_14: v = &program.vars[14]; break;
+                        case Local_15: v = &program.vars[15]; break;
+                        case Local_16: v = &program.vars[16]; break;
+                        case Local_17: v = &program.vars[17]; break;
+                        case Local_18: v = &program.vars[18]; break;
+                        case Local_19: v = &program.vars[19]; break;
+                        case LocalActive:
+                            v = &program.active;
+                            break;
+                        case LocalBanks:
+                            *v = program.banks.size();
+                            break;
+                        case LocalInstrSet:
+                            *v = program.instructionSet;
+                            break;
+                        case LocalMobile:
+                            *v = program.mobile;
+                            break;
+                        case LocalAge:
+                            *v = cycle - program.creationCycle;
+                            break;
+                        case LocalTasks:
+                            *v = program.tasks.size();
+                            break;
+                        case LocalGeneration:
+                            *v = program.generation;
+                            break;
+                        case LocalId:
+                            // @todo
+                            break;
+                        case RemoteActive: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            if(remoteProgram) {
+                                v = &remoteProgram->active;
+                            }
+                            else {
+                                *v = 0;
+                            }
+                            break;
+                        }
+                        case RemoteBanks: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? remoteProgram->banks.size() : 0;
+                            break;
+                        }
+                        case RemoteInstrSet: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? remoteProgram->instructionSet : 0;
+                            break;
+                        }
+                        case RemoteMobile: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? remoteProgram->mobile: 0;
+                            break;
+                        }
+                        case RemoteAge: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? (cycle - remoteProgram->creationCycle) : 0;
+                            break;
+                        }
+                        case RemoteTasks: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? remoteProgram->tasks.size() : 0;
+                            break;
+                        }
+                        case RemoteGeneration: {
+                            auto remotePosition = calcPosition(program.position, task.direction, 1);
+                            auto remoteProgram = findProgram(remotePosition);
+                            *v = remoteProgram ? remoteProgram->generation : 0;
+                            break;
+                        }
+                        case GlobalPub:
+                            // @todo
+                            break;
+                        case GlobalTeam:
+                            // @todo
+                            break;
+                        case GlobalOwn:
+                            // @todo
+                            break;
+                        case GlobalOthers:
+                            // @todo
+                            break;
+                        case GlobalFields:
+                            *v = FIELDS_XY * FIELDS_XY;
+                            break;
+                        case GlobalTime:
+                            *v = cycle;
+                            break;
+                        case GlobalTimeout:
+                            *v = CYCLE_TIMEOUT;
+                            break;
                     }
                 };