a03d6539d5a21887fb290e3249b7e4150a0d67c7
[supertux.git] / 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
24 #include "player.hpp"
25 #include "object_factory.hpp"
26 #include "sector.hpp"
27 #include "tile.hpp"
28 #include "object/tilemap.hpp"
29 #include "lisp/writer.hpp"
30 #include "sprite/sprite.hpp"
31
32 Ispy::Ispy(const lisp::Lisp& reader)
33        : MovingSprite(reader, "images/objects/ispy/ispy.sprite", LAYER_TILES+5, COLGROUP_DISABLED), state(ISPYSTATE_IDLE), dir(AUTO)
34 {
35   // read script to execute
36   reader.get("script", script);
37
38   // read direction to face in
39   std::string dir_str;
40   bool facing_down = false;
41   reader.get("direction", dir_str);
42   if( dir_str == "left" ) dir = LEFT;
43   if( dir_str == "right" ) dir = RIGHT;
44   reader.get("facing-down", facing_down);
45   if (facing_down) dir = DOWN;
46   if (dir == AUTO) log_warning << "Setting an Ispy's direction to AUTO is no good idea" << std::endl;
47
48   // set initial sprite action
49   sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
50 }
51
52 void
53 Ispy::write(lisp::Writer& writer)
54 {
55   writer.start_list("ispy");
56   writer.write("x", bbox.p1.x);
57   writer.write("y", bbox.p1.y);
58   writer.write("script", script);
59   switch (dir)
60   {
61     case DOWN:
62       writer.write("direction", "down"); break;
63     case LEFT:
64       writer.write("direction", "left"); break;
65     case RIGHT:
66       writer.write("direction", "right"); break;
67     default: break;
68   }
69   writer.end_list("ispy");
70 }
71
72 HitResponse
73 Ispy::collision(GameObject& , const CollisionHit& )
74 {
75   return ABORT_MOVE;
76 }
77
78 bool
79 Ispy::line_intersects_line(Vector line1_start, Vector line1_end, Vector line2_start, Vector line2_end) {
80   // Adapted from Striker, (C) 1999 Joris van der Hoeven, GPL
81
82   float a1 = line1_start.x, b1 = line1_start.y, a2 = line1_end.x, b2 = line1_end.y;
83   float c1 = line2_start.x, d1 = line2_start.y, c2 = line2_end.x, d2 = line2_end.y;
84
85   float num = (b2-b1)*(c2-c1) - (a2-a1)*(d2-d1);
86   float den1 = (d2-b2)*(c1-c2) + (a2-c2)*(d1-d2);
87   float den2 = (d2-b2)*(a1-a2) + (a2-c2)*(b1-b2);
88
89   // normalize to positive numerator
90   if (num < 0) { 
91     num =- num; 
92     den1 =- den1; 
93     den2 =- den2; 
94   }
95
96   // numerator is zero -> Check for parallel or coinciding lines
97   if (num == 0) {
98     if ((b1-b2)*(c1-a2) != (a1-a2)*(d1-b2)) return false;
99     if (a1 == a2) { 
100       std::swap(a1, b1); 
101       std::swap(a2, b2); 
102       std::swap(c1, d1); 
103       std::swap(c2, d2); 
104     }
105     if (a1 > a2) std::swap(a1, a2);
106     if (c1 > c2) std::swap(c1, c2);
107     return ((a1 <= c2) && (a2 >= c1));
108   }
109
110   // Standard check
111   return (den1>=0) && (den1<=num) && (den2>=0) && (den2<=num);
112
113 }
114
115 bool
116 Ispy::intersects_line(Rect r, Vector line_start, Vector line_end)
117 {
118   Vector p1 = r.p1;
119   Vector p2 = Vector(r.p2.x, r.p1.y);
120   Vector p3 = r.p2;
121   Vector p4 = Vector(r.p1.x, r.p2.y);
122   if (line_intersects_line(p1, p2, line_start, line_end)) return true;
123   if (line_intersects_line(p2, p3, line_start, line_end)) return true;
124   if (line_intersects_line(p3, p4, line_start, line_end)) return true;
125   if (line_intersects_line(p4, p1, line_start, line_end)) return true;
126   return false;
127 }
128
129 bool
130 Ispy::free_line_of_sight(Vector line_start, Vector line_end, const MovingObject* ignore_object)
131 {
132
133   // check if no tile is in the way
134   float lsx = std::min(line_start.x, line_end.x);
135   float lex = std::max(line_start.x, line_end.x);
136   float lsy = std::min(line_start.y, line_end.y);
137   float ley = std::max(line_start.y, line_end.y);
138   std::list<TileMap*> solid_tilemaps = Sector::current()->solid_tilemaps;
139   for (float test_x = lsx; test_x <= lex; test_x += 16) {
140     for (float test_y = lsy; test_y <= ley; test_y += 16) {
141       for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
142         TileMap* solids = *i;
143         const Tile* tile = solids->get_tile_at(Vector(test_x, test_y));
144         if(!tile) continue;
145         // FIXME: check collision with slope tiles
146         if((tile->getAttributes() & Tile::SOLID)) return false;
147       }
148     }
149   }
150
151   // check if no object is in the way
152   using namespace collision;
153   Sector::MovingObjects& moving_objects = Sector::current()->moving_objects;
154   for(Sector::MovingObjects::const_iterator i = moving_objects.begin();
155       i != moving_objects.end(); ++i) {
156     const MovingObject* moving_object = *i;
157     if (moving_object == ignore_object) continue;
158     if (!moving_object->is_valid()) continue;
159     if ((moving_object->get_group() == COLGROUP_MOVING)
160       || (moving_object->get_group() == COLGROUP_MOVING_STATIC)
161       || (moving_object->get_group() == COLGROUP_STATIC)) {
162       if(intersects_line(moving_object->get_bbox(), line_start, line_end)) return false;
163     }
164   }
165
166   return true;
167 }
168
169 void 
170 Ispy::update(float )
171 {
172
173   if (state == ISPYSTATE_IDLE) {
174     // check if a player has been spotted
175     bool playerSpotted = false;
176     std::vector<Player*> players = Sector::current()->get_players();
177     for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
178       Player* player = *playerIter;
179
180       Vector eye = get_bbox().get_middle();
181       if (dir == LEFT) eye = Vector(get_bbox().p1.x, get_bbox().get_middle().y);
182       if (dir == RIGHT) eye = Vector(get_bbox().p2.x, get_bbox().get_middle().y);
183       if (dir == UP) eye = Vector(get_bbox().get_middle().x, get_bbox().p1.y);
184       if (dir == DOWN) eye = Vector(get_bbox().get_middle().x, get_bbox().p2.y);
185
186       // test for free line of sight to any of all four corners and the middle of a player's bounding box
187       if (free_line_of_sight(eye, player->get_bbox().p1, player)) playerSpotted = true;
188       if (free_line_of_sight(eye, Vector(player->get_bbox().p2.x, player->get_bbox().p1.y), player)) playerSpotted = true;
189       if (free_line_of_sight(eye, player->get_bbox().p2, player)) playerSpotted = true;
190       if (free_line_of_sight(eye, Vector(player->get_bbox().p1.x, player->get_bbox().p2.y), player)) playerSpotted = true;
191       if (free_line_of_sight(eye, player->get_bbox().get_middle(), player)) playerSpotted = true;
192     }
193
194     if (playerSpotted) {
195       sprite->set_action((dir == DOWN) ? "alert-down" : ((dir == LEFT) ? "alert-left" : "alert-right"), 1);
196       state = ISPYSTATE_ALERT;
197
198       std::istringstream stream(script);
199       Sector::current()->run_script(stream, "Ispy");
200     }
201   }
202   if (state == ISPYSTATE_ALERT) {
203     if (sprite->animation_done()) {
204       sprite->set_action((dir == DOWN) ? "hiding-down" : ((dir == LEFT) ? "hiding-left" : "hiding-right"), 1);
205       state = ISPYSTATE_HIDING;
206     }
207   }
208   if (state == ISPYSTATE_HIDING) {
209     if (sprite->animation_done()) {
210       sprite->set_action((dir == DOWN) ? "showing-down" : ((dir == LEFT) ? "showing-left" : "showing-right"), 1);
211       state = ISPYSTATE_SHOWING;
212     }
213   }
214   if (state == ISPYSTATE_SHOWING) {
215     if (sprite->animation_done()) {
216       sprite->set_action((dir == DOWN) ? "idle-down" : ((dir == LEFT) ? "idle-left" : "idle-right"));
217       state = ISPYSTATE_IDLE;
218     }
219   }
220 }
221
222 IMPLEMENT_FACTORY(Ispy, "ispy");
223
224