Пример #1
0
/*
 * Method: ExportToLua
 *
 * Export of generated system for personal interest, customisation, etc
 *
 * Availability:
 *
 *   alpha 33
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_export_to_lua(lua_State *l)
{
	PROFILE_SCOPED()
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);

	static const std::string EXPORTED_SYSTEMS_DIR_NAME("exported_systems");
	if (!FileSystem::userFiles.MakeDirectory(EXPORTED_SYSTEMS_DIR_NAME)) {
		throw CouldNotOpenFileException();
	}

	// construct the filename with folder and extension
	try {
		const std::string filename(EXPORTED_SYSTEMS_DIR_NAME + "/" + FileSystem::SanitiseFileName(s->GetName()) + ".lua");
		const std::string finalPath = FileSystem::NormalisePath(
				FileSystem::JoinPathBelow(FileSystem::GetUserDir(), filename));
		s->ExportToLua(finalPath.c_str());
	} catch (std::invalid_argument &) {
		return luaL_error(l, "could not export system -- name forms an invalid path");
	}

	LUA_DEBUG_END(l, 0);
	return 0;
}
Пример #2
0
/*
 * Attribute: name
 *
 * The name of the system. This is usually the same as the name of the primary
 * star.
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_starsystem_attr_name(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	lua_pushstring(l, s->GetName().c_str());
	return 1;
}
Пример #3
0
//Sends a fleet back to it's home planet
void TaeTurn::sendHome(uint32_t fleet) {
    Game* game = Game::getGame();
    ObjectManager* obm = game->getObjectManager();
    ObjectTypeManager* obtm = game->getObjectTypeManager();
    PlayerManager::Ptr pm = game->getPlayerManager();

    IGObject::Ptr fleetobj = obm->getObject(fleet);
    //Check to make sure it is really a fleet
    if(fleetobj->getType() != obtm->getObjectTypeByName("Fleet")) {
        return;
    }

    //Get all the required objects
    Fleet* f = (Fleet*) fleetobj->getObjectBehaviour();
    Player::Ptr p = pm->getPlayer(f->getOwner());
    IGObject::Ptr sys = obm->getObject(fleetobj->getParent());
    StarSystem* sysData = (StarSystem*) sys->getObjectBehaviour();

    //Remove fleet from system
    sysData->setRegion(0);
    fleetobj->removeFromParent();

    //Find it's home planet
    std::set<uint32_t> objects = obm->getAllIds();
    std::set<uint32_t>::iterator itcurr;
    for(itcurr = objects.begin(); itcurr != objects.end(); ++itcurr) {
        IGObject::Ptr ob = obm->getObject(*itcurr);
        if(ob->getName().compare(string(p->getName() + "'s Home Planet")) == 0) {
            Planet* p = (Planet*) ob->getObjectBehaviour();
            f->setPosition(p->getPosition());
            fleetobj->addToParent(ob->getID());
        }
    }
}
Пример #4
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;
}
Пример #5
0
/*
 * Attribute: lawlessness
 *
 * The lawlessness value for the system, 0 for peaceful, 1 for raging
 * hordes of pirates
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_attr_lawlessness(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	lua_pushnumber(l, s->GetSysPolit().lawlessness.ToDouble());
	return 1;
}
Пример #6
0
/*
 * Attribute: population
 *
 * The population of this system, in billions of people
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_attr_population(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	lua_pushnumber(l, s->GetTotalPop().ToDouble());
	return 1;
}
Пример #7
0
static int l_starsystem_attr_explored(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	lua_pushboolean(l, !s->GetUnexplored());
	return 1;
}
Пример #8
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;
}
Пример #9
0
/*
 * Method: GetStarSystem
 *
 * Get a <StarSystem> object for the system that this path points to
 *
 * > system = path:GetStarSystem()
 *
 * Return:
 *
 *   system - the <StarSystem>
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   stable
 */
static int l_sbodypath_get_star_system(lua_State *l)
{
	SystemPath *path = LuaSystemPath::GetFromLua(1);
	StarSystem *s = StarSystem::GetCached(path);
	LuaStarSystem::PushToLua(s);
	s->Release();
	return 1;
}
Пример #10
0
/*
 * 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 = LuaSystemPath::GetFromLua(1);
	StarSystem *s = StarSystem::GetCached(path);
	SBody *sbody = s->GetBodyByPath(path);
	LuaSBody::PushToLua(sbody);
	s->Release();
	return 1;
}
StarSystem* Universe::getStarSystem( string name )
{
    vector< StarSystem* >::iterator iter;
    for (iter = star_system.begin(); iter != star_system.end(); iter++) {
        StarSystem *ss = *iter;
        if (ss->getName() == name)
            return ss;
    }
    return NULL;
}
Пример #12
0
/*
 * Attribute: faction
 *
 * The faction that controls this system
 *
 * Availability:
 *
 *   alpha 28
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_attr_faction(lua_State *l)
{
    StarSystem *s = LuaStarSystem::CheckFromLua(1);
    if (s->GetFaction()->IsValid()) {
        LuaFaction::PushToLua(s->GetFaction());
        return 1;
    } else {
        return 0;
    }
}
Пример #13
0
/*
 * Attribute: faction
 *
 * The <Faction> that controls this system
 *
 * Availability:
 *
 *   alpha 28
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_attr_faction(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	if (s->GetFaction()->IsValid()) {
		LuaObject<Faction>::PushToLua(s->GetFaction());
		return 1;
	} else {
		return 0;
	}
}
Пример #14
0
/*
 * Attribute: faction
 *
 * The <Faction> that controls this system
 *
 * Availability:
 *
 *   alpha 28
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_attr_faction(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	if (s->GetFaction()->IsValid()) {
		LuaObject<Faction>::PushToLua(const_cast<Faction*>(s->GetFaction())); // XXX const-correctness violation
		return 1;
	} else {
		return 0;
	}
}
Пример #15
0
/*
 * Method: IsCommodityLegal
 *
 * Determine if a given cargo item is legal for trade in this system
 *
 * > is_legal = system:IsCommodityLegal(cargo)
 *
 * Parameters:
 *
 *   cargo - the wanted commodity (for instance, Equipment.cargo.hydrogen)
 *
 * Return:
 *
 *   is_legal - true if the commodity is legal, otherwise false
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_is_commodity_legal(lua_State *l)
{
	PROFILE_SCOPED()
	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	// XXX: Don't use the l10n_key hack, this is just UGLY!!
	luaL_checktype(l, 2, LUA_TTABLE);
	LuaTable(l, 2).PushValueToStack("l10n_key");
	GalacticEconomy::Commodity e = static_cast<GalacticEconomy::Commodity>(
			LuaConstants::GetConstantFromArg(l, "CommodityType", -1));
	lua_pushboolean(l, s->IsCommodityLegal(e));
	return 1;
}
Пример #16
0
StarSystem*
Galaxy::FindSystemByRegion(const char* rgn_name)
{
    ListIter<StarSystem> iter = systems;
    while (++iter) {
        StarSystem* sys = iter.value();
        if (sys->FindRegion(rgn_name))
        return sys;
    }

    return 0;
}
void Mission::findNextEnemyTarget( Unit *my_unit )
{
    StarSystem *ssystem = _Universe->scriptStarSystem();
    un_iter     uiter( ssystem->getUnitList().createIterator() );
    Unit *unit;
    Unit *target_unit   = NULL;
    for (; (unit = *uiter); ++uiter)
        if (my_unit->getRelation( unit ) < 0.0) {
            target_unit = *uiter;
            break;
        }
    if (target_unit)
        my_unit->Target( target_unit );
}
Пример #18
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;
}
Пример #19
0
/*
 * Method: Explore
 *
 * Set the star system to be explored by the Player.
 *
 * > system:Explore(time)
 *
 * Parameters:
 *
 *   time - optional, the game time at which the system was explored.
 *          Defaults to current game time.
 *
 * Availability:
 *
 *   October 2014
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_explore(lua_State *l)
{
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	double time;
	if (lua_isnumber(l, 2))
		time = luaL_checknumber(l, 2);
	else
		time = Pi::game->GetTime();

	s->ExploreSystem(time);

	LUA_DEBUG_END(l,0);
	return 0;
}
Пример #20
0
void Colonize::inputFrame(InputFrame::Ptr f, uint32_t playerid) {
    FleetOrder::inputFrame(f, playerid);

    Game *game = Game::getGame();
    ObjectManager *obm = game->getObjectManager();
    ObjectTypeManager *obtm = game->getObjectTypeManager();

    IGObject::Ptr starSysObj = obm->getObject(starSys->getObjectId());
    StarSystem* starSysData = (StarSystem*) starSysObj->getObjectBehaviour();

    // Check to see if it is a legal system to colonize
    if(starSysObj->getType() == obtm->getObjectTypeByName("Star System") && !starSysData->canBeColonized(isMining)) {
        starSys->setObjectId(0);
        Logger::getLogger()->debug("Player tried to colonize a system which cannot be colonized.");
    }        
}
Пример #21
0
void Galaxy::randomize(size_t numSystems) {
  for (size_t i = 0; i < numSystems; i++) {
    StarSystem *starSystem = new StarSystem();
    if (starSystem == NULL) {
      printf("Galaxy::Galaxy(): out of memory\n");
      return;
    }

    starSystem->mHandle = mNextHandle++;
    starSystem->mPosition.x = r_num(0.0, GALACTIC_WIDTH);
    starSystem->mPosition.y = r_num(0.0, GALACTIC_HEIGHT);

    starSystem->randomize();
    mNextHandle += starSystem->mPlanets.size();

    mStarSystems.push_back(starSystem);
  }
}
Пример #22
0
/*
 * Method: GetCommodityBasePriceAlterations
 *
 * Get the price alterations for cargo items bought and sold in this system
 *
 * > alterations = system:GetCommodityBasePriceAlterations()
 *
 * Return:
 *
 *   alterations - a table. The keys are <Constants.EquipType> strings for
 *                 each cargo. The values are numbers that indicate the
 *                 percentage change to each cargo base price. Loosely,
 *                 positive values make the commodity more expensive,
 *                 indicating it is in demand, while negative values make the
 *                 commodity cheaper, indicating a surplus.
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_get_commodity_base_price_alterations(lua_State *l)
{
    LUA_DEBUG_START(l);

    StarSystem *s = LuaStarSystem::CheckFromLua(1);

    lua_newtable(l);

    for (int e = Equip::FIRST_COMMODITY; e <= Equip::LAST_COMMODITY; e++) {
        lua_pushstring(l, EnumStrings::GetString("EquipType", e));
        lua_pushnumber(l, s->GetCommodityBasePriceModPercent(e));
        lua_rawset(l, -3);
    }

    LUA_DEBUG_END(l, 1);

    return 1;
}
Пример #23
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;
		}
	}
}
Пример #24
0
/*
 * Method: GetBodyPaths
 *
 * Get the <SystemPaths> to bodies (planets, stations, starports) in this system
 *
 * > paths = system:GetBodyPaths()
 *
 * Return:
 *
 *   paths - an array of <SystemPath> objects, one for each <SystemBody>
 *
 * Availability:
 *
 *   alpha 13
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_get_body_paths(lua_State *l)
{
	PROFILE_SCOPED()
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);

	lua_newtable(l);

	for (RefCountedPtr<const SystemBody> sb : s->GetBodies())
	{
		lua_pushinteger(l, lua_rawlen(l, -1)+1);
		LuaObject<SystemPath>::PushToLua(&sb->GetPath());
		lua_rawset(l, -3);
	}

	LUA_DEBUG_END(l, 1);

	return 1;
}
Пример #25
0
/*
 * Method: GetStationPaths
 *
 * Get the <SystemPaths> to stations in this system
 *
 * > paths = system:GetStationPaths()
 *
 * Return:
 *
 *   paths - an array of <SystemPath> objects, one for each space station
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_get_station_paths(lua_State *l)
{
	PROFILE_SCOPED()
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);

	lua_newtable(l);

	for (const SystemBody *station : s->GetSpaceStations())
	{
		lua_pushinteger(l, lua_rawlen(l, -1)+1);
		LuaObject<SystemPath>::PushToLua(&station->GetPath());
		lua_rawset(l, -3);
	}

	LUA_DEBUG_END(l, 1);

	return 1;
}
Пример #26
0
void Galaxy::load(FILE* file) {
  // clear out anything that might be in memory
  clear();

  // load the members
  fread(&mNextHandle, sizeof mNextHandle, 1, file);

  // load from the file
  size_t numSystems;
  fread(&numSystems, sizeof numSystems, 1, file);
  for (size_t i = 0; i < numSystems; i++) {
    StarSystem* starSystem = new StarSystem();
    if (starSystem == NULL) {
      printf("Galaxy::load(): error: out of memory\n");
      return;
    }
    starSystem->load(file);
    mStarSystems.push_back(starSystem);
  }
}
Пример #27
0
/*
 * Method: GetCommodityBasePriceAlterations
 *
 * Get the price alterations for cargo items bought and sold in this system
 *
 * > alteration = system:GetCommodityBasePriceAlterations(cargo_item)
 *
 * Parameters:
 *
 *   cargo_item - The cargo item for which one wants to know the alteration
 * Return:
 *
 *   percentage -  percentage change to the cargo base price. Loosely,
 *                 positive values make the commodity more expensive,
 *                 indicating it is in demand, while negative values make the
 *                 commodity cheaper, indicating a surplus.
 *
 * Availability:
 *
 *   June 2014
 *
 * Status:
 *
 *   experimental
 */
static int l_starsystem_get_commodity_base_price_alterations(lua_State *l)
{
	PROFILE_SCOPED()
	LUA_DEBUG_START(l);

	StarSystem *s = LuaObject<StarSystem>::CheckFromLua(1);
	LuaTable equip(l, 2);

	if (!equip.CallMethod<bool>("IsValidSlot", "cargo")) {
		luaL_error(l, "GetCommodityBasePriceAlterations takes a valid cargo item as argument.");
		return 0;
	}
	equip.PushValueToStack("l10n_key"); // For now let's just use this poor man's hack.
	GalacticEconomy::Commodity e = static_cast<GalacticEconomy::Commodity>(
			LuaConstants::GetConstantFromArg(l, "CommodityType", -1));
	lua_pop(l, 1);
	lua_pushnumber(l, s->GetCommodityBasePriceModPercent(e));

	LUA_DEBUG_END(l, 1);
	return 1;
}
void
MsnEditNavDlg::ScrapeForm()
{
	if (mission) {
		if (txt_name) {
			mission->SetName(txt_name->GetText());
		}

		if (cmb_type) {
			mission->SetType(cmb_type->GetSelectedIndex());

			if (mission_info)
			mission_info->type = cmb_type->GetSelectedIndex();
		}

		Galaxy*     galaxy = Galaxy::GetInstance();
		StarSystem* system = 0;

		if (galaxy)
		system = galaxy->GetSystem(cmb_system->GetSelectedItem());

		if (cmb_system && system) {
			mission->ClearSystemList();
			mission->SetStarSystem(system);

			if (mission_info)
			mission_info->system = system->Name();
		}

		if (cmb_region) {
			mission->SetRegion(cmb_region->GetSelectedItem());

			if (mission_info)
			mission_info->region = cmb_region->GetSelectedItem();
		}

		SetSystem(system);
	}
}
Пример #29
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;
}
Пример #30
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;
}