Missile::Missile(const ShipType::Id &shipId, Body *owner, int power)//: Ship(shipId) { if (power < 0) { m_power = 0; if (shipId == ShipType::MISSILE_GUIDED) m_power = 1; if (shipId == ShipType::MISSILE_SMART) m_power = 2; if (shipId == ShipType::MISSILE_NAVAL) m_power = 3; } else m_power = power; m_owner = owner; m_type = &ShipType::types[shipId]; SetMass(m_type->hullMass*1000); SetModel(m_type->modelName.c_str()); SetMassDistributionFromModel(); SetLabel(Lang::MISSILE); Disarm(); SetFuel(1.0); SetFuelReserve(0.0); m_curAICmd = 0; m_aiMessage = AIERROR_NONE; m_decelerating = false; Propulsion::Init( this, GetModel(), m_type->fuelTankMass, m_type->effectiveExhaustVelocity, m_type->linThrust, m_type->angThrust ); }
void Ship::Load(Serializer::Reader &rd, Space *space) { DynamicBody::Load(rd, space); // needs fixups m_angThrusters = rd.Vector3d(); m_thrusters = rd.Vector3d(); m_wheelTransition = rd.Int32(); m_wheelState = rd.Float(); m_launchLockTimeout = rd.Float(); m_testLanded = rd.Bool(); m_flightState = FlightState(rd.Int32()); m_alertState = AlertState(rd.Int32()); m_lastFiringAlert = rd.Double(); m_hyperspace.dest = SystemPath::Unserialize(rd); m_hyperspace.countdown = rd.Float(); for (int i=0; i<ShipType::GUNMOUNT_MAX; i++) { m_gunState[i] = rd.Int32(); m_gunRecharge[i] = rd.Float(); m_gunTemperature[i] = rd.Float(); } m_ecmRecharge = rd.Float(); m_shipFlavour.Load(rd); m_type = &ShipType::types[m_shipFlavour.id]; m_dockedWithPort = rd.Int32(); m_dockedWithIndex = rd.Int32(); m_equipment.InitSlotSizes(m_shipFlavour.id); m_equipment.Load(rd); Init(); m_stats.hull_mass_left = rd.Float(); // must be after Init()... m_stats.shield_mass_left = rd.Float(); if(rd.Int32()) m_curAICmd = AICommand::Load(rd); else m_curAICmd = 0; m_aiMessage = AIError(rd.Int32()); SetFuel(rd.Double()); m_stats.fuel_tank_mass_left = GetShipType().fuelTankMass * GetFuel(); m_reserveFuel = rd.Double(); UpdateStats(); // this is necessary, UpdateStats() in Ship::Init has wrong values of m_thrusterFuel after Load m_controller = 0; const ShipController::Type ctype = static_cast<ShipController::Type>(rd.Int32()); if (ctype == ShipController::PLAYER) SetController(new PlayerShipController()); else SetController(new ShipController()); m_controller->Load(rd); m_equipment.onChange.connect(sigc::mem_fun(this, &Ship::OnEquipmentChange)); }
void Ship::UpdateFuel(const float timeStep, const vector3d &thrust) { const double fuelUseRate = GetShipType()->GetFuelUseRate() * 0.01; double totalThrust = (fabs(thrust.x) + fabs(thrust.y) + fabs(thrust.z)) / -GetShipType()->linThrust[ShipType::THRUSTER_FORWARD]; FuelState lastState = GetFuelState(); SetFuel(GetFuel() - timeStep * (totalThrust * fuelUseRate)); FuelState currentState = GetFuelState(); UpdateFuelStats(); if (currentState != lastState) LuaEvent::Queue("onShipFuelChanged", this, EnumStrings::GetString("ShipFuelStatus", currentState)); }
void Ship::LoadFromJson(const Json::Value &jsonObj, Space *space) { DynamicBody::LoadFromJson(jsonObj, space); if (!jsonObj.isMember("ship")) throw SavedGameCorruptException(); Json::Value shipObj = jsonObj["ship"]; if (!shipObj.isMember("ang_thrusters")) throw SavedGameCorruptException(); if (!shipObj.isMember("thrusters")) throw SavedGameCorruptException(); if (!shipObj.isMember("wheel_transition")) throw SavedGameCorruptException(); if (!shipObj.isMember("wheel_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("launch_lock_timeout")) throw SavedGameCorruptException(); if (!shipObj.isMember("test_landed")) throw SavedGameCorruptException(); if (!shipObj.isMember("flight_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("alert_state")) throw SavedGameCorruptException(); if (!shipObj.isMember("last_firing_alert")) throw SavedGameCorruptException(); if (!shipObj.isMember("hyperspace_destination")) throw SavedGameCorruptException(); if (!shipObj.isMember("hyperspace_countdown")) throw SavedGameCorruptException(); if (!shipObj.isMember("guns")) throw SavedGameCorruptException(); if (!shipObj.isMember("ecm_recharge")) throw SavedGameCorruptException(); if (!shipObj.isMember("ship_type_id")) throw SavedGameCorruptException(); if (!shipObj.isMember("docked_with_port")) throw SavedGameCorruptException(); if (!shipObj.isMember("index_for_body_docked_with")) throw SavedGameCorruptException(); if (!shipObj.isMember("hull_mass_left")) throw SavedGameCorruptException(); if (!shipObj.isMember("shield_mass_left")) throw SavedGameCorruptException(); if (!shipObj.isMember("shield_cooldown")) throw SavedGameCorruptException(); if (!shipObj.isMember("ai_message")) throw SavedGameCorruptException(); if (!shipObj.isMember("thruster_fuel")) throw SavedGameCorruptException(); if (!shipObj.isMember("reserve_fuel")) throw SavedGameCorruptException(); if (!shipObj.isMember("controller_type")) throw SavedGameCorruptException(); if (!shipObj.isMember("name")) throw SavedGameCorruptException(); m_skin.LoadFromJson(shipObj); m_skin.Apply(GetModel()); // needs fixups JsonToVector(&m_angThrusters, shipObj, "ang_thrusters"); JsonToVector(&m_thrusters, shipObj, "thrusters"); m_wheelTransition = shipObj["wheel_transition"].asInt(); m_wheelState = StrToFloat(shipObj["wheel_state"].asString()); m_launchLockTimeout = StrToFloat(shipObj["launch_lock_timeout"].asString()); m_testLanded = shipObj["test_landed"].asBool(); m_flightState = static_cast<FlightState>(shipObj["flight_state"].asInt()); m_alertState = static_cast<AlertState>(shipObj["alert_state"].asInt()); Properties().Set("flightState", EnumStrings::GetString("ShipFlightState", m_flightState)); Properties().Set("alertStatus", EnumStrings::GetString("ShipAlertStatus", m_alertState)); m_lastFiringAlert = StrToDouble(shipObj["last_firing_alert"].asString()); Json::Value hyperspaceDestObj = shipObj["hyperspace_destination"]; m_hyperspace.dest = SystemPath::FromJson(hyperspaceDestObj); m_hyperspace.countdown = StrToFloat(shipObj["hyperspace_countdown"].asString()); m_hyperspace.duration = 0; Json::Value gunArray = shipObj["guns"]; if (!gunArray.isArray()) throw SavedGameCorruptException(); assert(ShipType::GUNMOUNT_MAX == gunArray.size()); for (unsigned int i = 0; i < ShipType::GUNMOUNT_MAX; i++) { Json::Value gunArrayEl = gunArray[i]; if (!gunArrayEl.isMember("state")) throw SavedGameCorruptException(); if (!gunArrayEl.isMember("recharge")) throw SavedGameCorruptException(); if (!gunArrayEl.isMember("temperature")) throw SavedGameCorruptException(); m_gun[i].state = gunArrayEl["state"].asUInt(); m_gun[i].recharge = StrToFloat(gunArrayEl["recharge"].asString()); m_gun[i].temperature = StrToFloat(gunArrayEl["temperature"].asString()); } m_ecmRecharge = StrToFloat(shipObj["ecm_recharge"].asString()); SetShipId(shipObj["ship_type_id"].asString()); // XXX handle missing thirdparty ship m_dockedWithPort = shipObj["docked_with_port"].asInt(); m_dockedWithIndex = shipObj["index_for_body_docked_with"].asUInt(); Init(); m_stats.hull_mass_left = StrToFloat(shipObj["hull_mass_left"].asString()); // must be after Init()... m_stats.shield_mass_left = StrToFloat(shipObj["shield_mass_left"].asString()); m_shieldCooldown = StrToFloat(shipObj["shield_cooldown"].asString()); m_curAICmd = 0; m_curAICmd = AICommand::LoadFromJson(shipObj); m_aiMessage = AIError(shipObj["ai_message"].asInt()); SetFuel(StrToDouble(shipObj["thruster_fuel"].asString())); m_stats.fuel_tank_mass_left = GetShipType()->fuelTankMass * GetFuel(); m_reserveFuel = StrToDouble(shipObj["reserve_fuel"].asString()); PropertyMap &p = Properties(); p.Set("hullMassLeft", m_stats.hull_mass_left); p.Set("hullPercent", 100.0f * (m_stats.hull_mass_left / float(m_type->hullMass))); p.Set("shieldMassLeft", m_stats.shield_mass_left); p.Set("fuelMassLeft", m_stats.fuel_tank_mass_left); p.PushLuaTable(); lua_State *l = Lua::manager->GetLuaState(); lua_getfield(l, -1, "equipSet"); m_equipSet = LuaRef(l, -1); lua_pop(l, 2); UpdateLuaStats(); m_controller = 0; const ShipController::Type ctype = static_cast<ShipController::Type>(shipObj["controller_type"].asInt()); if (ctype == ShipController::PLAYER) SetController(new PlayerShipController()); else SetController(new ShipController()); m_controller->LoadFromJson(shipObj); m_navLights->LoadFromJson(shipObj); m_shipName = shipObj["name"].asString(); Properties().Set("shipName", m_shipName); }