Skip to content

Commit

Permalink
Task aug unit (#796)
Browse files Browse the repository at this point in the history
* Fix bug - Store::Subtract adds quantity instead of subtracting it

closes #751
Also add tests and a few additional methods to store.

* Refactor cloaking in the game

* Move cloak code from unit_csv to cloak class

Add missing return.
Other minor fixes.

* Add missing return statement
  • Loading branch information
royfalk authored Dec 9, 2023
1 parent 0245b88 commit adc6284
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 324 deletions.
1 change: 1 addition & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,7 @@ SET(LIBCMD_SOURCES

src/cmd/intelligent.cpp
src/cmd/energetic.cpp
src/cmd/cloak.cpp

src/cmd/planetary_orbit.cpp

Expand Down
4 changes: 2 additions & 2 deletions engine/src/cmd/ai/fire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ class ChooseTargetClass {
}

bool ShouldTargetUnit(Unit *un, float distance) {
if (un->CloakVisible() > .8) {
if (un->cloak.Visible()) {
float rangetotarget = distance;
float rel0 = parent->getRelation(un);
float rel[] = {
Expand Down Expand Up @@ -841,7 +841,7 @@ void FireAt::Execute() {
bool istargetjumpableplanet = false;
if ((targ = parent->Target())) {
istargetjumpableplanet = isJumpablePlanet(targ);
if (targ->CloakVisible() > .8 && !targ->Destroyed()) {
if (targ->cloak.Visible() && !targ->Destroyed()) {
had_target = true;
if (parent->getNumMounts() > 0) {
if (!istargetjumpableplanet) {
Expand Down
4 changes: 1 addition & 3 deletions engine/src/cmd/ai/firekeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ extern bool toggle_pause();
FireKeyboard::FireKeyboard(unsigned int whichplayer, unsigned int whichjoystick) : Order(WEAPON, 0) {
memset(savedTargets, 0, sizeof(void *) * NUMSAVEDTARGETS);
this->autotrackingtoggle = 1;
this->cloaktoggle = true;
this->whichjoystick = whichjoystick;
this->whichplayer = whichplayer;
gunspeed = gunrange = .0001;
Expand Down Expand Up @@ -1780,8 +1779,7 @@ void FireKeyboard::Execute() {
}
if (f().cloakkey == PRESS) {
f().cloakkey = DOWN;
parent->Cloak(cloaktoggle);
cloaktoggle = !cloaktoggle;
parent->cloak.Toggle();
}
if (f().lockkey == PRESS) {
f().lockkey = DOWN;
Expand Down
1 change: 0 additions & 1 deletion engine/src/cmd/ai/firekeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#define NUMSAVEDTARGETS 10
class FireKeyboard : public Order {
bool itts;
bool cloaktoggle;
bool refresh_target;
float gunspeed;
float gunrange;
Expand Down
6 changes: 3 additions & 3 deletions engine/src/cmd/ai/tactics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@

void CloakFor::Execute() {
if (time == 0) {
parent->Cloak(enable);
parent->cloak.Activate();
}
time += SIMULATION_ATOM;
if (time > maxtime) {
done = true;
if (maxtime != 0) {
parent->Cloak(!enable);
parent->cloak.Deactivate();
}
return;
}
Expand All @@ -48,7 +48,7 @@ CloakFor::~CloakFor() {
VS_LOG_AND_FLUSH(trace, (boost::format("clk%1$x") % this));
#endif
if (parent && time <= maxtime) {
parent->Cloak(!enable);
parent->cloak.Deactivate();
}
}

2 changes: 1 addition & 1 deletion engine/src/cmd/armed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void Armed::ActivateGuns(const WeaponInfo *sz, bool ms) {
void Armed::Fire(unsigned int weapon_type_bitmask, bool listen_to_owner) {
Unit *unit = static_cast<Unit *>(this);

if ((unit->cloaking >= 0 && !configuration()->weapons.can_fire_in_cloak) ||
if ((unit->cloak.Active() && !configuration()->weapons.can_fire_in_cloak) ||
(unit->graphicOptions.InWarp && !configuration()->weapons.can_fire_in_spec)) {
UnFire();
return;
Expand Down
6 changes: 3 additions & 3 deletions engine/src/cmd/basecomputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5565,17 +5565,17 @@ void showUnitStats(Unit *playerUnit, string &text, int subunitlevel, int mode, C
}
}
//cloaking device? If we don't have one, no need to mention it ever exists, right?
if (playerUnit->cloaking != -1) {
if (playerUnit->cloak.Capable()) {
if (!mode) {
PRETTY_ADDU(statcolor + "Cloaking device available, energy usage: #-c",
playerUnit->cloakenergy * RSconverter * Wconv,
playerUnit->cloak.Energy() * RSconverter * Wconv,
0,
"MJ/s");
} else {
switch (replacement_mode) {
case 0: //Replacement or new Module
PRETTY_ADDU(statcolor + "Installs a cloaking device.#n# Activated energy usage: #-c",
playerUnit->cloakenergy * RSconverter * Wconv,
playerUnit->cloak.Energy() * RSconverter * Wconv,
0,
"MJ/s");
break;
Expand Down
6 changes: 1 addition & 5 deletions engine/src/cmd/briefing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ void Briefing::Ship::Render(const Matrix &cam, double interpol) {
Matrix camfinal;
MultMatrix(camfinal, cam, final);
for (unsigned int i = 0; i < meshdata.size(); i++) {
int scloak = int(cloak * ((-1) > 1)); //FIXME short fix?
if ((scloak & 0x1) == 0) {
scloak += 1;
}
meshdata[i]->Draw(1, camfinal, 1, cloak > .99 ? -1 : scloak);
meshdata[i]->Draw(1, camfinal, 1);
}
}

Expand Down
144 changes: 144 additions & 0 deletions engine/src/cmd/cloak.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* cloak.cpp
*
* Copyright (C) 2001-2023 Daniel Horn, Benjaman Meyer, Roy Falk, Stephen G. Tuggy,
* and other Vega Strike contributors.
*
* https://github.com/vegastrike/Vega-Strike-Engine-Source
*
* This file is part of Vega Strike.
*
* Vega Strike is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Vega Strike is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

#include "cloak.h"
#include "unit_csv_factory.h"
#include "vegastrike.h"
#include "configuration/configuration.h"

Cloak::Cloak()
{
status = CloakingStatus::disabled;

energy = 0;
rate = 100;
glass = false;
current = 0;
minimum = 0;
}

Cloak::Cloak(std::string unit_key)
{
if(UnitCSVFactory::GetVariable(unit_key, "Can_Cloak", false)) {
status = CloakingStatus::ready;
} else {
status = CloakingStatus::disabled;
}

glass = UnitCSVFactory::GetVariable(unit_key, "Cloak_Glass", false);
rate = UnitCSVFactory::GetVariable(unit_key, "Cloak_Rate", 0.0);
energy = UnitCSVFactory::GetVariable(unit_key, "Cloak_Energy", 0.0);
minimum = UnitCSVFactory::GetVariable(unit_key, "Cloak_Min", 0.0);
minimum = std::min(1.0, std::max(0.0, minimum));
current = 0;
}

void Cloak::Save(std::map<std::string, std::string>& unit)
{
unit["Cloak_Min"] = std::to_string(minimum);
unit["Can_Cloak"] = std::to_string(Capable());
unit["Cloak_Rate"] = std::to_string(rate);
unit["Cloak_Energy"] = std::to_string(energy);
unit["Cloak_Glass"] = std::to_string(glass);
}

void Cloak::Update(Energetic *energetic)
{
// Unit is not capable of cloaking or damaged or just not cloaking
if(status == CloakingStatus::disabled ||
status == CloakingStatus::damaged ||
status == CloakingStatus::ready) {
return;
}

// Use warp power for cloaking (SPEC capacitor)
const static bool warp_energy_for_cloak = configuration()->warp_config.use_warp_energy_for_cloak;
double available_energy = warp_energy_for_cloak ? energetic->warpenergy : energetic->energy.Value();


// Insufficient energy to cloak ship
if(available_energy < this->energy) {
status = CloakingStatus::decloaking;
} else {
// Subtract the energy used
if (warp_energy_for_cloak) {
energetic->warpenergy -= (simulation_atom_var * energy);
} else {
energetic->energy -= (simulation_atom_var * energy);
}
}

if(status == CloakingStatus::decloaking) {
current = std::max(0.0, current - rate * simulation_atom_var);

if(current == 0) {
status = CloakingStatus::ready;
}
}

if(status == CloakingStatus::cloaking) {
current = std::min(1.0, current + rate * simulation_atom_var);

if(current > minimum) {
status = CloakingStatus::cloaked;
}
}


}

void Cloak::Toggle() {
// Unit is not capable of cloaking or damaged
if(status == CloakingStatus::disabled ||
status == CloakingStatus::damaged) {
return;
}

// If we're ready start cloaking
if(status == CloakingStatus::ready) {
status = CloakingStatus::cloaking;
return;
}

// In any other case, start decloaking
status = CloakingStatus::decloaking;
}

void Cloak::Activate() {
if(status == CloakingStatus::ready) {
status = CloakingStatus::cloaking;
}
}

void Cloak::Deactivate() {
// Unit is not capable of cloaking or damaged or just not cloaking
if(status == CloakingStatus::disabled ||
status == CloakingStatus::damaged ||
status == CloakingStatus::ready) {
return;
}

// Start decloaking
status = CloakingStatus::decloaking;
}
Loading

0 comments on commit adc6284

Please sign in to comment.