automatic commit
[universe.git] / hit_shape.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   int _square_size;
23   int _nb_shapes;
24   Polygon **_shapes;
25   bool *_targets;
26   const static int world_width = 500;
27   const static int world_height = 500;
28
29 public:
30
31   virtual char *name() { return "HIT_SQUARE"; }
32
33   virtual int nb_degrees() { return 1; }
34
35   virtual int width() { return world_width; }
36
37   virtual int height() { return world_height; }
38
39   void scramble(Universe *universe) {
40     scalar_t margin = sqrt(2)/2 * _square_size;
41     scalar_t x[] = { -_square_size/2, _square_size/2, _square_size/2, -_square_size/2 };
42     scalar_t y[] = { -_square_size/2, -_square_size/2, _square_size/2, _square_size/2 };
43
44     universe->clear();
45
46     for(int s = 0; s < _nb_shapes; s++) _shapes[s] = 0;
47
48     int s = 0;
49     for(int k = 0; k < _nb_shapes * 10 && s < _nb_shapes; k++) {
50       Polygon *p;
51 //       bool red = drand48() < 0.2;
52       bool red = 1-s%2;
53
54       if(red) p = new Polygon(0.5, 1.0, 0.0, 0.0, x, y, 4);
55       else    p = new Polygon(0.5, 1.0, 1.0, 0.0, x, y, 4);
56
57       p->set_position(margin + (world_width - 2 * margin) * drand48(),
58                       margin + (world_height - 2 * margin) * drand48(),
59                       2 * M_PI * drand48());
60       p->set_speed(0, 0, 0);
61       universe->initialize(p);
62       p->_nailed = true;
63       if(universe->collide(p)) delete p;
64       else {
65         universe->add_polygon(p);
66         _shapes[s] = p;
67         _targets[s] = red;
68         s++;
69       }
70     }
71   }
72
73   virtual void init(Universe *universe, int degree) {
74     _square_size = 150;
75     _nb_shapes = 2;
76     _shapes = new Polygon *[_nb_shapes];
77     _targets = new bool[_nb_shapes];
78     scramble(universe);
79   }
80
81   virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
82
83   virtual scalar_t reward(Universe *universe, Manipulator *manipulator) {
84     if(manipulator->grabbing()) {
85       for(int k = 0; k < _nb_shapes; k++) if(_shapes[k] == manipulator->grabbing()) {
86         bool hit = _targets[k];
87         if(hit & drand48() < 0.25) {
88           manipulator->force_release();
89           scramble(universe);
90           return  1.0;
91         }
92 //         manipulator->force_release();
93 //         scramble(universe);
94 //         if(hit) return  1.0;
95 //         else    return -1.0;
96       }
97     }
98     return 0.0;
99   }
100
101   virtual void draw(SimpleWindow *window) { }
102 };
103
104 extern "C" Task *new_task() {
105   return new MoveSquare();
106 }