int _define_ship(lua_State *L, ShipType::Tag tag, std::vector<ShipType::Id> *list) { if (s_currentShipFile.empty()) return luaL_error(L, "ship file contains multiple ship definitions"); Json::Value data; ShipType s; s.tag = tag; s.id = s_currentShipFile; LUA_DEBUG_START(L); LuaTable t(L, -1); s.name = t.Get("name", ""); s.shipClass = t.Get("ship_class", "unknown"); s.manufacturer = t.Get("manufacturer", "unknown"); s.modelName = t.Get("model", ""); data["name"] = s.name; data["ship_class"] = s.shipClass; data["manufacturer"] = s.manufacturer; data["model"] = s.modelName; s.cockpitName = t.Get("cockpit", ""); 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); data["cockpit"] = s.cockpitName; data["reverse_thrust"] = s.linThrust[ShipType::THRUSTER_REVERSE]; data["forward_thrust"] = s.linThrust[ShipType::THRUSTER_FORWARD]; data["up_thrust"] = s.linThrust[ShipType::THRUSTER_UP]; data["down_thrust"] = s.linThrust[ShipType::THRUSTER_DOWN]; data["left_thrust"] = s.linThrust[ShipType::THRUSTER_LEFT]; data["right_thrust"] = s.linThrust[ShipType::THRUSTER_RIGHT]; data["angular_thrust"] = s.angThrust; // 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; s.capacity = t.Get("capacity", 0); s.hullMass = t.Get("hull_mass", 100); s.fuelTankMass = t.Get("fuel_tank_mass", 5); data["capacity"] = s.capacity; data["hull_mass"] = s.hullMass; data["fuel_tank_mass"] = s.fuelTankMass; LuaTable slot_table = t.Sub("slots"); if (slot_table.GetLua()) { s.slots = slot_table.GetMap<std::string, int>(); } lua_pop(L, 1); { const auto it = s.slots.find("engine"); if (it != s.slots.end()) { it->second = Clamp(it->second, 0, 1); } } for( auto slot : s.slots ) { data["slots"][slot.first] = slot.second; } // fuel_use_rate can be given in two ways float thruster_fuel_use = 0; s.effectiveExhaustVelocity = t.Get("effective_exhaust_velocity", -1.0f); thruster_fuel_use = t.Get("thruster_fuel_use", -1.0f); data["effective_exhaust_velocity"] = s.effectiveExhaustVelocity; data["thruster_fuel_use"] = thruster_fuel_use; if(s.effectiveExhaustVelocity < 0 && thruster_fuel_use < 0) { // default value of v_c is used s.effectiveExhaustVelocity = 55000000; } else if(s.effectiveExhaustVelocity < 0 && thruster_fuel_use >= 0) { // v_c undefined and thruster fuel use defined -- use it! s.effectiveExhaustVelocity = GetEffectiveExhaustVelocity(s.fuelTankMass, thruster_fuel_use, s.linThrust[ShipType::THRUSTER_FORWARD]); } else { if(thruster_fuel_use >= 0) Output("Warning: Both thruster_fuel_use and effective_exhaust_velocity defined for %s, using effective_exhaust_velocity.\n", s.modelName.c_str()); } s.baseprice = t.Get("price", 0.0); s.minCrew = t.Get("min_crew", 1); s.maxCrew = t.Get("max_crew", 1); s.hyperdriveClass = t.Get("hyperdrive_class", 1); data["price"] = s.baseprice; data["min_crew"] = s.minCrew; data["max_crew"] = s.maxCrew; data["hyperdrive_class"] = s.hyperdriveClass; Json::StyledWriter writer; const std::string saveMe = writer.write( data ); const std::string path("ships/" + s_currentShipFile + ".json"); FileSystem::FileSourceFS newFS(FileSystem::GetDataDir()); FILE *f = newFS.OpenWriteStream(path); if (!f) { Output("couldn't open file for writing '%s'\n", path.c_str()); abort(); } fwrite(saveMe.data(), saveMe.length(), 1, f); fclose(f); lua_pop(L, 1); LUA_DEBUG_END(L, 0); //sanity check if (s.name.empty()) return luaL_error(L, "Ship has no name"); if (s.modelName.empty()) return luaL_error(L, "Missing model name in ship"); if (s.minCrew < 1 || s.maxCrew < 1 || s.minCrew > s.maxCrew) return luaL_error(L, "Invalid values for min_crew and max_crew"); const std::string& id = s_currentShipFile; typedef std::map<std::string, const ShipType>::iterator iter; 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(); return 0; }
/*Matrix* matrixU = newMatrix(3, 3, (double []){ 2.0, 1.0, -1.0, 0.0, 3.0, 2.0, 0.0, 0.0, -3.0 });*/ Matrix* matrixY = newMatrix(3, 1, (double []){ 2.0, 3.0, -1.0, }); /*Matrix* matrixY2 = newMatrix(3, 1, (double []){ 2.0, -3.0, 9.0, });*/ Matrix* c = newFS(matrixL, matrixY); //Matrix* c = newBS(matrixU, matrixY2); Matrix_showFlat(c); deleteMatrix(matrixL); //deleteMatrix(matrixU); deleteMatrix(matrixY); //deleteMatrix(matrixY2); deleteMatrix(c); }