Selaa lähdekoodia

Almost finished building of buildings.

Michał W. Urbańczyk 17 vuotta sitten
vanhempi
sitoutus
1e61235aa9

+ 1 - 1
AdventureMapButton.h

@@ -120,9 +120,9 @@ void AdventureMapButton<T>::clickRight (tribool down)
 template <typename T>
 void AdventureMapButton<T>::hover (bool on)
 {
+	Hoverable::hover(on);
 	if(name.size()) //if there is no name, there is nohing to display also
 	{
-		Hoverable::hover(on);
 		if (on)
 			LOCPLINT->statusbar->print(name);
 		else if (LOCPLINT->statusbar->getCurrent()==name)

+ 19 - 1
CCallback.cpp

@@ -12,7 +12,7 @@
 #include "hch/CGeneralTextHandler.h"
 #include "CAdvmapInterface.h"
 #include "CPlayerInterface.h"
-
+#include "hch/CBuildingHandler.h"
 LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
 
 int CCallback::lowestSpeed(CGHeroInstance * chi)
@@ -50,6 +50,10 @@ void CCallback::newTurn()
 		{
 			(*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
 		}
+		for(int j=0;j<i->second.towns.size();j++)
+		{
+			i->second.towns[j]->builded=0;
+		}
 	}
 }
 bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
@@ -515,6 +519,20 @@ bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1,
 	return true;
 }
 
+bool CCallback::buildBuilding(const CGTownInstance *town, int buildingID)
+{
+	CGTownInstance * t = const_cast<CGTownInstance *>(town);
+	CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
+	//TODO: check if we are allowed to build
+
+	t->builtBuildings.insert(buildingID);
+	for(int i=0;i<7;i++)
+		gs->players[player].resources[i]-=b->resources[i];
+	t->builded++;
+
+	return true;
+}
+
 int CCallback::battleGetBattlefieldType()
 {
 	return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].terType;

+ 1 - 0
CCallback.h

@@ -92,6 +92,7 @@ public:
 	bool dismissHero(const CGHeroInstance * hero);
 	const CCreatureSet* getGarrison(const CGObjectInstance *obj);
 	bool swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2);
+	bool buildBuilding(const CGTownInstance *town, int buildingID);
 	//battle
 	int battleGetBattlefieldType(); //   1. sand/shore   2. sand/mesas   3. dirt/birches   4. dirt/hills   5. dirt/pines   6. grass/hills   7. grass/pines   8. lava   9. magic plains   10. snow/mountains   11. snow/trees   12. subterranean   13. swamp/trees   14. fiery fields   15. rock lands   16. magic clouds   17. lucid pools   18. holy ground   19. clover field   20. evil fog   21. "favourable winds" text on magic plains background   22. cursed ground   23. rough   24. ship to ship   25. ship
 	int battleGetObstaclesAtTile(int tile); //returns bitfield 

+ 114 - 15
CCastleInterface.cpp

@@ -11,6 +11,7 @@
 #include <sstream>
 #include "CMessage.h"
 #include "hch/CGeneralTextHandler.h"
+extern TTF_Font * GEOR16;
 CBuildingRect::CBuildingRect(Structure *Str)
 :str(Str)
 {	
@@ -539,6 +540,7 @@ void CHallInterface::CResDataBar::show(SDL_Surface * to)
 CHallInterface::CResDataBar::CResDataBar()
 {
 	bg = CGI->bitmaph->loadBitmap("Z2ESBAR.bmp");
+	SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
 	CSDL_Ext::blueToPlayersAdv(bg,LOCPLINT->playerID);
 	pos.x = 7;
 	pos.y = 575;
@@ -557,7 +559,7 @@ void CHallInterface::CBuildingBox::hover(bool on)
 	{
 		std::string toPrint = CGI->townh->hcommands[state];
 		std::vector<std::string> name;
-		name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][ID]->name);
+		name.push_back(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->name);
 		LOCPLINT->statusbar->print(CSDL_Ext::processStr(toPrint,name));
 	}
 	else
@@ -565,13 +567,25 @@ void CHallInterface::CBuildingBox::hover(bool on)
 }
 void CHallInterface::CBuildingBox::clickLeft (tribool down)
 {
+	if(pressedL && (!down))
+	{
+		LOCPLINT->castleInt->hallInt->deactivate();
+		new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,0);
+	}
+	ClickableL::clickLeft(down);
 }
 void CHallInterface::CBuildingBox::clickRight (tribool down)
 {
+	if(down)
+	{
+		LOCPLINT->castleInt->hallInt->deactivate();
+		new CHallInterface::CBuildWindow(LOCPLINT->castleInt->town->subID,BID,state,1);
+	}
+	ClickableR::clickRight(down);
 }
 void CHallInterface::CBuildingBox::show(SDL_Surface * to)
 {
-	blitAt(LOCPLINT->castleInt->bicons->ourImages[ID].bitmap,pos.x,pos.y);
+	blitAt(LOCPLINT->castleInt->bicons->ourImages[BID].bitmap,pos.x,pos.y);
 	int pom, pom2=-1;
 	switch (state)
 	{
@@ -597,7 +611,7 @@ void CHallInterface::CBuildingBox::show(SDL_Surface * to)
 	blitAt(LOCPLINT->castleInt->hallInt->bars->ourImages[pom].bitmap,pos.x-1,pos.y+71);
 	if(pom2>=0)
 		blitAt(LOCPLINT->castleInt->hallInt->status->ourImages[pom2].bitmap,pos.x+135, pos.y+54);
-	CSDL_Ext::printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][ID]->name,pos.x-1+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->w/2,pos.y+71+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->h/2, GEOR13,zwykly);
+	CSDL_Ext::printAtMiddle(CGI->buildh->buildings[LOCPLINT->castleInt->town->subID][BID]->name,pos.x-1+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->w/2,pos.y+71+LOCPLINT->castleInt->hallInt->bars->ourImages[0].bitmap->h/2, GEOR13,zwykly);
 }
 void CHallInterface::CBuildingBox::activate()
 {
@@ -615,13 +629,13 @@ CHallInterface::CBuildingBox::~CBuildingBox()
 {
 }
 CHallInterface::CBuildingBox::CBuildingBox(int id)
-	:ID(id)
+	:BID(id)
 {
 	pos.w = 150;
 	pos.h = 70;
 }
 CHallInterface::CBuildingBox::CBuildingBox(int id, int x, int y)
-	:ID(id)
+	:BID(id)
 {
 	pos.x = x;
 	pos.y = y;
@@ -638,6 +652,8 @@ CHallInterface::CHallInterface(CCastleInterface * owner)
 	status = CGI->spriteh->giveDefEss("TPTHCHK.DEF");
 	exit = new AdventureMapButton<CHallInterface>
 		(CGI->townh->tcommands[8],"",&CHallInterface::close,748,556,"TPMAGE1.DEF",this,false,NULL,false);
+
+	//preparing boxes with buildings//
 	boxes.resize(5);
 	for(int i=0;i<5;i++) //for each row
 	{
@@ -666,8 +682,17 @@ CHallInterface::CHallInterface(CCastleInterface * owner)
 					if(owner->town->forbiddenBuildings.find(CGI->buildh->hall[owner->town->subID].second[i][j][k])!=owner->town->forbiddenBuildings.end())
 						boxes[i][boxes[i].size()-1]->state = 2; //forbidden
 					else if(owner->town->builded >= MAX_BUILDING_PER_TURN)
-						boxes[i][boxes[i].size()-1]->state = 4; //already built
+						boxes[i][boxes[i].size()-1]->state = 5; //already built
+
+					//checking resources
+					CBuilding * pom = CGI->buildh->buildings[owner->town->subID][CGI->buildh->hall[owner->town->subID].second[i][j][k]];
+					for(int res=0;res<7;res++) //TODO: support custom amount of resources
+					{
+						if(pom->resources[res]>LOCPLINT->cb->getResourceAmount(res))
+							boxes[i][boxes[i].size()-1]->state = 6; //lack of res
+					}
 
+					//checking for requirements
 					for( std::set<int>::iterator ri  =  CGI->townh->requirements[owner->town->subID][ID].begin();
 						 ri != CGI->townh->requirements[owner->town->subID][ID].end();
 						 ri++ )
@@ -679,13 +704,6 @@ CHallInterface::CHallInterface(CCastleInterface * owner)
 					//TODO: check if capital is already built, check if there is water for shipyard
 					
 
-					CBuilding * pom = CGI->buildh->buildings[owner->town->subID][CGI->buildh->hall[owner->town->subID].second[i][j][k]];
-
-					for(int res=0;res<7;res++) //TODO: support custom amount of resources
-					{
-						if(pom->resources[res]>LOCPLINT->cb->getResourceAmount(res))
-							boxes[i][boxes[i].size()-1]->state = 6; //lack of res
-					}
 
 
 					break;
@@ -746,7 +764,6 @@ void CHallInterface::deactivate()
 		for(int j=0;j<boxes[i].size();j++)
 		{
 			boxes[i][j]->deactivate();
-			delete boxes[i][j];
 		}
 	}
 	exit->deactivate();
@@ -754,19 +771,101 @@ void CHallInterface::deactivate()
 
 void CHallInterface::CBuildWindow::activate()
 {
+	buy->activate();
+	cancel->activate();
+	ClickableR::activate();
+	LOCPLINT->objsToBlit.push_back(this);
 }
 void CHallInterface::CBuildWindow::deactivate()
 {
+	buy->deactivate();
+	cancel->deactivate();
+	ClickableR::deactivate();
+	LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
+}
+void CHallInterface::CBuildWindow::Buy()
+{
+	LOCPLINT->cb->buildBuilding(LOCPLINT->castleInt->town,bid);
+	close();
+}
+void CHallInterface::CBuildWindow::close()
+{
+	deactivate();
+	delete this;
+	LOCPLINT->castleInt->hallInt->activate();
+	LOCPLINT->castleInt->hallInt->show();
+}
+void CHallInterface::CBuildWindow::clickRight (tribool down)
+{
+	if((!down || indeterminate(down)) && mode)
+		close();
 }
 void CHallInterface::CBuildWindow::show(SDL_Surface * to)
 {
+	SDL_Rect pom = genRect(bitmap->h-1,bitmap->w-1,pos.x,pos.y);
+	SDL_Rect poms = pom; poms.x=0;poms.y=0;
+	SDL_BlitSurface(bitmap,&poms,to?to:ekran,&pom);
+	buy->show();
+	cancel->show();
+}
+std::string getTextForState(int state)
+{
+	if(state<7)
+		return CGI->townh->hcommands[state];
+	switch (state)
+	{
+	case 7:
+		return CGI->generaltexth->allTexts[219]; //all prereq. are met
+	default:
+		return "Error, wrong state!";
+	}
 }
 CHallInterface::CBuildWindow::CBuildWindow(int Tid, int Bid, int State, bool Mode)
 :tid(Tid),bid(Bid),mode(Mode), state(State)
 {
-	bitmap = CGI->bitmaph->loadBitmap("TPUBUILD.bmp");
+	SDL_Surface *hhlp = CGI->bitmaph->loadBitmap("TPUBUILD.bmp");
+	bitmap = SDL_ConvertSurface(hhlp,ekran->format,0); //na 8bitowej mapie by sie psulo
+	SDL_SetColorKey(hhlp,SDL_SRCCOLORKEY,SDL_MapRGB(hhlp->format,0,255,255));
+	SDL_FreeSurface(hhlp);
+	pos.x = ekran->w/2 - bitmap->w/2;
+	pos.y = ekran->h/2 - bitmap->h/2;
 	CSDL_Ext::blueToPlayersAdv(bitmap,LOCPLINT->playerID);
+	blitAt(LOCPLINT->castleInt->bicons->ourImages[bid].bitmap,125,50,bitmap);
+	std::vector<std::string> pom; pom.push_back(CGI->buildh->buildings[tid][bid]->name);
+	CSDL_Ext::printAtMiddleWB(CGI->buildh->buildings[tid][bid]->description,197,168,GEOR16,40,zwykly,bitmap);
+	CSDL_Ext::printAtMiddleWB(getTextForState(state),197,248,GEOR13,50,zwykly,bitmap);
+	CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(CGI->townh->hcommands[7],pom),197,30,GEOR16,tytulowy,bitmap);
+	int resamount=0; for(int i=0;i<7;i++) if(CGI->buildh->buildings[tid][bid]->resources[i]) resamount++;
+	int ah = (resamount>4) ? 304 : 341;
+	int cn=-1, it=0;
+	int row1w = std::min(resamount,4) * 32 + (std::min(resamount,4)-1) * 45,
+		row2w = (resamount-4) * 32 + (resamount-5) * 45;
+	char buf[15];
+	while(++cn<7)
+	{
+		if(!CGI->buildh->buildings[tid][bid]->resources[cn])
+			continue;
+		itoa(CGI->buildh->buildings[tid][bid]->resources[cn],buf,10);
+		if(it<4)
+		{
+			CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row1w/2)+77*it+16,ah+42,GEOR16,zwykly,bitmap);
+			blitAt(CGI->townh->resources->ourImages[cn].bitmap,(bitmap->w/2-row1w/2)+77*it++,ah,bitmap);
+		}
+		else
+		{
+			CSDL_Ext::printAtMiddle(buf,(bitmap->w/2-row2w/2)+77*it+16-308,ah+42,GEOR16,zwykly,bitmap);
+			blitAt(CGI->townh->resources->ourImages[cn].bitmap,(bitmap->w/2-row2w/2)+77*it++ - 308,ah,bitmap);
+		}
+		if(it==4)
+			ah+=75;
+	}
+	buy = new AdventureMapButton<CBuildWindow>("","",&CBuildWindow::Buy,pos.x+45,pos.y+446,"IBUY30.DEF",this,true,NULL,false);
+	cancel = new AdventureMapButton<CBuildWindow>("","",&CBuildWindow::close,pos.x+290,pos.y+445,"ICANCEL.DEF",this,true,NULL,false);
+	activate();
 }
 CHallInterface::CBuildWindow::~CBuildWindow()
 {
+	SDL_FreeSurface(bitmap);
+	delete buy;
+	delete cancel;
 }

+ 6 - 2
CCastleInterface.h

@@ -75,7 +75,7 @@ public:
 	class CBuildingBox : public Hoverable, public ClickableL, public ClickableR
 	{
 	public:
-		int ID;
+		int BID;
 		int state;// 0 - no more than one capitol, 1 - lack of water, 2 - forbidden, 3 - Add another level to Mage Guild, 4 - already built, 5 - cannot build, 6 - cannot afford, 7 - build
 		//(-1) - forbidden in this town, 0 - possible, 1 - lack of res, 2 - requirements/buildings per turn limit, (3) - already exists
 		void hover(bool on);
@@ -89,16 +89,20 @@ public:
 		~CBuildingBox();
 	};
 
-	class CBuildWindow: public IShowable, public CIntObject
+	class CBuildWindow: public IShowable, public ClickableR
 	{
 	public: 
 		int tid, bid, state; //town id, building id, state
 		bool mode; // 0 - normal (with buttons), 1 - r-click popup
 		SDL_Surface * bitmap; //main window bitmap, with blitted res/text, without buttons/subtitle in "statusbar"
+		AdventureMapButton<CBuildWindow> *buy, *cancel;
 
 		void activate();
 		void deactivate();
+		void clickRight (tribool down);
 		void show(SDL_Surface * to=NULL);
+		void Buy();
+		void close();
 		CBuildWindow(int Tid, int Bid, int State, bool Mode);
 		~CBuildWindow();
 	};

+ 7 - 0
CPlayerInterface.cpp

@@ -729,6 +729,13 @@ ClickableR::ClickableR()
 {
 	pressedR=false;
 }
+void ClickableR::clickRight(tribool down)
+{
+	if (down)
+		pressedR=true;
+	else
+		pressedR=false;
+}
 void ClickableR::activate()
 {
 	LOCPLINT->rclickable.push_back(this);

+ 1 - 1
config/requirements.txt

@@ -1,4 +1,4 @@
-2
+5
 0
 1 0
 2 1

+ 1 - 1
hch/CBuildingHandler.cpp

@@ -166,7 +166,7 @@ void CBuildingHandler::loadBuildings()
 					it = last + 1;
 					ss >> last;
 					(hall[tid].second)[j][box].push_back(last);
-					areboxes = it; //wyzeruje jak nie znajdzie kolejnej zpasji = koniec linii
+					areboxes = it; //wyzeruje jak nie znajdzie kolejnej spacji = koniec linii
 					if(!it)
 						it = seppos+1;
 				}

+ 2 - 2
hch/CLodHandler.h

@@ -25,8 +25,8 @@ namespace NLoadHandlerHelp
 struct Entry
 {
 	unsigned char name[12], //filename
-		hlam_1[4], //
-		hlam_2[4]; //
+		hlam_1[4], //???
+		hlam_2[4]; //probably type of file
 	std::string nameStr;
 	int offset, //from beginning
 		realSize, //size without compression

+ 2 - 1
hch/CTownHandler.cpp

@@ -7,6 +7,7 @@
 CTownHandler::CTownHandler()
 {
 	smallIcons = CGI->spriteh->giveDef("ITPA.DEF");
+	resources = CGI->spriteh->giveDef("RESOURCE.DEF");
 }
 CTownHandler::~CTownHandler()
 {
@@ -236,7 +237,7 @@ void CTownHandler::loadNames()
 				of >> town;
 				while(true)
 				{
-					of.getline(bufname,75);
+					of.getline(bufname,75);if(!(*bufname))of.getline(bufname,75);
 					std::istringstream ifs(bufname);
 					ifs >> build;
 					if(build<0)

+ 1 - 1
hch/CTownHandler.h

@@ -39,8 +39,8 @@ struct Structure
 
 class CTownHandler
 {
-	CDefHandler * smallIcons;
 public:
+	CDefHandler * smallIcons, *resources; //resources 32x32
 	CTownHandler();
 	~CTownHandler();
 	std::vector<CTown> towns;