2014-01-01 19:43:38 -08:00
|
|
|
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
|
2012-09-15 17:59:15 -07:00
|
|
|
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
|
2012-09-12 04:38:30 -07:00
|
|
|
|
2008-06-24 03:17:31 -07:00
|
|
|
#include "ShipType.h"
|
2012-05-04 20:04:20 -07:00
|
|
|
#include "LuaVector.h"
|
2011-03-28 03:56:42 -07:00
|
|
|
#include "LuaUtils.h"
|
2013-04-15 11:35:34 -07:00
|
|
|
#include "LuaTable.h"
|
2012-10-02 15:34:08 -07:00
|
|
|
#include "LuaConstants.h"
|
2012-08-18 07:29:48 -07:00
|
|
|
#include "FileSystem.h"
|
2010-03-30 05:44:20 -07:00
|
|
|
#include "utils.h"
|
2011-07-27 03:46:56 -07:00
|
|
|
#include "Lang.h"
|
2013-07-07 14:12:28 -07:00
|
|
|
#include <algorithm>
|
2008-06-24 03:17:31 -07:00
|
|
|
|
2009-06-23 13:22:27 -07:00
|
|
|
const char *ShipType::gunmountNames[GUNMOUNT_MAX] = {
|
2011-07-27 03:46:56 -07:00
|
|
|
Lang::FRONT, Lang::REAR };
|
2009-06-23 13:22:27 -07:00
|
|
|
|
2012-10-30 21:26:42 -07:00
|
|
|
std::map<ShipType::Id, ShipType> ShipType::types;
|
2011-03-13 15:55:32 -07:00
|
|
|
|
2012-10-30 21:26:42 -07:00
|
|
|
std::vector<ShipType::Id> ShipType::player_ships;
|
|
|
|
std::vector<ShipType::Id> ShipType::static_ships;
|
|
|
|
std::vector<ShipType::Id> ShipType::missile_ships;
|
2011-03-13 15:55:32 -07:00
|
|
|
|
2013-02-12 02:45:37 -08:00
|
|
|
std::string ShipType::POLICE = "kanara";
|
2012-08-02 08:35:27 -07:00
|
|
|
std::string ShipType::MISSILE_GUIDED = "missile_guided";
|
|
|
|
std::string ShipType::MISSILE_NAVAL = "missile_naval";
|
|
|
|
std::string ShipType::MISSILE_SMART = "missile_smart";
|
|
|
|
std::string ShipType::MISSILE_UNGUIDED = "missile_unguided";
|
2010-03-30 05:44:20 -07:00
|
|
|
|
2013-10-15 04:17:14 -07:00
|
|
|
float ShipType::GetFuelUseRate() const
|
|
|
|
{
|
|
|
|
const float denominator = fuelTankMass * effectiveExhaustVelocity * 10;
|
|
|
|
return denominator > 0 ? -linThrust[THRUSTER_FORWARD]/denominator : 1e9;
|
|
|
|
}
|
|
|
|
|
2012-11-05 08:57:01 -08:00
|
|
|
// returns velocity of engine exhausts in m/s
|
2013-01-15 11:28:27 -08:00
|
|
|
static double GetEffectiveExhaustVelocity(double fuelTankMass, double thrusterFuelUse, double forwardThrust) {
|
2013-01-06 06:25:59 -08:00
|
|
|
double denominator = fuelTankMass * thrusterFuelUse * 10;
|
2012-11-05 08:57:01 -08:00
|
|
|
return fabs(denominator > 0 ? forwardThrust/denominator : 1e9);
|
|
|
|
}
|
|
|
|
|
2013-07-07 14:12:28 -07:00
|
|
|
static bool ShipIsUnbuyable(const ShipType::Id &id)
|
|
|
|
{
|
|
|
|
const ShipType &t = ShipType::types[id];
|
|
|
|
return (t.baseprice == 0);
|
|
|
|
}
|
|
|
|
|
2012-08-18 07:29:48 -07:00
|
|
|
static std::string s_currentShipFile;
|
|
|
|
|
2012-10-30 21:26:42 -07:00
|
|
|
int _define_ship(lua_State *L, ShipType::Tag tag, std::vector<ShipType::Id> *list)
|
2012-02-29 06:21:37 -08:00
|
|
|
{
|
2012-08-18 07:29:48 -07:00
|
|
|
if (s_currentShipFile.empty())
|
|
|
|
return luaL_error(L, "ship file contains multiple ship definitions");
|
|
|
|
|
2012-02-29 06:21:37 -08:00
|
|
|
ShipType s;
|
|
|
|
s.tag = tag;
|
2012-10-30 21:26:42 -07:00
|
|
|
s.id = s_currentShipFile;
|
2012-02-29 06:21:37 -08:00
|
|
|
|
|
|
|
LUA_DEBUG_START(L);
|
2013-04-15 11:35:34 -07:00
|
|
|
LuaTable t(L, -1);
|
2013-09-17 04:28:13 -07:00
|
|
|
|
2013-04-15 11:35:34 -07:00
|
|
|
s.name = t.Get("name", "");
|
2014-01-04 15:07:45 -08:00
|
|
|
s.shipClass = t.Get("ship_class", "unknown");
|
|
|
|
s.manufacturer = t.Get("manufacturer", "unknown");
|
2013-04-15 11:35:34 -07:00
|
|
|
s.modelName = t.Get("model", "");
|
2013-09-20 00:31:02 -07:00
|
|
|
|
2013-12-14 15:12:18 -08:00
|
|
|
s.cockpitName = t.Get("cockpit", "");
|
2013-04-15 11:35:34 -07:00
|
|
|
s.linThrust[ShipType::THRUSTER_REVERSE] = t.Get("reverse_thrust", 0.0f);
|
|
|
|
s.linThrust[ShipType::THRUSTER_FORWARD] = t.Get("forward_thrust", 0.0f);
|
|
|
|
s.linThrust[ShipType::THRUSTER_UP] = t.Get("up_thrust", 0.0f);
|
|
|
|
s.linThrust[ShipType::THRUSTER_DOWN] = t.Get("down_thrust", 0.0f);
|
|
|
|
s.linThrust[ShipType::THRUSTER_LEFT] = t.Get("left_thrust", 0.0f);
|
|
|
|
s.linThrust[ShipType::THRUSTER_RIGHT] = t.Get("right_thrust", 0.0f);
|
|
|
|
s.angThrust = t.Get("angular_thrust", 0.0f);
|
2012-09-23 10:50:37 -07:00
|
|
|
// invert values where necessary
|
|
|
|
s.linThrust[ShipType::THRUSTER_FORWARD] *= -1.f;
|
|
|
|
s.linThrust[ShipType::THRUSTER_LEFT] *= -1.f;
|
|
|
|
s.linThrust[ShipType::THRUSTER_DOWN] *= -1.f;
|
|
|
|
// angthrust fudge (XXX: why?)
|
|
|
|
s.angThrust = s.angThrust / 2;
|
2012-10-16 13:37:01 -07:00
|
|
|
|
2013-09-17 04:28:13 -07:00
|
|
|
lua_pushstring(L, "camera_offset");
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
if (!lua_isnil(L, -1))
|
2014-01-28 01:40:09 -08:00
|
|
|
Output("ship definition for '%s' has deprecated 'camera_offset' field\n", s.id.c_str());
|
2013-09-17 04:28:13 -07:00
|
|
|
lua_pop(L, 1);
|
2013-04-15 11:35:34 -07:00
|
|
|
s.cameraOffset = t.Get("camera_offset", vector3d(0.0));
|
2013-09-17 04:28:13 -07:00
|
|
|
|
2012-02-29 06:21:37 -08:00
|
|
|
for (int i=0; i<Equip::SLOT_MAX; i++) s.equipSlotCapacity[i] = 0;
|
2013-04-15 11:35:34 -07:00
|
|
|
s.equipSlotCapacity[Equip::SLOT_CARGO] = t.Get("max_cargo", 0);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_ENGINE] = t.Get("max_engine", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_LASER] = t.Get("max_laser", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_MISSILE] = t.Get("max_missile", 0);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_ECM] = t.Get("max_ecm", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_SCANNER] = t.Get("max_scanner", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_RADARMAPPER] = t.Get("max_radarmapper", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_HYPERCLOUD] = t.Get("max_hypercloud", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_HULLAUTOREPAIR] = t.Get("max_hullautorepair", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_ENERGYBOOSTER] = t.Get("max_energybooster", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_ATMOSHIELD] = t.Get("max_atmoshield", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_CABIN] = t.Get("max_cabin", 50);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_SHIELD] = t.Get("max_shield", 9999);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_FUELSCOOP] = t.Get("max_fuelscoop", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_CARGOSCOOP] = t.Get("max_cargoscoop", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_LASERCOOLER] = t.Get("max_lasercooler", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_CARGOLIFESUPPORT] = t.Get("max_cargolifesupport", 1);
|
|
|
|
s.equipSlotCapacity[Equip::SLOT_AUTOPILOT] = t.Get("max_autopilot", 1);
|
|
|
|
|
|
|
|
s.capacity = t.Get("capacity", 0);
|
|
|
|
s.hullMass = t.Get("hull_mass", 100);
|
|
|
|
s.fuelTankMass = t.Get("fuel_tank_mass", 5);
|
2012-11-05 08:57:01 -08:00
|
|
|
|
2012-10-22 14:53:06 -07:00
|
|
|
LuaTable slot_table = t.Sub("slots");
|
|
|
|
if (slot_table.GetLua()) {
|
|
|
|
s.slots = slot_table.GetMap<std::string, int>();
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
2012-11-05 08:57:01 -08:00
|
|
|
// fuel_use_rate can be given in two ways
|
2013-01-06 06:25:59 -08:00
|
|
|
float thruster_fuel_use = 0;
|
2013-04-15 11:35:34 -07:00
|
|
|
s.effectiveExhaustVelocity = t.Get("effective_exhaust_velocity", -1.0f);
|
|
|
|
thruster_fuel_use = t.Get("thruster_fuel_use", -1.0f);
|
2013-01-06 06:25:59 -08:00
|
|
|
if(s.effectiveExhaustVelocity < 0 && thruster_fuel_use < 0) {
|
2012-11-05 08:57:01 -08:00
|
|
|
// default value of v_c is used
|
2013-01-06 06:25:59 -08:00
|
|
|
s.effectiveExhaustVelocity = 55000000;
|
|
|
|
} else if(s.effectiveExhaustVelocity < 0 && thruster_fuel_use >= 0) {
|
2012-11-05 08:57:01 -08:00
|
|
|
// v_c undefined and thruster fuel use defined -- use it!
|
2013-01-06 06:25:59 -08:00
|
|
|
s.effectiveExhaustVelocity = GetEffectiveExhaustVelocity(s.fuelTankMass, thruster_fuel_use, s.linThrust[ShipType::THRUSTER_FORWARD]);
|
2012-11-05 08:57:01 -08:00
|
|
|
} else {
|
2013-01-06 06:25:59 -08:00
|
|
|
if(thruster_fuel_use >= 0)
|
2014-01-28 01:40:09 -08:00
|
|
|
Output("Warning: Both thruster_fuel_use and effective_exhaust_velocity defined for %s, using effective_exhaust_velocity.\n", s.modelName.c_str());
|
2012-11-05 08:57:01 -08:00
|
|
|
}
|
|
|
|
|
2013-04-15 11:35:34 -07:00
|
|
|
s.baseprice = t.Get("price", 0);
|
2012-02-29 06:21:37 -08:00
|
|
|
s.baseprice *= 100; // in hundredths of credits
|
|
|
|
|
2013-04-15 11:35:34 -07:00
|
|
|
s.minCrew = t.Get("min_crew", 1);
|
|
|
|
s.maxCrew = t.Get("max_crew", 1);
|
2012-12-29 16:05:39 -08:00
|
|
|
|
2012-04-01 15:58:36 -07:00
|
|
|
s.equipSlotCapacity[Equip::SLOT_ENGINE] = Clamp(s.equipSlotCapacity[Equip::SLOT_ENGINE], 0, 1);
|
|
|
|
|
2014-04-11 09:46:49 -07:00
|
|
|
s.hyperdriveClass = t.Get("hyperdrive_class", 1);
|
2012-07-04 12:13:48 -07:00
|
|
|
|
2013-09-17 04:23:00 -07:00
|
|
|
for (int i = 0; i < ShipType::GUNMOUNT_MAX; i++) {
|
|
|
|
s.gunMount[i].pos = vector3f(0,0,0);
|
|
|
|
s.gunMount[i].dir = vector3f(0,0,1);
|
|
|
|
s.gunMount[i].sep = 5;
|
|
|
|
s.gunMount[i].orient = ShipType::DUAL_LASERS_HORIZONTAL;
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_pushstring(L, "gun_mounts");
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
if (lua_istable(L, -1)) {
|
2014-01-28 01:40:09 -08:00
|
|
|
Output("ship definition for '%s' has deprecated 'gun_mounts' field\n", s.id.c_str());
|
2012-05-06 11:41:51 -07:00
|
|
|
for (unsigned int i=0; i<lua_rawlen(L,-1); i++) {
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_pushinteger(L, i+1);
|
|
|
|
lua_gettable(L, -2);
|
2012-09-14 03:07:26 -07:00
|
|
|
if (lua_istable(L, -1) && lua_rawlen(L,-1) == 4) {
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_pushinteger(L, 1);
|
|
|
|
lua_gettable(L, -2);
|
2012-05-05 11:37:21 -07:00
|
|
|
s.gunMount[i].pos = LuaVector::CheckFromLuaF(L, -1);
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_pushinteger(L, 2);
|
|
|
|
lua_gettable(L, -2);
|
2012-05-05 11:37:21 -07:00
|
|
|
s.gunMount[i].dir = LuaVector::CheckFromLuaF(L, -1);
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_pop(L, 1);
|
2012-09-14 03:07:26 -07:00
|
|
|
lua_pushinteger(L, 3);
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
s.gunMount[i].sep = lua_tonumber(L,-1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
lua_pushinteger(L, 4);
|
|
|
|
lua_gettable(L, -2);
|
2012-10-02 15:34:08 -07:00
|
|
|
s.gunMount[i].orient = static_cast<ShipType::DualLaserOrientation>(
|
|
|
|
LuaConstants::GetConstantFromArg(L, "DualLaserOrientation", -1));
|
2012-09-14 03:07:26 -07:00
|
|
|
lua_pop(L, 1);
|
2012-02-29 06:21:37 -08:00
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
|
|
|
LUA_DEBUG_END(L, 0);
|
|
|
|
|
2012-03-05 07:25:03 -08:00
|
|
|
//sanity check
|
|
|
|
if (s.name.empty())
|
|
|
|
return luaL_error(L, "Ship has no name");
|
|
|
|
|
2013-02-02 07:31:00 -08:00
|
|
|
if (s.modelName.empty())
|
2012-03-05 07:25:03 -08:00
|
|
|
return luaL_error(L, "Missing model name in ship");
|
|
|
|
|
2012-12-29 16:05:39 -08:00
|
|
|
if (s.minCrew < 1 || s.maxCrew < 1 || s.minCrew > s.maxCrew)
|
|
|
|
return luaL_error(L, "Invalid values for min_crew and max_crew");
|
|
|
|
|
2012-08-18 07:29:48 -07:00
|
|
|
const std::string& id = s_currentShipFile;
|
2012-10-30 21:26:42 -07:00
|
|
|
typedef std::map<ShipType::Id, ShipType>::iterator iter;
|
2012-08-18 07:29:48 -07:00
|
|
|
std::pair<iter, bool> result = ShipType::types.insert(std::make_pair(id, s));
|
|
|
|
if (result.second)
|
|
|
|
list->push_back(s_currentShipFile);
|
|
|
|
else
|
|
|
|
return luaL_error(L, "Ship '%s' was already defined by a different file", id.c_str());
|
|
|
|
s_currentShipFile.clear();
|
|
|
|
|
2012-02-29 06:21:37 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int define_ship(lua_State *L)
|
|
|
|
{
|
|
|
|
return _define_ship(L, ShipType::TAG_SHIP, &ShipType::player_ships);
|
|
|
|
}
|
|
|
|
|
|
|
|
int define_static_ship(lua_State *L)
|
|
|
|
{
|
|
|
|
return _define_ship(L, ShipType::TAG_STATIC_SHIP, &ShipType::static_ships);
|
|
|
|
}
|
|
|
|
|
|
|
|
int define_missile(lua_State *L)
|
|
|
|
{
|
|
|
|
return _define_ship(L, ShipType::TAG_MISSILE, &ShipType::missile_ships);
|
|
|
|
}
|
|
|
|
|
2011-03-13 15:55:32 -07:00
|
|
|
void ShipType::Init()
|
|
|
|
{
|
|
|
|
static bool isInitted = false;
|
|
|
|
if (isInitted) return;
|
|
|
|
isInitted = true;
|
|
|
|
|
2012-05-04 20:04:20 -07:00
|
|
|
lua_State *l = luaL_newstate();
|
2012-02-29 06:21:37 -08:00
|
|
|
|
|
|
|
LUA_DEBUG_START(l);
|
|
|
|
|
2012-05-06 14:42:43 -07:00
|
|
|
luaL_requiref(l, "_G", &luaopen_base, 1);
|
|
|
|
luaL_requiref(l, LUA_DBLIBNAME, &luaopen_debug, 1);
|
|
|
|
luaL_requiref(l, LUA_MATHLIBNAME, &luaopen_math, 1);
|
|
|
|
lua_pop(l, 3);
|
2012-05-04 20:04:20 -07:00
|
|
|
|
2012-10-02 15:34:08 -07:00
|
|
|
LuaConstants::Register(l);
|
2012-05-04 20:04:20 -07:00
|
|
|
LuaVector::Register(l);
|
2012-05-05 08:58:48 -07:00
|
|
|
LUA_DEBUG_CHECK(l, 0);
|
|
|
|
|
2012-05-05 10:55:06 -07:00
|
|
|
// provide shortcut vector constructor: v = vector.new
|
|
|
|
lua_getglobal(l, LuaVector::LibName);
|
|
|
|
lua_getfield(l, -1, "new");
|
2012-05-04 20:04:20 -07:00
|
|
|
assert(lua_iscfunction(l, -1));
|
|
|
|
lua_setglobal(l, "v");
|
2012-05-05 10:55:06 -07:00
|
|
|
lua_pop(l, 1); // pop the vector library table
|
2012-05-04 20:04:20 -07:00
|
|
|
|
|
|
|
LUA_DEBUG_CHECK(l, 0);
|
|
|
|
|
|
|
|
// register ship definition functions
|
2012-02-29 06:21:37 -08:00
|
|
|
lua_register(l, "define_ship", define_ship);
|
|
|
|
lua_register(l, "define_static_ship", define_static_ship);
|
|
|
|
lua_register(l, "define_missile", define_missile);
|
|
|
|
|
2012-05-04 20:04:20 -07:00
|
|
|
LUA_DEBUG_CHECK(l, 0);
|
|
|
|
|
|
|
|
// load all ship definitions
|
2012-08-18 07:29:48 -07:00
|
|
|
namespace fs = FileSystem;
|
|
|
|
for (fs::FileEnumerator files(fs::gameDataFiles, "ships", fs::FileEnumerator::Recurse);
|
|
|
|
!files.Finished(); files.Next()) {
|
|
|
|
const fs::FileInfo &info = files.Current();
|
2013-12-05 13:18:31 -08:00
|
|
|
if (ends_with_ci(info.GetPath(), ".lua")) {
|
2012-08-18 07:29:48 -07:00
|
|
|
const std::string name = info.GetName();
|
|
|
|
s_currentShipFile = name.substr(0, name.size()-4);
|
|
|
|
pi_lua_dofile(l, info.GetPath());
|
|
|
|
s_currentShipFile.clear();
|
|
|
|
}
|
|
|
|
}
|
2012-02-29 06:21:37 -08:00
|
|
|
|
|
|
|
LUA_DEBUG_END(l, 0);
|
|
|
|
|
|
|
|
lua_close(l);
|
2012-03-06 12:00:01 -08:00
|
|
|
|
2013-07-07 14:12:28 -07:00
|
|
|
//remove unbuyable ships from player ship list
|
|
|
|
ShipType::player_ships.erase(
|
|
|
|
std::remove_if(ShipType::player_ships.begin(), ShipType::player_ships.end(), ShipIsUnbuyable),
|
|
|
|
ShipType::player_ships.end());
|
|
|
|
|
2012-03-06 12:00:01 -08:00
|
|
|
if (ShipType::player_ships.empty())
|
2012-05-26 16:30:55 -07:00
|
|
|
Error("No playable ships have been defined! The game cannot run.");
|
2010-03-30 05:44:20 -07:00
|
|
|
}
|
|
|
|
|