/* * Function: New * * Creates a new <SystemPath> object * * > path = SystemPath.New(sectorX, sectorY, sectorZ, systemIndex, bodyIndex) * * Parameters: * * sectorX - galactic sector X coordinate * * sectorY - galactic sector Y coordinate * * sectorZ - galactic sector Z coordinate * * systemIndex - optional. the numeric index of the system within the sector * * bodyIndex - optional. the numeric index of a specific body within the * system. Defaults to 0, which typically corresponds to the * primary star. * * Availability: * * alpha 10, alpha 13 (updated) * * Status: * * stable */ static int l_sbodypath_new(lua_State *l) { Sint32 sector_x = luaL_checkinteger(l, 1); Sint32 sector_y = luaL_checkinteger(l, 2); Sint32 sector_z = luaL_checkinteger(l, 3); SystemPath path(sector_x, sector_y, sector_z); if (lua_gettop(l) > 3) { path.systemIndex = luaL_checkinteger(l, 4); if (Pi::game) { // if this is a system path, then check that the system exists RefCountedPtr<const Sector> s = Pi::game->GetGalaxy()->GetSector(path); if (size_t(path.systemIndex) >= s->m_systems.size()) luaL_error(l, "System %d in sector <%d,%d,%d> does not exist", path.systemIndex, sector_x, sector_y, sector_z); } if (lua_gettop(l) > 4) { path.bodyIndex = luaL_checkinteger(l, 5); if (Pi::game) { // and if it's a body path, check that the body exists RefCountedPtr<StarSystem> sys = Pi::game->GetGalaxy()->GetStarSystem(path); if (path.bodyIndex >= sys->GetNumBodies()) { luaL_error(l, "Body %d in system <%d,%d,%d : %d ('%s')> does not exist", path.bodyIndex, sector_x, sector_y, sector_z, path.systemIndex, sys->GetName().c_str()); } } } } LuaObject<SystemPath>::PushToLua(path); return 1; }
Game::Game(const SystemPath &path, double time) : m_galaxy(GalaxyGenerator::Create()), m_time(time), m_state(STATE_NORMAL), m_wantHyperspace(false), m_timeAccel(TIMEACCEL_1X), m_requestedTimeAccel(TIMEACCEL_1X), m_forceTimeAccel(false) { // Now that we have a Galaxy, check the starting location if (!path.IsBodyPath()) throw InvalidGameStartLocation("SystemPath is not a body path"); RefCountedPtr<const Sector> s = m_galaxy->GetSector(path); if (size_t(path.systemIndex) >= s->m_systems.size()) { char buf[128]; std::sprintf(buf, "System %u in sector <%d,%d,%d> does not exist", unsigned(path.systemIndex), int(path.sectorX), int(path.sectorY), int(path.sectorZ)); throw InvalidGameStartLocation(std::string(buf)); } RefCountedPtr<StarSystem> sys = m_galaxy->GetStarSystem(path); if (path.bodyIndex >= sys->GetNumBodies()) { char buf[256]; std::sprintf(buf, "Body %d in system <%d,%d,%d : %d ('%s')> does not exist", unsigned(path.bodyIndex), int(path.sectorX), int(path.sectorY), int(path.sectorZ), unsigned(path.systemIndex), sys->GetName().c_str()); throw InvalidGameStartLocation(std::string(buf)); } m_space.reset(new Space(this, m_galaxy, path)); Body *b = m_space->FindBodyForPath(&path); assert(b); m_player.reset(new Player("kanara")); m_space->AddBody(m_player.get()); m_player->SetFrame(b->GetFrame()); if (b->GetType() == Object::SPACESTATION) { m_player->SetDockedWith(static_cast<SpaceStation*>(b), 0); } else { const SystemBody *sbody = b->GetSystemBody(); m_player->SetPosition(vector3d(0, 1.5*sbody->GetRadius(), 0)); m_player->SetVelocity(vector3d(0,0,0)); } Polit::Init(m_galaxy); CreateViews(); EmitPauseState(IsPaused()); }
static int l_set_hyperspace_target(lua_State *l) { LuaObject<Player>::CheckFromLua(1); if (Pi::game->IsNormalSpace()) { const SystemPath path = *LuaObject<SystemPath>::CheckFromLua(2); if (!path.IsSystemPath()) { if (!path.IsBodyPath()) { return luaL_error(l, "Player:SetHyperspaceTarget() -- second parameter is not a system path or the path of a star"); } RefCountedPtr<StarSystem> sys = Pi::game->GetGalaxy()->GetStarSystem(path); // Lua should never be able to get an invalid SystemPath // (note: this may change if it becomes possible to remove systems during the game) assert(path.bodyIndex < sys->GetNumBodies()); SystemBody *sbody = sys->GetBodyByPath(path); if (!sbody->GetSuperType() == SystemBody::SUPERTYPE_STAR) return luaL_error(l, "Player:SetHyperspaceTarget() -- second parameter is not a system path or the path of a star"); } Pi::game->GetSectorView()->SetHyperspaceTarget(path); return 0; } else return luaL_error(l, "Player:SetHyperspaceTarget() cannot be used while in hyperspace"); }
/* * Method: GetSystemBody * * Get a <SystemBody> object for the body that this path points to * * > body = path:GetSystemBody() * * Return: * * body - the <SystemBody> * * Availability: * * alpha 10 * * Status: * * stable */ static int l_sbodypath_get_system_body(lua_State *l) { SystemPath *path = LuaObject<SystemPath>::CheckFromLua(1); if (path->IsSectorPath()) { luaL_error(l, "Path <%d,%d,%d> does not name a system or body", path->sectorX, path->sectorY, path->sectorZ); return 0; } RefCountedPtr<StarSystem> sys = Pi::game->GetGalaxy()->GetStarSystem(path); if (path->IsSystemPath()) { luaL_error(l, "Path <%d,%d,%d : %d ('%s')> does not name a body", path->sectorX, path->sectorY, path->sectorZ, path->systemIndex, sys->GetName().c_str()); return 0; } // Lua should never be able to get an invalid SystemPath // (note: this may change if it becomes possible to remove systems during the game) assert(path->bodyIndex < sys->GetNumBodies()); SystemBody *sbody = sys->GetBodyByPath(path); LuaObject<SystemBody>::PushToLua(sbody); return 1; }