Пример #1
0
/*
 * Attribute: path
 *
 * The <SystemPath> to the system
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_starsystem_attr_path(lua_State *l)
{
    StarSystem *s = LuaStarSystem::CheckFromLua(1);
    SystemPath path = s->GetPath();
    LuaSystemPath::PushToLua(&path);
    return 1;
}
Пример #2
0
/*
 * Attribute: path
 *
 * The <SystemPath> to the system
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_starsystem_attr_path(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	SystemPath path = s->GetPath();
	LuaObject<SystemPath>::PushToLua(&path);
	return 1;
}
Пример #3
0
Ship::HyperjumpStatus Ship::InitiateHyperjumpTo(const SystemPath &dest, int warmup_time, double duration, LuaRef checks) {
	if (!dest.HasValidSystem() || GetFlightState() != FLYING || warmup_time < 1)
		return HYPERJUMP_SAFETY_LOCKOUT;
	StarSystem *s = Pi::game->GetSpace()->GetStarSystem().Get();
	if (s && s->GetPath().IsSameSystem(dest))
		return HYPERJUMP_CURRENT_SYSTEM;

	m_hyperspace.dest = dest;
	m_hyperspace.countdown = warmup_time;
	m_hyperspace.now = false;
	m_hyperspace.duration = duration;
	m_hyperspace.checks = checks;

	return Ship::HYPERJUMP_OK;
}
Пример #4
0
Ship::HyperjumpStatus Ship::GetHyperspaceDetails(const SystemPath &dest, int &outFuelRequired, double &outDurationSecs)
{
	assert(dest.HasValidSystem());

	outFuelRequired = 0;
	outDurationSecs = 0.0;

	UpdateStats();

	if (GetFlightState() == HYPERSPACE)
		return HYPERJUMP_DRIVE_ACTIVE;

	Equip::Type t = m_equipment.Get(Equip::SLOT_ENGINE);
	Equip::Type fuelType = GetHyperdriveFuelType();
	int hyperclass = Equip::types[t].pval;
	int fuel = m_equipment.Count(Equip::SLOT_CARGO, fuelType);
	if (hyperclass == 0)
		return HYPERJUMP_NO_DRIVE;

	StarSystem *s = Pi::game->GetSpace()->GetStarSystem().Get();
	if (s && s->GetPath().IsSameSystem(dest))
		return HYPERJUMP_CURRENT_SYSTEM;

	float dist = distance_to_system(dest);

	outFuelRequired = Pi::CalcHyperspaceFuelOut(hyperclass, dist, m_stats.hyperspace_range_max);
	double m_totalmass = GetMass()/1000;
	if (dist > m_stats.hyperspace_range_max) {
		outFuelRequired = 0;
		return HYPERJUMP_OUT_OF_RANGE;
	} else if (fuel < outFuelRequired) {
		return HYPERJUMP_INSUFFICIENT_FUEL;
	} else {
		outDurationSecs = Pi::CalcHyperspaceDuration(hyperclass, m_totalmass, dist);

		if (outFuelRequired <= fuel) {
			return HYPERJUMP_OK;
		} else {
			return HYPERJUMP_INSUFFICIENT_FUEL;
		}
	}
}
Пример #5
0
/*
 * Method: DistanceTo
 *
 * Calculate the distance between this and another system
 *
 * > dist = system:DistanceTo(system)
 *
 * Parameters:
 *
 *   system - a <SystemPath> or <StarSystem> to calculate the distance to
 *
 * Return:
 *
 *   dist - the distance, in light years
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_starsystem_distance_to(lua_State *l)
{
    LUA_DEBUG_START(l);

    StarSystem *s = LuaStarSystem::CheckFromLua(1);
    const SystemPath *loc1 = &(s->GetPath());

    const SystemPath *loc2 = LuaSystemPath::GetFromLua(2);
    if (!loc2) {
        StarSystem *s2 = LuaStarSystem::CheckFromLua(2);
        loc2 = &(s2->GetPath());
    }

    Sector sec1(loc1->sectorX, loc1->sectorY, loc1->sectorZ);
    Sector sec2(loc2->sectorX, loc2->sectorY, loc2->sectorZ);

    double dist = Sector::DistanceBetween(&sec1, loc1->systemIndex, &sec2, loc2->systemIndex);

    lua_pushnumber(l, dist);

    LUA_DEBUG_END(l, 1);
    return 1;
}
Пример #6
0
/*
 * Method: DistanceTo
 *
 * Calculate the distance between this and another system
 *
 * > dist = system:DistanceTo(system)
 *
 * Parameters:
 *
 *   system - a <SystemPath> or <StarSystem> to calculate the distance to
 *
 * Return:
 *
 *   dist - the distance, in light years
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_starsystem_distance_to(lua_State *l)
{
	PROFILE_SCOPED()
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	const SystemPath *loc1 = &(s->GetPath());

	const SystemPath *loc2 = LuaObject<SystemPath>::GetFromLua(2);
	if (!loc2) {
		StarSystem *s2 = LuaObject<StarSystem>::CheckFromLua(2);
		loc2 = &(s2->GetPath());
	}

	RefCountedPtr<const Sector> sec1 = Pi::GetGalaxy()->GetSector(*loc1);
	RefCountedPtr<const Sector> sec2 = Pi::GetGalaxy()->GetSector(*loc2);

	double dist = Sector::DistanceBetween(sec1, loc1->systemIndex, sec2, loc2->systemIndex);

	lua_pushnumber(l, dist);

	LUA_DEBUG_END(l, 1);
	return 1;
}
Пример #7
0
/*
 * Method: GetNearbySystems
 *
 * Get a list of nearby <StarSystems> that match some criteria
 *
 * > systems = system:GetNearbySystems(range, filter)
 *
 * Parameters:
 *
 *   range - distance from this system to search, in light years
 *
 *   filter - an optional function. If specified the function will be called
 *            once for each candidate system with the <StarSystem> object
 *            passed as the only parameter. If the filter function returns
 *            true then the system will be included in the array returned by
 *            <GetNearbySystems>, otherwise it will be omitted. If no filter
 *            function is specified then all systems in range are returned.
 *
 * Return:
 *
 *  systems - an array of systems in range that matched the filter
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_get_nearby_systems(lua_State *l)
{
    LUA_DEBUG_START(l);

    StarSystem *s = LuaStarSystem::CheckFromLua(1);
    double dist_ly = luaL_checknumber(l, 2);

    bool filter = false;
    if (lua_gettop(l) >= 3) {
        luaL_checktype(l, 3, LUA_TFUNCTION); // any type of function
        filter = true;
    }

    lua_newtable(l);

    SystemPath here = s->GetPath();

    int here_x = here.sectorX;
    int here_y = here.sectorY;
    int here_z = here.sectorZ;
    Uint32 here_idx = here.systemIndex;
    Sector here_sec(here_x, here_y, here_z);

    int diff_sec = int(ceil(dist_ly/Sector::SIZE));

    for (int x = here_x-diff_sec; x <= here_x+diff_sec; x++) {
        for (int y = here_y-diff_sec; y <= here_y+diff_sec; y++) {
            for (int z = here_z-diff_sec; z <= here_z+diff_sec; z++) {
                Sector sec(x, y, z);

                for (unsigned int idx = 0; idx < sec.m_systems.size(); idx++) {
                    if (x == here_x && y == here_y && z == here_z && idx == here_idx)
                        continue;

                    if (Sector::DistanceBetween(&here_sec, here_idx, &sec, idx) > dist_ly)
                        continue;

                    RefCountedPtr<StarSystem> sys = StarSystem::GetCached(SystemPath(x, y, z, idx));
                    if (filter) {
                        lua_pushvalue(l, 3);
                        LuaStarSystem::PushToLua(sys.Get());
                        lua_call(l, 1, 1);
                        if (!lua_toboolean(l, -1)) {
                            lua_pop(l, 1);
                            continue;
                        }
                        lua_pop(l, 1);
                    }

                    lua_pushinteger(l, lua_rawlen(l, -1)+1);
                    LuaStarSystem::PushToLua(sys.Get());
                    lua_rawset(l, -3);
                }
            }
        }
    }

    LUA_DEBUG_END(l, 1);

    return 1;
}