4ce403be65751ccc042312f3b9f33262a292bd6b
[universe.git] / move_square.cc
1
2 ////////////////////////////////////////////////////////////////////////////////
3 // This program is free software; you can redistribute it and/or              //
4 // modify it under the terms of the GNU General Public License                //
5 // version 2 as published by the Free Software Foundation.                    //
6 //                                                                            //
7 // This program is distributed in the hope that it will be useful, but        //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of                 //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU          //
10 // General Public License for more details.                                   //
11 //                                                                            //
12 // Written and (C) by François Fleuret                                        //
13 // Contact <francois.fleuret@epfl.ch> for comments & bug reports              //
14 ////////////////////////////////////////////////////////////////////////////////
15
16 #include "task.h"
17 #include "universe.h"
18 #include "manipulator.h"
19
20 class MoveSquare : public Task {
21
22   Polygon *_target;
23
24   int _square_size;
25   const static int world_width = 500;
26   const static int world_height = 500;
27
28 public:
29
30   virtual char *name() { return "MOVE_SQUARE"; }
31
32   virtual int nb_degrees() { return 5; }
33
34   virtual int width() { return world_width; }
35
36   virtual int height() { return world_height; }
37
38   virtual void init(Universe *universe, int degree) {
39     if(degree == 0) _square_size = 200;
40     else if(degree == 1) _square_size = 150;
41     else if(degree == 2) _square_size = 100;
42     else _square_size = 50;
43     scalar_t x[] = { -_square_size/2, _square_size/2, _square_size/2, -_square_size/2 };
44     scalar_t y[] = { -_square_size/2, -_square_size/2, _square_size/2, _square_size/2 };
45     _target = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
46     _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
47     _target->set_speed(0, 0, 0);
48     universe->initialize(_target);
49     universe->add_polygon(_target);
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 return 0.0;
74   }
75
76   virtual void draw(SimpleWindow *window) {
77     window->color(0.75, 0.75, 0.75);
78     int x = int (world_width - _square_size * 0.75);
79     window->draw_line(x, 0, x, world_height);
80   }
81 };
82
83 extern "C" Task *new_task() {
84   return new MoveSquare();
85 }