#include "stdafx.h" #include "CLua.h" #include "CLuaHandler.h" #include "hch/CHeroHandler.h" #include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "lobject.h" #include "lgc.h" #include "lapi.h" #include "CGameInfo.h" #include "CGameState.h" #include #include "hch/CObjectHandler.h" #include "CCallback.h" #include "hch/CGeneralTextHandler.h" #include #include "CPlayerInterface.h" #pragma warning (disable : 4311) bool getGlobalFunc(lua_State * L, std::string fname) { unsigned int hash = lua_calchash(fname.c_str(), fname.size()); lua_pushhstring(L, hash, fname.c_str(), fname.size()); lua_gettable(L, LUA_GLOBALSINDEX); return lua_isfunction(L, -1); } CObjectScript::CObjectScript() { language = ESLan::UNDEF; //std::cout << "Tworze obiekt objectscript "<ID)); lua_pushinteger(is, (int)os); if (lua_pcall (is, 1, 0, 0)) { lua_settop(is, 0); throw new std::exception(("Failed to call "+genFN("newObject",os->ID)+" function in lua script.").c_str()); } lua_settop(is, 0); return; } void CLuaObjectScript::onHeroVisit(CGObjectInstance *os, int heroID) { findF(genFN("heroVisit",os->ID)); lua_pushinteger(is, (int)os); lua_pushinteger(is, heroID); if (lua_pcall (is, 2, 0, 0)) { lua_settop(is, 0); throw new std::exception(("Failed to call "+genFN("heroVisit",os->ID)+" function in lua script.").c_str()); } lua_settop(is, 0); } std::string CLuaObjectScript::hoverText(CGObjectInstance *os) { findF(genFN("hoverText",os->ID)); lua_pushinteger(is, (int)os); if (lua_pcall (is, 1, 1, 0)) { lua_settop(is, 0); throw new std::exception(("Failed to call "+genFN("hoverText",os->ID)+" function in lua script.").c_str()); } std::string ret = lua_tostring(is,1); lua_settop(is, 0); return ret; } std::string CCPPObjectScript::hoverText(CGObjectInstance *os) { return CGI->objh->objects[os->defInfo->id].name; } void CVisitableOPH::newObject(CGObjectInstance *os) { visitors.insert (std::pair >(os,std::set())); }; void CVisitableOPH::onHeroVisit(CGObjectInstance *os, int heroID) { if (visitors.find(os)!=visitors.end()) { if(visitors[os].find(heroID)==visitors[os].end()) { onNAHeroVisit(os,heroID, false); visitors[os].insert(heroID); } else { onNAHeroVisit(os,heroID, true); } } else { throw new std::exception("Skrypt nie zainicjalizowal instancji tego obiektu. :("); } }; void CVisitableOPH::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited) { int w=0, ot=0; switch(os->ID) { case 51: w=0; ot=80; break; case 23: w=1; ot=39; break; case 61: w=2; ot=100; break; case 32: w=3; ot=59; break; } if (!alreadyVisited) { switch (os->ID) { case 51: case 23: case 61: case 32: { cb->changePrimSkill(heroID,w,1); std::vector weko; weko.push_back(new SComponent(SComponent::primskill,w,1)); cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&weko); //TODO: maybe we have memory leak with these windows //for (int ii=0; iishowInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[ot],&std::vector()); } } std::vector CVisitableOPH::yourObjects() { std::vector ret(4); ret.push_back(51); ret.push_back(23); ret.push_back(61); ret.push_back(32); return ret; } std::string CVisitableOPH::hoverText(CGObjectInstance *os) { std::string add; int pom; switch(os->ID) { case 51: pom = 8; break; case 23: pom = 7; break; case 61: pom = 11; break; case 32: pom = 4; break; default: throw new std::exception("Unsupported ID in CVisitableOPH::hoverText"); } add = " " + CGI->objh->xtrainfo[pom] + " "; int heroID = cb->getSelectedHero(); if (heroID>=0) { add += ( (visitors[os].find(heroID) == visitors[os].end()) ? (CGI->generaltexth->allTexts[353]) //not visited : ( CGI->generaltexth->allTexts[352]) ); //visited } return CGI->objh->objects[os->defInfo->id].name + add; } void CVisitableOPW::onNAHeroVisit(CGObjectInstance *os, int heroID, bool alreadyVisited) { int mid; switch (os->ID) { case 55: mid = 92; break; case 112: mid = 170; break; case 109: mid = 164; break; } if (alreadyVisited) { if (os->ID!=112) mid++; else mid--; cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&std::vector()); //TODO: maybe we have memory leak with these windows } else { int type, sub, val; type = SComponent::resource; switch (os->ID) { case 55: if (rand()%2) { sub = 5; val = 5; } else { sub = 6; val = 500; } break; case 112: mid = 170; sub = rand() % 6; val = (rand() % 4) + 3; break; case 109: mid = 164; sub = 6; if(cb->getDate(2)<2) val = 500; else val = 1000; } SComponent * com = new SComponent((SComponent::Etype)type,sub,val); std::vector weko; weko.push_back(com); cb->giveResource(cb->getHeroOwner(heroID),sub,val); cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->advobtxt[mid],&weko); visited[os] = true; } } void CVisitableOPW::newTurn () { if (cb->getDate(1)==1) { for (std::map::iterator i = visited.begin(); i != visited.end(); i++) { (*i).second = false; } } } void CVisitableOPW::newObject(CGObjectInstance *os) { visited.insert(std::pair(os,false)); } void CVisitableOPW::onHeroVisit(CGObjectInstance *os, int heroID) { if(visited[os]) onNAHeroVisit(os,heroID,true); else onNAHeroVisit(os,heroID,false); } std::vector CVisitableOPW::yourObjects() //returns IDs of objects which are handled by script { std::vector ret(3); ret.push_back(55); //mystical garden ret.push_back(112); //windmill ret.push_back(109); //water wheel return ret; } std::string CVisitableOPW::hoverText(CGObjectInstance *os) { return CGI->objh->objects[os->defInfo->id].name + " " + ( (visited[os]) ? (CGI->generaltexth->allTexts[352]) : (CGI->generaltexth->allTexts[353])) ; } void CMines::newObject(CGObjectInstance *os) { ourObjs.push_back(os); os->tempOwner = NEUTRAL_PLAYER; } void CMines::onHeroVisit(CGObjectInstance *os, int heroID) { int vv = 1; if (os->subID==0 || os->subID==2) vv++; else if (os->subID==6) vv = 1000; if (os->tempOwner == cb->getHeroOwner(heroID)) { //TODO: garrison } else { if (os->subID==7) return; //TODO: support for abandoned mine os->tempOwner = cb->getHeroOwner(heroID); SComponent * com = new SComponent(SComponent::Etype::resource,os->subID,vv); com->subtitle+=CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2); std::vector weko; weko.push_back(com); cb->showInfoDialog(cb->getHeroOwner(heroID),CGI->objh->mines[os->subID].second,&weko); } } std::vector CMines::yourObjects() { std::vector ret(1); ret.push_back(53); return ret; } std::string CMines::hoverText(CGObjectInstance *os) { if (os->tempOwner == NEUTRAL_PLAYER) return CGI->objh->mines[os->subID].first; else return CGI->objh->mines[os->subID].first + " " + CGI->generaltexth->arraytxt[23+os->tempOwner]; } void CMines::newTurn () { for (int i=0;itempOwner == NEUTRAL_PLAYER) continue; int vv = 1; if (ourObjs[i]->subID==0 || ourObjs[i]->subID==2) vv++; else if (ourObjs[i]->subID==6) vv = 1000; cb->giveResource(ourObjs[i]->tempOwner,ourObjs[i]->subID,vv); } } void CPickable::newObject(CGObjectInstance *os) { os->blockVisit = true; } void CPickable::onHeroVisit(CGObjectInstance *os, int heroID) { switch(os->ID) { case 79: { int val; switch(os->subID) { case 6: val = 500 + (rand()%6)*100; break; case 0: case 2: val = 6 + (rand()%5); break; default: val = 3 + (rand()%3); break; } cb->giveResource(cb->getHeroOwner(heroID),os->subID,val); break; } } CGI->mh->removeObject(os); } std::string CPickable::hoverText(CGObjectInstance *os) { switch (os->ID) { case 79: return CGI->objh->restypes[os->subID]; break; case 5: return CGI->arth->artifacts[os->subID].name; break; default: return CGI->objh->objects[os->defInfo->id].name; break; } } std::vector CPickable::yourObjects() //returns IDs of objects which are handled by script { std::vector ret(3); ret.push_back(79); //resource ret.push_back(5); //artifact ret.push_back(101); //treasure chest / commander stone return ret; }