-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramHandler.cpp
More file actions
203 lines (184 loc) · 6.88 KB
/
ProgramHandler.cpp
File metadata and controls
203 lines (184 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// This file is part of LightBot.
//
// LightBot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LightBot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LightBot. If not, see <http://www.gnu.org/licenses/>.
// Authors : Lilian Gallon, Tristan Renaudon
#include "ProgramHandler.h"
#include <iostream>
// Move the robot forward and returns the "state" of the robot
// with magic numbers
// RESULT ->
// -4 = ALL CELLS ARE NOT TURNED ON
// -3 = FAILED (cell way too high)
// -2 = FAILED (had to jump)
// -1 = FAILED (out of the grid)
// 0 = IT'S FINE FOR THE MOMENT (MAIN)
// 1 = IT'S FINE FOR THE MOMENT (PROGRAM 1)
// 2 = IT'S FINE FOR THE MOMENT (PROGRAM 2)
int ProgramHandler::moveForward(int result)
{
// Init the new pos at the current robot position
sf::Vector2i new_pos = {m_robot->getPos().x,m_robot->getPos().y};
// Move the robot pos according to it's orientation
switch (m_robot->getOrientation()) {
case Utils::Orientation::DOWN:
new_pos.y ++;
break;
case Utils::Orientation::DOWN_LEFT:
if(new_pos.x%2!=0){
new_pos.x --;
}else{
new_pos.x --;
new_pos.y ++;
}
break;
case Utils::Orientation::DOWN_RIGHT:
if(new_pos.x%2!=0){
new_pos.x ++;
}else{
new_pos.x ++;
new_pos.y ++;
}
break;
case Utils::Orientation::UP:
new_pos.y --;
break;
case Utils::Orientation::UP_LEFT:
if(new_pos.x%2!=0){
new_pos.x --;
new_pos.y --;
}else{
new_pos.x --;
}
break;
case Utils::Orientation::UP_RIGHT:
if(new_pos.x%2!=0){
new_pos.x ++;
new_pos.y --;
}else{
new_pos.x ++;
}
break;
default:
break;
}
// Find final cell (when the robot should go)
unsigned int i_final = 0;
bool found_final = false;
while(!found_final && i_final<m_grid->getGrid().size()){
if(m_grid->getGrid().at(i_final)->getPos().x==(float)new_pos.x
&& m_grid->getGrid().at(i_final)->getPos().y==(float)new_pos.y){
found_final = true;
}else{
i_final ++;
}
}
if(found_final){
unsigned int i_init = 0;
bool found_init = false;
// Find the cell index where the robot is (initially)
while(!found_init && i_init<m_grid->getGrid().size()){
if(m_grid->getGrid().at(i_init)->getPos().x==(float)m_robot->getPos().x
&& m_grid->getGrid().at(i_init)->getPos().y==(float)m_robot->getPos().y){
found_init = true;
}else{
i_init ++;
}
}
if(!found_init){
std::cout << "[ProgramHandler-CRITICAL]: Could not find the robot cell (is he dead?)" << std::endl;
}else{
// If the final cell height is 1 more that the initial, we have to check if the robot jumped
if(m_grid->getGrid().at(i_init)->getHeight()==m_grid->getGrid().at(i_final)->getHeight()-1){
if(m_robot->isJumping()){
// OK
m_robot->setPos(new_pos);
}else{
// ROBOT DID NOT JUMPED BUT IT HAD TO!!
result = -2;
}
}else if(m_grid->getGrid().at(i_init)->getHeight()<m_grid->getGrid().at(i_final)->getHeight()){
// THE FINAL CELL IS WAY TOO HIGH! (dif >1)
result = -3;
}else{
m_robot->setPos(new_pos);
// The initial cell has an higher or equal height as the final one, the robot can move
}
}
}else{
// The robot moved in an inexistant cell
// THE ROBOT HAS TO DIE :)
result = -1;
}
return result;
}
ProgramHandler::ProgramHandler(ProgramBox* program_main, ProgramBox *program_p1, ProgramBox *program_p2, Robot* robot, Grid *grid)
:m_program_main{program_main}, m_program_p1{program_p1}, m_program_p2{program_p2}, m_robot{robot}, m_grid{grid},m_robot_jumped_before{false}
{}
int ProgramHandler::runProgram(ProgramBox *program, const unsigned int &index, int result, Theme* defaultTheme, Theme* currentActionTheme)
{
std::vector<Cell *> cells = m_grid->getGrid();
// The current action has a different theme
//(like that we know that the robot is doing this action)
program->getActions().at(index)->setTheme(currentActionTheme);
// Set default theme to the precedent action (button)
if(index!=0){
program->getActions().at(index-1)->setTheme(defaultTheme);
}
// We will do things according to the action
switch (program->getActions().at(index)->getAction()) {
case Utils::Action::FORWARD:
result = moveForward(result);
break;
case Utils::Action::JUMP:
m_robot->setJumping(true);
m_robot_jumped_before = true;
break;
case Utils::Action::LIGHT:
for(Cell* c : cells){
if(c->getPos().x == m_robot->getPos().x && c->getPos().y == m_robot->getPos().y){
c->setLight(true);
}
}
break;
case Utils::Action::TURN_CLOCKWISE:
m_robot->setOrientation(Utils::turn_clockwise(m_robot->getOrientation()));
break;
case Utils::Action::TURN_COUNTERCLOCK:
m_robot->setOrientation(Utils::turn_counterclock(m_robot->getOrientation()));
break;
case Utils::Action::PROG_P1:
if(program->getType()==Utils::TypeProg::P1){
std::cout << Utils::getTime() + "[ProgramHandler-INFO]: Infinite loop detected in P1. The instruction causing this loop will be ignored." << std::endl;
}else{
// result = 1 -> say that we are running P1
result = 1;
}
break;
case Utils::Action::PROG_P2:
if(program->getType()==Utils::TypeProg::P2){
std::cout << Utils::getTime() + "[ProgramHandler-INFO]: Infinite loop detected in P2. The instruction causing this loop will be ignored." << std::endl;
}else{
// result = 2 -> say that we are running P2
result = 2;
}
break;
default:
break;
}
// If the robot jumped and did go forward after, he lands on the floor
if(m_robot->isJumping() && program->getActions().at(index)->getAction()==Utils::Action::FORWARD){
m_robot->setJumping(false);
}
return result;
}