2014-10-14 13:11:37 +00:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of the Colobot: Gold Edition source code
|
|
|
|
|
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
|
|
|
|
|
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "object/auto/autonest.h"
|
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
#include "graphics/engine/terrain.h"
|
2013-02-16 21:37:43 +00:00
|
|
|
|
|
2015-06-20 18:02:40 +00:00
|
|
|
|
#include "object/object_manager.h"
|
2014-11-10 13:16:32 +00:00
|
|
|
|
#include "object/level/parserline.h"
|
|
|
|
|
#include "object/level/parserparam.h"
|
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-15 16:50:51 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Object's constructor.
|
|
|
|
|
|
2014-11-10 16:15:34 +00:00
|
|
|
|
CAutoNest::CAutoNest(CObject* object) : CAuto(object)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Object's destructor.
|
|
|
|
|
|
|
|
|
|
CAutoNest::~CAutoNest()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Destroys the object.
|
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
|
void CAutoNest::DeleteObject(bool all)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-21 09:16:09 +00:00
|
|
|
|
if ( !all )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CObject* cargo = SearchCargo();
|
|
|
|
|
if ( cargo != nullptr )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CObjectManager::GetInstancePointer()->DeleteObject(cargo);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
|
CAuto::DeleteObject(all);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the object.
|
|
|
|
|
|
|
|
|
|
void CAutoNest::Init()
|
|
|
|
|
{
|
|
|
|
|
Math::Vector pos;
|
|
|
|
|
|
|
|
|
|
m_phase = ANP_WAIT;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/4.0f;
|
|
|
|
|
|
|
|
|
|
m_time = 0.0f;
|
2012-09-09 12:28:19 +00:00
|
|
|
|
m_lastParticle = 0.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
pos = m_object->GetPosition(0);
|
2012-09-13 16:28:50 +00:00
|
|
|
|
m_terrain->AdjustToFloor(pos);
|
2015-06-29 21:05:31 +00:00
|
|
|
|
m_cargoPos = pos;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Management of an event.
|
|
|
|
|
|
|
|
|
|
bool CAutoNest::EventProcess(const Event &event)
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CObject* cargo;
|
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_progress += event.rTime*m_speed;
|
|
|
|
|
|
|
|
|
|
if ( m_phase == ANP_WAIT )
|
|
|
|
|
{
|
|
|
|
|
if ( m_progress >= 1.0f )
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
if ( !SearchFree(m_cargoPos) )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
m_phase = ANP_WAIT;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/4.0f;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CreateCargo(m_cargoPos, 0.0f, OBJECT_BULLET);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
m_phase = ANP_BIRTH;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/5.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( m_phase == ANP_BIRTH )
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
cargo = SearchCargo();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
if ( m_progress < 1.0f )
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
if ( cargo != 0 )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
cargo->SetZoom(0, m_progress);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
if ( cargo != 0 )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
cargo->SetZoom(0, 1.0f);
|
|
|
|
|
cargo->SetLock(false);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_phase = ANP_WAIT;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/5.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Seeks if a site is free.
|
|
|
|
|
|
|
|
|
|
bool CAutoNest::SearchFree(Math::Vector pos)
|
|
|
|
|
{
|
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_NEST ) continue;
|
|
|
|
|
|
2015-07-10 07:26:38 +00:00
|
|
|
|
for (const auto& crashSphere : obj->GetAllCrashSpheres())
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-07-10 07:26:38 +00:00
|
|
|
|
Math::Vector sPos = crashSphere.sphere.pos;
|
|
|
|
|
float sRadius = crashSphere.sphere.radius;
|
|
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
|
float distance = Math::Distance(sPos, pos);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
distance -= sRadius;
|
|
|
|
|
if ( distance < 2.0f ) return false; // location occupied
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true; // free location
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a transportable object.
|
|
|
|
|
|
2015-06-29 21:05:31 +00:00
|
|
|
|
void CAutoNest::CreateCargo(Math::Vector pos, float angle, ObjectType type)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CObject* cargo = CObjectManager::GetInstancePointer()->CreateObject(pos, angle, type);
|
|
|
|
|
cargo->SetLock(true); // not usable
|
|
|
|
|
cargo->SetZoom(0, 0.0f);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Looking for the ball during manufacture.
|
|
|
|
|
|
2015-06-29 21:05:31 +00:00
|
|
|
|
CObject* CAutoNest::SearchCargo()
|
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
|
|
|
|
if ( !obj->GetLock() ) continue;
|
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_BULLET ) continue;
|
|
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
|
Math::Vector oPos = obj->GetPosition(0);
|
2015-06-29 21:05:31 +00:00
|
|
|
|
if ( oPos.x == m_cargoPos.x &&
|
|
|
|
|
oPos.z == m_cargoPos.z )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-21 14:22:09 +00:00
|
|
|
|
return obj;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-21 14:22:09 +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
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
Error CAutoNest::GetError()
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
return ERR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Saves all parameters of the controller.
|
|
|
|
|
|
2014-11-10 13:16:32 +00:00
|
|
|
|
bool CAutoNest::Write(CLevelParserLine* line)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
if ( m_phase == ANP_WAIT ) return false;
|
2015-06-21 18:59:23 +00:00
|
|
|
|
|
|
|
|
|
line->AddParam("aExist", CLevelParserParamUPtr{new CLevelParserParam(true)});
|
2012-06-26 20:23:05 +00:00
|
|
|
|
CAuto::Write(line);
|
2015-06-21 18:59:23 +00:00
|
|
|
|
line->AddParam("aPhase", CLevelParserParamUPtr{new CLevelParserParam(static_cast<int>(m_phase))});
|
|
|
|
|
line->AddParam("aProgress", CLevelParserParamUPtr{new CLevelParserParam(m_progress)});
|
|
|
|
|
line->AddParam("aSpeed", CLevelParserParamUPtr{new CLevelParserParam(m_speed)});
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restores all parameters of the controller.
|
|
|
|
|
|
2014-11-10 16:15:34 +00:00
|
|
|
|
bool CAutoNest::Read(CLevelParserLine* line)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2014-11-10 16:15:34 +00:00
|
|
|
|
if ( !line->GetParam("aExist")->AsBool(false) ) return false;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
CAuto::Read(line);
|
2014-11-10 16:15:34 +00:00
|
|
|
|
m_phase = static_cast< AutoNestPhase >(line->GetParam("aPhase")->AsInt(ANP_WAIT));
|
|
|
|
|
m_progress = line->GetParam("aProgress")->AsFloat(0.0f);
|
|
|
|
|
m_speed = line->GetParam("aSpeed")->AsFloat(1.0f);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
m_lastParticle = 0.0f;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|