From fa5079809f19bf305d10c2761eb3325644e41e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20W=2E=20Urba=C5=84czyk?= Date: Sun, 25 Nov 2007 13:16:45 +0000 Subject: [PATCH] Partially done resources. --- CAdvmapInterface.cpp | 6 ++- CCallback.cpp | 110 +++++++++++++++++++++++++---------------- CCallback.h | 10 +++- CGameInterface.h | 1 + CLua.cpp | 54 ++++++++++++++++++++ CLua.h | 12 +++++ CMT.cpp | 3 +- CPlayerInterface.cpp | 57 ++++++++++++++++++++- CPlayerInterface.h | 1 + hch/CObjectHandler.cpp | 13 ++++- hch/CObjectHandler.h | 3 +- 11 files changed, 217 insertions(+), 53 deletions(-) diff --git a/CAdvmapInterface.cpp b/CAdvmapInterface.cpp index ce7d8481f..5d0e55274 100644 --- a/CAdvmapInterface.cpp +++ b/CAdvmapInterface.cpp @@ -814,12 +814,13 @@ void CTerrainRect::clickLeft(tribool down) int3 mp = whichTileIsIt(); if ((mp.x<0) || (mp.y<0)) return; + bool mres; if (currentPath) { if ( (currentPath->endPos()) == mp) { //move CPath sended(*currentPath); //temporary path - engine will operate on it - LOCPLINT->cb->moveHero( ((const CGHeroInstance*)LOCPLINT->adventureInt->selection.selected)->type->ID,&sended,1,0); + mres = LOCPLINT->cb->moveHero( ((const CGHeroInstance*)LOCPLINT->adventureInt->selection.selected)->type->ID,&sended,1,0); } else { @@ -830,7 +831,8 @@ void CTerrainRect::clickLeft(tribool down) const CGHeroInstance * currentHero = LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected].first; int3 bufpos = currentHero->getPosition(false); //bufpos.x-=1; - currentPath = LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected].second = CGI->pathf->getPath(bufpos,mp,currentHero,1); + if (mres) + currentPath = LOCPLINT->adventureInt->heroList.items[LOCPLINT->adventureInt->heroList.selected].second = CGI->pathf->getPath(bufpos,mp,currentHero,1); //if (LOCPLINT->objsToBlit.size()==0) //{ diff --git a/CCallback.cpp b/CCallback.cpp index a4893ae98..4cef039ec 100644 --- a/CCallback.cpp +++ b/CCallback.cpp @@ -116,56 +116,79 @@ bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType) { //performing move hero->movement-=CGI->mh->getCost(stpos, endpos, hero); - int heroSight = hero->getSightDistance(); + std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(CHeroInstance::convertPosition(curd.dst,false)); + bool blockvis = false; + for (int pit = 0; pitblockVisit) + blockvis = true; - int xbeg = stpos.x - heroSight - 2; - if(xbeg < 0) - xbeg = 0; - - int xend = stpos.x + heroSight + 2; - if(xend >= CGI->ac->map.width) - xend = CGI->ac->map.width; - - int ybeg = stpos.y - heroSight - 2; - if(ybeg < 0) - ybeg = 0; - - int yend = stpos.y + heroSight + 2; - if(yend >= CGI->ac->map.height) - yend = CGI->ac->map.height; - - for(int xd=xbeg; xdgetSightDistance(); + + int xbeg = stpos.x - heroSight - 2; + if(xbeg < 0) + xbeg = 0; + + int xend = stpos.x + heroSight + 2; + if(xend >= CGI->ac->map.width) + xend = CGI->ac->map.width; + + int ybeg = stpos.y - heroSight - 2; + if(ybeg < 0) + ybeg = 0; + + int yend = stpos.y + heroSight + 2; + if(yend >= CGI->ac->map.height) + yend = CGI->ac->map.height; + + for(int xd=xbeg; xdgetPosition(false).x-xd)*(hero->getPosition(false).x-xd); - int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd); - if(deltaX+deltaYgetSightDistance()*hero->getSightDistance()) - gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1; + for(int yd=ybeg; ydgetPosition(false).x-xd)*(hero->getPosition(false).x-xd); + int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd); + if(deltaX+deltaYgetSightDistance()*hero->getSightDistance()) + gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1; + } + } + + hero->pos = curd.dst; + int nn=0; //number of interfece of currently browsed player + for(std::map::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing + { + if (j->first > PLAYER_LIMIT) + break; + if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z]) + { //player should be notified + CGI->playerint[j->second.serial]->heroMoved(curd); + } + ++nn; + } + for (int iii=0; iiicheckFunc(vis[iii]->ID,"heroVisit")) //script function + gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID); + if(vis[iii]->state) //hard-coded function + vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID); } } - - hero->pos = curd.dst; - int nn=0; //number of interfece of currently browsed player - for(std::map::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing + else { - if (j->first > PLAYER_LIMIT) - break; - if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z]) - { //player should be notified - CGI->playerint[j->second.serial]->heroMoved(curd); + curd.successful = false; + CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd); + for (int iii=0; iiiblockVisit) + { + if(gs->checkFunc(vis[iii]->ID,"heroVisit")) //script function + gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID); + if(vis[iii]->state) //hard-coded function + vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID); + } } - ++nn; - } - - std::vector< CGObjectInstance * > vis = CGI->mh->getVisitableObjs(hero->getPosition(false)); - for (int iii=0; iiicheckFunc(vis[iii]->ID,"heroVisit")) - gs->objscr[vis[iii]->ID]["heroVisit"]->onHeroVisit(vis[iii],curd.ho->subID); - if(vis[iii]->state) - vis[iii]->state->onHeroVisit(vis[iii],curd.ho->subID); - //std::cout<< CGI->objh->objects[vis[iii]->ID].name<players[player].resources[which]+=val; + CGI->playerint[gs->players[player].serial]->receivedResource(which,val); } void CLuaCallback::registerFuncs(lua_State * L) { diff --git a/CCallback.h b/CCallback.h index 200b12e65..1be0da37d 100644 --- a/CCallback.h +++ b/CCallback.h @@ -15,6 +15,7 @@ struct HeroMoveDetails int3 src, dst; //source and destination points CGHeroInstance * ho; //object instance of this hero int owner; + bool successful; }; class CCallback @@ -56,13 +57,18 @@ class CScriptCallback public: CGameState * gs; + //get info static int3 getPos(CGObjectInstance * ob); - static void changePrimSkill(int ID, int which, int val); - void showInfoDialog(int player, std::string text, std::vector * components); int getHeroOwner(int heroID); int getSelectedHero(); int getDate(int mode=0); + + //do sth + static void changePrimSkill(int ID, int which, int val); + void showInfoDialog(int player, std::string text, std::vector * components); void giveResource(int player, int which, int val); + + //friends friend void initGameState(CGameInfo * cgi); }; class CLuaCallback : public CScriptCallback diff --git a/CGameInterface.h b/CGameInterface.h index 6f4b858b4..7da77dc98 100644 --- a/CGameInterface.h +++ b/CGameInterface.h @@ -21,6 +21,7 @@ public: virtual void heroCreated(const CGHeroInstance*)=0{}; virtual void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)=0{}; virtual void heroMoved(const HeroMoveDetails & details)=0; + virtual void receivedResource(int type, int val){}; }; class CAIHandler { diff --git a/CLua.cpp b/CLua.cpp index 05e599f34..f78b34ed9 100644 --- a/CLua.cpp +++ b/CLua.cpp @@ -444,4 +444,58 @@ void CMines::newTurn () 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; } \ No newline at end of file diff --git a/CLua.h b/CLua.h index e42892772..f4e5d6b29 100644 --- a/CLua.h +++ b/CLua.h @@ -127,5 +127,17 @@ class CMines : public CCPPObjectScript //flaggable, and giving resource at each std::string hoverText(CGObjectInstance *os); void newTurn (); + friend void initGameState(CGameInfo * cgi); +}; + +class CPickable : public CCPPObjectScript //pickable - resources, artifacts, etc +{ + CPickable(CScriptCallback * CB):CCPPObjectScript(CB){}; + + void newObject(CGObjectInstance *os); + void onHeroVisit(CGObjectInstance *os, int heroID); + std::string hoverText(CGObjectInstance *os); + std::vector yourObjects(); //returns IDs of objects which are handled by script + friend void initGameState(CGameInfo * cgi); }; \ No newline at end of file diff --git a/CMT.cpp b/CMT.cpp index fdfa1762e..ec35f110f 100644 --- a/CMT.cpp +++ b/CMT.cpp @@ -222,11 +222,10 @@ void initGameState(CGameInfo * cgi) csc->gs = cgi->state; handleCPPObjS(&scripts,new CVisitableOPH(csc)); handleCPPObjS(&scripts,new CVisitableOPW(csc)); + handleCPPObjS(&scripts,new CPickable(csc)); handleCPPObjS(&scripts,new CMines(csc)); //created map - - /****************************LUA OBJECT SCRIPTS************************************************/ std::vector * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files for (int i=0; isize(); i++) diff --git a/CPlayerInterface.cpp b/CPlayerInterface.cpp index 760064052..d7849915d 100644 --- a/CPlayerInterface.cpp +++ b/CPlayerInterface.cpp @@ -67,7 +67,7 @@ SComponent::SComponent(Etype Type, int Subtype, int Val) subtitle = oss.str(); break; case resource: - //description = CGI->generaltexth->arraytxt[2+Subtype]; + description = CGI->generaltexth->allTexts[242]; std::ostringstream oss; oss << Val; subtitle = oss.str(); @@ -388,13 +388,61 @@ inline void delObjRect(const int & x, const int & y, const int & z, const int & break; } } - +int getDir(int3 src, int3 dst) +{ + int ret = -1; + if(dst.x+1 == src.x && dst.y+1 == src.y) //tl + { + ret = 1; + } + else if(dst.x == src.x && dst.y+1 == src.y) //t + { + ret = 2; + } + else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr + { + ret = 3; + } + else if(dst.x-1 == src.x && dst.y == src.y) //r + { + ret = 4; + } + else if(dst.x-1 == src.x && dst.y-1 == src.y) //br + { + ret = 5; + } + else if(dst.x == src.x && dst.y-1 == src.y) //b + { + ret = 6; + } + else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl + { + ret = 7; + } + else if(dst.x+1 == src.x && dst.y == src.y) //l + { + ret = 8; + } + return ret; +} void CPlayerInterface::heroMoved(const HeroMoveDetails & details) { adventureInt->minimap.draw(); //initializing objects and performing first step of move CGHeroInstance * ho = details.ho; //object representing this hero int3 hp = details.src; + if (!details.successful) + { + ho->moveDir = getDir(details.src,details.dst); + ho->isStanding = true; + adventureInt->heroList.draw(); + if (adventureInt->terrain.currentPath) + { + delete adventureInt->terrain.currentPath; + adventureInt->terrain.currentPath = NULL; + } + return; + } if (adventureInt->terrain.currentPath) //&& hero is moving { @@ -1197,6 +1245,11 @@ void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int adventureInt->infoBar.draw(); return; } + +void CPlayerInterface::receivedResource(int type, int val) +{ + adventureInt->resdatabar.draw(); +} void CPlayerInterface::showInfoDialog(std::string text, std::vector & components) { adventureInt->hide(); //dezaktywacja starego interfejsu diff --git a/CPlayerInterface.h b/CPlayerInterface.h index c3a2fa016..8cb8df2fb 100644 --- a/CPlayerInterface.h +++ b/CPlayerInterface.h @@ -159,6 +159,7 @@ public: void heroKilled(const CGHeroInstance*); void heroCreated(const CGHeroInstance*); void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val); + void receivedResource(int type, int val); SDL_Surface * infoWin(const void * specific); //specific=0 => draws info about selected town/hero //TODO - gdy sie dorobi sensowna hierarchie klas ins. to wywalic tego brzydkiego void* void handleEvent(SDL_Event * sEvent); diff --git a/hch/CObjectHandler.cpp b/hch/CObjectHandler.cpp index 22def5dcc..19a54ae99 100644 --- a/hch/CObjectHandler.cpp +++ b/hch/CObjectHandler.cpp @@ -59,6 +59,16 @@ void CObjectHandler::loadObjects() temp = temp.substr(1,temp.length()-2); mines[i++].second = temp; } + + buf = CGameInfo::mainObj->bitmaph->getTextFile("RESTYPES.TXT"); + it=0; + while (it); //*state = *right.state; //state = right.state; diff --git a/hch/CObjectHandler.h b/hch/CObjectHandler.h index 95242a14e..b09fc97ce 100644 --- a/hch/CObjectHandler.h +++ b/hch/CObjectHandler.h @@ -450,7 +450,8 @@ public: std::vector advobtxt; std::vector xtrainfo; - std::vector > mines; //first - name; second - event + std::vector restypes; + std::vector > mines; //first - name; second - event description };