/
kart.cpp
2318 lines (2050 loc) · 84.9 KB
/
kart.cpp
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2004-2005 Steve Baker <sjbaker1@airmail.net>
// Copyright (C) 2006 SuperTuxKart-Team, Joerg Henrichs, Steve Baker
//
// 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 3
// 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.
#include "karts/kart.hpp"
#include <math.h>
#include <iostream>
#include <algorithm> // for min and max
#include <ICameraSceneNode.h>
#include <ISceneManager.h>
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "audio/sfx_base.hpp"
#include "challenges/unlock_manager.hpp"
#include "config/user_config.hpp"
#include "graphics/camera.hpp"
#include "graphics/material_manager.hpp"
#include "graphics/particle_emitter.hpp"
#include "graphics/particle_kind.hpp"
#include "graphics/particle_kind_manager.hpp"
#include "graphics/shadow.hpp"
#include "graphics/skid_marks.hpp"
#include "graphics/slip_stream.hpp"
#include "graphics/stars.hpp"
#include "guiengine/scalable_font.hpp"
#include "karts/explosion_animation.hpp"
#include "karts/kart_gfx.hpp"
#include "karts/rescue_animation.hpp"
#include "modes/overworld.hpp"
#include "modes/world.hpp"
#include "io/file_manager.hpp"
#include "items/attachment.hpp"
#include "items/item_manager.hpp"
#include "karts/controller/end_controller.hpp"
#include "karts/abstract_kart_animation.hpp"
#include "karts/kart_model.hpp"
#include "karts/kart_properties_manager.hpp"
#include "karts/max_speed.hpp"
#include "karts/skidding.hpp"
#include "modes/linear_world.hpp"
#include "network/network_manager.hpp"
#include "physics/btKart.hpp"
#include "physics/btKartRaycast.hpp"
#include "physics/btUprightConstraint.hpp"
#include "physics/physics.hpp"
#include "race/history.hpp"
#include "tracks/track.hpp"
#include "tracks/track_manager.hpp"
#include "utils/constants.hpp"
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
// Disable warning for using 'this' in base member initializer list
# pragma warning(disable:4355)
#endif
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
# define isnan _isnan
#else
# include <math.h>
#endif
//NETWORK_UPDATE_PLZ
/** The kart constructor.
* \param ident The identifier for the kart model to use.
* \param position The position (or rank) for this kart (between 1 and
* number of karts). This is used to determine the start position.
* \param init_transform The initial position and rotation for this kart.
*/
Kart::Kart (const std::string& ident, unsigned int world_kart_id,
int position, const btTransform& init_transform)
: AbstractKart(ident, world_kart_id, position, init_transform)
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
# pragma warning(1:4355)
#endif
{
m_max_speed = new MaxSpeed(this);
m_terrain_info = new TerrainInfo();
m_powerup = new Powerup(this);
m_vehicle = NULL;
m_initial_position = position;
m_race_position = position;
m_collected_energy = 0;
m_finished_race = false;
m_finish_time = 0.0f;
m_bubblegum_time = 0.0f;
m_bubblegum_torque = 0.0f;
m_invulnerable_time = 0.0f;
m_squash_time = 0.0f;
m_shadow_enabled = false;
m_shadow = NULL;
m_collision_particles = NULL;
m_slipstream = NULL;
m_skidmarks = NULL;
m_controller = NULL;
m_saved_controller = NULL;
m_flying = false;
m_sky_particles_emitter= NULL;
m_stars_effect = NULL;
m_view_blocked_by_plunger = 0;
m_has_caught_nolok_bubblegum = false;
// Initialize custom sound vector (TODO: add back when properly done)
// m_custom_sounds.resize(SFXManager::NUM_CUSTOMS);
// Set position and heading:
m_reset_transform = init_transform;
m_speed = 0.0f;
m_wheel_rotation = 0;
m_wheel_rotation_dt = 0;
m_kart_model->setKart(this);
// Create SFXBase for each custom sound (TODO: add back when properly done)
/*
for (int n = 0; n < SFXManager::NUM_CUSTOMS; n++)
{
int id = m_kart_properties->getCustomSfxId((SFXManager::CustomSFX)n);
// If id == -1 the custom sound was not defined in the .irrkart config file
if (id != -1)
{
m_custom_sounds[n] = sfx_manager->newSFX(id);
}
}*/
m_engine_sound = sfx_manager->createSoundSource(m_kart_properties->getEngineSfxType());
m_beep_sound = sfx_manager->createSoundSource( "horn" );
m_crash_sound = sfx_manager->createSoundSource( "crash" );
m_goo_sound = sfx_manager->createSoundSource( "goo" );
m_skid_sound = sfx_manager->createSoundSource( "skid" );
m_terrain_sound = NULL;
m_previous_terrain_sound = NULL;
} // Kart
// -----------------------------------------------------------------------------
/** This is a second initialisation phase, necessary since in the constructor
* virtual functions are not called for any superclasses.
* \param type Type of the kart.
*/
void Kart::init(RaceManager::KartType type)
{
// In multiplayer mode, sounds are NOT positional
if (race_manager->getNumLocalPlayers() > 1)
{
if (type == RaceManager::KT_PLAYER)
{
// players have louder sounds than AIs
const float factor = std::min(1.0f, race_manager->getNumLocalPlayers()/2.0f);
m_goo_sound->volume( 1.0f / factor );
m_skid_sound->volume( 1.0f / factor );
m_crash_sound->volume( 1.0f / factor );
m_beep_sound->volume( 1.0f / factor );
}
else
{
m_goo_sound->volume( 1.0f / race_manager->getNumberOfKarts() );
m_skid_sound->volume( 1.0f / race_manager->getNumberOfKarts() );
m_crash_sound->volume( 1.0f / race_manager->getNumberOfKarts() );
m_beep_sound->volume( 1.0f / race_manager->getNumberOfKarts() );
}
}
if(!m_engine_sound)
{
Log::error("Kart","Could not allocate a sfx object for the kart. Further errors may ensue!\n");
}
bool animations = true;
const int anims = UserConfigParams::m_show_steering_animations;
if (anims == ANIMS_NONE)
{
animations = false;
}
else if (anims == ANIMS_PLAYERS_ONLY && type != RaceManager::KT_PLAYER)
{
animations = false;
}
loadData(type, animations);
m_kart_gfx = new KartGFX(this);
m_skidding = new Skidding(this,
m_kart_properties->getSkiddingProperties());
// Create the stars effect
m_stars_effect =
new Stars(getNode(),
core::vector3df(0.0f,
getKartModel()->getModel()
->getBoundingBox().MaxEdge.Y,
0.0f) );
reset();
} // init
// ----------------------------------------------------------------------------
/** The destructor frees the memory of this kart, but note that the actual kart
* model is still stored in the kart_properties (m_kart_model variable), so
* it is not reloaded).
*/
Kart::~Kart()
{
// Delete all custom sounds (TODO: add back when properly done)
/*
for (int n = 0; n < SFXManager::NUM_CUSTOMS; n++)
{
if (m_custom_sounds[n] != NULL)
sfx_manager->deleteSFX(m_custom_sounds[n]);
}*/
sfx_manager->deleteSFX(m_engine_sound );
sfx_manager->deleteSFX(m_crash_sound );
sfx_manager->deleteSFX(m_skid_sound );
sfx_manager->deleteSFX(m_goo_sound );
sfx_manager->deleteSFX(m_beep_sound );
delete m_kart_gfx;
if(m_terrain_sound) sfx_manager->deleteSFX(m_terrain_sound);
if(m_previous_terrain_sound) sfx_manager->deleteSFX(m_previous_terrain_sound);
if(m_collision_particles) delete m_collision_particles;
if(m_slipstream) delete m_slipstream;
if(m_sky_particles_emitter) delete m_sky_particles_emitter;
if(m_attachment) delete m_attachment;
if (m_stars_effect) delete m_stars_effect;
delete m_shadow;
if(m_skidmarks) delete m_skidmarks ;
// Ghost karts don't have a body
if(m_body)
{
World::getWorld()->getPhysics()->removeKart(this);
delete m_vehicle;
delete m_vehicle_raycaster;
delete m_uprightConstraint;
}
for(int i=0; i<m_kart_chassis.getNumChildShapes(); i++)
{
delete m_kart_chassis.getChildShape(i);
}
delete m_skidding;
delete m_max_speed;
delete m_terrain_info;
delete m_powerup;
if(m_controller)
delete m_controller;
if(m_saved_controller)
delete m_saved_controller;
} // ~Kart
//-----------------------------------------------------------------------------
/** Reset before a new race. It will remove all attachments, and
* puts the kart back at its original start position.
*/
void Kart::reset()
{
if (m_flying)
{
m_flying = false;
stopFlying();
}
// Add karts back in case that they have been removed (i.e. in battle
// mode) - but only if they actually have a body (e.g. ghost karts
// don't have one).
if(m_body)
World::getWorld()->getPhysics()->addKart(this);
// Reset star effect in case that it is currently being shown.
m_stars_effect->reset();
m_max_speed->reset();
m_powerup->reset();
// Reset animations and wheels
m_kart_model->reset();
// undo bubblegum effect
if (m_bubblegum_time > 0.0f)
{
m_bubblegum_time = 0.0f;
m_bubblegum_torque = 0.0f;
m_body->setDamping(m_kart_properties->getChassisLinearDamping(),
m_kart_properties->getChassisAngularDamping() );
}
// If the controller was replaced (e.g. replaced by end controller),
// restore the original controller.
if(m_saved_controller)
{
m_controller = m_saved_controller;
m_saved_controller = NULL;
}
m_kart_model->setAnimation(KartModel::AF_DEFAULT);
m_attachment->clear();
m_kart_gfx->reset();
m_skidding->reset();
if (m_collision_particles)
m_collision_particles->setCreationRateAbsolute(0.0f);
m_race_position = m_initial_position;
m_finished_race = false;
m_eliminated = false;
m_finish_time = 0.0f;
m_bubblegum_time = 0.0f;
m_bubblegum_torque = 0.0f;
m_invulnerable_time = 0.0f;
m_squash_time = 0.0f;
m_node->setScale(core::vector3df(1.0f, 1.0f, 1.0f));
m_collected_energy = 0;
m_has_started = false;
m_wheel_rotation = 0;
m_bounce_back_time = 0.0f;
m_time_last_crash = 0.0f;
m_speed = 0.0f;
m_view_blocked_by_plunger = 0.0f;
m_has_caught_nolok_bubblegum = false;
if(m_terrain_sound)
{
sfx_manager->deleteSFX(m_terrain_sound);
}
if(m_previous_terrain_sound)
{
sfx_manager->deleteSFX(m_previous_terrain_sound);
}
m_terrain_sound = NULL;
m_previous_terrain_sound = NULL;
if(m_engine_sound)
m_engine_sound->stop();
m_controls.reset();
m_slipstream->reset();
if(m_vehicle)
{
m_vehicle->reset();
}
// Randomize wheel rotation if needed
if (m_kart_properties->hasRandomWheels() && m_vehicle && m_kart_model)
{
scene::ISceneNode** graphic_wheels = m_kart_model->getWheelNodes();
// FIXME Hardcoded i < 4 comes from the arrays in KartModel
for (int i = 0; i < m_vehicle->getNumWheels() && i < 4; i++)
{
// Physics
btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
wheel.m_rotation = btScalar(rand() % 360);
// And graphics
core::vector3df wheel_rotation(wheel.m_rotation, 0, 0);
if (graphic_wheels[i])
graphic_wheels[i]->setRotation(wheel_rotation);
} // for wheels
} // if random wheel rotation
setTrans(m_reset_transform);
applyEngineForce (0.0f);
AbstractKart::reset();
if (m_skidmarks)
{
m_skidmarks->reset();
const Track *track =
track_manager->getTrack( race_manager->getTrackName() );
m_skidmarks->adjustFog(track->isFogEnabled() );
}
m_terrain_info->update(getXYZ());
// Reset is also called when the kart is created, at which time
// m_controller is not yet defined, so this has to be tested here.
if(m_controller)
m_controller->reset();
// 3 strikes mode can hide the wheels
scene::ISceneNode** wheels = getKartModel()->getWheelNodes();
if(wheels[0]) wheels[0]->setVisible(true);
if(wheels[1]) wheels[1]->setVisible(true);
if(wheels[2]) wheels[2]->setVisible(true);
if(wheels[3]) wheels[3]->setVisible(true);
} // reset
// -----------------------------------------------------------------------------
void Kart::increaseMaxSpeed(unsigned int category, float add_speed,
float engine_force, float duration,
float fade_out_time)
{
m_max_speed->increaseMaxSpeed(category, add_speed, engine_force, duration,
fade_out_time);
} // increaseMaxSpeed
// -----------------------------------------------------------------------------
void Kart::setSlowdown(unsigned int category, float max_speed_fraction,
float fade_in_time)
{
m_max_speed->setSlowdown(category, max_speed_fraction, fade_in_time);
} // setSlowdown
// -----------------------------------------------------------------------------
float Kart::getCurrentMaxSpeed() const
{
return m_max_speed->getCurrentMaxSpeed();
} // getCurrentMaxSpeed
// -----------------------------------------------------------------------------
float Kart::getSpeedIncreaseTimeLeft(unsigned int category) const
{
return m_max_speed->getSpeedIncreaseTimeLeft(category);
} // getSpeedIncreaseTimeLeft
// -----------------------------------------------------------------------------
/** Returns the current material the kart is on. */
const Material *Kart::getMaterial() const
{
return m_terrain_info->getMaterial();
} // getMaterial
// -----------------------------------------------------------------------------
/** Returns the previous material the kart was one (which might be
* the same as getMaterial() ). */
const Material *Kart::getLastMaterial() const
{
return m_terrain_info->getLastMaterial();
} // getLastMaterial
// -----------------------------------------------------------------------------
/** Returns the pitch of the terrain depending on the heading. */
float Kart::getTerrainPitch(float heading) const
{
return m_terrain_info->getTerrainPitch(heading);
} // getTerrainPitch
// -----------------------------------------------------------------------------
/** Returns the height of the terrain. we're currently above */
float Kart::getHoT() const
{
return m_terrain_info->getHoT();
} // getHoT
// ----------------------------------------------------------------------------
/** Sets the powerup this kart has collected.
* \param t Type of the powerup.
* \param n Number of powerups collected.
*/
void Kart::setPowerup(PowerupManager::PowerupType t, int n)
{
m_powerup->set(t, n);
} // setPowerup
// -----------------------------------------------------------------------------
int Kart::getNumPowerup() const
{
return m_powerup->getNum();
} // getNumPowerup
// -----------------------------------------------------------------------------
/** Saves the old controller in m_saved_controller and stores a new
* controller. The save controller is needed in case of a reset.
* \param controller The new controller to use (atm it's always an
* end controller).
*/
void Kart::setController(Controller *controller)
{
assert(m_saved_controller==NULL);
m_saved_controller = m_controller;
m_controller = controller;
} // setController
// -----------------------------------------------------------------------------
/** Sets the position in race this kart has .
* The position in this race for this kart (1<=p<=n)
*/
void Kart::setPosition(int p)
{
m_controller->setPosition(p);
m_race_position = p;
} // setPosition
// -----------------------------------------------------------------------------
/** Returns a transform that will align an object with the kart: the heading
* and the pitch will be set appropriately. A custom pitch value can be
* specified in order to overwrite the terrain pitch (which would be used
* otherwise).
* \param custom_pitch Pitch value to overwrite the terrain pitch. A value of
* -1 indicates that the custom_pitch value should not be used, instead
* the actual pitch of the terrain is to be used.
*/
btTransform Kart::getAlignedTransform(const float custom_pitch)
{
btTransform trans = getTrans();
float pitch = (custom_pitch == -1 ? getTerrainPitch(getHeading())
: custom_pitch);
btMatrix3x3 m;
m.setEulerZYX(pitch, getHeading()+m_skidding->getVisualSkidRotation(),
0.0f);
trans.setBasis(m);
return trans;
} // getAlignedTransform
// ----------------------------------------------------------------------------
/** Creates the physical representation of this kart. Atm it uses the actual
* extention of the kart model to determine the size of the collision body.
*/
void Kart::createPhysics()
{
// First: Create the chassis of the kart
// -------------------------------------
float kart_width = getKartWidth();
float kart_length = getKartLength();
float kart_height = getKartHeight();
// improve physics for tall karts
if (kart_height > kart_length*0.6f)
{
kart_height = kart_length*0.6f;
}
btCollisionShape *shape;
const Vec3 &bevel = m_kart_properties->getBevelFactor();
if(bevel.getX() || bevel.getY() || bevel.getZ())
{
Vec3 orig_factor(1, 1, 1-bevel.getZ());
Vec3 bevel_factor(1.0f-bevel.getX(),
1.0f-bevel.getY(),
1.0f );
btConvexHullShape *hull = new btConvexHullShape();
for(int x=-1; x<=1; x+=2)
{
for(int y=-1; y<=1; y+=2)
{
for(int z=-1; z<=1; z+=2)
{
Vec3 p(x*getKartModel()->getWidth()*0.5f,
y*getKartModel()->getHeight()*0.5f,
z*getKartModel()->getLength()*0.5f);
hull->addPoint(p*orig_factor);
hull->addPoint(p*bevel_factor);
} // for z
} // for y
} // for x
// This especially enables proper drawing of the point cloud
hull->initializePolyhedralFeatures();
shape = hull;
} // bevel.getX()!=0
else
{
shape = new btBoxShape(btVector3(0.5f*kart_width,
0.5f*kart_height,
0.5f*kart_length));
}
btTransform shiftCenterOfGravity;
shiftCenterOfGravity.setIdentity();
// Shift center of gravity downwards, so that the kart
// won't topple over too easy.
shiftCenterOfGravity.setOrigin(m_kart_properties->getGravityCenterShift());
m_kart_chassis.addChildShape(shiftCenterOfGravity, shape);
// Set mass and inertia
// --------------------
float mass = m_kart_properties->getMass();
// Position the chassis
// --------------------
btTransform trans;
trans.setIdentity();
createBody(mass, trans, &m_kart_chassis,
m_kart_properties->getRestitution());
m_user_pointer.set(this);
m_body->setDamping(m_kart_properties->getChassisLinearDamping(),
m_kart_properties->getChassisAngularDamping() );
// Reset velocities
// ----------------
m_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
m_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
// Create the actual vehicle
// -------------------------
m_vehicle_raycaster =
new btKartRaycaster(World::getWorld()->getPhysics()->getPhysicsWorld(),
stk_config->m_smooth_normals &&
World::getWorld()->getTrack()->smoothNormals());
m_vehicle = new btKart(m_body, m_vehicle_raycaster, this);
// never deactivate the vehicle
m_body->setActivationState(DISABLE_DEACTIVATION);
// Add wheels
// ----------
float wheel_radius = m_kart_properties->getWheelRadius();
float suspension_rest = m_kart_properties->getSuspensionRest();
btVector3 wheel_direction(0.0f, -1.0f, 0.0f);
btVector3 wheel_axle(-1.0f, 0.0f, 0.0f);
btKart::btVehicleTuning tuning;
tuning.m_maxSuspensionTravelCm =
m_kart_properties->getSuspensionTravelCM();
tuning.m_maxSuspensionForce =
m_kart_properties->getMaxSuspensionForce();
for(unsigned int i=0; i<4; i++)
{
bool is_front_wheel = i<2;
btWheelInfo& wheel = m_vehicle->addWheel(
m_kart_model->getWheelPhysicsPosition(i),
wheel_direction, wheel_axle, suspension_rest,
wheel_radius, tuning, is_front_wheel);
wheel.m_suspensionStiffness = m_kart_properties->getSuspensionStiffness();
wheel.m_wheelsDampingRelaxation = m_kart_properties->getWheelDampingRelaxation();
wheel.m_wheelsDampingCompression = m_kart_properties->getWheelDampingCompression();
wheel.m_frictionSlip = m_kart_properties->getFrictionSlip();
wheel.m_rollInfluence = m_kart_properties->getRollInfluence();
}
// Obviously these allocs have to be properly managed/freed
btTransform t;
t.setIdentity();
m_uprightConstraint=new btUprightConstraint(this, t);
m_uprightConstraint->setLimit(m_kart_properties->getUprightTolerance());
m_uprightConstraint->setBounce(0.0f);
m_uprightConstraint->setMaxLimitForce(m_kart_properties->getUprightMaxForce());
m_uprightConstraint->setErp(1.0f);
m_uprightConstraint->setLimitSoftness(1.0f);
m_uprightConstraint->setDamping(0.0f);
World::getWorld()->getPhysics()->addKart(this);
} // createPhysics
// ----------------------------------------------------------------------------
void Kart::flyUp()
{
m_flying = true;
Moveable::flyUp();
}
void Kart::flyDown()
{
if (isNearGround())
{
stopFlying();
m_flying = false;
}
else
{
Moveable::flyDown();
}
} // flyUp
// ----------------------------------------------------------------------------
/** Starts the engine sound effect. Called once the track intro phase is over.
*/
void Kart::startEngineSFX()
{
if(!m_engine_sound)
return;
// In multiplayer mode, sounds are NOT positional (because we have
// multiple listeners) so the engine sounds of all AIs is constantly
// heard. So reduce volume of all sounds.
if (race_manager->getNumLocalPlayers() > 1)
{
const int np = race_manager->getNumLocalPlayers();
const int nai = race_manager->getNumberOfKarts() - np;
// player karts twice as loud as AIs toghether
const float players_volume = (np * 2.0f) / (np*2.0f + np);
if (m_controller->isPlayerController())
m_engine_sound->volume( players_volume / np );
else
m_engine_sound->volume( (1.0f - players_volume) / nai );
}
m_engine_sound->speed(0.6f);
m_engine_sound->setLoop(true);
m_engine_sound->play();
} // startEngineSFX
//-----------------------------------------------------------------------------
/** Returns true if the kart is 'resting', i.e. (nearly) not moving.
*/
bool Kart::isInRest() const
{
return fabs(m_body->getLinearVelocity ().y())<0.2;
} // isInRest
//-----------------------------------------------------------------------------
/** Multiplies the velocity of the kart by a factor f (both linear
* and angular). This is used by anvils, which suddenly slow down the kart
* when they are attached.
*/
void Kart::adjustSpeed(float f)
{
m_body->setLinearVelocity(m_body->getLinearVelocity()*f);
m_body->setAngularVelocity(m_body->getAngularVelocity()*f);
} // adjustSpeed
//-----------------------------------------------------------------------------
/** This method is to be called every time the mass of the kart is updated,
* which includes attaching an anvil to the kart (and detaching).
*/
void Kart::updateWeight()
{
float mass = m_kart_properties->getMass() + m_attachment->weightAdjust();
btVector3 inertia;
m_kart_chassis.calculateLocalInertia(mass, inertia);
m_body->setMassProps(mass, inertia);
} // updateWeight
//-----------------------------------------------------------------------------
/** Sets that this kart has finished the race and finishing time. It also
* notifies the race_manager about the race completion for this kart.
* \param time The finishing time for this kart. It can either be the
* actual time when the kart finished (in which case time() =
* world->getTime()), or the estimated time in case that all
* player kart have finished the race and all AI karts get
* an estimated finish time set.
*/
void Kart::finishedRace(float time)
{
// m_finished_race can be true if e.g. an AI kart was set to finish
// because the race was over (i.e. estimating the finish time). If
// this kart then crosses the finish line (with the end controller)
// it would trigger a race end again.
if(m_finished_race) return;
m_finished_race = true;
m_finish_time = time;
m_controller->finishedRace(time);
m_kart_model->finishedRace();
race_manager->kartFinishedRace(this, time);
if (race_manager->getMinorMode() == RaceManager::MINOR_MODE_NORMAL_RACE ||
race_manager->getMinorMode() == RaceManager::MINOR_MODE_TIME_TRIAL)
{
// in modes that support it, start end animation
setController(new EndController(this, m_controller->getPlayer(),
m_controller));
GameSlot *slot = unlock_manager->getCurrentSlot();
const Challenge *challenge = slot->getCurrentChallenge();
// In case of a GP challenge don't make the end animation depend
// on if the challenge is fulfilled
if(challenge && !challenge->getData()->isGrandPrix())
{
if(challenge->getData()->isChallengeFulfilled())
m_kart_model->setAnimation(KartModel::AF_WIN_START);
else
m_kart_model->setAnimation(KartModel::AF_LOSE_START);
}
else if(m_race_position<=0.5f*race_manager->getNumberOfKarts() ||
m_race_position==1)
m_kart_model->setAnimation(KartModel::AF_WIN_START);
else
m_kart_model->setAnimation(KartModel::AF_LOSE_START);
RaceGUIBase* m = World::getWorld()->getRaceGUI();
if(m)
{
m->addMessage((getPosition() == 1 ? _("You won the race!") : _("You finished the race!")) ,
this, 2.0f);
}
}
else if (race_manager->getMinorMode() == RaceManager::MINOR_MODE_FOLLOW_LEADER)
{
// start end animation
setController(new EndController(this, m_controller->getPlayer(),
m_controller));
if(m_race_position<=2)
m_kart_model->setAnimation(KartModel::AF_WIN_START);
else if(m_race_position>=0.7f*race_manager->getNumberOfKarts())
m_kart_model->setAnimation(KartModel::AF_LOSE_START);
RaceGUIBase* m = World::getWorld()->getRaceGUI();
if(m)
{
m->addMessage((getPosition() == 2 ? _("You won the race!") : _("You finished the race!")) ,
this, 2.0f);
}
}
else if (race_manager->getMinorMode() == RaceManager::MINOR_MODE_3_STRIKES)
{
setController(new EndController(this, m_controller->getPlayer(),
m_controller));
}
} // finishedRace
//-----------------------------------------------------------------------------
/** Called when an item is collected. It will either adjust the collected
* energy, or update the attachment or powerup for this kart.
* \param item The item that was hit.
* \param add_info Additional info, used in networking games to force
* a specific item to be used (instead of a random item) to keep
* all karts in synch.
*/
void Kart::collectedItem(Item *item, int add_info)
{
float old_energy = m_collected_energy;
const Item::ItemType type = item->getType();
switch (type)
{
case Item::ITEM_BANANA:
m_attachment->hitBanana(item, add_info);
break;
case Item::ITEM_NITRO_SMALL:
m_collected_energy += m_kart_properties->getNitroSmallContainer();
break;
case Item::ITEM_NITRO_BIG:
m_collected_energy += m_kart_properties->getNitroBigContainer();
break;
case Item::ITEM_BONUS_BOX :
{
m_powerup->hitBonusBox(*item, add_info);
break;
}
case Item::ITEM_BUBBLEGUM:
m_has_caught_nolok_bubblegum = (item->getEmitter() != NULL && item->getEmitter()->getIdent() == "nolok");
// slow down
m_bubblegum_time = 1.0f;
m_bubblegum_torque = rand()%2 ? 500.0f : -500.0f;
m_body->setDamping(0.8f, 0.8f);
m_goo_sound->position(getXYZ());
m_goo_sound->play();
// Play appropriate custom character sound
playCustomSFX(SFXManager::CUSTOM_GOO);
break;
default : break;
} // switch TYPE
// Attachments and powerups are stored in the corresponding
// functions (hit{Red,Green}Item), so only coins need to be
// stored here.
/*if(NetworkManager::getInstance()->isServer() &&
(type==Item::ITEM_NITRO_BIG || type==Item::ITEM_NITRO_SMALL) )
{
race_state->itemCollected(getWorldKartId(), item->getItemId());
}*/
if ( m_collected_energy > m_kart_properties->getNitroMax())
m_collected_energy = m_kart_properties->getNitroMax();
m_controller->collectedItem(*item, add_info, old_energy);
} // collectedItem
//-----------------------------------------------------------------------------
/** Simulates gears by adjusting the force of the engine. It also takes the
* effect of the zipper into account.
*/
float Kart::getActualWheelForce()
{
float add_force = m_max_speed->getCurrentAdditionalEngineForce();
assert(!isnan(add_force));
const std::vector<float>& gear_ratio=m_kart_properties->getGearSwitchRatio();
for(unsigned int i=0; i<gear_ratio.size(); i++)
{
if(m_speed <= m_kart_properties->getMaxSpeed()*gear_ratio[i])
{
assert(!isnan(m_kart_properties->getMaxPower()));
assert(!isnan(m_kart_properties->getGearPowerIncrease()[i]));
return m_kart_properties->getMaxPower()
*m_kart_properties->getGearPowerIncrease()[i]
+add_force;
}
}
assert(!isnan(m_kart_properties->getMaxPower()));
return m_kart_properties->getMaxPower()+add_force;
} // getActualWheelForce
//-----------------------------------------------------------------------------
/** The kart is on ground if all 4 wheels touch the ground, and if no special
* animation (rescue, explosion etc.) is happening).
*/
bool Kart::isOnGround() const
{
return ((int)m_vehicle->getNumWheelsOnGround() == m_vehicle->getNumWheels()
&& !getKartAnimation());
} // isOnGround
//-----------------------------------------------------------------------------
/** The kart is near the ground, but not necessarily on it (small jumps). This
* is used to determine when to switch off the upright constraint, so that
* explosions can be more violent, while still
*/
bool Kart::isNearGround() const
{
if(m_terrain_info->getHoT()==Track::NOHIT)
return false;
else
return ((getXYZ().getY() - m_terrain_info->getHoT())
< stk_config->m_near_ground);
} // isNearGround
//-----------------------------------------------------------------------------
/** Shows the star effect for a certain time.
* \param t Time to show the star effect for.
*/
void Kart::showStarEffect(float t)
{
m_stars_effect->showFor(t);
} // showStarEffect
//-----------------------------------------------------------------------------
void Kart::eliminate()
{
if (!getKartAnimation())
{
World::getWorld()->getPhysics()->removeKart(this);
}
if (m_stars_effect)
{
m_stars_effect->reset();
m_stars_effect->update(1);
}
m_eliminated = true;
m_node->setVisible(false);
} // eliminate
//-----------------------------------------------------------------------------
/** Updates the kart in each time step. It updates the physics setting,
* particle effects, camera position, etc.
* \param dt Time step size.
*/
void Kart::update(float dt)
{
if ( UserConfigParams::m_graphical_effects )
{
// update star effect (call will do nothing if stars are not activated)
m_stars_effect->update(dt);
}
if(m_squash_time>=0)
{
m_squash_time-=dt;
// If squasing time ends, reset the model
if(m_squash_time<=0)
{
m_node->setScale(core::vector3df(1.0f, 1.0f, 1.0f));
m_max_speed->setSlowdown(MaxSpeed::MS_DECREASE_SQUASH,
/*slowdown*/1.0f, /*fade in*/0.0f);
}
}
if (m_bubblegum_time > 0.0f)
{
m_bubblegum_time -= dt;
if (m_bubblegum_time <= 0.0f)
{
// undo bubblegum effect
m_body->setDamping(m_kart_properties->getChassisLinearDamping(),
m_kart_properties->getChassisAngularDamping() );
m_bubblegum_torque = 0.0f;
}
}
// Update the position and other data taken from the physics
Moveable::update(dt);
if(!history->replayHistory())
m_controller->update(dt);