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/autoegg.h"
|
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
|
#include "math/geometry.h"
|
|
|
|
|
|
2015-03-29 12:21:02 +00:00
|
|
|
|
#include "object/brain.h"
|
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
|
|
|
|
#include "script/cmdtoken.h"
|
|
|
|
|
|
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.
|
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
|
CAutoEgg::CAutoEgg(CObject* object) : CAuto(object)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
m_type = OBJECT_NULL;
|
|
|
|
|
m_value = 0.0f;
|
|
|
|
|
m_string[0] = 0;
|
|
|
|
|
|
|
|
|
|
m_param = 0;
|
|
|
|
|
m_phase = AEP_NULL;
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Object's destructor.
|
|
|
|
|
|
|
|
|
|
CAutoEgg::~CAutoEgg()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Destroys the object.
|
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
|
void CAutoEgg::DeleteObject(bool all)
|
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
|
|
|
|
|
2015-06-21 09:16:09 +00:00
|
|
|
|
if ( !all )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-21 09:16:09 +00:00
|
|
|
|
CObject* alien = SearchAlien();
|
|
|
|
|
if ( alien != nullptr )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
// Probably the intended action
|
2012-09-09 12:28:19 +00:00
|
|
|
|
// Original code: ( alien->GetZoom(0) == 1.0f )
|
|
|
|
|
if ( alien->GetZoomY(0) == 1.0f )
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
alien->SetLock(false);
|
|
|
|
|
alien->SetActivity(true); // the insect is active
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-06-21 09:16:09 +00:00
|
|
|
|
CObjectManager::GetInstancePointer()->DeleteObject(alien);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the object.
|
|
|
|
|
|
|
|
|
|
void CAutoEgg::Init()
|
|
|
|
|
{
|
|
|
|
|
CObject* alien;
|
|
|
|
|
|
|
|
|
|
alien = SearchAlien();
|
|
|
|
|
if ( alien == 0 )
|
|
|
|
|
{
|
|
|
|
|
m_phase = AEP_NULL;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/5.0f;
|
|
|
|
|
m_time = 0.0f;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_phase = AEP_INCUB;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/5.0f;
|
|
|
|
|
m_time = 0.0f;
|
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
m_type = alien->GetType();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
if ( m_type == OBJECT_ANT ||
|
|
|
|
|
m_type == OBJECT_SPIDER ||
|
|
|
|
|
m_type == OBJECT_BEE )
|
|
|
|
|
{
|
|
|
|
|
alien->SetZoom(0, 0.2f);
|
|
|
|
|
}
|
|
|
|
|
if ( m_type == OBJECT_WORM )
|
|
|
|
|
{
|
|
|
|
|
alien->SetZoom(0, 0.01f); // invisible !
|
|
|
|
|
}
|
|
|
|
|
alien->SetLock(true);
|
|
|
|
|
alien->SetActivity(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-11 18:59:35 +00:00
|
|
|
|
// Getes a value.
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
bool CAutoEgg::SetType(ObjectType type)
|
|
|
|
|
{
|
|
|
|
|
m_type = type;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:59:35 +00:00
|
|
|
|
// Getes a value.
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
bool CAutoEgg::SetValue(int rank, float value)
|
|
|
|
|
{
|
|
|
|
|
if ( rank != 0 ) return false;
|
|
|
|
|
m_value = value;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 18:59:35 +00:00
|
|
|
|
// Getes the string.
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
bool CAutoEgg::SetString(char *string)
|
|
|
|
|
{
|
|
|
|
|
strcpy(m_string, string);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Start object.
|
|
|
|
|
|
|
|
|
|
void CAutoEgg::Start(int param)
|
|
|
|
|
{
|
|
|
|
|
if ( m_type == OBJECT_NULL ) return;
|
|
|
|
|
if ( m_value == 0.0f ) return;
|
|
|
|
|
|
|
|
|
|
m_phase = AEP_DELAY;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/m_value;
|
|
|
|
|
|
|
|
|
|
m_param = param;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Management of an event.
|
|
|
|
|
|
|
|
|
|
bool CAutoEgg::EventProcess(const Event &event)
|
|
|
|
|
{
|
|
|
|
|
CAuto::EventProcess(event);
|
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
if ( m_engine->GetPause() ) return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
2012-09-09 12:28:19 +00:00
|
|
|
|
if ( event.type != EVENT_FRAME ) return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
if ( m_phase == AEP_NULL ) return true;
|
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_DELAY )
|
|
|
|
|
{
|
|
|
|
|
m_progress += event.rTime*m_speed;
|
|
|
|
|
if ( m_progress < 1.0f ) return true;
|
|
|
|
|
|
2015-06-20 17:27:43 +00:00
|
|
|
|
Math::Vector pos = m_object->GetPosition(0);
|
|
|
|
|
float angle = m_object->GetAngleY(0);
|
|
|
|
|
CObject* alien = CObjectManager::GetInstancePointer()->CreateObject(pos, angle, m_type);
|
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
|
alien->SetActivity(false);
|
2015-03-29 12:21:02 +00:00
|
|
|
|
CBrain* brain = alien->GetBrain();
|
|
|
|
|
if(brain != nullptr)
|
|
|
|
|
{
|
|
|
|
|
Program* program = brain->AddProgram();
|
|
|
|
|
brain->ReadProgram(program, m_string);
|
|
|
|
|
brain->RunProgram(program);
|
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-20 17:27:43 +00:00
|
|
|
|
CObject* alien = SearchAlien();
|
|
|
|
|
if ( alien == nullptr ) return true;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
alien->SetActivity(false);
|
|
|
|
|
|
|
|
|
|
m_progress += event.rTime*m_speed;
|
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_ZOOM )
|
|
|
|
|
{
|
|
|
|
|
if ( m_type == OBJECT_ANT ||
|
|
|
|
|
m_type == OBJECT_SPIDER ||
|
|
|
|
|
m_type == OBJECT_BEE )
|
|
|
|
|
{
|
|
|
|
|
alien->SetZoom(0, 0.2f+m_progress*0.8f); // Others push
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Indicates whether the controller has completed its activity.
|
|
|
|
|
|
|
|
|
|
Error CAutoEgg::IsEnded()
|
|
|
|
|
{
|
|
|
|
|
CObject* alien;
|
2012-09-09 12:28:19 +00:00
|
|
|
|
Gfx::CPyro* pyro;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_DELAY )
|
|
|
|
|
{
|
|
|
|
|
return ERR_CONTINUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
alien = SearchAlien();
|
|
|
|
|
if ( alien == 0 ) return ERR_STOP;
|
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_INCUB )
|
|
|
|
|
{
|
|
|
|
|
if ( m_progress < 1.0f ) return ERR_CONTINUE;
|
|
|
|
|
|
|
|
|
|
m_phase = AEP_ZOOM;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/5.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_ZOOM )
|
|
|
|
|
{
|
|
|
|
|
if ( m_progress < 1.0f ) return ERR_CONTINUE;
|
|
|
|
|
|
2013-02-16 21:37:43 +00:00
|
|
|
|
pyro = new Gfx::CPyro();
|
2012-09-09 12:28:19 +00:00
|
|
|
|
pyro->Create(Gfx::PT_EGG, m_object); // exploding egg
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
alien->SetZoom(0, 1.0f); // this is a big boy now
|
|
|
|
|
|
|
|
|
|
m_phase = AEP_WAIT;
|
|
|
|
|
m_progress = 0.0f;
|
|
|
|
|
m_speed = 1.0f/3.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( m_phase == AEP_WAIT )
|
|
|
|
|
{
|
|
|
|
|
if ( m_progress < 1.0f ) return ERR_CONTINUE;
|
|
|
|
|
|
|
|
|
|
alien->SetLock(false);
|
|
|
|
|
alien->SetActivity(true); // the insect is active
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ERR_STOP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
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 CAutoEgg::GetError()
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
return ERR_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Seeking the insect that starts in the egg.
|
|
|
|
|
|
|
|
|
|
CObject* CAutoEgg::SearchAlien()
|
|
|
|
|
{
|
2015-06-21 14:22:09 +00:00
|
|
|
|
Math::Vector cPos = m_object->GetPosition(0);
|
|
|
|
|
float min = 100000.0f;
|
|
|
|
|
CObject* best = nullptr;
|
|
|
|
|
for (CObject* obj : CObjectManager::GetInstancePointer()->GetAllObjects())
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
2015-06-21 14:22:09 +00:00
|
|
|
|
if ( obj->GetTruck() != nullptr ) 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_ANT &&
|
|
|
|
|
type != OBJECT_BEE &&
|
|
|
|
|
type != OBJECT_SPIDER &&
|
|
|
|
|
type != OBJECT_WORM ) continue;
|
|
|
|
|
|
2015-06-21 14:22:09 +00:00
|
|
|
|
Math::Vector oPos = obj->GetPosition(0);
|
|
|
|
|
float dist = Math::DistanceProjected(oPos, cPos);
|
2012-06-26 20:23:05 +00:00
|
|
|
|
if ( dist < 8.0f && dist < min )
|
|
|
|
|
{
|
|
|
|
|
min = dist;
|
2015-06-21 14:22:09 +00:00
|
|
|
|
best = obj;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-21 14:22:09 +00:00
|
|
|
|
return best;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Saves all parameters of the controller.
|
|
|
|
|
|
2014-11-10 13:16:32 +00:00
|
|
|
|
bool CAutoEgg::Write(CLevelParserLine* line)
|
2012-06-26 20:23:05 +00:00
|
|
|
|
{
|
|
|
|
|
if ( m_phase == AEP_NULL ) return false;
|
2014-11-10 13:16:32 +00:00
|
|
|
|
|
|
|
|
|
line->AddParam("aExist", new CLevelParserParam(true));
|
2012-06-26 20:23:05 +00:00
|
|
|
|
CAuto::Write(line);
|
2014-11-10 13:16:32 +00:00
|
|
|
|
line->AddParam("aPhase", new CLevelParserParam(static_cast<int>(m_phase)));
|
|
|
|
|
line->AddParam("aProgress", new CLevelParserParam(m_progress));
|
|
|
|
|
line->AddParam("aSpeed", new CLevelParserParam(m_speed));
|
|
|
|
|
line->AddParam("aParamType", new CLevelParserParam(m_type));
|
|
|
|
|
line->AddParam("aParamValue1", new CLevelParserParam(m_value));
|
2014-11-10 21:45:06 +00:00
|
|
|
|
line->AddParam("aParamString", new CLevelParserParam(std::string(m_string)));
|
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 CAutoEgg::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< AutoEggPhase >(line->GetParam("aPhase")->AsInt(AEP_NULL));
|
|
|
|
|
m_progress = line->GetParam("aProgress")->AsFloat(0.0f);
|
|
|
|
|
m_speed = line->GetParam("aSpeed")->AsFloat(1.0f);
|
|
|
|
|
m_type = line->GetParam("aParamType")->AsObjectType(OBJECT_NULL);
|
|
|
|
|
m_value = line->GetParam("aParamValue1")->AsFloat(0.0f);
|
|
|
|
|
strcpy(m_string, line->GetParam("aParamString")->AsString("").c_str());
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|