added some new lua methods to determine day or night and new event

This commit is contained in:
Mark Vejvoda 2013-05-25 20:46:36 +00:00
parent de68fc759d
commit aa69a5818b
7 changed files with 132 additions and 8 deletions

View File

@ -5158,12 +5158,12 @@ void Game::render2d() {
if(Renderer::renderText3DEnabled == true) {
renderer.renderText3D(
scriptManager.getDisplayText(), coreData.getMenuFontNormal3D(),
Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 680, false);
Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 660, false);
}
else {
renderer.renderText(
scriptManager.getDisplayText(), coreData.getMenuFontNormal(),
Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 680, false);
Vec3f(fontColor.x,fontColor.y,fontColor.z), 200, 660, false);
}
}

View File

@ -212,6 +212,8 @@ ScriptManager::ScriptManager() {
rootNode = NULL;
currentCellTriggeredEventAreaEntryUnitId = 0;
currentCellTriggeredEventAreaExitUnitId = 0;
lastDayNightTriggerStatus = 0;
registeredDayNightEvent = false;
}
ScriptManager::~ScriptManager() {
@ -381,6 +383,12 @@ void ScriptManager::init(World* world, GameCamera *gameCamera, const XmlNode *ro
luaScript.registerFunction(setAttackWarningsEnabled, "setAttackWarningsEnabled");
luaScript.registerFunction(getAttackWarningsEnabled, "getAttackWarningsEnabled");
luaScript.registerFunction(getIsDayTime, "getIsDayTime");
luaScript.registerFunction(getIsNightTime, "getIsNightTime");
luaScript.registerFunction(getTimeOfDay, "getTimeOfDay");
luaScript.registerFunction(registerDayNightEvent, "registerDayNightEvent");
luaScript.registerFunction(unregisterDayNightEvent, "unregisterDayNightEvent");
//load code
for(int i= 0; i<scenario->getScriptCount(); ++i){
const Script* script= scenario->getScript(i);
@ -1784,6 +1792,66 @@ bool ScriptManager::getAttackWarningsEnabled() {
return world->getAttackWarningsEnabled();
}
void ScriptManager::registerDayNightEvent() {
registeredDayNightEvent = true;
}
void ScriptManager::unregisterDayNightEvent() {
registeredDayNightEvent = false;
}
void ScriptManager::onDayNightTriggerEvent() {
if(registeredDayNightEvent == true) {
bool isDay = this->getIsDayTime();
if((lastDayNightTriggerStatus != 1 && isDay == true) ||
(lastDayNightTriggerStatus != 2 && isDay == false)) {
if(isDay == true) {
lastDayNightTriggerStatus = 1;
}
else {
lastDayNightTriggerStatus = 2;
}
printf("Triggering daynight event isDay: %d [%f]\n",isDay,getTimeOfDay());
luaScript.beginCall("dayNightTriggerEvent");
luaScript.endCall();
}
}
}
int ScriptManager::getIsDayTime() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ScriptManager_STREFLOP_Wrapper streflopWrapper;
const TimeFlow *tf= world->getTimeFlow();
if(tf == NULL) {
throw megaglest_runtime_error("tf == NULL");
}
return tf->isDay();
}
int ScriptManager::getIsNightTime() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ScriptManager_STREFLOP_Wrapper streflopWrapper;
const TimeFlow *tf= world->getTimeFlow();
if(tf == NULL) {
throw megaglest_runtime_error("tf == NULL");
}
return tf->isNight();
}
float ScriptManager::getTimeOfDay() {
if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
ScriptManager_STREFLOP_Wrapper streflopWrapper;
const TimeFlow *tf= world->getTimeFlow();
if(tf == NULL) {
throw megaglest_runtime_error("tf == NULL");
}
return tf->getTime();
}
// ========================== lua callbacks ===============================================
int ScriptManager::showMessage(LuaHandle* luaHandle){
@ -2822,6 +2890,33 @@ int ScriptManager::getAttackWarningsEnabled(LuaHandle* luaHandle) {
return luaArguments.getReturnCount();
}
int ScriptManager::getIsDayTime(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
luaArguments.returnInt(thisScriptManager->getIsDayTime());
return luaArguments.getReturnCount();
}
int ScriptManager::getIsNightTime(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
luaArguments.returnInt(thisScriptManager->getIsNightTime());
return luaArguments.getReturnCount();
}
int ScriptManager::getTimeOfDay(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
luaArguments.returnFloat(thisScriptManager->getTimeOfDay());
return luaArguments.getReturnCount();
}
int ScriptManager::registerDayNightEvent(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
thisScriptManager->registerDayNightEvent();
return luaArguments.getReturnCount();
}
int ScriptManager::unregisterDayNightEvent(LuaHandle* luaHandle) {
LuaArguments luaArguments(luaHandle);
thisScriptManager->unregisterDayNightEvent();
return luaArguments.getReturnCount();
}
void ScriptManager::saveGame(XmlNode *rootNode) {
std::map<string,string> mapTagReplacements;
XmlNode *scriptManagerNode = rootNode->addChild("ScriptManager");

View File

@ -28,7 +28,6 @@
#include "leak_dumper.h"
using std::string;
//using std::queue;
using std::list;
using Shared::Graphics::Vec2i;
using Shared::Lua::LuaScript;
@ -198,6 +197,9 @@ private:
bool inCellTriggerEvent;
std::vector<int> unRegisterCellTriggerEventList;
bool registeredDayNightEvent;
int lastDayNightTriggerStatus;
RandomGen random;
const XmlNode *rootNode;
@ -228,8 +230,10 @@ public:
void onUnitAttacked(const Unit* unit);
void onUnitAttacking(const Unit* unit);
void onGameOver(bool won);
void onCellTriggerEvent(Unit *movingUnit);
void onTimerTriggerEvent();
void onDayNightTriggerEvent();
bool getGameWon() const;
bool getIsGameOver() const;
@ -381,6 +385,12 @@ private:
void setAttackWarningsEnabled(bool enabled);
bool getAttackWarningsEnabled();
int getIsDayTime();
int getIsNightTime();
float getTimeOfDay();
void registerDayNightEvent();
void unregisterDayNightEvent();
//callbacks, commands
static int networkShowMessageForFaction(LuaHandle* luaHandle);
static int networkShowMessageForTeam(LuaHandle* luaHandle);
@ -528,6 +538,12 @@ private:
static int setAttackWarningsEnabled(LuaHandle* luaHandle);
static int getAttackWarningsEnabled(LuaHandle* luaHandle);
static int getIsDayTime(LuaHandle* luaHandle);
static int getIsNightTime(LuaHandle* luaHandle);
static int getTimeOfDay(LuaHandle* luaHandle);
static int registerDayNightEvent(LuaHandle* luaHandle);
static int unregisterDayNightEvent(LuaHandle* luaHandle);
};
}}//end namespace

View File

@ -2424,7 +2424,7 @@ void Renderer::renderResourceStatus() {
glPushAttrib(GL_ENABLE_BIT);
int j= 0;
int resourceCountRendered = 0;
for(int i= 0; i < world->getTechTree()->getResourceTypeCount(); ++i) {
const ResourceType *rt = world->getTechTree()->getResourceType(i);
const Resource *r = thisFaction->getResource(rt);
@ -2475,7 +2475,11 @@ void Renderer::renderResourceStatus() {
if(isNegativeConsumableDisplayCycle == false) {
glColor3f(1.f, 1.f, 1.f);
}
renderQuad(j*100+200, metrics.getVirtualH()-30, 16, 16, rt->getImage());
const int MAX_RESOURCES_PER_ROW = 6;
int resourceRow = (resourceCountRendered > 0 ? resourceCountRendered / MAX_RESOURCES_PER_ROW : 0);
int resourceCol = resourceCountRendered % MAX_RESOURCES_PER_ROW;
//renderQuad(resourceCountRendered*100+200, metrics.getVirtualH()-30 - (30 * resourceRows), 16, 16, rt->getImage());
renderQuad(resourceCol * 100 + 200, metrics.getVirtualH()-30 - (30 * resourceRow), 16, 16, rt->getImage());
if(rt->getClass() != rcStatic) {
str+= "/" + intToStr(thisFaction->getStoreAmount(rt));
@ -2494,15 +2498,15 @@ void Renderer::renderResourceStatus() {
renderTextShadow3D(
str, CoreData::getInstance().getDisplayFontSmall3D(),
resourceFontColor,
j*100+220, metrics.getVirtualH()-30, false);
resourceCol * 100 + 220, metrics.getVirtualH()-30 - (30 * resourceRow), false);
}
else {
renderTextShadow(
str, CoreData::getInstance().getDisplayFontSmall(),
resourceFontColor,
j*100+220, metrics.getVirtualH()-30, false);
resourceCol * 100 + 220, metrics.getVirtualH()-30 - (30 * resourceRow), false);
}
++j;
++resourceCountRendered;
}
}

View File

@ -754,6 +754,7 @@ void World::update() {
//time
timeFlow.update();
scriptManager->onDayNightTriggerEvent();
if(showPerfStats) {
sprintf(perfBuf,"In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chronoPerf.getMillis());

View File

@ -100,6 +100,7 @@ public:
int getReturnCount() const {return returnCount;}
void returnInt(int value);
void returnFloat(float value);
void returnString(const string &value);
void returnVec2i(const Vec2i &value);
void returnVec4i(const Vec4i &value);

View File

@ -930,6 +930,13 @@ void LuaArguments::returnInt(int value){
lua_pushinteger(luaState, value);
}
void LuaArguments::returnFloat(float value){
Lua_STREFLOP_Wrapper streflopWrapper;
++returnCount;
lua_pushnumber(luaState, value);
}
void LuaArguments::returnString(const string &value){
Lua_STREFLOP_Wrapper streflopWrapper;