diff options
Diffstat (limited to 'Stars45/CombatUnit.cpp')
-rw-r--r-- | Stars45/CombatUnit.cpp | 424 |
1 files changed, 212 insertions, 212 deletions
diff --git a/Stars45/CombatUnit.cpp b/Stars45/CombatUnit.cpp index cdbf993..ed65f4c 100644 --- a/Stars45/CombatUnit.cpp +++ b/Stars45/CombatUnit.cpp @@ -1,15 +1,15 @@ /* Project Starshatter 4.5
- Destroyer Studios LLC
- Copyright © 1997-2004. All Rights Reserved.
+ Destroyer Studios LLC
+ Copyright © 1997-2004. All Rights Reserved.
- SUBSYSTEM: Stars.exe
- FILE: CombatUnit.cpp
- AUTHOR: John DiCamillo
+ SUBSYSTEM: Stars.exe
+ FILE: CombatUnit.cpp
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- A ship, station, or ground unit in the dynamic campaign.
+ OVERVIEW
+ ========
+ A ship, station, or ground unit in the dynamic campaign.
*/
#include "MemDebug.h"
@@ -28,20 +28,20 @@ inline double random() { return (double) rand() / (double) RAND_MAX; } // +----------------------------------------------------------------------+
CombatUnit::CombatUnit(const char* n, const char* reg, int t, const char* d, int c, int i)
- : name(n), regnum(reg), type(t), design_name(d), design(0),
- count(c), iff(i), leader(false), dead_count(0), available(c),
- carrier(0), plan_value(0), launch_time(-1e6), jump_time(0),
- sustained_damage(0), target(0), group(0), heading(0)
+: name(n), regnum(reg), type(t), design_name(d), design(0),
+count(c), iff(i), leader(false), dead_count(0), available(c),
+carrier(0), plan_value(0), launch_time(-1e6), jump_time(0),
+sustained_damage(0), target(0), group(0), heading(0)
{ }
CombatUnit::CombatUnit(const CombatUnit& u)
- : name(u.name), regnum(u.regnum), type(u.type), design_name(u.design_name),
- design(u.design), count(u.count), iff(u.iff),
- dead_count(u.dead_count), available(u.available),
- leader(u.leader), region(u.region), location(u.location),
- carrier(u.carrier), plan_value(0), launch_time(u.launch_time),
- jump_time(u.jump_time), sustained_damage(u.sustained_damage),
- target(0), group(0), heading(u.heading)
+: name(u.name), regnum(u.regnum), type(u.type), design_name(u.design_name),
+design(u.design), count(u.count), iff(u.iff),
+dead_count(u.dead_count), available(u.available),
+leader(u.leader), region(u.region), location(u.location),
+carrier(u.carrier), plan_value(0), launch_time(u.launch_time),
+jump_time(u.jump_time), sustained_damage(u.sustained_damage),
+target(0), group(0), heading(u.heading)
{ }
// +----------------------------------------------------------------------+
@@ -49,31 +49,31 @@ CombatUnit::CombatUnit(const CombatUnit& u) const ShipDesign*
CombatUnit::GetDesign()
{
- if (!design)
- design = ShipDesign::Get(design_name);
+ if (!design)
+ design = ShipDesign::Get(design_name);
- return design;
+ return design;
}
int
CombatUnit::GetShipClass() const
{
- if (design)
- return design->type;
+ if (design)
+ return design->type;
- return type;
+ return type;
}
int
CombatUnit::GetValue() const
{
- return GetSingleValue() * LiveCount();
+ return GetSingleValue() * LiveCount();
}
int
CombatUnit::GetSingleValue() const
{
- return Ship::Value(GetShipClass());
+ return Ship::Value(GetShipClass());
}
// +----------------------------------------------------------------------+
@@ -81,34 +81,34 @@ CombatUnit::GetSingleValue() const const char*
CombatUnit::GetDescription() const
{
- if (!design) {
- CombatUnit* pThis = (CombatUnit*) this; // cast-away const
- pThis->GetDesign();
- }
-
- static char desc[256];
-
- if (!design) {
- strcpy(desc, Game::GetText("[unknown]").data());
- }
-
- else if (count > 1) {
- sprintf(desc, "%dx %s %s", LiveCount(), design->abrv, design->DisplayName());
- }
-
- else {
- if (regnum.length() > 0)
- sprintf(desc, "%s-%s %s", design->abrv, (const char*) regnum, (const char*) name);
- else
- sprintf(desc, "%s %s", design->abrv, (const char*) name);
-
- if (dead_count > 0) {
- strcat(desc, " ");
- strcat(desc, Game::GetText("killed.in.action"));
- }
- }
-
- return desc;
+ if (!design) {
+ CombatUnit* pThis = (CombatUnit*) this; // cast-away const
+ pThis->GetDesign();
+ }
+
+ static char desc[256];
+
+ if (!design) {
+ strcpy(desc, Game::GetText("[unknown]").data());
+ }
+
+ else if (count > 1) {
+ sprintf(desc, "%dx %s %s", LiveCount(), design->abrv, design->DisplayName());
+ }
+
+ else {
+ if (regnum.length() > 0)
+ sprintf(desc, "%s-%s %s", design->abrv, (const char*) regnum, (const char*) name);
+ else
+ sprintf(desc, "%s %s", design->abrv, (const char*) name);
+
+ if (dead_count > 0) {
+ strcat(desc, " ");
+ strcat(desc, Game::GetText("killed.in.action"));
+ }
+ }
+
+ return desc;
}
// +----------------------------------------------------------------------+
@@ -116,19 +116,19 @@ CombatUnit::GetDescription() const bool
CombatUnit::CanAssign() const
{
- bool result = false;
-
- switch (type) {
- case Ship::FIGHTER:
- case Ship::ATTACK:
- case Ship::CORVETTE:
- case Ship::FRIGATE:
- case Ship::DESTROYER:
- case Ship::CRUISER:
- case Ship::CARRIER: result = true; break;
- }
-
- return result;
+ bool result = false;
+
+ switch (type) {
+ case Ship::FIGHTER:
+ case Ship::ATTACK:
+ case Ship::CORVETTE:
+ case Ship::FRIGATE:
+ case Ship::DESTROYER:
+ case Ship::CRUISER:
+ case Ship::CARRIER: result = true; break;
+ }
+
+ return result;
}
// +----------------------------------------------------------------------+
@@ -136,22 +136,22 @@ CombatUnit::CanAssign() const bool
CombatUnit::CanLaunch() const
{
- bool result = false;
-
- switch (type) {
- case Ship::FIGHTER:
- case Ship::ATTACK: result = (Campaign::Stardate() - launch_time) >= 300;
- break;
-
- case Ship::CORVETTE:
- case Ship::FRIGATE:
- case Ship::DESTROYER:
- case Ship::CRUISER:
- case Ship::CARRIER: result = true;
- break;
- }
-
- return result;
+ bool result = false;
+
+ switch (type) {
+ case Ship::FIGHTER:
+ case Ship::ATTACK: result = (Campaign::Stardate() - launch_time) >= 300;
+ break;
+
+ case Ship::CORVETTE:
+ case Ship::FRIGATE:
+ case Ship::DESTROYER:
+ case Ship::CRUISER:
+ case Ship::CARRIER: result = true;
+ break;
+ }
+
+ return result;
}
// +----------------------------------------------------------------------+
@@ -159,31 +159,31 @@ CombatUnit::CanLaunch() const Color
CombatUnit::MarkerColor() const
{
- return Ship::IFFColor(iff);
+ return Ship::IFFColor(iff);
}
bool
CombatUnit::IsGroundUnit() const
{
- return (design && (design->type & Ship::GROUND_UNITS)) ? true : false;
+ return (design && (design->type & Ship::GROUND_UNITS)) ? true : false;
}
bool
CombatUnit::IsStarship() const
{
- return (design && (design->type & Ship::STARSHIPS)) ? true : false;
+ return (design && (design->type & Ship::STARSHIPS)) ? true : false;
}
bool
CombatUnit::IsDropship() const
{
- return (design && (design->type & Ship::DROPSHIPS)) ? true : false;
+ return (design && (design->type & Ship::DROPSHIPS)) ? true : false;
}
bool
CombatUnit::IsStatic() const
{
- return design && (design->type >= Ship::STATION);
+ return design && (design->type >= Ship::STATION);
}
// +----------------------------------------------------------------------+
@@ -191,92 +191,92 @@ CombatUnit::IsStatic() const double
CombatUnit::MaxRange() const
{
- return 100e3;
+ return 100e3;
}
double CombatUnit::MaxEffectiveRange() const
{
- return 50e3;
+ return 50e3;
}
double CombatUnit::OptimumRange() const
{
- if (type == Ship::FIGHTER || type == Ship::ATTACK)
- return 15e3;
+ if (type == Ship::FIGHTER || type == Ship::ATTACK)
+ return 15e3;
- return 30e3;
+ return 30e3;
}
// +----------------------------------------------------------------------+
bool CombatUnit::CanDefend(CombatUnit* unit) const
{
- if (unit == 0 || unit == this)
- return false;
+ if (unit == 0 || unit == this)
+ return false;
- if (type > Ship::STATION)
- return false;
+ if (type > Ship::STATION)
+ return false;
- double distance = (location - unit->location).length();
+ double distance = (location - unit->location).length();
- if (type > unit->type)
- return false;
+ if (type > unit->type)
+ return false;
- if (distance > MaxRange())
- return false;
+ if (distance > MaxRange())
+ return false;
- return true;
+ return true;
}
// +----------------------------------------------------------------------+
double CombatUnit::PowerVersus(CombatUnit* tgt) const
{
- if (tgt == 0 || tgt == this || available < 1)
- return 0;
-
- if (type > Ship::STATION)
- return 0;
-
- double effectiveness = 1;
- double distance = (location - tgt->location).length();
-
- if (distance > MaxRange())
- return 0;
-
- if (distance > MaxEffectiveRange())
- effectiveness = 0.5;
-
- if (type == Ship::FIGHTER) {
- if (tgt->type == Ship::FIGHTER || tgt->type == Ship::ATTACK)
- return Ship::FIGHTER * 2 * available * effectiveness;
- else
- return 0;
- }
- else if (type == Ship::ATTACK) {
- if (tgt->type > Ship::ATTACK)
- return Ship::ATTACK * 3 * available * effectiveness;
- else
- return 0;
- }
- else if (type == Ship::CARRIER) {
- return 0;
- }
- else if (type == Ship::CRUISER) {
- if (tgt->type <= Ship::ATTACK)
- return type * effectiveness;
-
- else
- return 0;
- }
- else {
- if (tgt->type > Ship::ATTACK)
- return type * effectiveness;
- else
- return type * 0.1 * effectiveness;
- }
-
- return 0;
+ if (tgt == 0 || tgt == this || available < 1)
+ return 0;
+
+ if (type > Ship::STATION)
+ return 0;
+
+ double effectiveness = 1;
+ double distance = (location - tgt->location).length();
+
+ if (distance > MaxRange())
+ return 0;
+
+ if (distance > MaxEffectiveRange())
+ effectiveness = 0.5;
+
+ if (type == Ship::FIGHTER) {
+ if (tgt->type == Ship::FIGHTER || tgt->type == Ship::ATTACK)
+ return Ship::FIGHTER * 2 * available * effectiveness;
+ else
+ return 0;
+ }
+ else if (type == Ship::ATTACK) {
+ if (tgt->type > Ship::ATTACK)
+ return Ship::ATTACK * 3 * available * effectiveness;
+ else
+ return 0;
+ }
+ else if (type == Ship::CARRIER) {
+ return 0;
+ }
+ else if (type == Ship::CRUISER) {
+ if (tgt->type <= Ship::ATTACK)
+ return type * effectiveness;
+
+ else
+ return 0;
+ }
+ else {
+ if (tgt->type > Ship::ATTACK)
+ return type * effectiveness;
+ else
+ return type * 0.1 * effectiveness;
+ }
+
+ return 0;
}
// +----------------------------------------------------------------------+
@@ -284,18 +284,18 @@ double CombatUnit::PowerVersus(CombatUnit* tgt) const int
CombatUnit::AssignMission()
{
- int assign = count;
+ int assign = count;
- if (count > 4)
- assign = 4;
+ if (count > 4)
+ assign = 4;
- if (assign > 0) {
- available -= assign;
- launch_time = Campaign::Stardate();
- return assign;
- }
+ if (assign > 0) {
+ available -= assign;
+ launch_time = Campaign::Stardate();
+ return assign;
+ }
- return 0;
+ return 0;
}
// +----------------------------------------------------------------------+
@@ -303,13 +303,13 @@ CombatUnit::AssignMission() void
CombatUnit::CompleteMission()
{
- Disengage();
+ Disengage();
- if (count > 4)
- available += 4;
+ if (count > 4)
+ available += 4;
- else
- available += count;
+ else
+ available += count;
}
// +----------------------------------------------------------------------+
@@ -317,10 +317,10 @@ CombatUnit::CompleteMission() void
CombatUnit::MoveTo(const Point& loc)
{
- if (!carrier)
- location = loc;
- else
- location = carrier->location;
+ if (!carrier)
+ location = loc;
+ else
+ location = carrier->location;
}
// +----------------------------------------------------------------------+
@@ -328,75 +328,75 @@ CombatUnit::MoveTo(const Point& loc) void
CombatUnit::Engage(CombatUnit* tgt)
{
- if (!tgt)
- Disengage();
+ if (!tgt)
+ Disengage();
- else if (!tgt->attackers.contains(this))
- tgt->attackers.append(this);
+ else if (!tgt->attackers.contains(this))
+ tgt->attackers.append(this);
- target = tgt;
+ target = tgt;
}
void
CombatUnit::Disengage()
{
- if (target)
- target->attackers.remove(this);
+ if (target)
+ target->attackers.remove(this);
- target = 0;
+ target = 0;
}
// +----------------------------------------------------------------------+
static int KillGroup(CombatGroup* group)
{
- int value_killed = 0;
-
- if (group) {
- ListIter<CombatUnit> u_iter = group->GetUnits();
- while (++u_iter) {
- CombatUnit* u = u_iter.value();
- value_killed += u->Kill(u->LiveCount());
- }
-
- ListIter<CombatGroup> g_iter = group->GetComponents();
- while (++g_iter) {
- CombatGroup* g = g_iter.value();
- value_killed += KillGroup(g);
- }
- }
-
- return value_killed;
+ int value_killed = 0;
+
+ if (group) {
+ ListIter<CombatUnit> u_iter = group->GetUnits();
+ while (++u_iter) {
+ CombatUnit* u = u_iter.value();
+ value_killed += u->Kill(u->LiveCount());
+ }
+
+ ListIter<CombatGroup> g_iter = group->GetComponents();
+ while (++g_iter) {
+ CombatGroup* g = g_iter.value();
+ value_killed += KillGroup(g);
+ }
+ }
+
+ return value_killed;
}
int
CombatUnit::Kill(int n)
{
- int killed = n;
+ int killed = n;
- if (killed > LiveCount())
- killed = LiveCount();
+ if (killed > LiveCount())
+ killed = LiveCount();
- dead_count += killed;
+ dead_count += killed;
- int value_killed = killed * GetSingleValue();
+ int value_killed = killed * GetSingleValue();
- if (killed) {
- // if unit could support children, kill them too:
- if (type == Ship::CARRIER ||
- type == Ship::STATION ||
- type == Ship::STARBASE) {
+ if (killed) {
+ // if unit could support children, kill them too:
+ if (type == Ship::CARRIER ||
+ type == Ship::STATION ||
+ type == Ship::STARBASE) {
- if (group) {
- ListIter<CombatGroup> iter = group->GetComponents();
- while (++iter) {
- CombatGroup* g = iter.value();
- value_killed += KillGroup(g);
- }
- }
- }
- }
+ if (group) {
+ ListIter<CombatGroup> iter = group->GetComponents();
+ while (++iter) {
+ CombatGroup* g = iter.value();
+ value_killed += KillGroup(g);
+ }
+ }
+ }
+ }
- return value_killed;
+ return value_killed;
}
|