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