1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-19 12:02:24 +02:00

Implemented artifact placement rules for advMap objects, but not for the random artifacts placed via editor. (feature #119)

This commit is contained in:
DjWarmonger 2009-10-24 19:21:32 +00:00
parent 21f574f5ee
commit 1683a419a3
5 changed files with 120 additions and 77 deletions

View File

@ -1201,6 +1201,8 @@ void CGHeroInstance::recreateArtBonuses()
void CGDwelling::initObj() void CGDwelling::initObj()
{ {
if (getOwner() != 255)
cb->gameState()->players[getOwner()].dwellings.push_back (this);
switch(ID) switch(ID)
{ {
case 17: case 17:
@ -1272,7 +1274,14 @@ void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
} }
if(h->tempOwner != tempOwner) if(h->tempOwner != tempOwner)
{
std::vector<CGDwelling *>* dwellings = &(cb->gameState()->players[tempOwner].dwellings);
dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
cb->setOwner(id, h->tempOwner); cb->setOwner(id, h->tempOwner);
dwellings = &(cb->gameState()->players[h->tempOwner].dwellings);
//dwellings->push_back (this);
//cb->gameState()->players[getOwner()].dwellings.push_back (this);
}
BlockingDialog bd; BlockingDialog bd;
bd.player = h->tempOwner; bd.player = h->tempOwner;
@ -2836,7 +2845,7 @@ void CGPickable::initObj()
else else
{ {
val1 = 1000; val1 = 1000;
val2 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id; val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
type = 1; type = 1;
} }
} }
@ -2845,13 +2854,13 @@ void CGPickable::initObj()
{ {
int hlp = ran()%100; int hlp = ran()%100;
if(hlp < 55) if(hlp < 55)
val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id; val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
else if(hlp < 75) else if(hlp < 75)
val1 = VLC->arth->minors[ran()%VLC->arth->minors.size()]->id; val1 = cb->getRandomArt (CArtifact::ART_MINOR);
else if(hlp < 95) else if(hlp < 95)
val1 = VLC->arth->majors[ran()%VLC->arth->majors.size()]->id; val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
else else
val1 = VLC->arth->relics[ran()%VLC->arth->relics.size()]->id; val1 = cb->getRandomArt (CArtifact::ART_RELIC);
} }
break; break;
case 101: //treasure chest case 101: //treasure chest
@ -2860,7 +2869,7 @@ void CGPickable::initObj()
if(hlp >= 95) if(hlp >= 95)
{ {
type = 1; type = 1;
val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id; val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
return; return;
} }
else if (hlp >= 65) else if (hlp >= 65)
@ -3902,9 +3911,7 @@ void CGOnceVisitable::initObj()
if(hlp < 20) if(hlp < 20)
{ {
artOrRes = 1; artOrRes = 1;
std::vector<CArtifact*> arts; bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
bonusType = arts[ran() % arts.size()]->id;
} }
else else
{ {
@ -3925,19 +3932,15 @@ void CGOnceVisitable::initObj()
{ {
artOrRes = 1; artOrRes = 1;
std::vector<CArtifact*> arts;
int hlp = ran()%100; int hlp = ran()%100;
if(hlp < 30) if(hlp < 30)
cb->getAllowed(arts,CArtifact::ART_TREASURE); bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
else if(hlp < 80) else if(hlp < 80)
cb->getAllowed(arts,CArtifact::ART_MINOR); bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
else if(hlp < 95) else if(hlp < 95)
cb->getAllowed(arts,CArtifact::ART_MAJOR); bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
else else
cb->getAllowed(arts,CArtifact::ART_RELIC); bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
bonusType = arts[ran() % arts.size()]->id;
} }
break; break;
@ -3952,9 +3955,7 @@ void CGOnceVisitable::initObj()
else if(hlp < 50) //minor or treasure art else if(hlp < 50) //minor or treasure art
{ {
artOrRes = 1; artOrRes = 1;
std::vector<CArtifact*> arts; bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
bonusType = arts[ran() % arts.size()]->id;
} }
else //2 - 5 of non-gold resource else //2 - 5 of non-gold resource
{ {
@ -4031,7 +4032,7 @@ const std::string & CBank::getHoverText() const
hoverName += " " + VLC->generaltexth->allTexts[353]; hoverName += " " + VLC->generaltexth->allTexts[353];
return hoverName; return hoverName;
} }
void CBank::reset(ui16 var1, ui16 var2) //prevents desync void CBank::reset(ui16 var1) //prevents desync
{ {
ui8 chance = 0; ui8 chance = 0;
for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++) for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
@ -4043,35 +4044,32 @@ void CBank::reset(ui16 var1, ui16 var2) //prevents desync
} }
} }
artifacts.clear(); artifacts.clear();
std::vector<CArtifact*>::iterator index; }
void CBank::initialize() const
{
cb->setObjProperty (id, 14, ran()); //synchronous reset
for (ui8 i = 0; i <= 3; i++) for (ui8 i = 0; i <= 3; i++)
{ {
std::vector<CArtifact*> arts; //to avoid addition of different tiers for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
{
switch (i) switch (i)
{ {
case 0: case 0:
cb->getAllowed (arts, CArtifact::ART_TREASURE); cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
break; break;
case 1: case 1:
cb->getAllowed (arts, CArtifact::ART_MINOR); cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
break; break;
case 2: case 2:
cb->getAllowed (arts, CArtifact::ART_MAJOR); cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
break; break;
case 3: case 3:
cb->getAllowed (arts, CArtifact::ART_RELIC); cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
break; break;
} }
for (ui8 n = 0; n < bc->artifacts[i]; n++)
{
index = arts.begin() + var2 % arts.size();
artifacts.push_back ((*index)->id);
arts.erase(index);
var2 *= (var1 + n * i); //almost like random
} }
} }
} }
void CBank::setPropertyDer (ui8 what, ui32 val) void CBank::setPropertyDer (ui8 what, ui32 val)
/// random values are passed as arguments and processed identically on all clients /// random values are passed as arguments and processed identically on all clients
@ -4091,7 +4089,7 @@ void CBank::setPropertyDer (ui8 what, ui32 val)
bc = &VLC->objh->banksInfo[index][val]; bc = &VLC->objh->banksInfo[index][val];
break; break;
case 14: case 14:
reset (val%100, val); reset (val%100);
break; break;
case 15: case 15:
bc = NULL; bc = NULL;
@ -4100,6 +4098,7 @@ void CBank::setPropertyDer (ui8 what, ui32 val)
artifacts.clear(); artifacts.clear();
break; break;
case 17: //set ArmedInstance army case 17: //set ArmedInstance army
{
int upgraded = 0; int upgraded = 0;
if (val%100 < bc->upgradeChance) //once again anti-desync if (val%100 < bc->upgradeChance) //once again anti-desync
upgraded = 1; upgraded = 1;
@ -4123,6 +4122,10 @@ void CBank::setPropertyDer (ui8 what, ui32 val)
tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found"; tlog1 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
return; return;
} }
}
break;
case 18: //add Artifact
artifacts.push_back (val);
break; break;
} }
} }
@ -4132,10 +4135,10 @@ void CBank::newTurn() const
if (bc == NULL) if (bc == NULL)
{ {
if (cb->getDate(0) == 1) if (cb->getDate(0) == 1)
cb->setObjProperty (id, 14, ran()); //initialize on first day initialize(); //initialize on first day
else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
{ {
cb->setObjProperty (id, 14, ran()); //reset initialize();
cb->setObjProperty (id, 11, 0); //daycounter 0 cb->setObjProperty (id, 11, 0); //daycounter 0
if (ID == 24 && cb->getDate(0) > 1) if (ID == 24 && cb->getDate(0) > 1)
cb->setObjProperty (id, 16, 0); //derelict ships are usable only once cb->setObjProperty (id, 16, 0); //derelict ships are usable only once
@ -4311,7 +4314,7 @@ void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) cons
cb->setObjProperty (id, 15, 0); //bc = NULL cb->setObjProperty (id, 15, 0); //bc = NULL
} }
else //in case of defeat else //in case of defeat
cb->setObjProperty (id, 14, ran()); //reset initialize();
} }
void CGPyramid::initObj() void CGPyramid::initObj()

View File

@ -918,7 +918,8 @@ class DLL_EXPORT CBank : public CArmedInstance
void initObj(); void initObj();
const std::string & getHoverText() const; const std::string & getHoverText() const;
void setPropertyDer (ui8 what, ui32 val); void setPropertyDer (ui8 what, ui32 val);
void reset(ui16 var1, ui16 var2); void initialize() const;
void reset(ui16 var1);
void newTurn() const; void newTurn() const;
virtual void onHeroVisit (const CGHeroInstance * h) const; virtual void onHeroVisit (const CGHeroInstance * h) const;
virtual void fightGuards (const CGHeroInstance *h, ui32 accept) const; virtual void fightGuards (const CGHeroInstance *h, ui32 accept) const;

View File

@ -35,6 +35,7 @@ class CStack;
class CGHeroInstance; class CGHeroInstance;
class CGTownInstance; class CGTownInstance;
class CArmedInstance; class CArmedInstance;
class CGDwelling;
class CGDefInfo; class CGDefInfo;
class CObjectScript; class CObjectScript;
class CGObjectInstance; class CGObjectInstance;
@ -66,6 +67,7 @@ public:
std::vector<CGHeroInstance *> heroes; std::vector<CGHeroInstance *> heroes;
std::vector<CGTownInstance *> towns; std::vector<CGTownInstance *> towns;
std::vector<CGHeroInstance *> availableHeroes; //heroes available in taverns std::vector<CGHeroInstance *> availableHeroes; //heroes available in taverns
std::vector<CGDwelling *> dwellings; //used for town growth
PlayerState(); PlayerState();

View File

@ -18,6 +18,8 @@
* *
*/ */
extern boost::rand48 ran;
CGameState *const IGameCallback::gameState () CGameState *const IGameCallback::gameState ()
{ {
return gs; return gs;
@ -167,8 +169,16 @@ bool IGameCallback::isAllowed( int type, int id )
} }
} }
void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts) void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts, int flag)
{ {
if (!(VLC->arth->*arts).size()) //restock avaliable arts
{
for (int i = 0; i < VLC->arth->artifacts.size(); i++)
{
if (VLC->arth->artifacts[i].aClass == flag)
(VLC->arth->*arts).push_back(&(VLC->arth->artifacts[i]));
}
}
for (int i = 0; i < (VLC->arth->*arts).size(); i++) for (int i = 0; i < (VLC->arth->*arts).size(); i++)
{ {
CArtifact *art = (VLC->arth->*arts)[i]; CArtifact *art = (VLC->arth->*arts)[i];
@ -182,13 +192,38 @@ void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CAr
void IGameCallback::getAllowed(std::vector<CArtifact*> &out, int flags) void IGameCallback::getAllowed(std::vector<CArtifact*> &out, int flags)
{ {
if(flags & CArtifact::ART_TREASURE) if(flags & CArtifact::ART_TREASURE)
getAllowedArts(out,&CArtHandler::treasures); getAllowedArts(out,&CArtHandler::treasures, CArtifact::ART_TREASURE);
if(flags & CArtifact::ART_MINOR) if(flags & CArtifact::ART_MINOR)
getAllowedArts(out,&CArtHandler::minors); getAllowedArts(out,&CArtHandler::minors, CArtifact::ART_MINOR);
if(flags & CArtifact::ART_MAJOR) if(flags & CArtifact::ART_MAJOR)
getAllowedArts(out,&CArtHandler::majors); getAllowedArts(out,&CArtHandler::majors, CArtifact::ART_MAJOR);
if(flags & CArtifact::ART_RELIC) if(flags & CArtifact::ART_RELIC)
getAllowedArts(out,&CArtHandler::relics); getAllowedArts(out,&CArtHandler::relics, CArtifact::ART_RELIC);
}
ui16 IGameCallback::getRandomArt (int flags)
{
std::vector<CArtifact*> out;
getAllowed(out, flags);
CArtifact *art = out[ran() % out.size()];
std::vector<CArtifact*>* ptr;
switch (art->aClass)
{
case CArtifact::ART_TREASURE:
ptr = &VLC->arth->treasures;
break;
case CArtifact::ART_MINOR:
ptr = &VLC->arth->minors;
break;
case CArtifact::ART_MAJOR:
ptr = &VLC->arth->majors;
break;
case CArtifact::ART_RELIC:
ptr = &VLC->arth->relics;
break;
}
ptr->erase (std::find(ptr->begin(), ptr->end(), art)); //remove the artifact from avaliable list
return art->id;
} }
void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level) void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)

View File

@ -4,6 +4,7 @@
#include "../global.h" #include "../global.h"
#include <vector> #include <vector>
#include <set> #include <set>
#include <boost/random/linear_congruential.hpp>
#include "../client/FunctionList.h" #include "../client/FunctionList.h"
/* /*
@ -57,7 +58,8 @@ public:
virtual void getTilesInRange(std::set<int3> &tiles, int3 pos, int radious, int player=-1, int mode=0); //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 - only unrevealed virtual void getTilesInRange(std::set<int3> &tiles, int3 pos, int radious, int player=-1, int mode=0); //mode 1 - only unrevealed tiles; mode 0 - all, mode -1 - only unrevealed
virtual void getAllTiles (std::set<int3> &tiles, int player=-1, int level=-1, int surface=0); //returns all tiles on given level (-1 - both levels, otherwise number of level); surface: 0 - land and water, 1 - only land, 2 - only water virtual void getAllTiles (std::set<int3> &tiles, int player=-1, int level=-1, int surface=0); //returns all tiles on given level (-1 - both levels, otherwise number of level); surface: 0 - land and water, 1 - only land, 2 - only water
virtual bool isAllowed(int type, int id); //type: 0 - spell; 1- artifact virtual bool isAllowed(int type, int id); //type: 0 - spell; 1- artifact
virtual void getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts); virtual ui16 getRandomArt (int flags);
virtual void getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts, int flag);
virtual void getAllowed(std::vector<CArtifact*> &out, int flags); //flags: bitfield uses EartClass virtual void getAllowed(std::vector<CArtifact*> &out, int flags); //flags: bitfield uses EartClass
virtual void getAllowedSpells(std::vector<ui16> &out, ui16 level); virtual void getAllowedSpells(std::vector<ui16> &out, ui16 level);
virtual int3 getMapSize(); //returns size of the map virtual int3 getMapSize(); //returns size of the map