From f70532788acee93ae63d4844551e465dc0a417ec Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 17 Jan 2026 17:40:25 +0200 Subject: [PATCH] dropping depth charges --- .../Projectiles/projectileData.lua | 20 +++++++-- Assets/Scripts/GameObjects/Units/unitData.lua | 37 +++++++++++----- CMakeLists.txt | 2 +- depthCharge.cpp | 13 ++++++ depthCharge.h | 15 +++++++ gameObjectFactory.cpp | 3 ++ map.cpp | 4 +- projectile.h | 2 +- unit.cpp | 32 ++++++++++++-- weapon.cpp | 42 ++++++++++++++++++- weapon.h | 5 ++- 11 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 depthCharge.cpp create mode 100644 depthCharge.h diff --git a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua index 9e0e007..d098787 100644 --- a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua +++ b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua @@ -1,5 +1,5 @@ -ProjectileId = {HE_SHELL = 0, EMP_SHELL = 1, CRUISE_MISSILE = 2, MISSILE = 3, TORPEDO = 4} -ProjectileClass = {SHELL = 0, CRUISE_MISSILE = 1, MISSILE = 2, TORPEDO = 3} +ProjectileId = {HE_SHELL = 0, EMP_SHELL = 1, CRUISE_MISSILE = 2, MISSILE = 3, TORPEDO = 4, DEPTH_CHARGE = 5} +ProjectileClass = {SHELL = 0, CRUISE_MISSILE = 1, MISSILE = 2, TORPEDO = 3, DEPTH_CHARGE = 4} G = 9.8 explosionVfx = { @@ -20,7 +20,7 @@ explosionSfx = { path = PATH .. 'Sounds/SFX/Explosions/explosion02.ogg', duration = 2500 } -Detonation = {EXPLOSION = 0, EMP = 1, CRYO = 2} +Detonation = {EXPLOSION = 0, EMP = 1, CRYO = 2, UNDERWATER = 3} projectiles = { { @@ -81,12 +81,24 @@ projectiles = { projectileClass = ProjectileClass.TORPEDO, size = {x = 1, y = 6, z = 1}, speed = .06, - explosion = {detonation = Detonation.EXPLOSION, damage = 20, radius = 3, fx = {explosionVfx, explosionSfx}}, + explosion = {detonation = Detonation.UNDERWATER, damage = 20, radius = 3, fx = {explosionVfx, explosionSfx}}, directHitDamage = 450, rayLength = 3.5, name = "Torpedo", basePath = PATH .. "Models/GameObjects/Projectiles/Torpedos/", meshPath = "torpedo.xml", albedoPath = 'torpedo.jpg' + }, + { + projectileClass = ProjectileClass.DEPTH_CHARGE, + size = {x = 5.3, y = 5.3, z = 5.8}, + speed = .06, + explosion = {detonation = Detonation.UNDERWATER, damage = 20, radius = 3, fx = {explosionVfx, explosionSfx}}, + directHitDamage = 450, + rayLength = 3.5, + name = "Depth charge", + basePath = PATH .. "Models/GameObjects/Projectiles/DepthCharges/", + meshPath = "depthCharge.xml", + albedoPath = 'depthCharge.jpg' } } diff --git a/Assets/Scripts/GameObjects/Units/unitData.lua b/Assets/Scripts/GameObjects/Units/unitData.lua index 0418482..771f9ea 100644 --- a/Assets/Scripts/GameObjects/Units/unitData.lua +++ b/Assets/Scripts/GameObjects/Units/unitData.lua @@ -200,6 +200,7 @@ units = { rateOfFire = 100, maxRange = 14, damage = 50, + maxFireAngle = .15, fireFx = { { vfx = true, @@ -260,7 +261,7 @@ units = { selectionSfx = PATH .. 'Sounds/Units/WarMechs/selection.ogg', speed = .3, destinationOffset = .1, - anglePrecision = .1, + anglePrecision = .01, maxTurnAngle = .1, garrisonCategory = 1, deathFx = {explosionVfx, explosionSfx}, @@ -623,7 +624,7 @@ units = { colorNodes = {'barrel'}, selectionSfx = PATH .. 'Sounds/Units/Tanks/selection.ogg', speed = .3, - destinationOffset = .1, + destinationOffset = .01, anglePrecision = .1, maxTurnAngle = .1, garrisonCategory = 2, @@ -1199,7 +1200,7 @@ units = { meshPath = 'acsCargoShip.xml', albedoPath = 'cargoship.jpg', selectionSfx = PATH .. 'Sounds/Units/Cargoships/selection.ogg', - speed = .1, + speed = .3, destinationOffset = .1, anglePrecision = .1, maxTurnAngle = .1, @@ -1234,7 +1235,7 @@ units = { meshPath = 'aincCargoShip.xml', albedoPath = 'cargoship.jpg', selectionSfx = PATH .. 'Sounds/Units/Cargoships/selection.ogg', - speed = .1, + speed = .3, destinationOffset = .1, anglePrecision = .1, maxTurnAngle = .1, @@ -1269,7 +1270,7 @@ units = { meshPath = 'erCargoShip.xml', albedoPath = 'cargoship.jpg', selectionSfx = PATH .. 'Sounds/Units/Cargoships/selection.ogg', - speed = .1, + speed = .3, destinationOffset = .1, anglePrecision = .1, maxTurnAngle = .1, @@ -1607,10 +1608,25 @@ units = { }, { weapons = { + { + orderType = OrderType.ATTACK, + rateOfFire = 2000, + damage = 10, + maxRange = 100, + targetUnits = {UnitType.UNDERWATER}, + targetProjectiles = {}, + fireDir = {x = 0, y = -1, z = 0}, + projectile = {id = ProjectileId.DEPTH_CHARGE, parent = 'hull', pos = {x = 0, y = -2, z = 0}, rot = {w = .707, x = .707, y = 0, z = 0}}, + fireFx = { + {vfx = false, duration = 50, path = PATH .. 'Sounds/Units/Submarines/fire.ogg'} + }, + hitFx = {} + }, { orderType = OrderType.ATTACK, rateOfFire = 200, damage = 10, + minRange = 10, maxRange = 100, nodes = { {name = 'FrontTurret', rotationSpeed = .05, angleConstraints = {min = -2.53, max = 2.53}, vertical = false}, @@ -1634,6 +1650,7 @@ units = { orderType = OrderType.ATTACK, rateOfFire = 200, damage = 10, + minRange = 10, maxRange = 100, nodes = { {name = 'RearTurret', rotationSpeed = .05, angleConstraints = {min = -2.53, max = 2.53}, vertical = false}, @@ -1663,14 +1680,14 @@ units = { cost = 500, size = {x = 6.93, y = 17.7, z = 76.1}, hitboxOffset = {x = 0, y = 4.77, z = 0}, - lineOfSight = 5, + lineOfSight = 100, colorNodes = {'FrontTurret', 'RearTurret'}, name = 'Anti sub cruiser', basePath = PATH .. vehiclePrefix .. 'Cruisers/', meshPath = 'acsAntiSubCruiser.xml', albedoPath = 'cruiser.jpg', selectionSfx = PATH .. 'Sounds/Units/Cruisers/selection.ogg', - speed = .1, + speed = .3, destinationOffset = .1, anglePrecision = .1, maxTurnAngle = .1, @@ -2120,7 +2137,7 @@ units = { cost = 500, size = {x = 9.29, y = 11.6, z = 32.9}, hitboxOffset = {x = 0, y = 1.8, z = -1.3}, - lineOfSight = 5, + lineOfSight = 0, name = 'Stealth submarine', colorNodes = {'Cube.001'}, basePath = PATH .. vehiclePrefix .. 'Submarines/', @@ -2159,7 +2176,7 @@ units = { cost = 500, hitboxOffset = {x = 0, y = 1.83, z = -3.46}, size = {x = 8.32, y = 12.1, z = 57.4}, - lineOfSight = 5, + lineOfSight = 0, name = 'Stealth submarine', colorNodes = {'Cube.001'}, basePath = PATH .. vehiclePrefix .. 'Submarines/', @@ -2198,7 +2215,7 @@ units = { cost = 500, hitboxOffset = {x = 0, y = 1.03, z = -1.41}, size = {x = 5.37, y = 7.66, z = 43}, - lineOfSight = 5, + lineOfSight = 0, name = 'Stealth submarine', colorNodes = {'Sphere.001'}, basePath = PATH .. vehiclePrefix .. 'Submarines/', diff --git a/CMakeLists.txt b/CMakeLists.txt index b5ee7c7..8ce9f21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp toggleDebugCommand.cpp) set(CORE gameManager.cpp game.cpp pathfinder.cpp environment.cpp fxManager.cpp defConfigs.cpp player.cpp trader.cpp ${CONSOLE} ${CONTROLLERS}) -set(PROJECTILES projectile.cpp missile.cpp cruiseMissile.cpp shell.cpp) +set(PROJECTILES projectile.cpp missile.cpp cruiseMissile.cpp shell.cpp depthCharge.cpp) set(VEHICLES vehicle.cpp submarine.cpp engineer.cpp resourceRover.cpp freezer.cpp) set(STRUCTURES structure.cpp factory.cpp pointDefense.cpp extractor.cpp researchStruct.cpp iceSheet.cpp) set(WEAPONS weapon.cpp cryoGun.cpp) diff --git a/depthCharge.cpp b/depthCharge.cpp new file mode 100644 index 0000000..d79a650 --- /dev/null +++ b/depthCharge.cpp @@ -0,0 +1,13 @@ +#include "depthCharge.h" + +namespace battleship{ + using namespace vb01; + + DepthCharge::DepthCharge(Unit *un, int id, Vector3 pos, Quaternion rot) : Projectile(un, id, pos, rot){} + + void DepthCharge::update(){ + Projectile::update(); + + if(!remove) checkUnitCollision(); + } +} diff --git a/depthCharge.h b/depthCharge.h new file mode 100644 index 0000000..bf14216 --- /dev/null +++ b/depthCharge.h @@ -0,0 +1,15 @@ +#ifndef DEPTH_CHARGE_H +#define DEPTH_CHARGE_H + +#include "projectile.h" + +namespace battleship{ + class DepthCharge : public Projectile{ + public: + DepthCharge(Unit*, int, vb01::Vector3, vb01::Quaternion); + void update(); + private: + }; +} + +#endif diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index f4550ee..226a043 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -14,6 +14,7 @@ #include "cruiseMissile.h" #include "iceSheet.h" #include "shell.h" +#include "depthCharge.h" #include "defConfigs.h" #include @@ -76,6 +77,8 @@ namespace battleship{ return new CruiseMissile(unit, id, targetPos, pos, rot); case ProjectileClass::MISSILE: return new Missile(unit, id, targetPos, pos, rot); + case ProjectileClass::DEPTH_CHARGE: + return new DepthCharge(unit, id, pos, rot); default: return new Projectile(unit, id, pos, rot); } diff --git a/map.cpp b/map.cpp index e70357a..eb5b813 100755 --- a/map.cpp +++ b/map.cpp @@ -722,7 +722,7 @@ namespace battleship{ int numHorCells = int(mapSize.x / CELL_SIZE.x), horId = -1; for(int i = 0; i < numHorCells; i++) - if(fabs(cells[i].pos.x - pos.x) < .5 * CELL_SIZE.x){ + if(fabs(cells[i].pos.x - pos.x) <= .5 * CELL_SIZE.x){ horId = i; break; } @@ -730,7 +730,7 @@ namespace battleship{ int numVertCells = int(mapSize.z / CELL_SIZE.z), vertId = -1; for(int i = 0; i < numVertCells; i++) - if(fabs(cells[i * numHorCells].pos.z - pos.z) < .5 * CELL_SIZE.z){ + if(fabs(cells[i * numHorCells].pos.z - pos.z) <= .5 * CELL_SIZE.z){ vertId = i; break; } diff --git a/projectile.h b/projectile.h index 2d24cb2..59d0309 100755 --- a/projectile.h +++ b/projectile.h @@ -13,7 +13,7 @@ namespace vb01{ namespace battleship { class Unit; - enum class ProjectileClass{SHELL, CRUISE_MISSILE, MISSILE, TORPEDO}; + enum class ProjectileClass{SHELL, CRUISE_MISSILE, MISSILE, TORPEDO, DEPTH_CHARGE}; class Projectile : public GameObject{ public: diff --git a/unit.cpp b/unit.cpp index 0fb6b40..9637818 100755 --- a/unit.cpp +++ b/unit.cpp @@ -374,15 +374,39 @@ namespace battleship{ targets.insert(targets.end(), targs.begin(), targs.end()); } + vector attackWeapons = getWeaponsByOrder(Order::TYPE::ATTACK); + for(Order::Target &target : orders[0].targets) if(target.unit){ - if(find(targets.begin(), targets.end(), target.unit) != targets.end()) - break; + if(find(targets.begin(), targets.end(), target.unit) != targets.end()){ + bool canAttack = false; - removeOrder(0); + for(Weapon *aw : attackWeapons){ + int tc; + + switch(target.unit->getType()){ + case GameObject::Type::UNIT: + tc = (int)((Unit*)target.unit)->getType(); + break; + case GameObject::Type::PROJECTILE: + tc = (int)((Projectile*)target.unit)->getProjectileClass(); + break; + } + + if(aw->canAttackTarget((int)target.unit->getType(), tc)) canAttack = true; + } + + if(!canAttack){ + removeOrder(0); + return; + } + } + else{ + removeOrder(0); + return; + } } - vector attackWeapons = getWeaponsByOrder(Order::TYPE::ATTACK); Vector3 targPos = (order.targets[0].unit ? order.targets[0].unit->getPos() : order.targets[0].pos); for(Weapon *weapon : attackWeapons) diff --git a/weapon.cpp b/weapon.cpp index 2b6197c..77d38b3 100644 --- a/weapon.cpp +++ b/weapon.cpp @@ -28,6 +28,16 @@ namespace battleship{ maxFireAngle = weaponTable["maxFireAngle"].get_or(.1); orderType = (Order::TYPE)weaponTable["orderType"]; + sol::optional fireDirOpt = weaponTable["fireDir"]; + + if(fireDirOpt != sol::nullopt){ + sol::table fireDirTbl = weaponTable["fireDir"]; + fireDir = Vector3((float)fireDirTbl["x"], (float)fireDirTbl["y"], (float)fireDirTbl["z"]); + } + + initTargetData(targetUnits, weaponTable, "targetUnits", "UnitType"); + initTargetData(targetProjectiles, weaponTable, "targetProjeciles", "ProjectileClass"); + initProjectileData(weaponTable); initNodes(weaponTable); @@ -41,6 +51,19 @@ namespace battleship{ } } + void Weapon::initTargetData(std::vector &targetVec, sol::table weaponTable, string tblKey, string defTblKey){ + sol::optional unitTblOpt = weaponTable[tblKey]; + sol::table tbl; + + if(unitTblOpt != sol::nullopt) tbl = weaponTable[tblKey]; + else tbl = generateView()[defTblKey]; + + int tblSize = tbl.size(); + + for(int i = 0; i < tblSize; i++) + targetVec.push_back((int)tbl[i + 1]); + } + void Weapon::initNodes(sol::table weaponTable){ sol::optional nodesTblOpt = weaponTable["nodes"]; @@ -136,7 +159,13 @@ namespace battleship{ Vector3 refPos = (isWeaponPos ? components[0].node->localToGlobalPosition(Vector3::VEC_ZERO) : unit->getPos()); if(ordTp == (int)orderType){ - Vector3 dirVec = (isWeaponPos ? components[components.size() - 1].node->getGlobalAxis(2) : unit->getDirVec()); + Vector3 dirVec; + + if(!isWeaponPos) + dirVec = (fireDir.x * unit->getLeftVec() + fireDir.y * unit->getUpVec() + fireDir.z * unit->getDirVec()).norm(); + else + dirVec = components[components.size() - 1].node->getGlobalAxis(2); + bool aimedAtTarget = false; if(targDestruct && targDestruct->getHitbox()){ @@ -306,4 +335,15 @@ namespace battleship{ component.node->setOrientation(rot); } } + + bool Weapon::canAttackTarget(int objType, int typeOrClass){ + switch((GameObject::Type)objType){ + case GameObject::Type::UNIT: + return (find(targetUnits.begin(), targetUnits.end(), typeOrClass) != targetUnits.end()); + case GameObject::Type::PROJECTILE: + return (find(targetProjectiles.begin(), targetProjectiles.end(), typeOrClass) != targetProjectiles.end()); + } + + return true; + } } diff --git a/weapon.h b/weapon.h index 675ef01..6fba44c 100644 --- a/weapon.h +++ b/weapon.h @@ -40,6 +40,7 @@ namespace battleship{ virtual void update(); virtual void fire(Order); void trackTarget(vb01::Vector3); + bool canAttackTarget(int, int); inline int getProjectileId(){return projId;} inline int getRateOfFire(){return rateOfFire;} inline int getDamage(){return damage;} @@ -51,14 +52,16 @@ namespace battleship{ Unit *unit = nullptr; Order::TYPE orderType; int id, projId = -1, damage = 0; + std::vector targetUnits, targetProjectiles; float minRange = 0, maxRange, maxFireAngle; vb01::s64 lastFireTime = 0; FxManager::Fx *fireFx = nullptr; vb01::Quaternion projRot; - vb01::Vector3 projPos, initUnitSpaceDir; + vb01::Vector3 projPos, initUnitSpaceDir, fireDir = vb01::Vector3::VEC_K; vb01::Node *projPar = nullptr; static std::string LASER_FLAG; + void initTargetData(std::vector&, sol::table, std::string, std::string); void initProjectileData(sol::table); void initNodes(sol::table); void useFx(FxManager::Fx*, vb01::Vector3, bool);