浏览代码

Partially done resources.

Michał W. Urbańczyk 18 年之前
父节点
当前提交
fa5079809f
共有 11 个文件被更改,包括 213 次插入49 次删除
  1. 4 2
      CAdvmapInterface.cpp
  2. 63 39
      CCallback.cpp
  3. 8 2
      CCallback.h
  4. 1 0
      CGameInterface.h
  5. 54 0
      CLua.cpp
  6. 12 0
      CLua.h
  7. 1 2
      CMT.cpp
  8. 55 2
      CPlayerInterface.cpp
  9. 1 0
      CPlayerInterface.h
  10. 12 1
      hch/CObjectHandler.cpp
  11. 2 1
      hch/CObjectHandler.h

+ 4 - 2
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)
 	//{

+ 63 - 39
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; pit<vis.size();pit++)
+				if (vis[pit]->blockVisit)
+					blockvis = true;
 
-			int xbeg = stpos.x - heroSight - 2;
-			if(xbeg < 0)
-				xbeg = 0;
+			if (!blockvis)
+			{
+				curd.successful = true;
+				int heroSight = hero->getSightDistance();
 
-			int xend = stpos.x + heroSight + 2;
-			if(xend >= CGI->ac->map.width)
-				xend = CGI->ac->map.width;
+				int xbeg = stpos.x - heroSight - 2;
+				if(xbeg < 0)
+					xbeg = 0;
 
-			int ybeg = stpos.y - heroSight - 2;
-			if(ybeg < 0)
-				ybeg = 0;
+				int xend = stpos.x + heroSight + 2;
+				if(xend >= CGI->ac->map.width)
+					xend = CGI->ac->map.width;
 
-			int yend = stpos.y + heroSight + 2;
-			if(yend >= CGI->ac->map.height)
-				yend = CGI->ac->map.height;
+				int ybeg = stpos.y - heroSight - 2;
+				if(ybeg < 0)
+					ybeg = 0;
 
-			for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
-			{
-				for(int yd=ybeg; yd<yend; ++yd)
+				int yend = stpos.y + heroSight + 2;
+				if(yend >= CGI->ac->map.height)
+					yend = CGI->ac->map.height;
+
+				for(int xd=xbeg; xd<xend; ++xd) //revealing part of map around heroes
 				{
-					int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
-					int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
-					if(deltaX+deltaY<hero->getSightDistance()*hero->getSightDistance())
-						gs->players[player].fogOfWarMap[xd][yd][hero->getPosition(false).z] = 1;
+					for(int yd=ybeg; yd<yend; ++yd)
+					{
+						int deltaX = (hero->getPosition(false).x-xd)*(hero->getPosition(false).x-xd);
+						int deltaY = (hero->getPosition(false).y-yd)*(hero->getPosition(false).y-yd);
+						if(deltaX+deltaY<hero->getSightDistance()*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<int, PlayerState>::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);
+				hero->pos = curd.dst;
+				int nn=0; //number of interfece of currently browsed player
+				for(std::map<int, PlayerState>::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; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
+				{
+					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; iii<vis.size(); iii++)
+			else
 			{
-				if(gs->checkFunc(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<<std::endl;
+				curd.successful = false;
+				CGI->playerint[gs->players[hero->getOwner()].serial]->heroMoved(curd);
+				for (int iii=0; iii<vis.size(); iii++) //if object is visitable we call onHeroVisit
+				{
+					if (vis[iii]->blockVisit)
+					{
+						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);
+					}
+				}
+				return false;
 			}
 
 		}
@@ -407,6 +430,7 @@ int CScriptCallback::getDate(int mode)
 void CScriptCallback::giveResource(int player, int which, int val)
 {
 	gs->players[player].resources[which]+=val;
+	CGI->playerint[gs->players[player].serial]->receivedResource(which,val);
 }
 void CLuaCallback::registerFuncs(lua_State * L)
 {

+ 8 - 2
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<SComponent*> * 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<SComponent*> * components);
 	void giveResource(int player, int which, int val);
+
+	//friends
 	friend void initGameState(CGameInfo * cgi);
 };
 class CLuaCallback : public CScriptCallback

+ 1 - 0
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
 {

+ 54 - 0
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<int> CPickable::yourObjects() //returns IDs of objects which are handled by script
+{
+	std::vector<int> ret(3);
+	ret.push_back(79); //resource
+	ret.push_back(5); //artifact
+	ret.push_back(101); //treasure chest / commander stone
+	return ret;
 }

+ 12 - 0
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<int> yourObjects(); //returns IDs of objects which are handled by script
+
 	friend void initGameState(CGameInfo * cgi);
 };

+ 1 - 2
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<std::string> * lf = CLuaHandler::searchForScripts("scripts/lua/objects"); //files
 	for (int i=0; i<lf->size(); i++)

+ 55 - 2
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<SComponent*> & components)
 {
 	adventureInt->hide(); //dezaktywacja starego interfejsu

+ 1 - 0
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);

+ 12 - 1
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<buf.length()-1)
+	{
+		CGeneralTextHandler::loadToIt(temp,buf,it,3);
+		restypes.push_back(temp);
+	}
+
+
 	
 }
 
@@ -273,6 +283,7 @@ CGObjectInstance::CGObjectInstance()
 	//state = NULL;
 	defObjInfoNumber = -1;
 	tempOwner = 254;
+	blockVisit = false;
 }
 CGObjectInstance::~CGObjectInstance()
 {
@@ -297,7 +308,7 @@ CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
 	defInfo = right.defInfo;
 	info = right.info;
 	defObjInfoNumber = right.defObjInfoNumber;
-	blockVisit = false;
+	blockVisit = right.blockVisit;
 	//state = new CLuaObjectScript(right.state->);
 	//*state = *right.state;
 	//state = right.state;

+ 2 - 1
hch/CObjectHandler.h

@@ -450,7 +450,8 @@ public:
 
 	std::vector<std::string> advobtxt;
 	std::vector<std::string> xtrainfo;
-	std::vector<std::pair<std::string,std::string> > mines; //first - name; second - event
+	std::vector<std::string> restypes;
+	std::vector<std::pair<std::string,std::string> > mines; //first - name; second - event description
 };