Initial commit.
[universe.git] / move_square.cc
1
2 // Written and (C) by Francois Fleuret
3 // Contact <francois.fleuret@idiap.ch> for comments & bug reports
4
5 #include "task.h"
6 #include "universe.h"
7 #include "manipulator.h"
8
9 class MoveSquare : public Task {
10
11   Polygon *_target;
12
13   int _square_size;
14   const static int world_width = 500;
15   const static int world_height = 500;
16
17 public:
18
19   virtual const char *name() { return "MOVE_SQUARE"; }
20
21   virtual int nb_degrees() { return 5; }
22
23   virtual int width() { return world_width; }
24
25   virtual int height() { return world_height; }
26
27   virtual void init(Universe *universe, int degree) {
28     if(degree == 0) _square_size = 200;
29     else if(degree == 1) _square_size = 150;
30     else if(degree == 2) _square_size = 100;
31     else _square_size = 50;
32     scalar_t x[] = { -_square_size/2, _square_size/2, _square_size/2, -_square_size/2 };
33     scalar_t y[] = { -_square_size/2, -_square_size/2, _square_size/2, _square_size/2 };
34     _target = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
35     _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
36     _target->set_speed(0, 0, 0);
37     universe->initialize(_target);
38     universe->add_polygon(_target);
39     if(degree == 4) {
40       Polygon *obstacle;
41       scalar_t x[] = { ( 9 * world_width)/20, (11 * world_width)/20,
42                        (11 * world_width)/20, ( 9 * world_width)/20 };
43       scalar_t y[] = { ( 3 * world_height)/20, ( 3 * world_height)/20,
44                        (17 * world_height)/20, (17 * world_height)/20 };
45       obstacle = new Polygon(1.0, 0.0, 1.0, 0.0, x, y, 4);
46       obstacle->set_position(world_width/2, world_height/2, 0);
47       obstacle->set_speed(0, 0, 0);
48       obstacle->_nailed = true;
49       universe->initialize(obstacle);
50       universe->add_polygon(obstacle);
51     }
52   }
53
54   virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
55
56   virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
57     if(_target->_center_x > world_width - _square_size * 0.75) {
58       _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
59       _target->set_speed(0, 0, 0);
60       manipulator->force_release();
61       return 1.0;
62     } else return 0.0;
63   }
64
65   virtual void draw(SimpleWindow *window) {
66     window->color(0.75, 0.75, 0.75);
67     int x = int (world_width - _square_size * 0.75);
68     window->draw_line(x, 0, x, world_height);
69   }
70 };
71
72 extern "C" Task *new_task() {
73   return new MoveSquare();
74 }