Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / ispy.cpp
1 //  SuperTux - Ispy
2 //  Copyright (C) 2007 Christoph Sommer <christoph.sommer@2007.expires.deltadevelopment.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/ispy.hpp"
18
19 #include "object/player.hpp"
20 #include "object/tilemap.hpp"
21 #include "sprite/sprite.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/sector.hpp"
24 #include "supertux/tile.hpp"
25
26 Ispy::Ispy(const Reader& reader)
27   : MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED), state(ISPYSTATE_IDLE), dir(AUTO)
28 {
29   // read script to execute
30   reader.get("script", script);
31
32   // read direction to face in
33   std::string dir_str;
34   bool facing_down = false;
35   reader.get("direction", dir_str);
36   if( dir_str == "left" ) dir = LEFT;
37   if( dir_str == "right" ) dir = RIGHT;
38   reader.get("facing-down", facing_down);
39   if (facing_down) dir = DOWN;
40   if (dir == AUTO) log_warning << "Setting an Ispy's direction to AUTO is no good idea" << std::endl;
41
42   // set initial sprite action
43   sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
44 }
45
46 HitResponse
47 Ispy::collision(GameObject& , const CollisionHit& )
48 {
49   return ABORT_MOVE;
50 }
51
52 bool
53 Ispy::line_intersects_line(Vector line1_start, Vector line1_end, Vector line2_start, Vector line2_end) {
54   // Adapted from Striker, (C) 1999 Joris van der Hoeven, GPL
55
56   float a1 = line1_start.x, b1 = line1_start.y, a2 = line1_end.x, b2 = line1_end.y;
57   float c1 = line2_start.x, d1 = line2_start.y, c2 = line2_end.x, d2 = line2_end.y;
58
59   float num = (b2-b1)*(c2-c1) - (a2-a1)*(d2-d1);
60   float den1 = (d2-b2)*(c1-c2) + (a2-c2)*(d1-d2);
61   float den2 = (d2-b2)*(a1-a2) + (a2-c2)*(b1-b2);
62
63   // normalize to positive numerator
64   if (num < 0) { 
65     num =- num; 
66     den1 =- den1; 
67     den2 =- den2; 
68   }
69
70   // numerator is zero -> Check for parallel or coinciding lines
71   if (num == 0) {
72     if ((b1-b2)*(c1-a2) != (a1-a2)*(d1-b2)) return false;
73     if (a1 == a2) { 
74       std::swap(a1, b1); 
75       std::swap(a2, b2); 
76       std::swap(c1, d1); 
77       std::swap(c2, d2); 
78     }
79     if (a1 > a2) std::swap(a1, a2);
80     if (c1 > c2) std::swap(c1, c2);
81     return ((a1 <= c2) && (a2 >= c1));
82   }
83
84   // Standard check
85   return (den1>=0) && (den1<=num) && (den2>=0) && (den2<=num);
86
87 }
88
89 bool
90 Ispy::intersects_line(Rect r, Vector line_start, Vector line_end)
91 {
92   Vector p1 = r.p1;
93   Vector p2 = Vector(r.p2.x, r.p1.y);
94   Vector p3 = r.p2;
95   Vector p4 = Vector(r.p1.x, r.p2.y);
96   if (line_intersects_line(p1, p2, line_start, line_end)) return true;
97   if (line_intersects_line(p2, p3, line_start, line_end)) return true;
98   if (line_intersects_line(p3, p4, line_start, line_end)) return true;
99   if (line_intersects_line(p4, p1, line_start, line_end)) return true;
100   return false;
101 }
102
103 bool
104 Ispy::free_line_of_sight(Vector line_start, Vector line_end, const MovingObject* ignore_object)
105 {
106
107   // check if no tile is in the way
108   float lsx = std::min(line_start.x, line_end.x);
109   float lex = std::max(line_start.x, line_end.x);
110   float lsy = std::min(line_start.y, line_end.y);
111   float ley = std::max(line_start.y, line_end.y);
112   std::list<TileMap*> solid_tilemaps = Sector::current()->solid_tilemaps;
113   for (float test_x = lsx; test_x <= lex; test_x += 16) {
114     for (float test_y = lsy; test_y <= ley; test_y += 16) {
115       for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
116         TileMap* solids = *i;
117         const Tile* tile = solids->get_tile_at(Vector(test_x, test_y));
118         if(!tile) continue;
119         // FIXME: check collision with slope tiles
120         if((tile->getAttributes() & Tile::SOLID)) return false;
121       }
122     }
123   }
124
125   // check if no object is in the way
126   using namespace collision;
127   Sector::MovingObjects& moving_objects = Sector::current()->moving_objects;
128   for(Sector::MovingObjects::const_iterator i = moving_objects.begin();
129       i != moving_objects.end(); ++i) {
130     const MovingObject* moving_object = *i;
131     if (moving_object == ignore_object) continue;
132     if (!moving_object->is_valid()) continue;
133     if ((moving_object->get_group() == COLGROUP_MOVING)
134         || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
135         || (moving_object->get_group() == COLGROUP_STATIC)) {
136       if(intersects_line(moving_object->get_bbox(), line_start, line_end)) return false;
137     }
138   }
139
140   return true;
141 }
142
143 void 
144 Ispy::update(float )
145 {
146
147   if (state == ISPYSTATE_IDLE) {
148     // check if a player has been spotted
149     bool playerSpotted = false;
150     std::vector<Player*> players = Sector::current()->get_players();
151     for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
152       Player* player = *playerIter;
153
154       Vector eye = get_bbox().get_middle();
155       if (dir == LEFT) eye = Vector(get_bbox().p1.x, get_bbox().get_middle().y);
156       if (dir == RIGHT) eye = Vector(get_bbox().p2.x, get_bbox().get_middle().y);
157       if (dir == UP) eye = Vector(get_bbox().get_middle().x, get_bbox().p1.y);
158       if (dir == DOWN) eye = Vector(get_bbox().get_middle().x, get_bbox().p2.y);
159
160       // test for free line of sight to any of all four corners and the middle of a player's bounding box
161       if (free_line_of_sight(eye, player->get_bbox().p1, player)) playerSpotted = true;
162       if (free_line_of_sight(eye, Vector(player->get_bbox().p2.x, player->get_bbox().p1.y), player)) playerSpotted = true;
163       if (free_line_of_sight(eye, player->get_bbox().p2, player)) playerSpotted = true;
164       if (free_line_of_sight(eye, Vector(player->get_bbox().p1.x, player->get_bbox().p2.y), player)) playerSpotted = true;
165       if (free_line_of_sight(eye, player->get_bbox().get_middle(), player)) playerSpotted = true;
166     }
167
168     if (playerSpotted) {
169       sprite->set_action((dir == DOWN) ? "alert-down" : ((dir == LEFT) ? "alert-left" : "alert-right"), 1);
170       state = ISPYSTATE_ALERT;
171
172       std::istringstream stream(script);
173       Sector::current()->run_script(stream, "Ispy");
174     }
175   }
176   if (state == ISPYSTATE_ALERT) {
177     if (sprite->animation_done()) {
178       sprite->set_action((dir == DOWN) ? "hiding-down" : ((dir == LEFT) ? "hiding-left" : "hiding-right"), 1);
179       state = ISPYSTATE_HIDING;
180     }
181   }
182   if (state == ISPYSTATE_HIDING) {
183     if (sprite->animation_done()) {
184       sprite->set_action((dir == DOWN) ? "showing-down" : ((dir == LEFT) ? "showing-left" : "showing-right"), 1);
185       state = ISPYSTATE_SHOWING;
186     }
187   }
188   if (state == ISPYSTATE_SHOWING) {
189     if (sprite->animation_done()) {
190       sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
191       state = ISPYSTATE_IDLE;
192     }
193   }
194 }
195
196 IMPLEMENT_FACTORY(Ispy, "ispy");
197
198 /* EOF */