2014-10-14 13:11:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
2016-02-13 13:11:30 +00:00
|
|
|
* Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
|
2015-08-22 14:40:02 +00:00
|
|
|
* http://epsitec.ch; http://colobot.info; http://github.com/colobot
|
2014-10-14 13:11:37 +00:00
|
|
|
*
|
|
|
|
* 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, see http://gnu.org/licenses
|
|
|
|
*/
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
#include "object/auto/autopowerstation.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
#include "graphics/engine/particle.h"
|
|
|
|
#include "graphics/engine/terrain.h"
|
2013-02-16 21:37:43 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
#include "math/geometry.h"
|
2013-02-16 21:37:43 +00:00
|
|
|
|
2015-06-20 18:02:40 +00:00
|
|
|
#include "object/object_manager.h"
|
2015-07-12 09:33:52 +00:00
|
|
|
#include "object/old_object.h"
|
2015-08-02 11:09:48 +00:00
|
|
|
|
2015-07-10 20:13:39 +00:00
|
|
|
#include "object/interface/carrier_object.h"
|
2015-07-10 21:47:59 +00:00
|
|
|
#include "object/interface/powered_object.h"
|
2014-12-21 16:06:55 +00:00
|
|
|
|
2015-08-13 09:47:32 +00:00
|
|
|
#include "sound/sound.h"
|
|
|
|
|
2015-08-06 11:25:24 +00:00
|
|
|
#include "ui/controls/gauge.h"
|
|
|
|
#include "ui/controls/interface.h"
|
|
|
|
#include "ui/controls/window.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Object's constructor.
|
|
|
|
|
2015-07-12 09:33:52 +00:00
|
|
|
CAutoPowerStation::CAutoPowerStation(COldObject* object) : CAuto(object)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-08-11 20:51:09 +00:00
|
|
|
assert(object->Implements(ObjectInterfaceType::PowerContainer));
|
2012-06-26 20:23:05 +00:00
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Object's destructor.
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
CAutoPowerStation::~CAutoPowerStation()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Destroys the object.
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
void CAutoPowerStation::DeleteObject(bool bAll)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
if ( m_soundChannel != -1 )
|
|
|
|
{
|
|
|
|
m_sound->Stop(m_soundChannel);
|
|
|
|
m_soundChannel = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
CAuto::DeleteObject(bAll);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the object.
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
void CAutoPowerStation::Init()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
m_time = 0.0f;
|
|
|
|
m_timeVirus = 0.0f;
|
|
|
|
m_lastUpdateTime = 0.0f;
|
2012-09-09 12:28:19 +00:00
|
|
|
m_lastParticle = 0.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
m_soundChannel = -1;
|
|
|
|
m_bLastVirus = false;
|
|
|
|
|
|
|
|
CAuto::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Management of an event.
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
bool CAutoPowerStation::EventProcess(const Event &event)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
CAuto::EventProcess(event);
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( m_engine->GetPause() ) return true;
|
|
|
|
if ( event.type != EVENT_FRAME ) return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
m_timeVirus -= event.rTime;
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( m_object->GetVirusMode() ) // contaminated by a virus?
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
if ( !m_bLastVirus )
|
|
|
|
{
|
|
|
|
m_bLastVirus = true;
|
2015-08-11 20:51:09 +00:00
|
|
|
m_energyVirus = dynamic_cast<CPowerContainerObject*>(m_object)->GetEnergyLevel();
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( m_timeVirus <= 0.0f )
|
|
|
|
{
|
|
|
|
m_timeVirus = 0.1f+Math::Rand()*0.3f;
|
|
|
|
|
2015-08-11 20:51:09 +00:00
|
|
|
dynamic_cast<CPowerContainerObject*>(m_object)->SetEnergyLevel(Math::Rand());
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( m_bLastVirus )
|
|
|
|
{
|
|
|
|
m_bLastVirus = false;
|
2015-08-11 20:51:09 +00:00
|
|
|
dynamic_cast<CPowerContainerObject*>(m_object)->SetEnergyLevel(m_energyVirus);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateInterface(event.rTime);
|
|
|
|
|
2015-08-11 20:51:09 +00:00
|
|
|
float big = dynamic_cast<CPowerContainerObject*>(m_object)->GetEnergy();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-07-12 09:01:16 +00:00
|
|
|
Gfx::TerrainRes res = m_terrain->GetResource(m_object->GetPosition());
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( res == Gfx::TR_POWER )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
big += event.rTime*0.01f; // recharges the large battery
|
|
|
|
}
|
|
|
|
|
2015-07-10 21:47:59 +00:00
|
|
|
float used = big;
|
|
|
|
float freq = 1.0f;
|
|
|
|
if (big > 0.0f)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-10 21:47:59 +00:00
|
|
|
CObject* vehicle = SearchVehicle();
|
|
|
|
if (vehicle != nullptr)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-10 21:47:59 +00:00
|
|
|
if (vehicle->Implements(ObjectInterfaceType::Powered))
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-10 21:47:59 +00:00
|
|
|
CObject* power = dynamic_cast<CPoweredObject*>(vehicle)->GetPower();
|
2015-08-11 20:51:09 +00:00
|
|
|
if ( power != nullptr && power->Implements(ObjectInterfaceType::PowerContainer) )
|
2015-07-10 21:47:59 +00:00
|
|
|
{
|
2015-08-11 20:51:09 +00:00
|
|
|
CPowerContainerObject* powerContainer = dynamic_cast<CPowerContainerObject*>(power);
|
|
|
|
if (powerContainer->IsRechargeable())
|
|
|
|
{
|
|
|
|
float energy = powerContainer->GetEnergy();
|
|
|
|
float add = event.rTime*0.2f;
|
|
|
|
if ( add > big*4.0f ) add = big*4.0f;
|
|
|
|
if ( add > 1.0f-energy ) add = 1.0f-energy;
|
|
|
|
energy += add; // Charging the battery
|
|
|
|
powerContainer->SetEnergy(energy);
|
|
|
|
if ( energy < freq ) freq = energy;
|
|
|
|
big -= add/4.0f; // discharge the large battery
|
|
|
|
}
|
2015-07-10 21:47:59 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 20:13:39 +00:00
|
|
|
if (vehicle->Implements(ObjectInterfaceType::Carrier))
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-10 21:47:59 +00:00
|
|
|
CObject* power = dynamic_cast<CCarrierObject*>(vehicle)->GetCargo();
|
2015-08-11 20:51:09 +00:00
|
|
|
if ( power != nullptr && power->Implements(ObjectInterfaceType::PowerContainer) )
|
2015-07-10 20:13:39 +00:00
|
|
|
{
|
2015-08-11 20:51:09 +00:00
|
|
|
CPowerContainerObject* powerContainer = dynamic_cast<CPowerContainerObject*>(power);
|
|
|
|
if (powerContainer->IsRechargeable())
|
|
|
|
{
|
|
|
|
float energy = powerContainer->GetEnergy();
|
|
|
|
float add = event.rTime*0.2f;
|
|
|
|
if ( add > big*4.0f ) add = big*4.0f;
|
|
|
|
if ( add > 1.0f-energy ) add = 1.0f-energy;
|
|
|
|
energy += add; // Charging the battery
|
|
|
|
powerContainer->SetEnergy(energy);
|
|
|
|
if ( energy < freq ) freq = energy;
|
|
|
|
big -= add/4.0f; // discharge the large battery
|
|
|
|
}
|
2015-07-10 20:13:39 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
used -= big; // energy used
|
|
|
|
|
|
|
|
if ( freq < 1.0f ) // charging in progress?
|
|
|
|
{
|
|
|
|
freq = 1.0f+3.0f*freq;
|
|
|
|
if ( m_soundChannel == -1 )
|
|
|
|
{
|
2015-07-12 09:01:16 +00:00
|
|
|
m_soundChannel = m_sound->Play(SOUND_STATION, m_object->GetPosition(),
|
2012-06-26 20:23:05 +00:00
|
|
|
0.3f, freq, true);
|
|
|
|
}
|
|
|
|
m_sound->Frequency(m_soundChannel, freq);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( m_soundChannel != -1 )
|
|
|
|
{
|
|
|
|
m_sound->Stop(m_soundChannel);
|
|
|
|
m_soundChannel = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( used != 0.0f &&
|
2012-09-09 12:28:19 +00:00
|
|
|
m_lastParticle+m_engine->ParticleAdapt(0.05f) <= m_time )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-09 12:28:19 +00:00
|
|
|
m_lastParticle = m_time;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-07-10 21:47:59 +00:00
|
|
|
Math::Vector pos, ppos, speed;
|
|
|
|
Math::Point dim;
|
|
|
|
|
|
|
|
Math::Matrix* mat = m_object->GetWorldMatrix(0);
|
2012-06-26 20:23:05 +00:00
|
|
|
pos = Math::Vector(-15.0f, 7.0f, 0.0f); // battery position
|
|
|
|
pos = Math::Transform(*mat, pos);
|
|
|
|
speed.x = (Math::Rand()-0.5f)*20.0f;
|
|
|
|
speed.y = (Math::Rand()-0.5f)*20.0f;
|
|
|
|
speed.z = (Math::Rand()-0.5f)*20.0f;
|
|
|
|
ppos.x = pos.x;
|
|
|
|
ppos.y = pos.y+(Math::Rand()-0.5f)*4.0f;
|
|
|
|
ppos.z = pos.z;
|
|
|
|
dim.x = 1.5f;
|
|
|
|
dim.y = 1.5f;
|
2012-09-09 12:28:19 +00:00
|
|
|
m_particle->CreateParticle(ppos, speed, dim, Gfx::PARTIBLITZ, 1.0f, 0.0f, 0.0f);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
ppos = pos;
|
|
|
|
ppos.y += 1.0f;
|
|
|
|
ppos.x += (Math::Rand()-0.5f)*3.0f;
|
|
|
|
ppos.z += (Math::Rand()-0.5f)*3.0f;
|
|
|
|
speed.x = 0.0f;
|
|
|
|
speed.z = 0.0f;
|
|
|
|
speed.y = 2.5f+Math::Rand()*5.0f;
|
|
|
|
dim.x = Math::Rand()*1.0f+0.6f;
|
|
|
|
dim.y = dim.x;
|
2012-09-09 12:28:19 +00:00
|
|
|
m_particle->CreateParticle(ppos, speed, dim, Gfx::PARTIVAPOR, 3.0f);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( big < 0.0f ) big = 0.0f;
|
|
|
|
if ( big > 1.0f ) big = 1.0f;
|
2015-08-11 20:51:09 +00:00
|
|
|
dynamic_cast<CPowerContainerObject*>(m_object)->SetEnergy(big); // Shift the large battery
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Seeking the vehicle on the station.
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
CObject* CAutoPowerStation::SearchVehicle()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-07-12 09:01:16 +00:00
|
|
|
Math::Vector sPos = m_object->GetPosition();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects())
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2015-06-21 14:22:09 +00:00
|
|
|
ObjectType type = obj->GetType();
|
2012-06-26 20:23:05 +00:00
|
|
|
if ( type != OBJECT_HUMAN &&
|
|
|
|
type != OBJECT_MOBILEfa &&
|
|
|
|
type != OBJECT_MOBILEta &&
|
|
|
|
type != OBJECT_MOBILEwa &&
|
|
|
|
type != OBJECT_MOBILEia &&
|
2017-11-16 17:43:45 +00:00
|
|
|
type != OBJECT_MOBILEfb &&
|
|
|
|
type != OBJECT_MOBILEtb &&
|
|
|
|
type != OBJECT_MOBILEwb &&
|
|
|
|
type != OBJECT_MOBILEib &&
|
2012-06-26 20:23:05 +00:00
|
|
|
type != OBJECT_MOBILEfc &&
|
|
|
|
type != OBJECT_MOBILEtc &&
|
|
|
|
type != OBJECT_MOBILEwc &&
|
|
|
|
type != OBJECT_MOBILEic &&
|
|
|
|
type != OBJECT_MOBILEfi &&
|
|
|
|
type != OBJECT_MOBILEti &&
|
|
|
|
type != OBJECT_MOBILEwi &&
|
|
|
|
type != OBJECT_MOBILEii &&
|
|
|
|
type != OBJECT_MOBILEfs &&
|
|
|
|
type != OBJECT_MOBILEts &&
|
|
|
|
type != OBJECT_MOBILEws &&
|
|
|
|
type != OBJECT_MOBILEis &&
|
|
|
|
type != OBJECT_MOBILErt &&
|
|
|
|
type != OBJECT_MOBILErc &&
|
|
|
|
type != OBJECT_MOBILErr &&
|
|
|
|
type != OBJECT_MOBILErs &&
|
|
|
|
type != OBJECT_MOBILEsa &&
|
|
|
|
type != OBJECT_MOBILEft &&
|
|
|
|
type != OBJECT_MOBILEtt &&
|
|
|
|
type != OBJECT_MOBILEwt &&
|
|
|
|
type != OBJECT_MOBILEit &&
|
|
|
|
type != OBJECT_MOBILEdr ) continue;
|
|
|
|
|
2015-07-12 09:01:16 +00:00
|
|
|
Math::Vector oPos = obj->GetPosition();
|
2015-06-21 14:22:09 +00:00
|
|
|
float dist = Math::Distance(oPos, sPos);
|
|
|
|
if ( dist <= 5.0f ) return obj;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 20:40:52 +00:00
|
|
|
return nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-17 19:55:45 +00:00
|
|
|
// Returns an error due the state of the automation.
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
Error CAutoPowerStation::GetError()
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-09 12:28:19 +00:00
|
|
|
Gfx::TerrainRes res;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( m_object->GetVirusMode() )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
return ERR_BAT_VIRUS;
|
|
|
|
}
|
|
|
|
|
2015-07-12 09:01:16 +00:00
|
|
|
res = m_terrain->GetResource(m_object->GetPosition());
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( res != Gfx::TR_POWER ) return ERR_STATION_NULL;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Crée toute l'interface lorsque l'objet est sélectionné .
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
bool CAutoPowerStation::CreateInterface(bool bSelect)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-09 12:28:19 +00:00
|
|
|
Ui::CWindow* pw;
|
2012-06-26 20:23:05 +00:00
|
|
|
Math::Point pos, ddim;
|
|
|
|
float ox, oy, sx, sy;
|
|
|
|
|
|
|
|
CAuto::CreateInterface(bSelect);
|
|
|
|
|
|
|
|
if ( !bSelect ) return true;
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
|
2015-08-17 20:40:52 +00:00
|
|
|
if ( pw == nullptr ) return false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
ox = 3.0f/640.0f;
|
|
|
|
oy = 3.0f/480.0f;
|
|
|
|
sx = 33.0f/640.0f;
|
|
|
|
sy = 33.0f/480.0f;
|
|
|
|
|
|
|
|
pos.x = ox+sx*14.5f;
|
|
|
|
pos.y = oy+sy*0;
|
|
|
|
ddim.x = 14.0f/640.0f;
|
|
|
|
ddim.y = 66.0f/480.0f;
|
|
|
|
pw->CreateGauge(pos, ddim, 0, EVENT_OBJECT_GENERGY);
|
|
|
|
|
|
|
|
pos.x = ox+sx*0.0f;
|
|
|
|
pos.y = oy+sy*0;
|
|
|
|
ddim.x = 66.0f/640.0f;
|
|
|
|
ddim.y = 66.0f/480.0f;
|
|
|
|
pw->CreateGroup(pos, ddim, 104, EVENT_OBJECT_TYPE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the state of all buttons on the interface,
|
|
|
|
// following the time that elapses ...
|
|
|
|
|
2015-07-10 10:12:18 +00:00
|
|
|
void CAutoPowerStation::UpdateInterface(float rTime)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-09 12:28:19 +00:00
|
|
|
Ui::CWindow* pw;
|
|
|
|
Ui::CGauge* pg;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
CAuto::UpdateInterface(rTime);
|
|
|
|
|
|
|
|
if ( m_time < m_lastUpdateTime+0.1f ) return;
|
|
|
|
m_lastUpdateTime = m_time;
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
if ( !m_object->GetSelect() ) return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
pw = static_cast< Ui::CWindow* >(m_interface->SearchControl(EVENT_WINDOW0));
|
2015-08-17 20:40:52 +00:00
|
|
|
if ( pw == nullptr ) return;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
pg = static_cast< Ui::CGauge* >(pw->SearchControl(EVENT_OBJECT_GENERGY));
|
2015-08-17 20:40:52 +00:00
|
|
|
if ( pg != nullptr )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-09-09 12:28:19 +00:00
|
|
|
pg->SetLevel(m_object->GetEnergy());
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
}
|