1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-04 17:05:47 +00:00
Aquaria/Aquaria/ScriptedEntity.cpp

818 lines
16 KiB
C++
Raw Normal View History

/*
Copyright (C) 2007, 2010 - Bit-Blot
This file is part of Aquaria.
Aquaria 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.
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ScriptedEntity.h"
#include "DSQ.h"
#include "Game.h"
#include "Avatar.h"
#include "Shot.h"
bool ScriptedEntity::runningActivation = false;
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
ScriptedEntity::ScriptedEntity(const std::string &scriptName, Vector position, EntityType et) : CollideEntity(), Segmented(2, 26)
{
addType(SCO_SCRIPTED_ENTITY);
crushDelay = 0;
script = 0;
songNoteFunction = songNoteDoneFunction = true;
addChild(&pullEmitter, PM_STATIC);
hair = 0;
becomeSolidDelay = false;
strandSpacing = 10;
animKeyFunc = true;
canShotHitFunc = true;
//runningActivation = false;
setEntityType(et);
myTimer = 0;
layer = LR_ENTITIES;
surfaceMoveDir = 1;
this->position = position;
numSegments = 0;
reverseSegments = false;
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
manaBallAmount = 1;
this->name = scriptName;
std::string file;
if (!scriptName.empty())
{
if (scriptName[0]=='@' && dsq->mod.isActive())
{
file = dsq->mod.getPath() + "scripts/" + scriptName.substr(1, scriptName.size()) + ".lua";
this->name = scriptName.substr(1, scriptName.size());
}
else if (dsq->mod.isActive())
{
file = dsq->mod.getPath() + "scripts/" + scriptName + ".lua";
if (!exists(file))
{
file = "scripts/entities/" + scriptName + ".lua";
}
}
else
{
file = "scripts/entities/" + scriptName + ".lua";
}
}
script = dsq->scriptInterface.openScript(file);
if (!script)
{
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
debugLog("Could not load script [" + file + "]");
}
}
void ScriptedEntity::setAutoSkeletalUpdate(bool v)
{
skeletalSprite.ignoreUpdate = !v;
}
void ScriptedEntity::message(const std::string &msg, int v)
{
if (script)
{
if (!script->call("msg", this, msg.c_str(), v))
debugLog(name + " : msg : " + script->getLastError());
}
Entity::message(msg, v);
}
int ScriptedEntity::messageVariadic(lua_State *L, int nparams)
{
if (script)
{
int res = script->callVariadic("msg", L, nparams, this);
if (res < 0)
luaDebugMsg("msg", script->getLastError());
else
return res;
}
return Entity::messageVariadic(L, nparams);
}
void ScriptedEntity::warpSegments()
{
Segmented::warpSegments(position);
}
void ScriptedEntity::init()
{
if (script)
{
if (!script->call("init", this))
luaDebugMsg("init", script->getLastError());
}
Entity::init();
}
void ScriptedEntity::postInit()
{
if (script)
{
if (!script->call("postInit", this))
luaDebugMsg("postInit", script->getLastError());
}
Entity::postInit();
}
void ScriptedEntity::initEmitter(int emit, const std::string &file)
{
if (emitters.size() <= emit)
{
emitters.resize(emit+1);
}
if (emitters[emit] != 0)
{
errorLog("Trying to init emitter being used");
return;
}
emitters[emit] = new ParticleEffect;
addChild(emitters[emit], PM_POINTER);
emitters[emit]->load(file);
}
void ScriptedEntity::startEmitter(int emit)
{
if(emit >= emitters.size())
return;
if (emitters[emit])
{
emitters[emit]->start();
}
}
void ScriptedEntity::stopEmitter(int emit)
{
if(emit >= emitters.size())
return;
if (emitters[emit])
{
emitters[emit]->stop();
}
}
ParticleEffect *ScriptedEntity::getEmitter(int emit)
{
return (size_t(emit) < emitters.size()) ? emitters[emit] : NULL;
}
int ScriptedEntity::getNumEmitters() const
{
return emitters.size();
}
void ScriptedEntity::registerNewPart(RenderObject *r, const std::string &name)
{
partMap[name] = r;
}
void ScriptedEntity::initSegments(int numSegments, int minDist, int maxDist, std::string bodyTex, std::string tailTex, int w, int h, float taper, bool reverseSegments)
{
this->reverseSegments = reverseSegments;
this->numSegments = numSegments;
this->minDist = minDist;
this->maxDist = maxDist;
segments.resize(numSegments);
for (int i = segments.size()-1; i >= 0 ; i--)
{
Quad *q = new Quad;
if (i == segments.size()-1)
q->setTexture(tailTex);
else
q->setTexture(bodyTex);
q->setWidthHeight(w, h);
if (i > 0 && i < segments.size()-1 && taper !=0)
q->scale = Vector(1.0f-(i*taper), 1-(i*taper));
dsq->game->addRenderObject(q, LR_ENTITIES);
segments[i] = q;
}
Segmented::initSegments(position);
}
void ScriptedEntity::setupEntity(const std::string &tex, int lcode)
{
setEntityType(ET_NEUTRAL);
if (!tex.empty())
setTexture(tex);
updateCull = -1;
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
manaBallAmount = 0;
setState(STATE_IDLE);
this->layer = dsq->getEntityLayerToLayer(lcode);
}
2013-07-20 18:17:57 +00:00
void ScriptedEntity::setupBasicEntity(const std::string& texture, int health, int manaBall, int exp, int money, float collideRadius, int state, int w, int h, int expType, bool hitEntity, int updateCull, int layer)
{
//this->updateCull = updateCull;
updateCull = -1;
if (texture.empty())
renderQuad = false;
else
setTexture(texture);
this->health = maxHealth = health;
this->collideRadius = collideRadius;
setState(state);
this->manaBallAmount = manaBall;
width = w;
height = h;
setEntityLayer(layer);
}
void ScriptedEntity::setEntityLayer(int lcode)
{
this->layer = dsq->getEntityLayerToLayer(lcode);
}
void ScriptedEntity::initStrands(int num, int segs, int dist, int strandSpacing, Vector color)
{
this->strandSpacing = strandSpacing;
strands.resize(num);
for (int i = 0; i < strands.size(); i++)
{
strands[i] = new Strand(position, segs, dist);
strands[i]->color = color;
dsq->game->addRenderObject(strands[i], this->layer);
}
updateStrands(0);
}
void ScriptedEntity::onAlwaysUpdate(float dt)
{
Entity::onAlwaysUpdate(dt);
updateStrands(dt);
if (!isEntityDead() && getState() != STATE_DEAD && getState() != STATE_DEATHSCENE && isPresent())
{
if (frozenTimer > 0)
{
pullEmitter.update(dt);
doFriction(dt, 50);
updateCurrents(dt);
updateMovement(dt);
if (hair)
{
setHairHeadPosition(position);
updateHair(dt);
}
if (skeletalSprite.isLoaded())
dsq->game->handleShotCollisionsSkeletal(this);
else
dsq->game->handleShotCollisions(this);
}
if (isPullable() && !fillGridFromQuad)
{
bool doCrush = false;
crushDelay -= dt;
if (crushDelay < 0)
{
crushDelay = 0.2;
doCrush = true;
}
FOR_ENTITIES(i)
{
Entity *e = *i;
if (e && e != this && e->life == 1 && e->ridingOnEntity != this)
{
if ((e->position - this->position).isLength2DIn(collideRadius + e->collideRadius))
{
if (this->isEntityProperty(EP_BLOCKER) && doCrush)
{
bool doit = !vel.isLength2DIn(64) || (e->position.y > position.y && vel.y > 0);
if (doit)
{
if (e->getEntityType() == ET_ENEMY && e->isDamageTarget(DT_CRUSH))
{
DamageData d;
d.damageType = DT_CRUSH;
d.attacker = this;
d.damage = 1;
if (e->damage(d))
{
e->sound("RockHit");
dsq->spawnParticleEffect("rockhit", e->position, 0, 0);
}
//e->push(vel, 0.2, 500, 0);
Vector add = vel;
add.setLength2D(5000*dt);
e->vel += add;
}
}
}
Vector add = e->position - this->position;
add.capLength2D(10000 * dt);
e->vel += add;
e->doCollisionAvoidance(dt, 3, 1);
}
}
}
}
if (isPullable())
{
Entity *followEntity = dsq->game->avatar;
if (followEntity && dsq->game->avatar->pullTarget == this)
{
Vector dist = followEntity->position - this->position;
if (dist.isLength2DIn(followEntity->collideRadius + collideRadius + 16))
{
vel = 0;
}
else if (!dist.isLength2DIn(800))
{
// break;
vel.setZero();
dsq->game->avatar->pullTarget->stopPull();
dsq->game->avatar->pullTarget = 0;
}
else if (!dist.isLength2DIn(128))
{
Vector v = dist;
int moveSpeed = 1000;
moveSpeed = 4000;
v.setLength2D(moveSpeed);
vel += v*dt;
setMaxSpeed(dsq->game->avatar->getMaxSpeed());
}
else
{
if (!vel.isZero())
{
Vector sub = vel;
sub.setLength2D(getMaxSpeed()*maxSpeedLerp.x*dt);
vel -= sub;
if (vel.isLength2DIn(100))
vel = 0;
}
}
doCollisionAvoidance(dt, 2, 0.5);
}
}
}
}
void ScriptedEntity::updateStrands(float dt)
{
if (strands.empty()) return;
float angle = rotation.z;
angle = (PI*(360-(angle-90)))/180.0;
//angle = (180*angle)/PI;
float sz = (strands.size()/2);
for (int i = 0; i < strands.size(); i++)
{
float diff = (i-sz)*strandSpacing;
if (diff < 0)
strands[i]->position = position - Vector(sinf(angle)*fabsf(diff), cosf(angle)*fabsf(diff));
else
strands[i]->position = position + Vector(sinf(angle)*diff, cosf(angle)*diff);
if (dt > 0)
strands[i]->update(dt);
}
}
void ScriptedEntity::destroy()
{
CollideEntity::destroy();
if (script)
{
dsq->scriptInterface.closeScript(script);
script = 0;
}
}
void ScriptedEntity::song(SongType songType)
{
if (script)
{
if (!script->call("song", this, int(songType)))
debugLog(name + " : " + script->getLastError());
}
}
void ScriptedEntity::shiftWorlds(WorldType lastWorld, WorldType worldType)
{
if (script)
{
if (!script->call("shiftWorlds", this, int(lastWorld), int(worldType)))
debugLog(name + " : " + script->getLastError() + " shiftWorlds");
}
}
void ScriptedEntity::startPull()
{
Entity::startPull();
beforePullMaxSpeed = getMaxSpeed();
becomeSolidDelay = false;
debugLog("HERE!");
if (isEntityProperty(EP_BLOCKER))
{
fillGridFromQuad = false;
dsq->game->reconstructEntityGrid();
}
pullEmitter.load("Pulled");
pullEmitter.start();
// HACK: move this to the lower level at some point
if (isEntityProperty(EP_BLOCKER))
{
FOR_ENTITIES(i)
{
Entity *e = *i;
if (e != this && e->getEntityType() != ET_AVATAR && e->isv(EV_CRAWLING, 1))
{
if ((e->position - position).isLength2DIn(collideRadius+e->collideRadius+32))
{
debugLog(e->name + ": is now riding on : " + name);
e->ridingOnEntity = this;
e->ridingOnEntityOffset = e->position - position;
e->ridingOnEntityOffset.setLength2D(collideRadius);
}
}
}
}
}
void ScriptedEntity::sporesDropped(const Vector &pos, int type)
{
if (script)
{
script->call("sporesDropped", this, pos.x, pos.y, type);
}
}
void ScriptedEntity::stopPull()
{
Entity::stopPull();
pullEmitter.stop();
setMaxSpeed(beforePullMaxSpeed);
}
void ScriptedEntity::onUpdate(float dt)
{
BBGE_PROF(ScriptedEntity_onUpdate);
CollideEntity::onUpdate(dt);
if (becomeSolidDelay)
{
if (vel.isLength2DIn(5))
{
if (!isEntityInside())
{
becomeSolid();
becomeSolidDelay = false;
}
}
}
if (life != 1 || isEntityDead()) return;
if (myTimer > 0)
{
myTimer -= dt;
if (myTimer <= 0)
{
myTimer = 0;
onExitTimer();
}
}
if (this->isEntityDead() || this->getState() == STATE_DEATHSCENE || this->getState() == STATE_DEAD)
{
return;
}
if (script)
{
if (!script->call("update", this, dt))
debugLog(name + " : update : " + script->getLastError());
}
if (numSegments > 0)
{
updateSegments(position, reverseSegments);
updateAlpha(alpha.x);
}
}
void ScriptedEntity::resetTimer(float t)
{
myTimer = t;
}
void ScriptedEntity::stopTimer()
{
myTimer = 0;
}
void ScriptedEntity::onExitTimer()
{
if (script)
{
if (!script->call("exitTimer", this))
debugLog(this->name + " : " + script->getLastError() + " exitTimer");
}
}
void ScriptedEntity::onAnimationKeyPassed(int key)
{
if (script && animKeyFunc)
{
if (!script->call("animationKey", this, key))
{
debugLog(this->name + " : " + script->getLastError() + " animationKey");
animKeyFunc = false;
}
}
Entity::onAnimationKeyPassed(key);
}
void ScriptedEntity::lightFlare()
{
if (script && !isEntityDead())
{
script->call("lightFlare", this);
}
}
bool ScriptedEntity::canShotHit(const DamageData &d)
{
bool doDefault = true;
if (script && canShotHitFunc)
{
if (!script->call("canShotHit", this, d.attacker, d.bone, int(d.damageType), d.damage, d.hitPos.x, d.hitPos.y, d.shot, &doDefault))
{
debugLog(name + ": canShotHit function failed");
canShotHitFunc = false;
}
}
if (doDefault)
{
return Entity::canShotHit(d);
}
return false;
}
bool ScriptedEntity::damage(const DamageData &d)
{
if (d.damageType == DT_NONE) return false;
bool doDefault = true;
if (script)
{
if (!script->call("damage", this, d.attacker, d.bone, int(d.damageType), d.damage, d.hitPos.x, d.hitPos.y, d.shot, &doDefault))
{
debugLog(name + ": damage function failed");
}
}
if (doDefault)
{
return Entity::damage(d);
}
return false;
}
void ScriptedEntity::songNote(int note)
{
Entity::songNote(note);
if (script && songNoteFunction)
{
if (!script->call("songNote", this, note))
{
songNoteFunction = false;
debugLog(this->name + " : " + script->getLastError() + " songNote");
}
}
}
void ScriptedEntity::songNoteDone(int note, float len)
{
Entity::songNoteDone(note, len);
if (script && songNoteDoneFunction)
{
if (!script->call("songNoteDone", this, note, len))
{
songNoteDoneFunction = false;
debugLog(this->name + " : " + script->getLastError() + " songNoteDone");
}
}
}
void ScriptedEntity::becomeSolid()
{
//vel = 0;
float oldRot = 0;
bool doRot=false;
Vector n = dsq->game->getWallNormal(position);
if (!n.isZero())
{
oldRot = rotation.z;
rotateToVec(n, 0);
doRot = true;
}
fillGridFromQuad = true;
dsq->game->reconstructEntityGrid();
FOR_ENTITIES(i)
{
Entity *e = *i;
if (e->ridingOnEntity == this)
{
e->ridingOnEntity = 0;
e->moveOutOfWall();
// if can't get the rider out of the wall, kill it
if (dsq->game->isObstructed(TileVector(e->position)))
{
e->setState(STATE_DEAD);
}
}
}
if (doRot)
{
rotation.z = oldRot;
rotateToVec(n, 0.01);
}
}
void ScriptedEntity::onHitWall()
{
if (isEntityProperty(EP_BLOCKER) && !fillGridFromQuad && dsq->game->avatar->pullTarget != this)
{
becomeSolidDelay = true;
}
if (isEntityProperty(EP_BLOCKER) && !fillGridFromQuad)
{
Vector n = dsq->game->getWallNormal(position);
if (!n.isZero())
{
rotateToVec(n, 0.2);
}
}
CollideEntity::onHitWall();
if (script)
{
if (!script->call("hitSurface", this))
debugLog(this->name + " : " + script->getLastError() + " hitSurface");
}
}
void ScriptedEntity::activate()
{
if (runningActivation) return;
Entity::activate();
runningActivation = true;
if (script)
{
if (!script->call("activate", this))
luaDebugMsg("activate", script->getLastError());
}
runningActivation = false;
}
void ScriptedEntity::shotHitEntity(Entity *hit, Shot *shot, Bone *bone)
{
Entity::shotHitEntity(hit, shot, bone);
if (script)
{
script->call("shotHitEntity", this, hit, shot, bone);
}
}
void ScriptedEntity::entityDied(Entity *e)
{
CollideEntity::entityDied(e);
if (script)
{
script->call("entityDied", this, e);
}
}
void ScriptedEntity::luaDebugMsg(const std::string &func, const std::string &msg)
{
debugLog("luaScriptError: " + name + " : " + func + " : " + msg);
}
void ScriptedEntity::onDieNormal()
{
Entity::onDieNormal();
if (script)
{
script->call("dieNormal", this);
}
}
void ScriptedEntity::onDieEaten()
{
Entity::onDieEaten();
if (script)
{
script->call("dieEaten", this);
}
}
void ScriptedEntity::onEnterState(int action)
{
CollideEntity::onEnterState(action);
if (script)
{
if (!script->call("enterState", this))
luaDebugMsg("enterState", script->getLastError());
}
switch(action)
{
case STATE_DEAD:
if (!isGoingToBeEaten())
{
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
doDeathEffects(manaBallAmount);
dsq->spawnParticleEffect(deathParticleEffect, position);
onDieNormal();
}
else
{
// eaten
Fixed some script interface functions, error handling & code cleanups == Script interface changes: == Removed entity_setClampOnSwitchDir(), which was a no-op. Added entity_getFlag() function, for fairness, as nodes already have one. Added node_getLabel() function, which does not return the _full_ node string, but only the first part before parameters. dofile() supports relative paths now, and it is no longer necessary to use appendUserDataPath() + full path from a mod. entity_color() does now support all interpolation functions that similar vector-manipulation functions support (loop, ping-pong, ease). added entity_getVel() function (an entity_setVel() can be emulated with entity_clearVel(), followed by entity_addVel()). *_getNearestNode() and *_getNearestEntity() allow an optional ignore parameter. node_getNearestNode() allowed to specify a node to exclude from the search, the others not -- now the interface is more consistent. Deprecated entity_[incr/decr]TargetLeaches(entity) - the attached leach amount was never handled by any entity other than Naija. Replaced them with avatar_[incr/decr]Leaches(), but kept the old ames for now, too. Should be removed in a while. == Script interface & related fixes: == Added bool loudScriptErrors to control displaying script syntax error and global variable usage warnings. So far, syntax errors always went do debugLog(), but it is less time consuming when writing scripts to have them displayed right away. *_getNearestNode now scan by label, and not full name. This allows searching for nodes but ignoring its parameters. The original game scripts never search for nodes that have parameters, so this does not break anything, but allows modding without an exploding amount of specialized node scripts. entity_setBoneLock() failed for certain entites with throwLuaErrors=true, allowing to burst through them (gears, grouper, big mithalas jelly, probably more). Removed unnecessary int-casts in entity_setHealth(), entity_changeHealth(), and a few others, which truncated float fractional parts. Fixed possible crashes in many functions due to missing NULL-pointer checks, if wrong data were passed to Lua. Optimized entity_getNearestEntity() a little - do nocasecmp() after checking all other params UPPERCASE global variables as used in mod include files are now tolerated, even with all warnings on. This makes sense because it was impossible to include a custom flags definition file (which _must_ have globals, otherwise the including scripts won't see them) in a mod without beeing buried in spam. They won't change, or clobber any states, and UPPERCASE sort of implies a constant/#define, imho. Added better control over warnings about not-existing files. map_*.lua and premap_*.lua files are optional, and no warning should be given if they do not exist. Missing Entity scripts and mod-init.lua must be warned about, however. Fixed a crash in global variable access warning (NULL pushed into std::ostringstream) Fixed 2 random warnings i found in scrips == Misc stuff: == Related to the changes above, cleaned out unused variables from the Entity class, and removed the code that had to do with them somehow. Some parts of the removed code were still in use, although totally unnecessary. Saves a few bytes of memory per entity, and less code that can cause headache.
2011-09-18 21:12:02 +00:00
doDeathEffects(0);
onDieEaten();
}
destroySegments(1);
for (int i = 0; i < strands.size(); i++)
{
strands[i]->safeKill();
}
strands.clear();
// BASE ENTITY CLASS WILL HANDLE CLEANING UP HAIR
break;
}
}
void ScriptedEntity::onExitState(int action)
{
if (script)
{
if (!script->call("exitState", this))
luaDebugMsg("exitState", script->getLastError());
}
CollideEntity::onExitState(action);
}
Script interface improvements & extensions. - Pointer typechecks are now enabled by default. - enabled all script warnings for non-FULL or DEMO builds by default - Added generic obj_* functions that operate on any type of RenderObject. These give quite low-level control about the renderer, and are quite dangerous too. Subsequently, many functions sharing the same code (*_setPosition, for example) could be removed, and simply call the generic functions after a type check. - Added interface function deathNotify(). The original logic of death notifiers was never used, so i thought i'd make use of it. This is useful in scripts to safely drop dangling pointers. - removed sendEntityMessage, entity_setCollideWithAvatar, entity_setTouchDamage, which were essentially no-ops. - Replaced all unnecessary luaReturnNum(0) and luaReturnInt(0), now it does only push a return value on the stack if the function is actually supposed to have a retun value. - Allow variadic calling of entity_msg(). Now any parameters can be passed to the function, and the target entity will receive all of them. Any values returned from the entity's msg() callback will be returned by entity_msg() to the original caller. This allows nice RPC-like entity communication. - fixed possible crash in debugLog, bone_update, entity_debugText - added an override function for loadfile() that is case-insensitive like dofile() - entity_createEntity returns the created entity now - spawnParticleEffect returns the associated RenderObject - Added some text rendering functions - Added beam_setFirer() - removed the underflow check in avatar_decrLeaches() I added earlier - added a panic function for Lua. - added the Lua debug library - fixed a stupid typo in ScriptObject::isType() that made the type checks a lot less accurate - misc stuff I forgot
2012-02-05 19:22:54 +00:00
void ScriptedEntity::deathNotify(RenderObject *r)
{
if (script)
{
if (!script->call("deathNotify", this, r))
luaDebugMsg("deathNotify", script->getLastError());
}
CollideEntity::deathNotify(r);
}