object/bonus_block.[ch]pp: Fix a NULL-pointer dereference.
[supertux.git] / src / object / bonus_block.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.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/bonus_block.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "lisp/list_iterator.hpp"
22 #include "object/broken_brick.hpp"
23 #include "object/flower.hpp"
24 #include "object/bouncy_coin.hpp"
25 #include "object/growup.hpp"
26 #include "object/oneup.hpp"
27 #include "object/player.hpp"
28 #include "object/portable.hpp"
29 #include "object/specialriser.hpp"
30 #include "object/star.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "supertux/constants.hpp"
33 #include "supertux/level.hpp"
34 #include "supertux/object_factory.hpp"
35 #include "supertux/sector.hpp"
36
37 #include <stdexcept>
38
39 BonusBlock::BonusBlock(const Vector& pos, int data) :
40   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
41   contents(),
42   object(0)
43 {
44   bbox.set_pos(pos);
45   sprite->set_action("normal");
46   switch(data) {
47     case 1: contents = CONTENT_COIN; break;
48     case 2: contents = CONTENT_FIREGROW; break;
49     case 3: contents = CONTENT_STAR; break;
50     case 4: contents = CONTENT_1UP; break;
51     case 5: contents = CONTENT_ICEGROW; break;
52     default:
53       log_warning << "Invalid box contents" << std::endl;
54       contents = CONTENT_COIN;
55       break;
56   }
57 }
58
59 BonusBlock::BonusBlock(const Reader& lisp) :
60   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
61   contents(),
62   object(0)
63 {
64   Vector pos;
65
66   contents = CONTENT_COIN;
67   lisp::ListIterator iter(&lisp);
68   while(iter.next()) {
69     const std::string& token = iter.item();
70     if(token == "x") {
71       iter.value()->get(pos.x);
72     } else if(token == "y") {
73       iter.value()->get(pos.y);
74     } else if(token == "contents") {
75       std::string contentstring;
76       iter.value()->get(contentstring);
77       if(contentstring == "coin") {
78         contents = CONTENT_COIN;
79       } else if(contentstring == "firegrow") {
80         contents = CONTENT_FIREGROW;
81       } else if(contentstring == "icegrow") {
82         contents = CONTENT_ICEGROW;
83       } else if(contentstring == "star") {
84         contents = CONTENT_STAR;
85       } else if(contentstring == "1up") {
86         contents = CONTENT_1UP;
87       } else if(contentstring == "custom") {
88         contents = CONTENT_CUSTOM;
89       } else {
90         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
91       }
92     } else {
93       if(contents == CONTENT_CUSTOM) {
94         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
95         object = dynamic_cast<MovingObject*> (game_object);
96         if(object == 0)
97           throw std::runtime_error(
98             "Only MovingObjects are allowed inside BonusBlocks");
99       } else {
100         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
101       }
102     }
103   }
104
105   if(contents == CONTENT_CUSTOM && object == 0)
106     throw std::runtime_error("Need to specify content object for custom block");
107
108   bbox.set_pos(pos);
109 }
110
111 BonusBlock::~BonusBlock()
112 {
113   delete object;
114 }
115
116 void
117 BonusBlock::hit(Player & player)
118 {
119   try_open(&player);
120 }
121
122 HitResponse
123 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
124
125   Player* player = dynamic_cast<Player*> (&other);
126   if (player) {
127     if (player->does_buttjump)
128       try_open(player);
129   }
130
131   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
132   if(badguy) {
133     // hit contains no information for collisions with blocks.
134     // Badguy's bottom has to be below the top of the block
135     // SHIFT_DELTA is required to slide over one tile gaps.
136     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
137       try_open(player);
138     }
139   }
140   Portable* portable = dynamic_cast<Portable*> (&other);
141   if(portable) {
142     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
143     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
144       try_open(player);
145     }
146   }
147   return Block::collision(other, hit);
148 }
149
150 void
151 BonusBlock::try_open(Player *player)
152 {
153   if(sprite->get_action() == "empty") {
154     sound_manager->play("sounds/brick.wav");
155     return;
156   }
157
158   Sector* sector = Sector::current();
159   assert(sector);
160
161   if (player == NULL)
162     player = sector->player;
163   
164   if (player == NULL)
165     return;
166
167   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
168
169   switch(contents) {
170     case CONTENT_COIN:
171       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
172       player->get_status()->add_coins(1);
173       Sector::current()->get_level()->stats.coins++;
174       break;
175
176     case CONTENT_FIREGROW:
177       if(player->get_status()->bonus == NO_BONUS) {
178         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
179         sector->add_object(riser);
180       } else {
181         SpecialRiser* riser = new SpecialRiser(
182           get_pos(), new Flower(FIRE_BONUS));
183         sector->add_object(riser);
184       }
185       sound_manager->play("sounds/upgrade.wav");
186       break;
187
188     case CONTENT_ICEGROW:
189       if(player->get_status()->bonus == NO_BONUS) {
190         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
191         sector->add_object(riser);
192       } else {
193         SpecialRiser* riser = new SpecialRiser(
194           get_pos(), new Flower(ICE_BONUS));
195         sector->add_object(riser);
196       }
197       sound_manager->play("sounds/upgrade.wav");
198       break;
199
200     case CONTENT_STAR:
201       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
202       break;
203
204     case CONTENT_1UP:
205       sector->add_object(new OneUp(get_pos(), direction));
206       break;
207
208     case CONTENT_CUSTOM:
209       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
210       object = 0;
211       sector->add_object(riser);
212       sound_manager->play("sounds/upgrade.wav");
213       break;
214   }
215
216   start_bounce(player);
217   sprite->set_action("empty");
218 }
219
220 void
221 Block::break_me()
222 {
223   Sector* sector = Sector::current();
224   sector->add_object(
225     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
226   sector->add_object(
227     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
228                     Vector(-150, -300)));
229   sector->add_object(
230     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
231                     Vector(100, -400)));
232   sector->add_object(
233     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
234                     Vector(150, -300)));
235   remove_me();
236 }
237
238 /* EOF */