Initial commit
[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 // $Id: move_square.cc,v 1.16 2006-07-21 08:44:16 fleuret Exp $
17
18 #include "task.h"
19 #include "universe.h"
20 #include "manipulator.h"
21
22 class MoveSquare : public Task {
23
24   Polygon *_target;
25
26   int _square_size;
27   const static int world_width = 500;
28   const static int world_height = 500;
29
30 public:
31
32   virtual char *name() { return "MOVE_SQUARE"; }
33
34   virtual int nb_degrees() { return 5; }
35
36   virtual int width() { return world_width; }
37
38   virtual int height() { return world_height; }
39
40   virtual void init(Universe *universe, int degree) {
41     if(degree == 0) _square_size = 200;
42     else if(degree == 1) _square_size = 150;
43     else if(degree == 2) _square_size = 100;
44     else _square_size = 50;
45     scalar_t x[] = { -_square_size/2, _square_size/2, _square_size/2, -_square_size/2 };
46     scalar_t y[] = { -_square_size/2, -_square_size/2, _square_size/2, _square_size/2 };
47     _target = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
48     _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
49     _target->set_speed(0, 0, 0);
50     universe->initialize(_target);
51     universe->add_polygon(_target);
52     if(degree == 4) {
53       Polygon *obstacle;
54       scalar_t x[] = { ( 9 * world_width)/20, (11 * world_width)/20,
55                        (11 * world_width)/20, ( 9 * world_width)/20 };
56       scalar_t y[] = { ( 3 * world_height)/20, ( 3 * world_height)/20,
57                        (17 * world_height)/20, (17 * world_height)/20 };
58       obstacle = new Polygon(1.0, 0.0, 1.0, 0.0, x, y, 4);
59       obstacle->set_position(world_width/2, world_height/2, 0);
60       obstacle->set_speed(0, 0, 0);
61       obstacle->_nailed = true;
62       universe->initialize(obstacle);
63       universe->add_polygon(obstacle);
64     }
65   }
66
67   virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
68
69   virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
70     if(_target->_center_x > world_width - _square_size * 0.75) {
71       _target->set_position(_square_size/2, _square_size/2 + (world_height - _square_size) * drand48(), 0);
72       _target->set_speed(0, 0, 0);
73       manipulator->force_release();
74       return 1.0;
75     } else return 0.0;
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 }