36
40
#define DEFAULT_ARMY_TILE_SIZE 40
37
41
Armyset::Armyset(guint32 id, std::string name)
38
: d_id(id), d_name(name), d_dir(""), d_tilesize(DEFAULT_ARMY_TILE_SIZE),
39
d_ship(0), d_shipmask(0), d_standard(0), d_standard_mask(0),
40
private_collection(true)
42
: d_id(id), d_name(name), d_copyright(""), d_license(""), d_subdir(""),
43
d_tilesize(DEFAULT_ARMY_TILE_SIZE), d_ship(0), d_shipmask(0),
44
d_standard(0), d_standard_mask(0), d_bag(0)
47
d_stackship_name = "";
44
Armyset::Armyset(XML_Helper *helper, bool from_private_collection)
45
: d_id(0), d_name(""), d_dir(""), d_tilesize(DEFAULT_ARMY_TILE_SIZE),
46
d_ship(0), d_shipmask(0), d_standard(0), d_standard_mask(0)
51
Armyset::Armyset(XML_Helper *helper, std::string directory)
52
: d_id(0), d_name(""), d_copyright(""), d_license(""), d_subdir(""),
53
d_tilesize(DEFAULT_ARMY_TILE_SIZE), d_ship(0), d_shipmask(0),
54
d_standard(0), d_standard_mask(0), d_bag(0)
48
private_collection = from_private_collection;
57
d_stackship_name = "";
59
setDirectory(directory);
49
60
helper->getData(d_id, "id");
50
61
helper->getData(d_name, "name");
62
helper->getData(d_copyright, "copyright");
63
helper->getData(d_license, "license");
51
64
helper->getData(d_tilesize, "tilesize");
52
65
helper->getData(d_stackship_name, "stackship");
53
66
helper->getData(d_standard_name, "plantedstandard");
67
helper->getData(d_bag_name, "bag");
54
68
helper->registerTag(ArmyProto::d_tag,
55
69
sigc::mem_fun((*this), &Armyset::loadArmyProto));
58
72
Armyset::~Armyset()
74
uninstantiateImages();
60
75
for (iterator it = begin(); it != end(); it++)
64
79
bool Armyset::loadArmyProto(string tag, XML_Helper* helper)
97
ArmyProto * Armyset::lookupArmyByType(guint32 army_type_id)
99
for (iterator it = begin(); it != end(); it++)
115
ArmyProto * Armyset::lookupSimilarArmy(ArmyProto *army) const
117
for (const_iterator it = begin(); it != end(); it++)
119
if ((*it)->getGender() == army->getGender() &&
120
(*it)->getStrength() == army->getStrength() &&
121
(*it)->getProduction() == army->getProduction() &&
122
(*it)->getArmyBonus() == army->getArmyBonus() &&
123
(*it)->getMoveBonus() == army->getMoveBonus() &&
124
(*it)->getMaxMoves() == army->getMaxMoves() &&
125
(*it)->getAwardable() == army->getAwardable() &&
126
(*it)->getDefendsRuins() == army->getDefendsRuins())
129
for (const_iterator it = begin(); it != end(); it++)
131
if ((*it)->getGender() == army->getGender() &&
132
(*it)->getStrength() == army->getStrength() &&
133
(*it)->getProduction() == army->getProduction() &&
134
(*it)->getArmyBonus() == army->getArmyBonus() &&
135
(*it)->getMoveBonus() == army->getMoveBonus() &&
136
(*it)->getMaxMoves() == army->getMaxMoves())
139
for (const_iterator it = begin(); it != end(); it++)
141
if ((*it)->getGender() == army->getGender() &&
142
(*it)->getStrength() == army->getStrength() &&
143
(*it)->getProduction() == army->getProduction() &&
144
(*it)->getMaxMoves() == army->getMaxMoves())
150
ArmyProto * Armyset::lookupArmyByGender(Hero::Gender gender) const
152
for (const_iterator it = begin(); it != end(); it++)
154
if ((*it)->getGender() == gender)
159
ArmyProto * Armyset::lookupArmyByStrengthAndTurns(guint32 str, guint32 turns) const
161
for (const_iterator it = begin(); it != end(); it++)
165
if ((*it)->getStrength() == str && (*it)->getProduction() == turns)
170
if ((*it)->getStrength() == str)
175
if ((*it)->getProduction() == turns)
182
ArmyProto * Armyset::lookupArmyByName(std::string name) const
184
for (const_iterator it = begin(); it != end(); it++)
186
if ((*it)->getName() == name)
192
ArmyProto * Armyset::lookupArmyByType(guint32 army_type_id) const
194
for (const_iterator it = begin(); it != end(); it++)
101
196
if ((*it)->getTypeId() == army_type_id)
422
void Armyset::instantiateImages()
424
for (iterator it = begin(); it != end(); it++)
425
(*it)->instantiateImages(this);
426
if (getShipImageName().empty() == false)
427
loadShipPic(getFile(getShipImageName()));
428
if (getStandardImageName().empty() == false)
429
loadStandardPic(getFile(getStandardImageName()));
430
if (getBagImageName().empty() == false)
431
loadBagPic(getFile(getBagImageName()));
434
void Armyset::uninstantiateImages()
436
for (iterator it = begin(); it != end(); it++)
437
(*it)->uninstantiateImages();
440
void Armyset::loadShipPic(std::string image_filename)
442
if (image_filename.empty() == true)
444
std::vector<PixMask*> half;
445
half = disassemble_row(image_filename, 2);
446
int size = getTileSize();
447
PixMask::scale(half[0], size, size);
448
PixMask::scale(half[1], size, size);
449
setShipImage(half[0]);
450
setShipMask(half[1]);
453
void Armyset::loadBagPic(std::string image_filename)
455
if (image_filename.empty() == true)
457
setBagPic(PixMask::create(image_filename));
459
void Armyset::loadStandardPic(std::string image_filename)
461
if (image_filename.empty() == true)
463
std::vector<PixMask*> half = disassemble_row(image_filename, 2);
464
int size = getTileSize();
465
PixMask::scale(half[0], size, size);
466
PixMask::scale(half[1], size, size);
467
setStandardPic(half[0]);
468
setStandardMask(half[1]);
471
std::string Armyset::getConfigurationFile()
473
return getDirectory() + d_subdir + file_extension;
476
std::list<std::string> Armyset::scanUserCollection()
478
return File::scanFiles(File::getUserArmysetDir(), file_extension);
481
std::list<std::string> Armyset::scanSystemCollection()
483
std::list<std::string> retlist = File::scanFiles(File::getArmysetDir(),
487
std::cerr << "Couldn't find any armysets (*" << file_extension <<
488
") in directories below: " << File::getArmysetDir() << std::endl;
489
std::cerr << "Please check the path settings in /etc/lordsawarrc or ~/.lordsawarrc" << std::endl;
490
std::cerr << "Exiting!" << std::endl;
497
void Armyset::switchArmysetForRuinKeeper(Army *army, const Armyset *armyset)
499
//do our best to change the armyset for the given ruin keeper.
501
//go find an equivalent type in the new armyset.
503
= Armysetlist::getInstance()->getArmyset(army->getOwner()->getArmyset());
504
ArmyProto *old_armyproto = old_armyset->lookupArmyByType(army->getTypeId());
505
const ArmyProto *new_armyproto = armyset->lookupArmyByType(army->getTypeId());
507
//try looking at the same id first
508
if (new_armyproto != NULL &&
509
old_armyproto->getName() == new_armyproto->getName() &&
510
old_armyproto->getDefendsRuins() == new_armyproto->getDefendsRuins())
512
army->morph(new_armyproto);
516
//try finding an army by the same name
517
new_armyproto = armyset->lookupArmyByName(old_armyproto->getName());
518
if (new_armyproto != NULL &&
519
old_armyproto->getDefendsRuins() == new_armyproto->getDefendsRuins())
521
army->morph(new_armyproto);
525
//failing that, any ruin keeper will do.
526
new_armyproto = armyset->getRandomRuinKeeper();
527
if (new_armyproto != NULL)
529
army->morph(new_armyproto);
535
void Armyset::switchArmyset(ArmyProdBase *army, const Armyset *armyset)
537
//do our best to change the armyset for the given armyprodbase.
539
//go find an equivalent type in the new armyset.
541
= Armysetlist::getInstance()->getArmyset(army->getArmyset());
542
ArmyProto *old_armyproto = old_armyset->lookupArmyByType(army->getTypeId());
543
ArmyProto *new_armyproto = armyset->lookupArmyByType(army->getTypeId());
545
//try looking at the same id first
546
if (new_armyproto != NULL &&
547
old_armyproto->getName() == new_armyproto->getName())
549
army->morph(new_armyproto);
553
//try finding an army by the same name
554
new_armyproto = armyset->lookupArmyByName(old_armyproto->getName());
555
if (new_armyproto != NULL)
557
army->morph(new_armyproto);
561
//failing that, any army with similar characteristics will do.
562
new_armyproto = armyset->lookupSimilarArmy(old_armyproto);
563
if (new_armyproto != NULL)
565
army->morph(new_armyproto);
569
//failing that, any army with the same strength and turns will do.
571
armyset->lookupArmyByStrengthAndTurns(old_armyproto->getStrength(),
572
old_armyproto->getProduction());
573
if (new_armyproto != NULL)
575
army->morph(new_armyproto);
579
//failing that, any army with the same strength will do.
581
armyset->lookupArmyByStrengthAndTurns(old_armyproto->getStrength(), 0);
582
if (new_armyproto != NULL)
584
army->morph(new_armyproto);
588
//failing that, any army with the same turns will do.
590
armyset->lookupArmyByStrengthAndTurns(0, old_armyproto->getProduction());
591
if (new_armyproto != NULL)
593
army->morph(new_armyproto);
597
//failing that, any army will do.
598
new_armyproto = armyset->lookupArmyByGender(old_armyproto->getGender());
599
if (new_armyproto != NULL)
601
army->morph(new_armyproto);
607
void Armyset::switchArmyset(Army *army, const Armyset *armyset)
609
//do our best to change the armyset for the given army.
611
//go find an equivalent type in the new armyset.
613
= Armysetlist::getInstance()->getArmyset(army->getOwner()->getArmyset());
614
ArmyProto *old_armyproto = old_armyset->lookupArmyByType(army->getTypeId());
615
ArmyProto *new_armyproto = armyset->lookupArmyByType(army->getTypeId());
617
//try looking at the same id first
618
if (new_armyproto != NULL &&
619
old_armyproto->getName() == new_armyproto->getName())
621
army->morph(new_armyproto);
625
//try finding an army by the same name
626
new_armyproto = armyset->lookupArmyByName(old_armyproto->getName());
627
if (new_armyproto != NULL)
629
army->morph(new_armyproto);
633
//failing that, an army with the same gender (heroes).
634
if (army->isHero() == true)
636
new_armyproto = armyset->lookupArmyByGender(old_armyproto->getGender());
637
if (new_armyproto != NULL)
639
army->morph(new_armyproto);
644
//failing that, any army with similar characteristics will do.
645
new_armyproto = armyset->lookupSimilarArmy(old_armyproto);
646
if (new_armyproto != NULL)
648
army->morph(new_armyproto);
652
//failing that, any army with the same strength and turns will do.
654
armyset->lookupArmyByStrengthAndTurns(old_armyproto->getStrength(),
655
old_armyproto->getProduction());
656
if (new_armyproto != NULL)
658
army->morph(new_armyproto);
662
//failing that, any army with the same strength will do.
664
armyset->lookupArmyByStrengthAndTurns(old_armyproto->getStrength(), 0);
665
if (new_armyproto != NULL)
667
army->morph(new_armyproto);
671
//failing that, any army with the same turns will do.
673
armyset->lookupArmyByStrengthAndTurns(0, old_armyproto->getProduction());
674
if (new_armyproto != NULL)
676
army->morph(new_armyproto);
680
//failing that, any army will do.
681
new_armyproto = armyset->lookupArmyByGender(old_armyproto->getGender());
682
if (new_armyproto != NULL)
684
army->morph(new_armyproto);
690
const ArmyProto * Armyset::getRandomRuinKeeper() const
692
// list all the army types that can be a sentinel.
693
std::vector<const ArmyProto*> occupants;
694
for (const_iterator i = begin(); i != end(); i++)
696
const ArmyProto *a = *i;
697
if (a->getDefendsRuins())
698
occupants.push_back(a);
701
if (!occupants.empty())
702
return occupants[rand() % occupants.size()];
707
const ArmyProto *Armyset::getRandomAwardableAlly() const
709
// list all the army types that can be given out as a reward.
710
std::vector<const ArmyProto*> allies;
711
for (const_iterator i = begin(); i != end(); i++)
713
const ArmyProto *a = *i;
714
if (a->getAwardable() == true)
719
return allies[rand() % allies.size()];