Updating.
[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
33     scalar_t x[] = {
34       -_square_size / 2.0, _square_size / 2.0,
35       _square_size / 2.0, -_square_size / 2.0
36     };
37
38     scalar_t y[] = {
39       -_square_size / 2.0, -_square_size / 2.0,
40       _square_size / 2.0, _square_size / 2.0
41     };
42
43     _target = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
44     _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
45     _target->set_speed(0, 0, 0);
46
47     universe->initialize(_target);
48     universe->add_polygon(_target);
49
50     if(degree == 4) {
51       Polygon *obstacle;
52       scalar_t x[] = { ( 9 * world_width)/20, (11 * world_width)/20,
53                        (11 * world_width)/20, ( 9 * world_width)/20 };
54       scalar_t y[] = { ( 3 * world_height)/20, ( 3 * world_height)/20,
55                        (17 * world_height)/20, (17 * world_height)/20 };
56       obstacle = new Polygon(1.0, 0.0, 1.0, 0.0, x, y, 4);
57       obstacle->set_position(world_width/2, world_height/2, 0);
58       obstacle->set_speed(0, 0, 0);
59       obstacle->_nailed = true;
60       universe->initialize(obstacle);
61       universe->add_polygon(obstacle);
62     }
63   }
64
65   virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
66
67   virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
68     if(_target->_center_x > world_width - _square_size * 0.75) {
69       _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
70       _target->set_speed(0, 0, 0);
71       manipulator->force_release();
72       return 1.0;
73     } else {
74       return 0.0;
75     }
76   }
77
78   virtual void draw(SimpleWindow *window) {
79     window->color(0.75, 0.75, 0.75);
80     int x = int (world_width - _square_size * 0.75);
81     window->draw_line(x, 0, x, world_height);
82   }
83 };
84
85 extern "C" Task *new_task() {
86   return new MoveSquare();
87 }