File: gnu.cpp

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (244 lines) | stat: -rw-r--r-- 6,133 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Weapon gnu : a gnu jump in (more or less) random directions and explodes
 *****************************************************************************/

#include "character/character.h"
#include "game/config.h"
#include "game/game_time.h"
#include "graphic/sprite.h"
#include "interface/game_msg.h"
#include "map/camera.h"
#include "network/randomsync.h"
#include "object/objects_list.h"
#include "sound/jukebox.h"
#include "team/teams_list.h"
#include "tool/math_tools.h"
#include "tool/resource_manager.h"
#include "weapon/explosion.h"
#include "weapon/gnu.h"
#include "weapon/weapon_cfg.h"

const uint TIME_BETWEEN_REBOUND = 600;

class Gnu : public WeaponProjectile
{
  bool flipped;
  int save_x, save_y;
  uint last_rebound_time;
protected:
  void SignalOutOfMap();
public:
  Gnu(ExplosiveWeaponConfig& cfg,
      WeaponLauncher * p_launcher);
  void Shoot(Double strength);
  void Refresh();

  virtual void Explosion();
};

Gnu::Gnu(ExplosiveWeaponConfig& cfg,
         WeaponLauncher * p_launcher) :
  WeaponProjectile("gnu", cfg, p_launcher)
{
  explode_with_collision = false;
  explode_with_timeout = true;
  last_rebound_time = 0;
  image->EnableCaches(true, 0); // Only cache flipping
}

void Gnu::Shoot(Double strength)
{
  WeaponProjectile::Shoot(strength);

  save_x=GetX();
  save_y=GetY();

  Double angle = ActiveCharacter().GetFiringAngle();

  flipped = angle>=HALF_PI || angle<=-HALF_PI;
}

void Gnu::Refresh()
{
  if (m_energy == 0) {
    Explosion();
    return;
  }
  int tmp = GetMSSinceTimeoutStart();
  if (cfg.timeout && tmp > 1000 * (GetTotalTimeout()))
    SignalTimeout();

  Double norm, angle;
  //When we hit the ground, jump !
  if (!IsMoving()&& !FootsInVacuum()) {
    // Limiting number of rebound to avoid desync
    if (last_rebound_time + TIME_BETWEEN_REBOUND > GameTime::GetInstance()->Read()) {
      image->SetRotation_rad(ZERO);
      return;
    }
    last_rebound_time = GameTime::GetInstance()->Read();
    MSG_DEBUG("weapon.gnu", "Jump ! (time = %d)", last_rebound_time);
    //If the GNU is stuck in ground -> change direction
    int x = GetX();
    int y = GetY();
    if (x==save_x && y==save_y)
      flipped = !flipped;;
    save_x = x;
    save_y = y;

    //Do the jump
    norm = RandomSync().GetDouble(2.0, 5.0);
    PutOutOfGround();
    SetSpeedXY(Point2d((flipped) ? -norm : norm , - norm * THREE));
    JukeBox::GetInstance()->Play("default", "weapon/gnu_bounce");
  }

  //Due to a bug in the physic engine
  //sometimes, angle==infinite (according to gdb) ??
  GetSpeed(norm, angle);

  angle = RestrictAngle(angle)*ONE_HALF;
  if (flipped) {
    if (angle > ZERO)
      angle -= HALF_PI;
    else
      angle += HALF_PI;
  }

  image->SetRotation_rad(angle);
  image->SetFlipped(flipped);
  image->Update();
}

void Gnu::Explosion()
{
  WeaponProjectile::Explosion();
}

void Gnu::SignalOutOfMap()
{
  Weapon::Message(_("The Gnu left the battlefield before exploding!"));
  WeaponProjectile::SignalOutOfMap();
}

//-----------------------------------------------------------------------------

GnuLauncher::GnuLauncher() :
  WeaponLauncher(WEAPON_GNU, "gnulauncher", new ExplosiveWeaponConfig()),
  current_gnu(NULL),
  gnu_death_time(0)
{
  UpdateTranslationStrings();

  current_gnu = NULL;
  gnu_death_time = 0;

  m_category = SPECIAL;
  ReloadLauncher();

  // unit will be used when the gnu disappears
  use_unit_on_first_shoot = false;
}

void GnuLauncher::UpdateTranslationStrings()
{
  m_name = _("Gnu Launcher");
  m_help = _("Set timer 1-6 using +/- or 1-6 keys\nPress space to shoot\nOne shoot per turn");
}

bool GnuLauncher::p_Shoot()
{
  if (current_gnu || gnu_death_time)
    return false;

  current_gnu = static_cast<Gnu *>(projectile);
  gnu_death_time = 0;
  bool r = WeaponLauncher::p_Shoot();
  return r;
}

void GnuLauncher::Refresh()
{
  WeaponLauncher::Refresh();
  if (current_gnu)
    return;

  if (gnu_death_time && gnu_death_time + 2000 < GameTime::GetInstance()->Read()) {

    UseAmmoUnit();
    gnu_death_time = 0;
  }
}

bool GnuLauncher::IsOnCooldownFromShot() const
{
  return (current_gnu || gnu_death_time);
}

bool GnuLauncher::IsReady() const
{
  return !IsOnCooldownFromShot() && WeaponLauncher::IsReady();
}

void GnuLauncher::StopShooting()
{
  if (current_gnu)
    current_gnu->Explosion();
  WeaponLauncher::StopShooting();
}

bool GnuLauncher::IsPreventingLRMovement()
{
  return (current_gnu || gnu_death_time);
}

bool GnuLauncher::IsPreventingJumps()
{
  return (current_gnu || gnu_death_time);
}

bool GnuLauncher::IsPreventingWeaponAngleChanges()
{
  return (current_gnu || gnu_death_time);
}

void GnuLauncher::SignalEndOfProjectile()
{
  if (!current_gnu)
    return;

  current_gnu = NULL;
  gnu_death_time = GameTime::GetInstance()->Read();
}

WeaponProjectile * GnuLauncher::GetProjectileInstance()
{
  return new Gnu(cfg(), this);
}

std::string GnuLauncher::GetWeaponWinString(const char *TeamName, uint items_count ) const
{
  return Format(ngettext(
            "%s team has won %u Gnu! Blow them all, cowboy!",
            "%s team has won %u Gnus! Blow them all, cowboy!",
            items_count), TeamName, items_count);
}