From a271d41a2c33a14c3703b7a3f7476dfaecd98d2e Mon Sep 17 00:00:00 2001 From: jowen005 Date: Mon, 24 Aug 2026 23:49:46 -0400 Subject: [PATCH] Moved ammo variables to weapons configs --- Assets/Scripts/GameObjects/Units/unitData.lua | 56 ++++++++--------- player.h | 2 +- resourceRover.h | 3 +- unit.cpp | 47 ++------------ unit.h | 6 +- weapon.cpp | 61 ++++++++----------- weapon.h | 8 ++- 7 files changed, 69 insertions(+), 114 deletions(-) diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index f9f9d49..97fb8e7 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -130,6 +130,8 @@ units = { orderType = OrderType.ATTACK, rateOfFire = 100, ammoConsumption = 1, --Just a test + maxAmmo = 100, -- Just a test + ammo = 50, -- Just a test maxRange = 40, damage = 50, maxFireAngle = .1, @@ -183,8 +185,6 @@ units = { health = 500, buildTime = 600, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 50, -- Just a test size = {x = 6, y = 9.22, z = 2.42}, hitboxOffset = {x = 0, y = 4.5, z = 0}, lineOfSight = 25, @@ -210,6 +210,8 @@ units = { maxRange = 40, damage = 50, ammoConsumption = 1, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxFireAngle = .1, nodes = {{name = 'turret', rotationSpeed = 1, angleConstraints = {min = -1.5, max = 1.5}, vertical = true}}, fireFx = { @@ -262,8 +264,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 5.37, y = 6, z = 8.57}, hitboxOffset = {x = 0, y = 3.02, z = .16}, lineOfSight = 25, @@ -289,6 +289,8 @@ units = { maxRange = 40, damage = 50, ammoConsumption = 1, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxFireAngle = .1, nodes = {{name = 'turret', rotationSpeed = 1, angleConstraints = {min = -1.5, max = 1.5}, vertical = true}}, fireFx = { @@ -341,8 +343,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 3.25, y = 3.86, z = 3.86}, hitboxOffset = {x = 0, y = 4.27, z = .13}, lineOfSight = 25, @@ -367,6 +367,8 @@ units = { rateOfFire = 1000, damage = 900, ammoConsumption = 1, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 50, maxFireAngle = .1, nodes = {{name = 'TankASHead', rotationSpeed = .05, vertical = false}, {name = 'barell', rotationSpeed = .05, angleConstraints = {min = 0, max = .349}, vertical = true}}, @@ -403,8 +405,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 11.505, y = 10.3979, z = 17.1902}, hitboxOffset = {x = 0, y = 5.46, z = .86}, lineOfSight = 75, @@ -429,6 +429,8 @@ units = { rateOfFire = 1000, damage = 200, ammoConsumption = 1, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 50, maxFireAngle = .1, nodes = {{name = 'TankGun_AM', rotationSpeed = .05, vertical = false}, {name = 'barell', rotationSpeed = .05, angleConstraints = {min = -.2, max = .74}, vertical = true}}, @@ -465,8 +467,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 13.6, y = 6.79, z = 21.8}, hitboxOffset = {x = 0, y = 3.31, z = 0}, lineOfSight = 75, @@ -491,6 +491,8 @@ units = { rateOfFire = 1000, damage = 200, ammoConsumption = 1, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 50, maxFireAngle = .1, nodes = { @@ -530,8 +532,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 7.2, y = 10.3, z = 15}, hitboxOffset = {x = 0, y = 5.27, z = .26}, lineOfSight = 75, @@ -616,6 +616,8 @@ units = { rateOfFire = 2000, damage = 5000, ammoConsumption = 2, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 70, maxFireAngle = .1, nodes = { @@ -652,8 +654,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 10.4, y = 14, z = 12.9}, hitboxOffset = {x = 0, y = 7, z = 1.3}, lineOfSight = 90, @@ -678,6 +678,8 @@ units = { rateOfFire = 2000, damage = 5000, ammoConsumption = 2, + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 70, maxFireAngle = .1, nodes = { @@ -715,8 +717,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 6.8, y = 6.8, z = 15.2}, hitboxOffset = {x = 0, y = 1, z = 0}, lineOfSight = 30, @@ -1691,6 +1691,8 @@ units = { rateOfFire = 200, damage = 10, ammoConsumption = 1, --Test + maxAmmo = 100, -- Just a test + ammo = 100, -- Just a test maxRange = 50, nodes = {{name = 'rearAATurret', rotationSpeed = .05, vertical = false}, {name = 'rearAATurretBarrel', rotationSpeed = .05, vertical = true}}, fireFx = { @@ -1718,8 +1720,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 100, -- Just a test size = {x = 21.2, y = 26.3, z = 85.8}, hitboxOffset = {x = 0, y = 6.92, z = .66}, lineOfSight = 5, @@ -1790,6 +1790,8 @@ units = { rateOfFire = 200, damage = 10, ammoConsumption = 1, -- Test + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 50, nodes = { {name = 'aaTurret', rotationSpeed = .05, vertical = false}, @@ -1832,8 +1834,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 21.1, y = 22.6, z = 67.3}, hitboxOffset = {x = 0, y = 6.42, z = 3.2}, lineOfSight = 5, @@ -2470,6 +2470,8 @@ units = { projectile = {id = ProjectileId.TORPEDO, pos = {x = 0, y = -.27, z = 4.6}, rot = {w = 1, x = 0, y = 0, z = 0}}, damage = 200, ammoConsumption = 1, --Test + maxAmmo = 50, -- Just a test + ammo = 50, -- Just a test maxRange = 200, rateOfFire = 500, fireFx = { @@ -2487,8 +2489,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 50, -- Just a test - ammo = 50, -- Just a test hitboxOffset = {x = 0, y = 1.03, z = -1.41}, size = {x = 5.37, y = 7.66, z = 43}, lineOfSight = 0, @@ -2587,6 +2587,8 @@ units = { maxRange = 500, damage = 0, ammoConsumption = 1, --Test + maxAmmo = 100, -- Just a test + ammo = 50, -- Just a test nodes = { {name = 'turret', rotationSpeed = .05, vertical = false}, {name = 'barrel', rotationSpeed = .05, angleConstraints = {min = -.523, max = .523}, vertical = true}, @@ -2610,8 +2612,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 50, -- Just a test size = {x = 20.3, y = 12.5, z = 56.2}, hitboxOffset = {x = 0, y = 4.7, z = 0}, lineOfSight = 25, @@ -2636,6 +2636,8 @@ units = { rateOfFire = 1000, damage = 0, ammoConsumption = 1, --Test + maxAmmo = 100, -- Just a test + ammo = 100, -- Just a test maxRange = 25, maxFireAngle = .1, nodes = {{name = 'turret', rotationSpeed = .05, vertical = false}}, @@ -2655,8 +2657,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 100, -- Just a test size = {x = 7.3, y = 7.15, z = 37.9}, hitboxOffset = {x = 0, y = 1.13, z = 1.83}, lineOfSight = 25, @@ -3151,6 +3151,8 @@ units = { rateOfFire = 100, damage = 7, ammoConsumption = 2, --Test + maxAmmo = 100, -- Just a test + ammo = 80, -- Just a test maxRange = 100, maxFireAngle = .4, nodes = { @@ -3198,8 +3200,6 @@ units = { health = 500, buildTime = 1000, cost = 500, - maxAmmo = 100, -- Just a test - ammo = 80, -- Just a test size = {x = 5, y = 8, z = 5}, hitboxOffset = {x = 0, y = 4, z = 0}, lineOfSight = 55, diff --git a/player.h b/player.h index b0901f6..167df19 100755 --- a/player.h +++ b/player.h @@ -95,7 +95,7 @@ namespace battleship{ bool cpuPlayer = false; std::vector technologies; int luaPlayerId; - int resources[3]{0, 0, 0}; // This is the same as 'cargo' in resourceRover.h: capacity, loadRate, loadSpeed + int resources[3]{0, 0, 0}; // refined, wealth and research resources respectively int faction, difficulty, team; int vehiclesBuilt = 0, vehiclesDestroyed = 0, vehiclesLost = 0; int structuresBuilt = 0, structuresDestroyed = 0, structuresLost = 0; diff --git a/resourceRover.h b/resourceRover.h index eb59a5e..eec21af 100644 --- a/resourceRover.h +++ b/resourceRover.h @@ -18,8 +18,9 @@ namespace battleship{ inline int calcTotalLoad(){return cargo[0] + cargo[1] + cargo[2];} inline int getCapacity(){return capacity;} // Checks if enough time has passed since the last load to keep with the rover's loading rate and that it hasn't loaded long enough to reach capacity + // from unit to rover inline bool canLoad(){return vb01::getTime() - lastLoadTime > loadRate && calcTotalLoad() < capacity;} - // Checks if enough time has passed since the last load to keep with the rover's loading rate + // Checks if enough time has passed since the last unload to keep with the rover's loading rate, from rover to unit inline bool canUnload(int id){return vb01::getTime() - lastLoadTime > loadRate && cargo[id] > 0;} private: void initProperties(); diff --git a/unit.cpp b/unit.cpp index 81765ab..10ddaa1 100755 --- a/unit.cpp +++ b/unit.cpp @@ -125,18 +125,6 @@ namespace battleship{ buildableUnits.push_back(BuildableUnit(buTable["id"], game->isUnitUnlocked(currTechs, id) | (bool)buTable["buildable"])); } } - - // Some units may not have ammo so will we do a check of maxAmmo before assigning it - sol::optional maxAmmoOpt = unitTable["maxAmmo"]; - if(maxAmmoOpt != sol::nullopt) - { - maxAmmo = unitTable["maxAmmo"].get(); - // Units that have maxAmmo should also have ammo but just in case they don't 0 will be assigned - ammo = unitTable["ammo"].get_or(0); - } - - cout << "Max Ammo " << maxAmmo << endl; - cout << "Starting Ammo " << ammo << endl; } /// @brief Initialize the weapons for this unit @@ -619,36 +607,9 @@ namespace battleship{ orderLineDispTime = getTime(); - cout << "Current Ammo For Selected" << ammo << endl; + for(auto i: weapons) + { + cout << "Current ammo of selected unit's weapons " << i->getAmmo() << endl; + } } - - /// @brief Add the passed amount of ammo to the current ammo amount unless the resulting amount would exceed the max ammo for the weapon - /// @param ammoToAdd - void Unit::addAmmo(int ammoToAdd) - { - int newAmmoAmount = ammoToAdd + ammo; - if(newAmmoAmount > maxAmmo) - { - ammo = maxAmmo; - } - else - { - ammo = newAmmoAmount; - } - } - - /// @brief Reduce the amount of ammo for the unit by the passed amount - /// @param amoToLose - void Unit::reduceAmmo(int ammoToLose) - { - int newAmmoAmount = ammo - ammoToLose; - if(newAmmoAmount < 0) - { - ammo = 0; - } - else - { - ammo = newAmmoAmount; - } - } } diff --git a/unit.h b/unit.h index 42b7cd2..e78ed77 100755 --- a/unit.h +++ b/unit.h @@ -138,10 +138,7 @@ namespace battleship{ if(cond == Condition::EM_JAMMED) lastJamTime = vb01::getTime(); } - inline int getMaxAmmo(){return maxAmmo;} - inline int getAmmo(){return ammo;} - void addAmmo(int ammoToAdd); - void reduceAmmo(int ammoToLose); + void sortPassAmmo(int ammoAmount); // Future function for when the ammo is received by a unit to pass to the weapon private: void renderOrderLine(bool); void initWeapons(); @@ -155,7 +152,6 @@ namespace battleship{ vb01::Node *losLightNode = nullptr; bool vehicle, currOrderStarted = false; Condition condition = Condition::ABLE; - int maxAmmo, ammo; protected: bool alignToSurface = false; UnitType type; diff --git a/weapon.cpp b/weapon.cpp index b068925..019d1e0 100644 --- a/weapon.cpp +++ b/weapon.cpp @@ -59,21 +59,15 @@ namespace battleship{ if(fireFx) fxManager->addFx(fireFx); } - // If a weapon doesn't fire it will not have an ammo consumptiopn - sol::optional ammoConsumpOpt = weaponTable["ammoConsumption"]; - if(ammoConsumpOpt != sol::nullopt) + // Some weapons may not have ammo so will we do a check of maxAmmo before assigning it + sol::optional maxAmmoOpt = weaponTable["maxAmmo"]; + if(maxAmmoOpt != sol::nullopt) { - ammoConsumption = weaponTable["ammoConsumption"].get(); + maxAmmo = weaponTable["maxAmmo"].get(); + // Units that have maxAmmo should also have ammo but just in case they don't 0 will be assigned + ammo = weaponTable["ammo"].get_or(0); + ammoConsumption = weaponTable["ammoConsumption"].get_or(1); } - - // // Some weapons may not have ammo so will we do a check of maxAmmo before assigning it - // sol::optional maxAmmoOpt = weaponTable["maxAmmo"]; - // if(maxAmmoOpt != sol::nullopt) - // { - // maxAmmo = weaponTable["maxAmmo"].get(); - // // Units that have maxAmmo should also have ammo but just in case they don't 0 will be assigned - // ammo = weaponTable["ammo"].get_or(0); - // } } void Weapon::initTargetData(vector &targetVec, sol::table weaponTable, string tblKey, vector allValues){ @@ -289,7 +283,7 @@ namespace battleship{ if(fireFx) useFx(fireFx, targPos, true); - // If there is no projectile to fire at + // If there is no projectile to fire if(projId == -1){ sol::table weaponTbl = generateView()["units"][unit->getId() + 1]["weapons"][id + 1]; FxManager *fxManager = FxManager::getSingleton(); @@ -311,7 +305,7 @@ namespace battleship{ if(numFx > 0) useFx(fxManager->initFx(weaponTbl[fxKey], unit->getModel(), false), targPos, false); } } - // If there is a projectile to fire at + // If there is a projectile to fire else{ Quaternion r = projPar->localToGlobalOrientation(projRot); Vector3 p = projPar->localToGlobalPosition(projPos); @@ -319,9 +313,7 @@ namespace battleship{ } // Reduce the ammo by the amount the weapon consumes - unit->reduceAmmo(ammoConsumption); - cout << "Current Ammo Amount " << unit->getAmmo() << endl; - + reduceAmmo(ammoConsumption); lastFireTime = getTime(); } @@ -391,23 +383,24 @@ namespace battleship{ // Return whether the typeOrClass is in the target projectiles if the game object type is a projectile case GameObject::Type::PROJECTILE: return (find(targetProjectiles.begin(), targetProjectiles.end(), typeOrClass) != targetProjectiles.end()); + default: + return true; } - // By default return true - return true; + } + + /// @brief Add the passed amount of ammo to the current ammo amount unless the resulting amount would exceed the max ammo for the weapon + /// @param ammoToAdd + void Weapon::addAmmo(int ammoToAdd) + { + int newAmmoAmount = ammoToAdd + ammo; + ammo = std::clamp(newAmmoAmount, 0, maxAmmo); } - // /// @brief Add the passed amount of ammo to the current ammo amount unless the resulting amount would exceed the max ammo for the weapon - // /// @param ammoToAdd - // void Weapon::addAmmo(int ammoToAdd) - // { - // int newAmmoAmount = ammoToAdd + ammo; - // if(newAmmoAmount > maxAmmo) - // { - // ammo = maxAmmo; - // } - // else - // { - // ammo = newAmmoAmount; - // } - // } + /// @brief Reduce the amount of ammo for the unit by the passed amount + /// @param amoToLose + void Weapon::reduceAmmo(int ammoToLose) + { + int newAmmoAmount = ammo - ammoToLose; + ammo = std::clamp(newAmmoAmount, 0, maxAmmo); + } } diff --git a/weapon.h b/weapon.h index 1311b89..00e160d 100644 --- a/weapon.h +++ b/weapon.h @@ -51,13 +51,17 @@ namespace battleship{ inline Unit* getUnit(){return unit;} inline Type getType(){return type;} inline Order::TYPE getOrderType(){return orderType;} + inline int getMaxAmmo(){return maxAmmo;} + inline int getAmmo(){return ammo;} + void addAmmo(int ammoToAdd); + void reduceAmmo(int ammoToLose); private: Unit *unit = nullptr; Order::TYPE orderType; int id, projId = -1, damage = 0; std::vector targetUnits, targetProjectiles; float minRange = 0, maxRange, maxFireAngle; - int ammoConsumption = 1; // ammount of ammo firing the weapon consumes + int maxAmmo, ammo, ammoConsumption = 1; // ammount of ammo firing the weapon consumes vb01::s64 lastFireTime = 0; FxManager::Fx *fireFx = nullptr; vb01::Quaternion projRot; @@ -70,7 +74,7 @@ namespace battleship{ void initProjectileData(sol::table); void initNodes(sol::table); void useFx(FxManager::Fx*, vb01::Vector3, bool); - inline bool canFire(){return (vb01::getTime() - lastFireTime > rateOfFire && unit->getAmmo() >= ammoConsumption);} + inline bool canFire(){return (vb01::getTime() - lastFireTime > rateOfFire && ammo >= ammoConsumption);} protected: int rateOfFire; std::vector components;