Teleport.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <actions/Teleport.h>
00022 #include <actions/CameraPositionAction.h>
00023 #include <common/Defines.h>
00024 #include <common/OptionsScorched.h>
00025 #include <tank/TankContainer.h>
00026 #include <tank/TankState.h>
00027 #include <tank/TankPosition.h>
00028 #include <target/TargetDamageCalc.h>
00029 #include <target/TargetLife.h>
00030 #include <engine/ActionController.h>
00031 #include <engine/ScorchedContext.h>
00032 #include <weapons/AccessoryStore.h>
00033 #include <landscapemap/DeformLandscape.h>
00034 #include <landscapemap/LandscapeMaps.h>
00035 #ifndef S3D_SERVER
00036         #include <sound/SoundUtils.h>
00037         #include <sprites/TeleportRenderer.h>
00038         #include <land/VisibilityPatchGrid.h>
00039 #endif
00040 
00041 Teleport::Teleport(FixedVector position,
00042                 WeaponFireContext &weaponContext,
00043                 WeaponTeleport *weapon) :
00044         ActionReferenced("Teleport"),
00045         position_(position), 
00046         weaponContext_(weaponContext),
00047         weapon_(weapon),
00048         totalTime_(0),
00049         firstTime_(true),
00050         vPoint_(0)
00051 {
00052 
00053 }
00054 
00055 Teleport::~Teleport()
00056 {
00057         if (vPoint_) context_->getViewPoints().releaseViewPoint(vPoint_);
00058 }
00059 
00060 void Teleport::init()
00061 {
00062         vPoint_ = context_->getViewPoints().getNewViewPoint(weaponContext_.getPlayerId());
00063 
00064 #ifndef S3D_SERVER
00065         if (!context_->getServerMode())
00066         {
00067                 Tank *tank = context_->getTankContainer().getTankById(weaponContext_.getPlayerId());
00068                 if (tank && tank->getState().getState() == TankState::sNormal)
00069                 {
00070                         Vector white(1.0f, 1.0f, 1.0f);
00071                         TeleportRenderer *teleport = new TeleportRenderer(
00072                                 tank->getPosition().getTankTurretPosition().asVector(),
00073                                 white);
00074                         context_->getActionController().addAction(new SpriteAction(teleport));
00075                 }
00076         }
00077 #endif
00078 
00079         CameraPositionAction *pos = new CameraPositionAction(
00080                 position_, 5, 5);
00081         context_->getActionController().addAction(pos);
00082 }
00083 
00084 void Teleport::simulate(fixed frameTime, bool &remove)
00085 {
00086         if (vPoint_) vPoint_->setPosition(position_);
00087 
00088         if (firstTime_)
00089         {
00090                 firstTime_ = false;
00091 
00092 #ifndef S3D_SERVER
00093                 if (!context_->getServerMode())
00094                 {
00095                         Tank *tank = context_->getTankContainer().getTankById(weaponContext_.getPlayerId());
00096                         if (tank && tank->getState().getState() == TankState::sNormal)
00097                         {
00098                                 SoundBuffer *activateSound = 
00099                                         Sound::instance()->fetchOrCreateBuffer(
00100                                                 S3D::getDataFile(weapon_->getSound()));
00101                                 SoundUtils::playAbsoluteSound(VirtualSoundPriority::eAction,
00102                                         activateSound, tank->getPosition().getTankPosition().asVector());
00103                         }
00104                 }
00105 #endif // #ifndef S3D_SERVER
00106         }
00107 
00108         totalTime_ += frameTime;
00109         if (totalTime_ > weapon_->getDelay(*context_))
00110         {
00111                 Tank *tank = context_->getTankContainer().getTankById(weaponContext_.getPlayerId());
00112                 if (tank && tank->getState().getState() == TankState::sNormal)
00113                 {
00114                         fixed height = context_->getLandscapeMaps().getGroundMaps().getInterpHeight(
00115                                 position_[0], position_[1]);
00116                         if (weapon_->getGroundOnly() || height >= position_[2])
00117                         {
00118                                 // Set the position on the ground
00119                                 position_[2] = height;
00120 
00121                                 if (context_->getOptionsGame().getActionSyncCheck())
00122                                 {
00123                                         context_->getActionController().addSyncCheck(
00124                                                 S3D::formatStringBuffer("Telport: %u %i, %i, %i", 
00125                                                         tank->getPlayerId(),
00126                                                         position_[0].getInternal(),
00127                                                         position_[1].getInternal(),
00128                                                         position_[2].getInternal()));
00129                                 }
00130 
00131                                 // Set this position and flatten the landscape
00132                                 tank->getLife().setTargetPosition(position_);
00133                                 DeformLandscape::flattenArea(*context_, position_);
00134 #ifndef S3D_SERVER
00135                                 if (!context_->getServerMode())
00136                                 {
00137                                         VisibilityPatchGrid::instance()->recalculateErrors(position_, 2);
00138                                 }
00139 #endif
00140                         }
00141                         else
00142                         {
00143                                 if (context_->getOptionsGame().getActionSyncCheck())
00144                                 {
00145                                         context_->getActionController().addSyncCheck(
00146                                                 S3D::formatStringBuffer("Telport: %u %i, %i, %i", 
00147                                                         tank->getPlayerId(),
00148                                                         position_[0].getInternal(),
00149                                                         position_[1].getInternal(),
00150                                                         position_[2].getInternal()));
00151                                 }
00152 
00153                                 // Set the position, what ever this is
00154                                 tank->getLife().setTargetPosition(position_);
00155 
00156                                 // Check if this tank can fall, this will result in flattening the area
00157                                 TargetDamageCalc::damageTarget(*context_, tank, weapon_, 
00158                                         weaponContext_, 0, false, true, false);
00159                         }
00160                 }
00161 
00162                 remove = true;
00163         }
00164 
00165         Action::simulate(frameTime, remove);
00166 }
00167 
00168 std::string Teleport::getActionDetails()
00169 {
00170         return S3D::formatStringBuffer("%u %i,%i,%i %s", 
00171                 weaponContext_.getPlayerId(), 
00172                 position_[0].getInternal(),
00173                 position_[1].getInternal(),
00174                 position_[2].getInternal(),
00175                 weapon_->getParent()->getName());
00176 }

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3