- some bugfixes for new LUA features

This commit is contained in:
Mark Vejvoda 2010-08-29 06:30:41 +00:00
parent 5ae0430928
commit f16bb3f28b
6 changed files with 132 additions and 45 deletions

View File

@ -105,6 +105,7 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){
luaScript.registerFunction(getCellTriggerEventCount, "getCellTriggerEventCount");
luaScript.registerFunction(unregisterCellTriggerEvent, "unregisterCellTriggerEvent");
luaScript.registerFunction(startTimerEvent, "startTimerEvent");
luaScript.registerFunction(resetTimerEvent, "resetTimerEvent");
luaScript.registerFunction(stopTimerEvent, "stopTimerEvent");
luaScript.registerFunction(getTimerEventSecondsElapsed, "timerEventSecondsElapsed");
luaScript.registerFunction(getCellTriggeredEventId, "triggeredCellEventId");
@ -203,6 +204,7 @@ void ScriptManager::onTimerTriggerEvent() {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
currentTimerTriggeredEventId = iterMap->first;
luaScript.beginCall("timerTriggerEvent");
luaScript.endCall();
}
@ -214,17 +216,22 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) {
if(CellTriggerEventList.size() <= 0) {
return;
}
else {
// remove any delayed removals
unregisterCellTriggerEvent(-1);
}
inCellTriggerEvent = true;
if(movingUnit != NULL) {
for(std::map<int,CellTriggerEvent>::iterator iterMap = CellTriggerEventList.begin();
iterMap != CellTriggerEventList.end(); iterMap++) {
bool triggerEvent = false;
CellTriggerEvent &event = iterMap->second;
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s\n",
__FILE__,__FUNCTION__,__LINE__,movingUnit->getId(),event.type,movingUnit->getPos().getString().c_str(), event.sourceId,event.destId,event.destPos.getString().c_str());
bool triggerEvent = false;
switch(event.type) {
case ctet_Unit:
{
@ -233,13 +240,17 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) {
if(movingUnit->getId() == event.sourceId) {
bool srcInDst = world->getMap()->isInUnitTypeCells(destUnit->getType(), destUnit->getPos(),movingUnit->getPos());
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n",
__FILE__,__FUNCTION__,__LINE__,movingUnit->getId(),event.type,movingUnit->getPos().getString().c_str(),event.sourceId,event.destId,event.destPos.getString().c_str(),destUnit->getPos().getString().c_str(),srcInDst);
__FILE__,__FUNCTION__,__LINE__,movingUnit->getId(), event.type,movingUnit->getPos().getString().c_str(),event.sourceId,event.destId, event.destPos.getString().c_str(), destUnit->getPos().getString().c_str(),srcInDst);
if(srcInDst == true) {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
triggerEvent = true;
}
else {
srcInDst = world->getMap()->isNextToUnitTypeCells(destUnit->getType(), destUnit->getPos(),movingUnit->getPos());
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n",
__FILE__,__FUNCTION__,__LINE__,movingUnit->getId(), event.type,movingUnit->getPos().getString().c_str(),event.sourceId,event.destId, event.destPos.getString().c_str(), destUnit->getPos().getString().c_str(),srcInDst);
}
triggerEvent = srcInDst;
}
}
}
@ -253,9 +264,8 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) {
if(srcInDst == true) {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
triggerEvent = true;
}
triggerEvent = srcInDst;
}
}
break;
@ -271,20 +281,25 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) {
if(srcInDst == true) {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
triggerEvent = true;
}
else {
srcInDst = world->getMap()->isNextToUnitTypeCells(destUnit->getType(), destUnit->getPos(),movingUnit->getPos());
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n",
__FILE__,__FUNCTION__,__LINE__,movingUnit->getId(),event.type,movingUnit->getPos().getString().c_str(),event.sourceId,event.destId,event.destPos.getString().c_str(),destUnit->getPos().getString().c_str(),srcInDst);
}
triggerEvent = srcInDst;
}
}
break;
case ctet_FactionPos:
{
if(movingUnit->getFactionIndex() == event.sourceId &&
world->getMap()->isInUnitTypeCells(0, event.destPos,movingUnit->getPos())) {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
triggerEvent = true;
if(movingUnit->getFactionIndex() == event.sourceId) {
bool srcInDst = world->getMap()->isInUnitTypeCells(0, event.destPos,movingUnit->getPos());
if(srcInDst == true) {
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
triggerEvent = srcInDst;
}
}
break;
@ -296,11 +311,14 @@ void ScriptManager::onCellTriggerEvent(Unit *movingUnit) {
currentCellTriggeredEventId = iterMap->first;
event.triggerCount++;
luaScript.beginCall("cellTriggerEvent");
luaScript.endCall();
}
}
}
inCellTriggerEvent = false;
}
// ========================== lua wrappers ===============================================
@ -507,7 +525,24 @@ int ScriptManager::getCellTriggerEventCount(int eventId) {
void ScriptManager::unregisterCellTriggerEvent(int eventId) {
if(CellTriggerEventList.find(eventId) != CellTriggerEventList.end()) {
CellTriggerEventList.erase(eventId);
if(inCellTriggerEvent == false) {
CellTriggerEventList.erase(eventId);
}
else {
unRegisterCellTriggerEventList.push_back(eventId);
}
}
if(inCellTriggerEvent == false) {
if(unRegisterCellTriggerEventList.size() > 0) {
for(int i = 0; i < unRegisterCellTriggerEventList.size(); ++i) {
int delayedEventId = unRegisterCellTriggerEventList[i];
if(CellTriggerEventList.find(delayedEventId) != CellTriggerEventList.end()) {
CellTriggerEventList.erase(delayedEventId);
}
}
unRegisterCellTriggerEventList.clear();
}
}
}
@ -525,6 +560,21 @@ int ScriptManager::startTimerEvent() {
return eventId;
}
int ScriptManager::resetTimerEvent(int eventId) {
int result = 0;
if(TimerTriggerEventList.find(eventId) != TimerTriggerEventList.end()) {
TimerTriggerEvent &trigger = TimerTriggerEventList[eventId];
result = getTimerEventSecondsElapsed(eventId);
trigger.startTime = time(NULL);
trigger.endTime = 0;
trigger.running = true;
SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld, result = %d\n",__FILE__,__FUNCTION__,__LINE__,TimerTriggerEventList.size(),eventId,(long long int)trigger.startTime,(long long int)trigger.endTime,result);
}
return result;
}
int ScriptManager::stopTimerEvent(int eventId) {
int result = 0;
if(TimerTriggerEventList.find(eventId) != TimerTriggerEventList.end()) {
@ -828,6 +878,13 @@ int ScriptManager::stopTimerEvent(LuaHandle* luaHandle) {
return luaArguments.getReturnCount();
}
int ScriptManager::resetTimerEvent(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
int result = thisScriptManager->resetTimerEvent(luaArguments.getInt(-1));
luaArguments.returnInt(result);
return luaArguments.getReturnCount();
}
int ScriptManager::getTimerEventSecondsElapsed(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
int result = thisScriptManager->getTimerEventSecondsElapsed(luaArguments.getInt(-1));

View File

@ -136,6 +136,8 @@ private:
int currentEventId;
std::map<int,CellTriggerEvent> CellTriggerEventList;
std::map<int,TimerTriggerEvent> TimerTriggerEventList;
bool inCellTriggerEvent;
std::vector<int> unRegisterCellTriggerEventList;
private:
static ScriptManager* thisScriptManager;
@ -191,6 +193,7 @@ private:
int getCellTriggerEventCount(int eventId);
void unregisterCellTriggerEvent(int eventId);
int startTimerEvent();
int resetTimerEvent(int eventId);
int stopTimerEvent(int eventId);
int getTimerEventSecondsElapsed(int eventId);
int getCellTriggeredEventId();
@ -249,6 +252,7 @@ private:
static int getCellTriggerEventCount(LuaHandle* luaHandle);
static int unregisterCellTriggerEvent(LuaHandle* luaHandle);
static int startTimerEvent(LuaHandle* luaHandle);
static int resetTimerEvent(LuaHandle* luaHandle);
static int stopTimerEvent(LuaHandle* luaHandle);
static int getTimerEventSecondsElapsed(LuaHandle* luaHandle);

View File

@ -439,9 +439,56 @@ bool Map::aproxCanMove(const Unit *unit, const Vec2i &pos1, const Vec2i &pos2) c
}
}
// is testPos in the cells of unitType where unitType's position is pos
bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &testPos) {
Vec2i Map::findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos,
const UnitType *ut) {
Vec2i pos = originalBuildPos;
float bestRange = -1;
Vec2i start = pos - Vec2i(1);
Vec2i end = pos + Vec2i(ut->getSize());
for(int i = start.x; i <= end.x; ++i) {
for(int j = start.y; j <= end.y; ++j){
Vec2i testPos(i,j);
if(isInUnitTypeCells(ut, originalBuildPos,testPos) == false) {
float distance = unitBuilderPos.dist(testPos);
if(bestRange < 0 || bestRange > distance) {
bestRange = distance;
pos = testPos;
}
}
}
}
return pos;
}
bool Map::isNextToUnitTypeCells(const UnitType *ut, const Vec2i &pos,
const Vec2i &testPos) {
bool isInsideDestUnitCells = isInUnitTypeCells(ut, pos,testPos);
if(isInsideDestUnitCells == false) {
Cell *testCell = getCell(testPos);
for(int i=-1; i <= ut->getSize(); ++i){
for(int j = -1; j <= ut->getSize(); ++j) {
Vec2i currPos = pos + Vec2i(i, j);
if(isInside(currPos) == true) {
//Cell *unitCell = getCell(currPos);
//if(unitCell == testCell) {
if(isNextTo(testPos,currPos) == true) {
return true;
}
}
}
}
}
return false;
}
// is testPos in the cells of unitType where unitType's position is pos
bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,
const Vec2i &testPos) {
assert(ut!=NULL);
Cell *testCell = getCell(testPos);
@ -449,9 +496,7 @@ bool Map::isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &te
for(int j = 0; j < ut->getSize(); ++j) {
Vec2i currPos = pos + Vec2i(i, j);
if(isInside(currPos) == true) {
//if(ut->hasCellMap() == false || ut->getCellMapCell(i, j, facing)) {
Cell *unitCell = getCell(currPos);
if(unitCell == testCell) {
return true;
}

View File

@ -215,6 +215,11 @@ public:
void putUnitCells(Unit *unit, const Vec2i &pos);
void clearUnitCells(Unit *unit, const Vec2i &pos);
bool isInUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &testPos);
bool isNextToUnitTypeCells(const UnitType *ut, const Vec2i &pos,const Vec2i &testPos);
Vec2i findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos,
const UnitType *ut);
//misc
bool isNextTo(const Vec2i &pos, const Unit *unit) const;

View File

@ -385,30 +385,6 @@ void UnitUpdater::updateAttackStopped(Unit *unit){
// ==================== updateBuild ====================
Vec2i UnitUpdater::findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos, const UnitType *ut) {
Vec2i pos = originalBuildPos;
float bestRange = -1;
Vec2i start = pos - Vec2i(1);
Vec2i end = pos + Vec2i(ut->getSize());
for(int i = start.x; i <= end.x; ++i) {
for(int j = start.y; j <= end.y; ++j){
Vec2i testPos(i,j);
if(map->isInUnitTypeCells(ut, originalBuildPos,testPos) == false) {
float distance = unitBuilderPos.dist(testPos);
if(bestRange < 0 || bestRange > distance) {
bestRange = distance;
pos = testPos;
}
}
}
}
return pos;
}
void UnitUpdater::updateBuild(Unit *unit){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -429,7 +405,7 @@ void UnitUpdater::updateBuild(Unit *unit){
case pfBasic:
{
//Vec2i buildPos = (command->getPos()-Vec2i(1));
Vec2i buildPos = findBestBuildApproach(unit->getPos(), command->getPos(), ut);
Vec2i buildPos = map->findBestBuildApproach(unit->getPos(), command->getPos(), ut);
//Vec2i buildPos = (command->getPos() + Vec2i(ut->getSize() / 2));
tsValue = pathFinder->findPath(unit, buildPos);
}

View File

@ -118,7 +118,7 @@ private:
Unit * findPeerUnitBuilder(Unit *unit);
void SwapActiveCommand(Unit *unitSrc, Unit *unitDest);
Vec2i findBestBuildApproach(Vec2i unitBuilderPos, Vec2i originalBuildPos, const UnitType *ut);
};
// =====================================================