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