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. //
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. //
12 // Written and (C) by François Fleuret //
13 // Contact <francois.fleuret@epfl.ch> for comments & bug reports //
14 ////////////////////////////////////////////////////////////////////////////////
18 #include "manipulator.h"
20 class MoveSquare : public Task {
26 const static int world_width = 500;
27 const static int world_height = 500;
31 virtual char *name() { return "HIT_SQUARE"; }
33 virtual int nb_degrees() { return 1; }
35 virtual int width() { return world_width; }
37 virtual int height() { return world_height; }
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 };
46 for(int s = 0; s < _nb_shapes; s++) _shapes[s] = 0;
49 for(int k = 0; k < _nb_shapes * 10 && s < _nb_shapes; k++) {
51 // bool red = drand48() < 0.2;
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);
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);
63 if(universe->collide(p)) delete p;
65 universe->add_polygon(p);
73 virtual void init(Universe *universe, int degree) {
76 _shapes = new Polygon *[_nb_shapes];
77 _targets = new bool[_nb_shapes];
81 virtual void update(scalar_t dt, Universe *universe, Manipulator *manipulator) { }
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();
92 // manipulator->force_release();
93 // scramble(universe);
94 // if(hit) return 1.0;
101 virtual void draw(SimpleWindow *window) { }
104 extern "C" Task *new_task() {
105 return new MoveSquare();