Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make links between ships and their database entry explicit #1271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/scienceDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ void fillDefaultDatabaseData()
{
P<ShipTemplate> ship_template = ShipTemplate::getTemplate(template_name);
P<ScienceDatabase> entry = class_database_entries[ship_template->getClass()]->addEntry(ship_template->getLocaleName());
ship_template->setScienceDatabaseEntry(entry);

entry->setModelData(ship_template->model_data);
entry->setImage(ship_template->radar_trace);
Expand Down
2 changes: 1 addition & 1 deletion src/scienceDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define SCIENCE_DATABASE_H

#include "engine.h"
#include "shipTemplate.h"
#include "modelData.h"

/*!
* \brief An entry for the science database that is formed by a number of key value pairs.
Expand Down
9 changes: 9 additions & 0 deletions src/screenComponents/databaseView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ bool DatabaseViewComponent::findAndDisplayEntry(string name)
return false;
}

void DatabaseViewComponent::selectAndDisplay(P<ScienceDatabase> entry)
{
if (entry)
{
selected_entry=entry;
display();
}
}

void DatabaseViewComponent::fillListBox()
{
item_list->setOptions({});
Expand Down
1 change: 1 addition & 0 deletions src/screenComponents/databaseView.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DatabaseViewComponent : public GuiElement
DatabaseViewComponent(GuiContainer* owner);

bool findAndDisplayEntry(string name);
void selectAndDisplay(P<ScienceDatabase>);

private:
P<ScienceDatabase> findEntryById(int32_t id);
Expand Down
4 changes: 3 additions & 1 deletion src/screens/crew6/scienceScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ ScienceScreen::ScienceScreen(GuiContainer* owner, ECrewPosition crew_position)
P<SpaceShip> ship = targets.get();
if (ship)
{
if (database_view->findAndDisplayEntry(ship->getTypeName()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the findAndDisplayEntry now still used or can it be purged?

auto entry=ship->getScienceDatabaseEntry();
if (entry)
{
database_view->selectAndDisplay(entry);
view_mode_selection->setSelectionIndex(1);
radar_view->hide();
background_gradient->hide();
Expand Down
5 changes: 5 additions & 0 deletions src/shipTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ void ShipTemplate::setRepairDocked(bool enabled)
repair_docked = enabled;
}

void ShipTemplate::setScienceDatabaseEntry(P<ScienceDatabase> entry)
{
science_database_entry = entry->getMultiplayerId();
}

void ShipTemplate::setRestocksScanProbes(bool enabled)
{
restocks_scan_probes = enabled;
Expand Down
4 changes: 4 additions & 0 deletions src/shipTemplate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unordered_set>
#include "engine.h"
#include "modelData.h"
#include "scienceDatabase.h"

#include "beamTemplate.h"
#include "missileWeaponData.h"
Expand Down Expand Up @@ -118,6 +119,8 @@ class ShipTemplate : public PObject
float jump_drive_max_distance;
int weapon_storage[MW_Count];

int32_t science_database_entry = -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

science_database_entry_id would match the other code that stores a multiplayer ID instead of a direct link


string radar_trace;
float long_range_radar_range = 30000.0f;
float short_range_radar_range = 5000.0f;
Expand Down Expand Up @@ -148,6 +151,7 @@ class ShipTemplate : public PObject
void setMesh(string model, string color_texture, string specular_texture, string illumination_texture);
void setEnergyStorage(float energy_amount);
void setRepairCrewCount(int amount);
void setScienceDatabaseEntry(P<ScienceDatabase> entry);

void setBeam(int index, float arc, float direction, float range, float cycle_time, float damage);
void setBeamWeapon(int index, float arc, float direction, float range, float cycle_time, float damage);
Expand Down
27 changes: 27 additions & 0 deletions src/spaceObjects/shipTemplateBasedObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ REGISTER_SCRIPT_SUBCLASS_NO_CREATE(ShipTemplateBasedObject, SpaceObject)
REGISTER_SCRIPT_CLASS_FUNCTION(ShipTemplateBasedObject, getRestocksMissilesDocked);
REGISTER_SCRIPT_CLASS_FUNCTION(ShipTemplateBasedObject, setRestocksMissilesDocked);

/// gets the database entry for this ship
/// Example: local description = ship:getScienceDatabaseEntry():getLongDescription()
REGISTER_SCRIPT_CLASS_FUNCTION(ShipTemplateBasedObject, getScienceDatabaseEntry);
/// sets the database entry for this ship
/// Example: local db=ScienceDatabase():setName(_("Mysterious Ship")):setLongDescription(_("A very mysterious ship, maybe you will find out more later"))
/// ship:setScienceDatabaseEntry(db)
REGISTER_SCRIPT_CLASS_FUNCTION(ShipTemplateBasedObject, setScienceDatabaseEntry);

/// [Depricated]
REGISTER_SCRIPT_CLASS_FUNCTION(ShipTemplateBasedObject, getFrontShield);
/// [Depricated]
Expand Down Expand Up @@ -374,6 +382,8 @@ void ShipTemplateBasedObject::setTemplate(string template_name)
ship_template->setCollisionData(this);
model_info.setData(ship_template->model_data);

science_database_entry = ship_template->science_database_entry;

//Call the virtual applyTemplateValues function so subclasses can get extra values from the ship templates.
applyTemplateValues();
}
Expand Down Expand Up @@ -406,6 +416,23 @@ ESystem ShipTemplateBasedObject::getShieldSystemForShieldIndex(int index)
return SYS_RearShield;
}

void ShipTemplateBasedObject::setScienceDatabaseEntry(P<ScienceDatabase> entry)
{
science_database_entry = entry->getMultiplayerId();
}

P<ScienceDatabase> ShipTemplateBasedObject::getScienceDatabaseEntry()
{
if (game_server)
{
return game_server->getObjectById(science_database_entry);
}
else
{
return game_client->getObjectById(science_database_entry);
}
}

void ShipTemplateBasedObject::onTakingDamage(ScriptSimpleCallback callback)
{
this->on_taking_damage = callback;
Expand Down
6 changes: 6 additions & 0 deletions src/spaceObjects/shipTemplateBasedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "engine.h"
#include "spaceObject.h"
#include "shipTemplate.h"
#include "scienceDatabase.h"

/**
An object which is based on a ship template. Contains generic behaviour for:
Expand Down Expand Up @@ -33,6 +34,8 @@ class ShipTemplateBasedObject : public SpaceObject, public Updatable
bool restocks_scan_probes;
bool restocks_missiles_docked; //only restocks cpuships; playerships should use comms

int32_t science_database_entry = -1;

ScriptSimpleCallback on_destruction;
ScriptSimpleCallback on_taking_damage;
public:
Expand Down Expand Up @@ -103,6 +106,9 @@ class ShipTemplateBasedObject : public SpaceObject, public Updatable
bool getRestocksMissilesDocked() { return restocks_missiles_docked; }
void setRestocksMissilesDocked(bool enabled) { restocks_missiles_docked = enabled; }

void setScienceDatabaseEntry(P<ScienceDatabase> entry);
P<ScienceDatabase> getScienceDatabaseEntry();

void onTakingDamage(ScriptSimpleCallback callback);
void onDestruction(ScriptSimpleCallback callback);

Expand Down