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