Skip to content

Commit

Permalink
craft slot min/max size implementation + code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
WarStalkeR committed Sep 30, 2023
1 parent e23a5a0 commit 07a9cd7
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 71 deletions.
3 changes: 2 additions & 1 deletion src/Basescape/BaseView.cpp
Expand Up @@ -564,7 +564,8 @@ void BaseView::draw()
Craft* slotCraft = craftSlotIt->craft;
const RuleCraft* slotRules = slotCraft->getRules();
Surface* frame = _texture->getFrame(slotCraft->getSkinSprite() + 33);
frame->blitNShade(this, craftSlotIt->x + slotRules->getBigOffsetX(), craftSlotIt->y + slotRules->getBigOffsetY());
frame->blitNShade(this, craftSlotIt->x + slotRules->getSizeOffsetX(),
craftSlotIt->y + slotRules->getSizeOffsetY());
fac->setCraftForDrawing(slotCraft);
}
++craftSlotIt;
Expand Down
10 changes: 5 additions & 5 deletions src/Basescape/CraftInfoState.cpp
Expand Up @@ -113,13 +113,13 @@ CraftInfoState::CraftInfoState(Base *base, size_t craftId) : _base(base), _craft
_txtWAmmo[i] = new Text(75, 24, x, y + 16);
}
const RuleCraft* craftRule = _craft->getRules();
if (craftRule->getBigOffsetX() != 0 || craftRule->getBigOffsetY() != 0)
if (craftRule->getSizeOffsetX() != 0 || craftRule->getSizeOffsetY() != 0)
{
_sprite = new InteractiveSurface(
32 + std::abs(craftRule->getBigOffsetX()) * 2,
40 + std::abs(craftRule->getBigOffsetY()) * 2,
144 + craftRule->getBigOffsetX(),
56 + craftRule->getBigOffsetY());
32 + std::abs(craftRule->getSizeOffsetX()) * 2,
40 + std::abs(craftRule->getSizeOffsetY()) * 2,
144 + craftRule->getSizeOffsetX(),
56 + craftRule->getSizeOffsetY());
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion src/Basescape/CraftWeaponsState.cpp
Expand Up @@ -195,7 +195,9 @@ void CraftWeaponsState::lstWeaponsClick(Action *)
return refSlot.craft == refCraft;
});
if (!((craftSlotIt != _base->getCraftSlots()->end() &&
(craftSlotIt->size == 0 || craftSlotIt->size >= newCraftSize)) ||
((craftSlotIt->min == 0 && craftSlotIt->max == 0) ||
(newCraftSize >= craftSlotIt->min &&
newCraftSize <= craftSlotIt->max))) ||
_base->getFreeCraftSlots(newCraftSize) > 0))
allowChange = false;
if (classChanged && !classChangeAllowed)
Expand Down
10 changes: 4 additions & 6 deletions src/Mod/RuleBaseFacility.cpp
Expand Up @@ -41,7 +41,7 @@ RuleBaseFacility::RuleBaseFacility(const std::string &type, int listOrder) :
_lift(false), _hyper(false), _mind(false), _grav(false), _mindPower(1),
_size(1), _buildCost(0), _refundValue(0), _buildTime(0), _monthlyCost(0),
_storage(0), _personnel(0), _aliens(0), _crafts(0), _labs(0), _workshops(0), _psiLabs(0),
_spriteEnabled(false), _craftsHidden(false), _craftOptions({Position(0, 0, 0)}),
_spriteEnabled(false), _craftsHidden(false), _craftOptions(),
_sightRange(0), _sightChance(0), _radarRange(0), _radarChance(0),
_defense(0), _hitRatio(0), _fireSound(0), _hitSound(0), _placeSound(-1), _ammoNeeded(1), _listOrder(listOrder),
_trainingRooms(0), _maxAllowedPerBase(0), _sickBayAbsoluteBonus(0.0f), _sickBayRelativeBonus(0.0f),
Expand Down Expand Up @@ -101,7 +101,7 @@ void RuleBaseFacility::load(const YAML::Node &node, Mod *mod)

_spriteEnabled = node["spriteEnabled"].as<bool>(_spriteEnabled);
_craftsHidden = node["craftsHidden"].as<bool>(_craftsHidden);
_craftOptions = node["craftOptions"].as<std::vector<Position>>(_craftOptions);
_craftOptions = node["craftOptions"].as<std::vector<CraftOption>>(_craftOptions);

_sightRange = node["sightRange"].as<int>(_sightRange);
_sightChance = node["sightChance"].as<int>(_sightChance);
Expand Down Expand Up @@ -475,11 +475,9 @@ bool RuleBaseFacility::getCraftsHidden() const

/**
* Gets the list of craft placement and limit options.
* X - horizontal render offset, Y - vertical render offset,
* Z - hangar slot craft size limit. If 0, size is ignored.
* @return the list of options as X/Y/Z positions.
* @return the list of CraftOption entries.
*/
const std::vector<Position> &RuleBaseFacility::getCraftOptions() const
const std::vector<CraftOption> &RuleBaseFacility::getCraftOptions() const
{
return _craftOptions;
}
Expand Down
70 changes: 68 additions & 2 deletions src/Mod/RuleBaseFacility.h
Expand Up @@ -34,6 +34,38 @@ class RuleItem;
struct VerticalLevel;
enum BasePlacementErrors : int;

struct CraftOption
{
/// Horizontal offset for rendering craft in the facility.
int x = 0;
/// Vertical offset for rendering craft in the facility.
int y = 0;
/// Minimum size of craft that can be housed in the slot.
int min = 0;
/// Maximum size of craft that can be housed in the slot.
int max = 0;
/// Is craft hidden or rendered in the base in the slot.
bool hide = false;
/// Constructor with default parameters. Needed for YAML.
CraftOption()
{
hide = false;
min = 0;
max = 0;
x = 0;
y = 0;
}
/// Constructor that allows to define facility craft slots.
CraftOption(int xOffset, int yOffset, int minSize, int maxSize, bool isHidden)
{
hide = isHidden;
min = minSize;
max = maxSize;
x = xOffset;
y = yOffset;
}
};

/**
* Represents a specific type of base facility.
* Contains constant info about a facility like
Expand All @@ -58,7 +90,7 @@ class RuleBaseFacility
std::map<std::string, std::pair<int, int> > _buildCostItems;
int _storage, _personnel, _aliens, _crafts, _labs, _workshops, _psiLabs;
bool _spriteEnabled, _craftsHidden;
std::vector<Position> _craftOptions;
std::vector<CraftOption> _craftOptions;
int _sightRange, _sightChance;
int _radarRange, _radarChance, _defense, _hitRatio, _fireSound, _hitSound, _placeSound;
int _ammoNeeded;
Expand Down Expand Up @@ -156,7 +188,7 @@ class RuleBaseFacility
/// Gets if facility's crafts are hidden or not.
bool getCraftsHidden() const;
/// Gets a list of which tiles are used to place items stored in this facility
const std::vector<Position>& getCraftOptions() const;
const std::vector<CraftOption>& getCraftOptions() const;
/// Gets the facility's sight range.
int getSightRange() const { return _sightRange; }
/// Gets the facility's alien base detection chance.
Expand Down Expand Up @@ -218,3 +250,37 @@ class RuleBaseFacility
};

}

namespace YAML
{
template<>
struct convert<OpenXcom::CraftOption>
{
static Node encode(const OpenXcom::CraftOption& crOpt)
{
Node node;
node.SetStyle(EmitterStyle::Flow);
node.push_back(crOpt.x);
node.push_back(crOpt.y);
node.push_back(crOpt.min);
node.push_back(crOpt.max);
node.push_back(crOpt.hide);
return node;
}

static bool decode(const Node& node, OpenXcom::CraftOption& crOpt)
{
if (!node.IsSequence() || node.size() != 5)
return false;

crOpt.x = node[0].as<int>();
crOpt.y = node[1].as<int>();
crOpt.min = node[2].as<int>();
crOpt.max = node[3].as<int>();
crOpt.hide = node[4].as<bool>();

if (crOpt.min > crOpt.max) return false;
return true;
}
};
}
23 changes: 20 additions & 3 deletions src/Mod/RuleCraft.cpp
Expand Up @@ -38,7 +38,7 @@ RuleCraft::RuleCraft(const std::string &type, int listOrder) :
_maxSmallSoldiers(-1), _maxLargeSoldiers(-1), _maxSmallVehicles(-1), _maxLargeVehicles(-1),
_maxSmallUnits(-1), _maxLargeUnits(-1), _maxSoldiers(-1), _maxVehicles(-1), _maxUnitsLimit(-1),
_monthlyBuyLimit(0), _costBuy(0), _costRent(0), _costSell(0), _repairRate(1), _refuelRate(1),
_transferTime(24), _score(0), _battlescapeTerrainData(0), _maxSkinIndex(0), _bigOffsetX(0), _bigOffsetY(0),
_transferTime(24), _score(0), _battlescapeTerrainData(0), _maxSkinIndex(0), _spriteSize(32, 40),
_keepCraftAfterFailedMission(false), _allowLanding(true), _spacecraft(false), _notifyWhenRefueled(false), _autoPatrol(false), _undetectable(false),
_listOrder(listOrder), _maxItems(0), _maxAltitude(-1), _maxStorageSpace(0.0), _stats(),
_shieldRechargeAtBase(1000),
Expand Down Expand Up @@ -139,8 +139,7 @@ void RuleCraft::load(const YAML::Node &node, Mod *mod, const ModScript &parsers)
_craftInventoryTile = craftInventoryTile.as<std::vector<int> >(_craftInventoryTile);
}
_maxSkinIndex = node["maxSkinIndex"].as<int>(_maxSkinIndex);
_bigOffsetX = node["bigOffsetX"].as<int>(_bigOffsetX);
_bigOffsetY = node["bigOffsetY"].as<int>(_bigOffsetY);
_spriteSize = node["spriteSize"].as<std::pair<int, int>>(_spriteSize);
_deployment = node["deployment"].as< RuleCraftDeployment >(_deployment);
_keepCraftAfterFailedMission = node["keepCraftAfterFailedMission"].as<bool>(_keepCraftAfterFailedMission);
_allowLanding = node["allowLanding"].as<bool>(_allowLanding);
Expand Down Expand Up @@ -273,6 +272,24 @@ int RuleCraft::getCraftSize() const
return _stats.craftSize;
}

/**
* Gets the craft's sprite horizontal offset.
* @return The offset in integer value.
*/
int RuleCraft::getSizeOffsetX() const
{
return (32 - _spriteSize.first) / 2;
}

/**
* Gets the craft's sprite vertical offset.
* @return The offset in integer value.
*/
int RuleCraft::getSizeOffsetY() const
{
return (40 - _spriteSize.second) / 2;
}

/**
* Gets the maximum fuel the craft can contain.
* @return The fuel amount.
Expand Down
6 changes: 3 additions & 3 deletions src/Mod/RuleCraft.h
Expand Up @@ -193,7 +193,7 @@ class RuleCraft
int _repairRate, _refuelRate, _transferTime, _score;
RuleTerrain *_battlescapeTerrainData;
int _maxSkinIndex;
int _bigOffsetX, _bigOffsetY;
std::pair<int, int> _spriteSize;
bool _keepCraftAfterFailedMission, _allowLanding, _spacecraft, _notifyWhenRefueled, _autoPatrol, _undetectable;
int _listOrder, _maxItems, _maxAltitude;
double _maxStorageSpace;
Expand Down Expand Up @@ -237,9 +237,9 @@ class RuleCraft
/// Gets the craft's hull size.
int getCraftSize() const;
/// Gets the craft's sprite horizontal offset.
int getBigOffsetX() const { return _bigOffsetX; }
int getSizeOffsetX() const;
/// Gets the craft's sprite vertical offset.
int getBigOffsetY() const { return _bigOffsetY; }
int getSizeOffsetY() const;
/// Gets the craft's maximum fuel.
int getMaxFuel() const;
/// Gets the craft's maximum damage.
Expand Down

0 comments on commit 07a9cd7

Please sign in to comment.