From 03287540c75bcf93c3cd4ca48caffe5fba32ce7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20W=2E=20Urba=C5=84czyk?= Date: Wed, 30 Jul 2008 21:27:15 +0000 Subject: [PATCH] Further code for objects (mines) - not finished. --- CGameInterface.h | 2 ++ CGameState.h | 1 - CHeroWindow.cpp | 16 ++++----- CLua.cpp | 40 ++++++++++----------- CPlayerInterface.cpp | 27 +++++++++++++++ CPlayerInterface.h | 5 +-- client/Client.cpp | 32 ++++++++++++++--- lib/NetPacks.h | 46 +++++++++++++++++++++++- server/CGameHandler.cpp | 71 +++++++++++++++++++------------------- server/CScriptCallback.cpp | 8 ++++- server/CScriptCallback.h | 3 +- 11 files changed, 175 insertions(+), 76 deletions(-) diff --git a/CGameInterface.h b/CGameInterface.h index c074caf50..c8a3bba4b 100644 --- a/CGameInterface.h +++ b/CGameInterface.h @@ -10,6 +10,7 @@ class CCallback; class ICallback; class CGlobalAI; class CGHeroInstance; +class Component; class CSelectableComponent; struct HeroMoveDetails; class CGHeroInstance; @@ -59,6 +60,7 @@ public: virtual void tileRevealed(int3 pos){}; virtual void tileHidden(int3 pos){}; virtual void receivedResource(int type, int val){}; + virtual void showInfoDialog(std::string text, std::vector &components)=0{}; virtual void showSelDialog(std::string text, std::vector & components, int askID)=0{}; virtual void garrisonChanged(const CGObjectInstance * obj){}; virtual void buildChanged(const CGTownInstance *town, int buildingID, int what){}; //what: 1 - built, 2 - demolished diff --git a/CGameState.h b/CGameState.h index 68f317186..0d01d6db2 100644 --- a/CGameState.h +++ b/CGameState.h @@ -104,7 +104,6 @@ public: friend class CClient; friend void initGameState(Mapa * map, CGameInfo * cgi); friend class CScriptCallback; - friend void handleCPPObjS(std::map * mapa, CCPPObjectScript * script); friend class CMapHandler; friend class CGameHandler; }; diff --git a/CHeroWindow.cpp b/CHeroWindow.cpp index c2c672a3a..93358842b 100644 --- a/CHeroWindow.cpp +++ b/CHeroWindow.cpp @@ -1034,10 +1034,10 @@ void LClickableArea::deactivate() } void LClickableArea::clickLeft(boost::logic::tribool down) { - if(!down) - { - LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector()); - } + //if(!down) + //{ + // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector()); + //} } void RClickableArea::activate() @@ -1050,10 +1050,10 @@ void RClickableArea::deactivate() } void RClickableArea::clickRight(boost::logic::tribool down) { - if(!down) - { - LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector()); - } + //if(!down) + //{ + // LOCPLINT->showInfoDialog("TEST TEST AAA", std::vector()); + //} } void LRClickableAreaWText::clickLeft(boost::logic::tribool down) diff --git a/CLua.cpp b/CLua.cpp index ed58fc320..e1f9f7894 100644 --- a/CLua.cpp +++ b/CLua.cpp @@ -291,7 +291,7 @@ void CVisitableOPH::onNAHeroVisit(int objid, int heroID, bool alreadyVisited) else { ot++; - cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->advobtxt[ot],&std::vector()); + //cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->advobtxt[ot],&std::vector()); } } @@ -415,36 +415,32 @@ void CMines::newObject(int objid) } void CMines::onHeroVisit(int objid, int heroID) { + //TODO: this is code for standard mines, no support for abandoned mine (subId==7) DEFOS; const CGHeroInstance *h = cb->getHero(heroID); cb->setOwner(objid,h->tempOwner); MetaString ms; ms << std::pair(9,os->subID) << " " << std::pair(6,23+h->tempOwner); cb->setHoverName(objid,&ms); - //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+=VLC->generaltexth->allTexts[3].substr(2,VLC->generaltexth->allTexts[3].length()-2); - // std::vector weko; - // weko.push_back(com); - // cb->showInfoDialog(cb->getHeroOwner(heroID),VLC->objh->mines[os->subID].second,&weko); - //} + ms.clear(); + + int vv=1; //amount of resource per turn + if (os->subID==0 || os->subID==2) + vv++; + else if (os->subID==6) + vv = 1000; + + InfoWindow iw; + iw.text << std::pair(10,os->subID); + iw.player = h->tempOwner; + iw.components.push_back(Component(2,os->subID,vv,-1)); + cb->showInfoDialog(&iw); + //TODO: leaving garrison + } std::vector CMines::yourObjects() { - std::vector ret(1); + std::vector ret; ret.push_back(53); return ret; } diff --git a/CPlayerInterface.cpp b/CPlayerInterface.cpp index 45e368bf0..24b0ec727 100644 --- a/CPlayerInterface.cpp +++ b/CPlayerInterface.cpp @@ -33,6 +33,7 @@ #include "client/CCreatureAnimation.h" #include "client/Graphics.h" #include "map.h" +#include "lib/NetPacks.h" using namespace CSDL_Ext; extern TTF_Font * GEOR16; @@ -41,6 +42,14 @@ CPlayerInterface * LOCPLINT; extern std::queue events; extern boost::mutex eventsM; +SComponent toSComponent(const Component *c) +{ + SComponent ret((SComponent::Etype)c->type,c->subtype,c->val); + if(c->type == 2) + ret.subtitle+=CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2); + return ret; +} + class OCM_HLP_CGIN { public: @@ -2014,6 +2023,24 @@ void CPlayerInterface::showComp(SComponent comp) adventureInt->infoBar.showComp(&comp,4000); } +void CPlayerInterface::showInfoDialog(std::string text, std::vector &components) +{ + curint->deactivate(); //dezaktywacja starego interfejsu + std::vector intComps; + CInfoWindow * temp = CMessage::genIWindow(text,LOCPLINT->playerID,32,intComps); + LOCPLINT->objsToBlit.push_back(temp); + temp->pos.x=300-(temp->pos.w/2); + temp->pos.y=300-(temp->pos.h/2); + temp->okb.pos.x = temp->okb.posr.x + temp->pos.x; + temp->okb.pos.y = temp->okb.posr.y + temp->pos.y; + temp->okb.activate(); + for (int i=0;icomponents.size();i++) + { + temp->components[i]->activate(); + temp->components[i]->pos.x += temp->pos.x; + temp->components[i]->pos.y += temp->pos.y; + } +} void CPlayerInterface::showInfoDialog(std::string text, std::vector & components) { curint->deactivate(); //dezaktywacja starego interfejsu diff --git a/CPlayerInterface.h b/CPlayerInterface.h index d3efc047a..69e098019 100644 --- a/CPlayerInterface.h +++ b/CPlayerInterface.h @@ -154,7 +154,7 @@ public: }; class CInfoWindow : public CSimpleWindow //text + comp. + ok button -{ //okno usuwa swoje komponenty w chwili zamkniecia +{ //window deletes its components when closed public: CSCButton okb; std::vector components; @@ -320,6 +320,7 @@ public: void heroCreated(const CGHeroInstance* hero); void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val); void receivedResource(int type, int val); + void showInfoDialog(std::string text, std::vector &components); void showSelDialog(std::string text, std::vector & components, int askID); void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town); void garrisonChanged(const CGObjectInstance * obj); @@ -350,8 +351,8 @@ public: void handleMouseMotion(SDL_Event *sEvent); void init(ICallback * CB); int3 repairScreenPos(int3 pos); + void removeObjToBlit(IShowable* obj); void showInfoDialog(std::string text, std::vector & components); - void removeObjToBlit(IShowable* obj); CPlayerInterface(int Player, int serial);//c-tor }; diff --git a/client/Client.cpp b/client/Client.cpp index 70ef92b11..85e74684f 100644 --- a/client/Client.cpp +++ b/client/Client.cpp @@ -29,20 +29,26 @@ std::string toString(MetaString &ms) else { std::vector *vec; - int type = ms.texts[-ms.message[i]-1].first; + int type = ms.texts[-ms.message[i]-1].first, + ser = ms.texts[-ms.message[i]-1].second; if(type == 5) { - ret += CGI->arth->artifacts[ms.texts[-ms.message[i]-1].second].name; + ret += CGI->arth->artifacts[ser].name; continue; } else if(type == 7) { - ret += CGI->creh->creatures[ms.texts[-ms.message[i]-1].second].namePl; + ret += CGI->creh->creatures[ser].namePl; continue; } else if(type == 9) { - ret += CGI->objh->mines[ms.texts[-ms.message[i]-1].second].first; + ret += CGI->objh->mines[ser].first; + continue; + } + else if(type == 10) + { + ret += CGI->objh->mines[ser].second; continue; } else @@ -68,7 +74,7 @@ std::string toString(MetaString &ms) vec = &CGI->objh->creGens; break; } - ret += (*vec)[ms.texts[-ms.message[i]-1].second]; + ret += (*vec)[ser]; } } } @@ -159,6 +165,22 @@ void CClient::process(int what) std::cout << "done!"<> sr; + std::cout << "Set amount of "<objh->restypes[sr.resid] + << " of player "<<(unsigned)sr.player <<" to "<apply(&sr); + playerint[sr.player]->receivedResource(sr.resid,sr.val); + break; + } + case 103: //show info dialog + { + InfoWindow iw; + *serv >> iw; + + } case 501: //hero movement response - we have to notify interfaces and callback { TryMoveHero *th = new TryMoveHero; //will be deleted by callback after processing diff --git a/lib/NetPacks.h b/lib/NetPacks.h index a04d1507f..07a535d49 100644 --- a/lib/NetPacks.h +++ b/lib/NetPacks.h @@ -59,6 +59,18 @@ struct NewTurn : public CPack //101 h & heroes & res & day & resetBuilded; } }; +struct SetResource : public CPack //102 +{ + SetResource(){type = 102;}; + + ui8 player, resid; + si32 val; + + template void serialize(Handler &h, const int version) + { + h & player & resid & val; + } +}; struct TryMoveHero : public CPack //501 { TryMoveHero(){type = 501;}; @@ -76,7 +88,7 @@ struct TryMoveHero : public CPack //501 struct MetaString : public CPack //2001 helper for object scrips { std::vector strings; - std::vector > texts; //pairs; types: 1 - generaltexthandler->all; 2 - objh->xtrainfo; 3 - objh->names; 4 - objh->restypes; 5 - arth->artifacts[id].name; 6 - generaltexth->arraytxt; 7 - creh->creatures[os->subID].namePl; 8 - objh->creGens; 9 - objh->mines[ID].first + std::vector > texts; //pairs; types: 1 - generaltexthandler->all; 2 - objh->xtrainfo; 3 - objh->names; 4 - objh->restypes; 5 - arth->artifacts[id].name; 6 - generaltexth->arraytxt; 7 - creh->creatures[os->subID].namePl; 8 - objh->creGens; 9 - objh->mines[ID].first; 10 - objh->mines[ID].second std::vector message; template void serialize(Handler &h, const int version) @@ -96,8 +108,40 @@ struct MetaString : public CPack //2001 helper for object scrips strings.push_back(txt); return *this; } + void clear() + { + strings.clear(); + texts.clear(); + message.clear(); + } MetaString(){type = 2001;}; +}; +struct Component : public CPack //2002 helper for object scrips informations +{ + ui16 type, subtype; //types: 0 - primskill; 1 - secskill; 2 - resource; 3 - creature; 4 - artifact; 5 - experience + si32 val; // + give; - take + si16 when; // 0 - now; +x - within x days; -x - per x days + + template void serialize(Handler &h, const int version) + { + h & type & subtype & val & when; + } + Component(){type = 2002;}; + Component(ui16 Type, ui16 Subtype, si32 Val, si16 When):type(Type),subtype(Subtype),val(Val),when(When){type = 2002;}; +}; + +struct InfoWindow : public CPack //103 - displays simple info window +{ + MetaString text; + std::vector components; + ui8 player; + + template void serialize(Handler &h, const int version) + { + h & text & components & player; + } + InfoWindow(){type = 103;}; }; struct SetObjectProperty : public CPack//1001 diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 981f4e5c8..9c4b4a812 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -213,22 +213,6 @@ void CGameHandler::init(StartInfo *si, int Seed) gs = new CGameState(); gs->init(si,map,Seed); - /****************************SCRIPTS************************************************/ - //std::map > * skrypty = &objscr; //alias for easier access - /****************************C++ OBJECT SCRIPTS************************************************/ - std::map scripts; - CScriptCallback * csc = new CScriptCallback(); - csc->gh = this; - handleCPPObjS(&scripts,new CVisitableOPH(csc)); - handleCPPObjS(&scripts,new CVisitableOPW(csc)); - handleCPPObjS(&scripts,new CPickable(csc)); - handleCPPObjS(&scripts,new CMines(csc)); - handleCPPObjS(&scripts,new CTownScript(csc)); - handleCPPObjS(&scripts,new CHeroScript(csc)); - handleCPPObjS(&scripts,new CMonsterS(csc)); - handleCPPObjS(&scripts,new CCreatureGen(csc)); - //created map - /****************************LUA OBJECT SCRIPTS************************************************/ //std::vector * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files //for (int i=0; isize(); i++) @@ -252,25 +236,6 @@ void CGameHandler::init(StartInfo *si, int Seed) // delete temp; // }HANDLE_EXCEPTION //} - /****************************INITIALIZING OBJECT SCRIPTS************************************************/ - //std::string temps("newObject"); - for (unsigned i=0; iobjects.size(); i++) - { - //c++ scripts - if (scripts.find(map->objects[i]->ID) != scripts.end()) - { - map->objects[i]->state = scripts[map->objects[i]->ID]; - map->objects[i]->state->newObject(map->objects[i]->id); - } - else - { - map->objects[i]->state = NULL; - } - - //// lua scripts - //if(checkFunc(map->objects[i]->ID,temps)) - // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]); - } //delete lf; } @@ -363,6 +328,42 @@ void CGameHandler::run() boost::thread(boost::bind(&CGameHandler::handleConnection,this,pom,boost::ref(**i))); } + + /****************************SCRIPTS************************************************/ + //std::map > * skrypty = &objscr; //alias for easier access + /****************************C++ OBJECT SCRIPTS************************************************/ + std::map scripts; + CScriptCallback * csc = new CScriptCallback(); + csc->gh = this; + handleCPPObjS(&scripts,new CVisitableOPH(csc)); + handleCPPObjS(&scripts,new CVisitableOPW(csc)); + handleCPPObjS(&scripts,new CPickable(csc)); + handleCPPObjS(&scripts,new CMines(csc)); + handleCPPObjS(&scripts,new CTownScript(csc)); + handleCPPObjS(&scripts,new CHeroScript(csc)); + handleCPPObjS(&scripts,new CMonsterS(csc)); + handleCPPObjS(&scripts,new CCreatureGen(csc)); + + /****************************INITIALIZING OBJECT SCRIPTS************************************************/ + //std::string temps("newObject"); + for (unsigned i=0; imap->objects.size(); i++) + { + //c++ scripts + if (scripts.find(gs->map->objects[i]->ID) != scripts.end()) + { + gs->map->objects[i]->state = scripts[gs->map->objects[i]->ID]; + gs->map->objects[i]->state->newObject(gs->map->objects[i]->id); + } + else + { + gs->map->objects[i]->state = NULL; + } + + //// lua scripts + //if(checkFunc(map->objects[i]->ID,temps)) + // (*skrypty)[map->objects[i]->ID][temps]->newObject(map->objects[i]); + } + while (!end) { newTurn(); diff --git a/server/CScriptCallback.cpp b/server/CScriptCallback.cpp index 8651f19f7..21cc08650 100644 --- a/server/CScriptCallback.cpp +++ b/server/CScriptCallback.cpp @@ -87,8 +87,9 @@ int CScriptCallback::getHeroOwner(int heroID) //return hero->getOwner(); return -1; } -void CScriptCallback::showInfoDialog(int player, std::string text, std::vector * components) +void CScriptCallback::showInfoDialog(InfoWindow *iw) { + gh->sendToAllClients(iw); //TODO: upewniac sie ze mozemy to zrzutowac (przy customowych interfejsach cos moze sie kopnac) //if (player>=0) //{ @@ -153,6 +154,11 @@ int CScriptCallback::getDate(int mode) } void CScriptCallback::giveResource(int player, int which, int val) { + SetResource sr; + sr.player = player; + sr.resid = which; + sr.val = (gh->gs->players[player].resources[which]+val); + gh->sendAndApply(&sr); //gh->gs->players[player].resources[which]+=val; //sv->playerint[player]->receivedResource(which,val); } diff --git a/server/CScriptCallback.h b/server/CScriptCallback.h index 4b4e9ef21..e90135a72 100644 --- a/server/CScriptCallback.h +++ b/server/CScriptCallback.h @@ -14,6 +14,7 @@ class CGTownInstance; class CGameState; struct lua_State; struct MetaString; +struct InfoWindow; class CScriptCallback { CScriptCallback(void); @@ -35,7 +36,7 @@ public: void setOwner(int objid, ui8 owner); void setHoverName(int objid, MetaString * name); void changePrimSkill(int ID, int which, int val); - void showInfoDialog(int player, std::string text, std::vector * components); //TODO: obslugiwac nulle + void showInfoDialog(InfoWindow *iw); void showSelDialog(int player, std::string text, std::vector*components, IChosen * asker); void giveResource(int player, int which, int val); void showCompInfo(int player, SComponent * comp);