2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "precompiled.h"
|
2006-06-09 20:32:00 +02:00
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "Aura.h"
|
2006-06-09 20:32:00 +02:00
|
|
|
|
2006-04-24 01:14:18 +02:00
|
|
|
#include "EntityManager.h"
|
2006-06-09 20:32:00 +02:00
|
|
|
#include "Entity.h"
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2009-07-14 17:11:05 +02:00
|
|
|
#include "scripting/ScriptableComplex.inl"
|
|
|
|
|
2006-06-09 20:32:00 +02:00
|
|
|
#include <algorithm>
|
2006-04-24 01:14:18 +02:00
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
CAura::CAura( JSContext* cx, CEntity* source, CStrW& name, float radius, int tickRate, const CVector4D& color, JSObject* handler )
|
2006-05-16 06:41:37 +02:00
|
|
|
: m_cx(cx), m_source(source), m_name(name), m_radius(radius), m_handler(handler),
|
2006-08-25 08:04:33 +02:00
|
|
|
m_tickRate(tickRate), m_tickCyclePos(0), m_color(color)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
JS_AddRoot( m_cx, &m_handler ); // don't GC it so we can call it later
|
|
|
|
}
|
|
|
|
|
|
|
|
CAura::~CAura()
|
|
|
|
{
|
|
|
|
JS_RemoveRoot( m_cx, &m_handler );
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
void CAura::Update( int timestep )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-09 20:32:00 +02:00
|
|
|
std::vector<CEntity*> inRange;
|
2006-04-24 01:14:18 +02:00
|
|
|
CVector3D pos = m_source->m_position;
|
|
|
|
g_EntityManager.GetInRange( pos.X, pos.Z, m_radius, inRange );
|
|
|
|
|
2006-06-09 20:32:00 +02:00
|
|
|
std::vector<CEntity*> prevInfluenced, curInfluenced, entered, exited;
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-01-21 04:33:52 +01:00
|
|
|
prevInfluenced.reserve(m_influenced.size());
|
|
|
|
curInfluenced.reserve(m_influenced.size());
|
|
|
|
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<HEntity>::iterator it = m_influenced.begin(); it != m_influenced.end(); it++ )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
CEntity* ent = *it;
|
2008-02-04 09:24:17 +01:00
|
|
|
if( ent && ent->m_extant )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
prevInfluenced.push_back(ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_influenced.clear();
|
|
|
|
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<CEntity*>::iterator it = inRange.begin(); it != inRange.end(); it++ )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
CEntity* ent = *it;
|
|
|
|
if(ent != m_source)
|
|
|
|
{
|
|
|
|
curInfluenced.push_back(ent);
|
|
|
|
m_influenced.push_back( HEntity(ent->me) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort( prevInfluenced.begin(), prevInfluenced.end() );
|
|
|
|
sort( curInfluenced.begin(), curInfluenced.end() );
|
|
|
|
|
|
|
|
jsval rval;
|
|
|
|
jsval argv[1];
|
|
|
|
|
2006-05-16 06:41:37 +02:00
|
|
|
// Call onEnter on any new unit that has entered the aura
|
2006-04-24 01:14:18 +02:00
|
|
|
jsval enterFunction;
|
2007-01-24 21:17:28 +01:00
|
|
|
if( JS_GetProperty( m_cx, m_handler, "onEnter", &enterFunction )
|
2006-05-16 06:41:37 +02:00
|
|
|
&& enterFunction != JSVAL_VOID)
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-09 20:32:00 +02:00
|
|
|
std::back_insert_iterator<std::vector<CEntity*> > ins( entered );
|
2006-04-24 01:14:18 +02:00
|
|
|
set_difference( curInfluenced.begin(), curInfluenced.end(),
|
|
|
|
prevInfluenced.begin(), prevInfluenced.end(),
|
|
|
|
ins );
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<CEntity*>::iterator it = entered.begin(); it != entered.end(); it++ )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
argv[0] = OBJECT_TO_JSVAL( (*it)->GetScript() );
|
|
|
|
JS_CallFunctionValue( m_cx, m_handler, enterFunction, 1, argv, &rval );
|
|
|
|
(*it)->m_aurasInfluencingMe.insert( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-16 06:41:37 +02:00
|
|
|
// Call onExit on any unit that has exited the aura
|
2006-04-24 01:14:18 +02:00
|
|
|
jsval exitFunction;
|
2007-01-24 21:17:28 +01:00
|
|
|
if( JS_GetProperty( m_cx, m_handler, "onExit", &exitFunction )
|
2006-05-16 06:41:37 +02:00
|
|
|
&& exitFunction != JSVAL_VOID )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-06-09 20:32:00 +02:00
|
|
|
std::back_insert_iterator<std::vector<CEntity*> > ins( exited );
|
2006-04-24 01:14:18 +02:00
|
|
|
set_difference( prevInfluenced.begin(), prevInfluenced.end(),
|
|
|
|
curInfluenced.begin(), curInfluenced.end(),
|
|
|
|
ins );
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<CEntity*>::iterator it = exited.begin(); it != exited.end(); it++ )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
argv[0] = OBJECT_TO_JSVAL( (*it)->GetScript() );
|
|
|
|
JS_CallFunctionValue( m_cx, m_handler, exitFunction, 1, argv, &rval );
|
|
|
|
(*it)->m_aurasInfluencingMe.erase( this );
|
|
|
|
}
|
|
|
|
}
|
2006-05-16 06:41:37 +02:00
|
|
|
|
|
|
|
m_tickCyclePos += timestep;
|
|
|
|
|
|
|
|
if( m_tickRate > 0 && m_tickCyclePos > m_tickRate )
|
|
|
|
{
|
|
|
|
// It's time to tick; call OnTick on any unit that is in the aura
|
|
|
|
jsval tickFunction;
|
2007-01-24 21:17:28 +01:00
|
|
|
if( JS_GetProperty( m_cx, m_handler, "onTick", &tickFunction )
|
2006-05-16 06:41:37 +02:00
|
|
|
&& tickFunction != JSVAL_VOID )
|
|
|
|
{
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<CEntity*>::iterator it = curInfluenced.begin(); it != curInfluenced.end(); it++ )
|
2006-05-16 06:41:37 +02:00
|
|
|
{
|
|
|
|
argv[0] = OBJECT_TO_JSVAL( (*it)->GetScript() );
|
|
|
|
JS_CallFunctionValue( m_cx, m_handler, tickFunction, 1, argv, &rval );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset cycle pos
|
|
|
|
m_tickCyclePos %= m_tickRate;
|
|
|
|
}
|
2006-04-24 01:14:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAura::RemoveAll()
|
|
|
|
{
|
|
|
|
jsval rval;
|
|
|
|
jsval argv[1];
|
|
|
|
jsval exitFunction;
|
2007-01-24 21:17:28 +01:00
|
|
|
if( JS_GetProperty( m_cx, m_handler, "onExit", &exitFunction )
|
2006-05-16 06:41:37 +02:00
|
|
|
&& exitFunction != JSVAL_VOID )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-05-16 06:41:37 +02:00
|
|
|
// Call the exit function on everything in our influence
|
2006-06-09 20:32:00 +02:00
|
|
|
for( std::vector<HEntity>::iterator it = m_influenced.begin(); it != m_influenced.end(); it++ )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
CEntity* ent = *it;
|
2008-02-04 09:24:17 +01:00
|
|
|
if( ent && ent->m_extant )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
|
|
|
argv[0] = OBJECT_TO_JSVAL( ent->GetScript() );
|
|
|
|
JS_CallFunctionValue( m_cx, m_handler, exitFunction, 1, argv, &rval );
|
|
|
|
(*it)->m_aurasInfluencingMe.erase( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_influenced.clear();
|
|
|
|
}
|
|
|
|
|
2007-01-25 08:00:31 +01:00
|
|
|
// Remove an entity from the aura, but does not remove the aura from its
|
|
|
|
// m_aurasInfluencingMe. (Used when the entity is asking to be removed from
|
|
|
|
// the aura and will clear its own m_aurasInfluencingMe list afterwards).
|
2006-04-24 01:14:18 +02:00
|
|
|
void CAura::Remove( CEntity* ent )
|
|
|
|
{
|
|
|
|
jsval rval;
|
|
|
|
jsval argv[1];
|
|
|
|
jsval exitFunction;
|
2007-01-24 21:17:28 +01:00
|
|
|
if( JS_GetProperty( m_cx, m_handler, "onExit", &exitFunction )
|
2006-05-16 06:41:37 +02:00
|
|
|
&& exitFunction != JSVAL_VOID )
|
2006-04-24 01:14:18 +02:00
|
|
|
{
|
2006-05-16 06:41:37 +02:00
|
|
|
// Call the exit function on it
|
2006-04-24 01:14:18 +02:00
|
|
|
argv[0] = OBJECT_TO_JSVAL( ent->GetScript() );
|
|
|
|
JS_CallFunctionValue( m_cx, m_handler, exitFunction, 1, argv, &rval );
|
2006-05-16 06:41:37 +02:00
|
|
|
|
|
|
|
// Remove it from the m_influenced array
|
2006-04-24 01:14:18 +02:00
|
|
|
for( size_t i=0; i < m_influenced.size(); i++ )
|
|
|
|
{
|
|
|
|
if( ((CEntity*) m_influenced[i]) == ent )
|
|
|
|
{
|
|
|
|
m_influenced.erase( m_influenced.begin() + i );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|