summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Element.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Stars45/Element.cpp')
-rw-r--r--Stars45/Element.cpp648
1 files changed, 324 insertions, 324 deletions
diff --git a/Stars45/Element.cpp b/Stars45/Element.cpp
index a375cc2..7b3c099 100644
--- a/Stars45/Element.cpp
+++ b/Stars45/Element.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
- FILE: Element.cpp
- AUTHOR: John DiCamillo
+ SUBSYSTEM: Stars
+ FILE: Element.cpp
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- Package Element (e.g. Flight) class implementation
+ OVERVIEW
+ ========
+ Package Element (e.g. Flight) class implementation
*/
#include "MemDebug.h"
@@ -28,30 +28,30 @@
static int id_key = 1000;
Element::Element(const char* call_sign, int a_iff, int a_type)
- : id(id_key++), name(call_sign), type(a_type), iff(a_iff),
- player(0), command_ai(1), commander(0), assignment(0), carrier(0),
- combat_group(0), combat_unit(0), launch_time(0), hold_time(0),
- zone_lock(0), respawns(0), count(0), rogue(false), playable(true), intel(0)
+: id(id_key++), name(call_sign), type(a_type), iff(a_iff),
+player(0), command_ai(1), commander(0), assignment(0), carrier(0),
+combat_group(0), combat_unit(0), launch_time(0), hold_time(0),
+zone_lock(0), respawns(0), count(0), rogue(false), playable(true), intel(0)
{
- if (!call_sign) {
- char buf[32];
- sprintf(buf, "Pkg %d", id);
- name = buf;
- }
+ if (!call_sign) {
+ char buf[32];
+ sprintf(buf, "Pkg %d", id);
+ name = buf;
+ }
- SetLoadout(0);
+ SetLoadout(0);
}
Element::~Element()
{
- flight_plan.destroy();
- objectives.destroy();
- instructions.destroy();
+ flight_plan.destroy();
+ objectives.destroy();
+ instructions.destroy();
- for (int i = 0; i < ships.size(); i++)
- ships[i]->SetElement(0);
+ for (int i = 0; i < ships.size(); i++)
+ ships[i]->SetElement(0);
- respawns = 0;
+ respawns = 0;
}
// +----------------------------------------------------------------------+
@@ -59,136 +59,136 @@ Element::~Element()
int
Element::AddShip(Ship* ship, int index)
{
- if (ship && !ships.contains(ship)) {
- Observe(ship);
+ if (ship && !ships.contains(ship)) {
+ Observe(ship);
- if (index < 0) {
- ships.append(ship);
- index = ships.size();
- }
- else {
- ships.insert(ship, index-1);
- }
+ if (index < 0) {
+ ships.append(ship);
+ index = ships.size();
+ }
+ else {
+ ships.insert(ship, index-1);
+ }
- ship->SetElement(this);
+ ship->SetElement(this);
- if (respawns < ship->RespawnCount())
- respawns = ship->RespawnCount();
- }
+ if (respawns < ship->RespawnCount())
+ respawns = ship->RespawnCount();
+ }
- return index;
+ return index;
}
void
Element::DelShip(Ship* ship)
{
- if (ship && ships.contains(ship)) {
- ships.remove(ship);
- ship->SetElement(0);
+ if (ship && ships.contains(ship)) {
+ ships.remove(ship);
+ ship->SetElement(0);
- if (ships.isEmpty())
- respawns = ship->RespawnCount();
- }
+ if (ships.isEmpty())
+ respawns = ship->RespawnCount();
+ }
}
Ship*
Element::GetShip(int index)
{
- if (index >= 1 && index <= ships.size())
- return ships[index-1];
+ if (index >= 1 && index <= ships.size())
+ return ships[index-1];
- return 0;
+ return 0;
}
int
Element::GetShipClass()
{
- if (ships.size())
- return ships[0]->Class();
+ if (ships.size())
+ return ships[0]->Class();
- return 0;
+ return 0;
}
int
Element::FindIndex(const Ship* s)
{
- return ships.index(s) + 1;
+ return ships.index(s) + 1;
}
bool
Element::Contains(const Ship* s)
{
- return ships.contains(s);
+ return ships.contains(s);
}
bool
Element::IsActive() const
{
- bool active = false;
+ bool active = false;
- for (int i = 0; i < ships.size() && !active; i++) {
- Ship* s = ships[i];
- if (s->Life() && s->MissionClock())
- active = true;
- }
+ for (int i = 0; i < ships.size() && !active; i++) {
+ Ship* s = ships[i];
+ if (s->Life() && s->MissionClock())
+ active = true;
+ }
- return active;
+ return active;
}
bool
Element::IsFinished() const
{
- bool finished = false;
+ bool finished = false;
- if (launch_time > 0 && respawns < 1) {
- finished = true;
+ if (launch_time > 0 && respawns < 1) {
+ finished = true;
- if (ships.size() > 0) {
- for (int i = 0; i < ships.size() && finished; i++) {
- Ship* s = ships[i];
- if (s->RespawnCount() > 0 ||
- s->MissionClock() == 0 ||
- s->Life() && !s->GetInbound())
- finished = false;
- }
- }
- }
+ if (ships.size() > 0) {
+ for (int i = 0; i < ships.size() && finished; i++) {
+ Ship* s = ships[i];
+ if (s->RespawnCount() > 0 ||
+ s->MissionClock() == 0 ||
+ s->Life() && !s->GetInbound())
+ finished = false;
+ }
+ }
+ }
- return finished;
+ return finished;
}
bool
Element::IsNetObserver() const
{
- bool observer = !IsSquadron();
+ bool observer = !IsSquadron();
- for (int i = 0; i < ships.size() && observer; i++) {
- Ship* s = ships[i];
+ for (int i = 0; i < ships.size() && observer; i++) {
+ Ship* s = ships[i];
- if (!s->IsNetObserver())
- observer = false;
- }
+ if (!s->IsNetObserver())
+ observer = false;
+ }
- return observer;
+ return observer;
}
bool
Element::IsSquadron() const
{
- return count > 0;
+ return count > 0;
}
bool
Element::IsStatic() const
{
- if (IsSquadron() || IsFinished())
- return false;
+ if (IsSquadron() || IsFinished())
+ return false;
- const Ship* s = ships.at(0);
- if (s && s->IsStatic())
- return true;
+ const Ship* s = ships.at(0);
+ if (s && s->IsStatic())
+ return true;
- return false;
+ return false;
}
// +----------------------------------------------------------------------+
@@ -196,87 +196,87 @@ Element::IsStatic() const
bool
Element::IsHostileTo(const Ship* s) const
{
- if (iff <= 0 || iff >= 100 || !s || launch_time == 0 || IsFinished())
- return false;
+ if (iff <= 0 || iff >= 100 || !s || launch_time == 0 || IsFinished())
+ return false;
- if (IsSquadron())
- return false;
+ if (IsSquadron())
+ return false;
- if (s->IsRogue())
- return true;
+ if (s->IsRogue())
+ return true;
- int s_iff = s->GetIFF();
+ int s_iff = s->GetIFF();
- if (s_iff <= 0 || s_iff >= 100 || s_iff == iff)
- return false;
+ if (s_iff <= 0 || s_iff >= 100 || s_iff == iff)
+ return false;
- if (ships.size() > 0 && ships[0]->GetRegion() != s->GetRegion())
- return false;
+ if (ships.size() > 0 && ships[0]->GetRegion() != s->GetRegion())
+ return false;
- return true;
+ return true;
}
bool
Element::IsHostileTo(int iff_code) const
{
- if (iff <= 0 || iff >= 100 || launch_time == 0 || IsFinished())
- return false;
+ if (iff <= 0 || iff >= 100 || launch_time == 0 || IsFinished())
+ return false;
- if (IsSquadron())
- return false;
+ if (IsSquadron())
+ return false;
- if (iff_code <= 0 || iff_code >= 100 || iff_code == iff)
- return false;
+ if (iff_code <= 0 || iff_code >= 100 || iff_code == iff)
+ return false;
- return true;
+ return true;
}
bool
Element::IsObjectiveTargetOf(const Ship* s) const
{
- if (!s || launch_time == 0 || IsFinished())
- return false;
+ if (!s || launch_time == 0 || IsFinished())
+ return false;
- const char* e_name = Name().data();
- int e_len = Name().length();
+ const char* e_name = Name().data();
+ int e_len = Name().length();
- Instruction* orders = s->GetRadioOrders();
- if (orders && orders->Action() > Instruction::SWEEP) {
- const char* o_name = orders->TargetName();
- int o_len = 0;
+ Instruction* orders = s->GetRadioOrders();
+ if (orders && orders->Action() > Instruction::SWEEP) {
+ const char* o_name = orders->TargetName();
+ int o_len = 0;
- if (o_name && *o_name)
- o_len = strlen(o_name);
+ if (o_name && *o_name)
+ o_len = strlen(o_name);
- if (e_len < o_len)
- o_len = e_len;
+ if (e_len < o_len)
+ o_len = e_len;
- if (!strncmp(e_name, o_name, o_len))
- return true;
- }
+ if (!strncmp(e_name, o_name, o_len))
+ return true;
+ }
- Element* elem = s->GetElement();
- if (elem) {
- for (int i = 0; i < elem->NumObjectives(); i++) {
- Instruction* obj = elem->GetObjective(i);
+ Element* elem = s->GetElement();
+ if (elem) {
+ for (int i = 0; i < elem->NumObjectives(); i++) {
+ Instruction* obj = elem->GetObjective(i);
- if (obj) {
- const char* o_name = obj->TargetName();
- int o_len = 0;
+ if (obj) {
+ const char* o_name = obj->TargetName();
+ int o_len = 0;
- if (o_name && *o_name)
- o_len = strlen(o_name);
+ if (o_name && *o_name)
+ o_len = strlen(o_name);
- if (e_len < o_len)
- o_len = e_len;
+ if (e_len < o_len)
+ o_len = e_len;
- if (!strncmp(e_name, o_name, o_len))
- return true;
- }
- }
- }
+ if (!strncmp(e_name, o_name, o_len))
+ return true;
+ }
+ }
+ }
- return false;
+ return false;
}
// +----------------------------------------------------------------------+
@@ -284,45 +284,45 @@ Element::IsObjectiveTargetOf(const Ship* s) const
void
Element::SetLaunchTime(DWORD t)
{
- if (launch_time == 0 || t == 0)
- launch_time = t;
+ if (launch_time == 0 || t == 0)
+ launch_time = t;
}
double
Element::GetHoldTime()
{
- return hold_time;
+ return hold_time;
}
void
Element::SetHoldTime(double t)
{
- if (t >= 0)
- hold_time = t;
+ if (t >= 0)
+ hold_time = t;
}
bool
Element::GetZoneLock()
{
- return zone_lock;
+ return zone_lock;
}
void
Element::SetZoneLock(bool z)
{
- zone_lock = z;
+ zone_lock = z;
}
void
Element::SetLoadout(int* l)
{
- if (l) {
- CopyMemory(load, l, sizeof(load));
- }
- else {
- for (int i = 0; i < 16; i++)
- load[i] = -1;
- }
+ if (l) {
+ CopyMemory(load, l, sizeof(load));
+ }
+ else {
+ for (int i = 0; i < 16; i++)
+ load[i] = -1;
+ }
}
// +----------------------------------------------------------------------+
@@ -330,25 +330,25 @@ Element::SetLoadout(int* l)
bool
Element::Update(SimObject* obj)
{
- // false alarm, keep watching:
- if (obj->Life() != 0) {
- ::Print("Element (%s) false update on (%s) life = %f\n", Name().data(), obj->Name(), obj->Life());
- return false;
- }
+ // false alarm, keep watching:
+ if (obj->Life() != 0) {
+ ::Print("Element (%s) false update on (%s) life = %f\n", Name().data(), obj->Name(), obj->Life());
+ return false;
+ }
- Ship* s = (Ship*) obj;
- ships.remove(s);
+ Ship* s = (Ship*) obj;
+ ships.remove(s);
- if (ships.isEmpty())
- respawns = s->RespawnCount();
+ if (ships.isEmpty())
+ respawns = s->RespawnCount();
- return SimObserver::Update(obj);
+ return SimObserver::Update(obj);
}
const char*
Element::GetObserverName() const
{
- return (const char*) (Text("Element ") + Name());
+ return (const char*) (Text("Element ") + Name());
}
// +----------------------------------------------------------------------+
@@ -356,45 +356,45 @@ Element::GetObserverName() const
void
Element::AddNavPoint(Instruction* pt, Instruction* afterPoint, bool send)
{
- if (pt && !flight_plan.contains(pt)) {
- int index = -1;
+ if (pt && !flight_plan.contains(pt)) {
+ int index = -1;
- if (afterPoint) {
- index = flight_plan.index(afterPoint);
+ if (afterPoint) {
+ index = flight_plan.index(afterPoint);
- if (index > -1)
- flight_plan.insert(pt, index+1);
- else
- flight_plan.append(pt);
- }
+ if (index > -1)
+ flight_plan.insert(pt, index+1);
+ else
+ flight_plan.append(pt);
+ }
- else {
- flight_plan.append(pt);
- }
+ else {
+ flight_plan.append(pt);
+ }
- if (send) {
- NetUtil::SendNavData(true, this, index, pt);
- }
- }
+ if (send) {
+ NetUtil::SendNavData(true, this, index, pt);
+ }
+ }
}
void
Element::DelNavPoint(Instruction* pt, bool send)
{
- // XXX MEMORY LEAK
- // This is a small memory leak, but I'm not sure if it is
- // safe to delete the navpoint when removing it from the
- // flight plan. Other ships in the element might have
- // pointers to the object...?
+ // XXX MEMORY LEAK
+ // This is a small memory leak, but I'm not sure if it is
+ // safe to delete the navpoint when removing it from the
+ // flight plan. Other ships in the element might have
+ // pointers to the object...?
- if (pt) {
- int index = flight_plan.index(pt);
- flight_plan.remove(pt);
+ if (pt) {
+ int index = flight_plan.index(pt);
+ flight_plan.remove(pt);
- if (send) {
- NetUtil::SendNavDelete(this, index);
- }
- }
+ if (send) {
+ NetUtil::SendNavDelete(this, index);
+ }
+ }
}
// +----------------------------------------------------------------------+
@@ -402,14 +402,14 @@ Element::DelNavPoint(Instruction* pt, bool send)
void
Element::ClearFlightPlan(bool send)
{
- hold_time = 0;
- flight_plan.destroy();
- objectives.destroy();
- instructions.destroy();
+ hold_time = 0;
+ flight_plan.destroy();
+ objectives.destroy();
+ instructions.destroy();
- if (send) {
- NetUtil::SendNavDelete(this, -1);
- }
+ if (send) {
+ NetUtil::SendNavDelete(this, -1);
+ }
}
// +----------------------------------------------------------------------+
@@ -417,20 +417,20 @@ Element::ClearFlightPlan(bool send)
Instruction*
Element::GetNextNavPoint()
{
- if (hold_time <= 0 && flight_plan.size() > 0) {
- ListIter<Instruction> iter = flight_plan;
- while (++iter) {
- Instruction* navpt = iter.value();
+ if (hold_time <= 0 && flight_plan.size() > 0) {
+ ListIter<Instruction> iter = flight_plan;
+ while (++iter) {
+ Instruction* navpt = iter.value();
- if (navpt->Status() == Instruction::COMPLETE && navpt->HoldTime() > 0)
- return navpt;
+ if (navpt->Status() == Instruction::COMPLETE && navpt->HoldTime() > 0)
+ return navpt;
- if (navpt->Status() <= Instruction::ACTIVE)
- return navpt;
- }
- }
+ if (navpt->Status() <= Instruction::ACTIVE)
+ return navpt;
+ }
+ }
- return 0;
+ return 0;
}
// +----------------------------------------------------------------------+
@@ -438,18 +438,18 @@ Element::GetNextNavPoint()
int
Element::GetNavIndex(const Instruction* n)
{
- int index = 0;
+ int index = 0;
- if (flight_plan.size() > 0) {
- ListIter<Instruction> navpt = flight_plan;
- while (++navpt) {
- index++;
- if (navpt.value() == n)
- return index;
- }
- }
+ if (flight_plan.size() > 0) {
+ ListIter<Instruction> navpt = flight_plan;
+ while (++navpt) {
+ index++;
+ if (navpt.value() == n)
+ return index;
+ }
+ }
- return 0;
+ return 0;
}
// +----------------------------------------------------------------------+
@@ -457,13 +457,13 @@ Element::GetNavIndex(const Instruction* n)
List<Instruction>&
Element::GetFlightPlan()
{
- return flight_plan;
+ return flight_plan;
}
int
Element::FlightPlanLength()
{
- return flight_plan.size();
+ return flight_plan.size();
}
// +----------------------------------------------------------------------+
@@ -471,55 +471,55 @@ Element::FlightPlanLength()
void
Element::ClearObjectives()
{
- objectives.destroy();
+ objectives.destroy();
}
void
Element::AddObjective(Instruction* obj)
{
- objectives.append(obj);
+ objectives.append(obj);
}
Instruction*
Element::GetObjective(int index)
{
- if (objectives.isEmpty())
- return 0;
+ if (objectives.isEmpty())
+ return 0;
- if (index < 0)
- index = 0;
+ if (index < 0)
+ index = 0;
- else if (index >= objectives.size())
- index = index % objectives.size();
+ else if (index >= objectives.size())
+ index = index % objectives.size();
- return objectives.at(index);
+ return objectives.at(index);
}
Instruction*
Element::GetTargetObjective()
{
- for (int i = 0; i < objectives.size(); i++) {
- Instruction* obj = objectives[i];
+ for (int i = 0; i < objectives.size(); i++) {
+ Instruction* obj = objectives[i];
- if (obj->Status() <= Instruction::ACTIVE) {
- switch (obj->Action()) {
- case Instruction::INTERCEPT:
- case Instruction::STRIKE:
- case Instruction::ASSAULT:
- case Instruction::SWEEP:
- case Instruction::PATROL:
- case Instruction::RECON:
- case Instruction::ESCORT:
- case Instruction::DEFEND:
- return obj;
+ if (obj->Status() <= Instruction::ACTIVE) {
+ switch (obj->Action()) {
+ case Instruction::INTERCEPT:
+ case Instruction::STRIKE:
+ case Instruction::ASSAULT:
+ case Instruction::SWEEP:
+ case Instruction::PATROL:
+ case Instruction::RECON:
+ case Instruction::ESCORT:
+ case Instruction::DEFEND:
+ return obj;
- default:
- break;
- }
- }
- }
+ default:
+ break;
+ }
+ }
+ }
- return 0;
+ return 0;
}
// +----------------------------------------------------------------------+
@@ -527,28 +527,28 @@ Element::GetTargetObjective()
void
Element::ClearInstructions()
{
- instructions.clear();
+ instructions.clear();
}
void
Element::AddInstruction(const char* instr)
{
- instructions.append(new(__FILE__,__LINE__) Text(instr));
+ instructions.append(new(__FILE__,__LINE__) Text(instr));
}
Text
Element::GetInstruction(int index)
{
- if (instructions.isEmpty())
- return Text();
+ if (instructions.isEmpty())
+ return Text();
- if (index < 0)
- index = 0;
+ if (index < 0)
+ index = 0;
- if (index >= instructions.size())
- index = index % instructions.size();
+ if (index >= instructions.size())
+ index = index % instructions.size();
- return *instructions.at(index);
+ return *instructions.at(index);
}
// +----------------------------------------------------------------------+
@@ -556,41 +556,41 @@ Element::GetInstruction(int index)
void
Element::ResumeAssignment()
{
- SetAssignment(0);
+ SetAssignment(0);
- if (objectives.isEmpty())
- return;
+ if (objectives.isEmpty())
+ return;
- Instruction* objective = 0;
+ Instruction* objective = 0;
- for (int i = 0; i < objectives.size() && !objective; i++) {
- Instruction* instr = objectives[i];
+ for (int i = 0; i < objectives.size() && !objective; i++) {
+ Instruction* instr = objectives[i];
- if (instr->Status() <= Instruction::ACTIVE) {
- switch (instr->Action()) {
- case Instruction::INTERCEPT:
- case Instruction::STRIKE:
- case Instruction::ASSAULT:
- objective = instr;
- break;
- }
- }
- }
+ if (instr->Status() <= Instruction::ACTIVE) {
+ switch (instr->Action()) {
+ case Instruction::INTERCEPT:
+ case Instruction::STRIKE:
+ case Instruction::ASSAULT:
+ objective = instr;
+ break;
+ }
+ }
+ }
- if (objective) {
- Sim* sim = Sim::GetSim();
+ if (objective) {
+ Sim* sim = Sim::GetSim();
- ListIter<Element> iter = sim->GetElements();
- while (++iter) {
- Element* elem = iter.value();
- SimObject* tgt = objective->GetTarget();
+ ListIter<Element> iter = sim->GetElements();
+ while (++iter) {
+ Element* elem = iter.value();
+ SimObject* tgt = objective->GetTarget();
- if (tgt && tgt->Type() == SimObject::SIM_SHIP && elem->Contains((const Ship*) tgt)) {
- SetAssignment(elem);
- return;
- }
- }
- }
+ if (tgt && tgt->Type() == SimObject::SIM_SHIP && elem->Contains((const Ship*) tgt)) {
+ SetAssignment(elem);
+ return;
+ }
+ }
+ }
}
// +----------------------------------------------------------------------+
@@ -598,30 +598,30 @@ Element::ResumeAssignment()
void
Element::HandleRadioMessage(RadioMessage* msg)
{
- if (!msg) return;
+ if (!msg) return;
- static RadioHandler rh;
+ static RadioHandler rh;
- // if this is a message from within the element,
- // then all ships should report in. Otherwise,
- // just the leader will acknowledge the message.
- int full_report = ships.contains(msg->Sender());
- int reported = false;
+ // if this is a message from within the element,
+ // then all ships should report in. Otherwise,
+ // just the leader will acknowledge the message.
+ int full_report = ships.contains(msg->Sender());
+ int reported = false;
- ListIter<Ship> s = ships;
- while (++s) {
- if (rh.ProcessMessage(msg, s.value())) {
- if (full_report) {
- if (s.value() != msg->Sender())
- rh.AcknowledgeMessage(msg, s.value());
- }
+ ListIter<Ship> s = ships;
+ while (++s) {
+ if (rh.ProcessMessage(msg, s.value())) {
+ if (full_report) {
+ if (s.value() != msg->Sender())
+ rh.AcknowledgeMessage(msg, s.value());
+ }
- else if (!reported) {
- rh.AcknowledgeMessage(msg, s.value());
- reported = true;
- }
- }
- }
+ else if (!reported) {
+ rh.AcknowledgeMessage(msg, s.value());
+ reported = true;
+ }
+ }
+ }
}
// +----------------------------------------------------------------------+
@@ -629,13 +629,13 @@ Element::HandleRadioMessage(RadioMessage* msg)
bool
Element::CanCommand(Element* e)
{
- while (e) {
- if (e->commander == this)
- return true;
- e = e->commander;
- }
+ while (e) {
+ if (e->commander == this)
+ return true;
+ e = e->commander;
+ }
- return false;
+ return false;
}
// +----------------------------------------------------------------------+
@@ -643,18 +643,18 @@ Element::CanCommand(Element* e)
void
Element::ExecFrame(double seconds)
{
- if (hold_time > 0) {
- hold_time -= seconds;
- return;
- }
+ if (hold_time > 0) {
+ hold_time -= seconds;
+ return;
+ }
- ListIter<Instruction> iter = flight_plan;
- while (++iter) {
- Instruction* instr = iter.value();
+ ListIter<Instruction> iter = flight_plan;
+ while (++iter) {
+ Instruction* instr = iter.value();
- if (instr->Status() == Instruction::COMPLETE && instr->HoldTime() > 0)
- instr->SetHoldTime(instr->HoldTime() - seconds);
- }
+ if (instr->Status() == Instruction::COMPLETE && instr->HoldTime() > 0)
+ instr->SetHoldTime(instr->HoldTime() - seconds);
+ }
}
// +----------------------------------------------------------------------+
@@ -662,6 +662,6 @@ Element::ExecFrame(double seconds)
void
Element::SetIFF(int iff)
{
- for (int i = 0; i < ships.size(); i++)
- ships[i]->SetIFF(iff);
+ for (int i = 0; i < ships.size(); i++)
+ ships[i]->SetIFF(iff);
}