コード例 #1
0
ファイル: LuaShip.cpp プロジェクト: zugz/pioneer
/*
 * Method: GetStats
 *
 * Returns statistics for the ship
 *
 * > stats = ship:GetStats()
 *
 * Returns:
 *
 *   stats - a table with the following fields
 *
 *     maxCapacity - maximum space for cargo and equipment (t)
 *     usedCapacity - amount of space used (t)
 *     usedCargo - amount of cargo space used (t)
 *     freeCapacity - total space remaining (t)
 *     totalMass - total mass of the ship (cargo, equipment & hull) (t)
 *     hullMassLeft - remaining hull mass. when this reaches 0, the ship is destroyed (t)
 *     shieldMass - total mass equivalent of all shields (t)
 *     shieldMassLeft - remaining shield mass. when this reaches 0, the shields are depleted and the hull is exposed (t)
 *     hyperspaceRange - distance of furthest possible jump based on current contents (ly)
 *     maxHyperspaceRange - distance furthest possible jump under ideal conditions (ly)
 *     maxFuelTankMass - mass of internal (thruster) fuel tank, when full (t)
 *     fuelMassLeft - current mass of the internal fuel tank (t)
 *     fuelUse - thruster fuel use, scaled for the strongest thrusters at full thrust (percentage points per second, e.g. 0.0003)
 *
 * Example:
 *
 * > local stats = ship:GetStats()
 * > if stats.shieldMass == stats.shieldMassLeft then
 * >     print("shields at full strength")
 * > end
 *
 * Availability:
 *
 *  alpha 10
 *
 * Status:
 *
 *  experimental
 */
static int l_ship_get_stats(lua_State *l)
{
    LUA_DEBUG_START(l);

    Ship *s = LuaObject<Ship>::CheckFromLua(1);
    const shipstats_t &stats = s->GetStats();

    lua_newtable(l);
    pi_lua_settable(l, "maxCapacity",        stats.max_capacity);
    pi_lua_settable(l, "usedCapacity",       stats.used_capacity);
    pi_lua_settable(l, "usedCargo",          stats.used_cargo);
    pi_lua_settable(l, "freeCapacity",       stats.free_capacity);
    pi_lua_settable(l, "totalMass",          stats.total_mass);
    pi_lua_settable(l, "hullMassLeft",       stats.hull_mass_left);
    pi_lua_settable(l, "hyperspaceRange",    stats.hyperspace_range);
    pi_lua_settable(l, "maxHyperspaceRange", stats.hyperspace_range_max);
    pi_lua_settable(l, "shieldMass",         stats.shield_mass);
    pi_lua_settable(l, "shieldMassLeft",     stats.shield_mass_left);
    pi_lua_settable(l, "maxFuelTankMass",    stats.fuel_tank_mass);
    pi_lua_settable(l, "fuelUse",            stats.fuel_use);
    pi_lua_settable(l, "fuelMassLeft",       stats.fuel_tank_mass_left);

    LUA_DEBUG_END(l, 1);

    return 1;
}
コード例 #2
0
ファイル: LuaShip.cpp プロジェクト: zugz/pioneer
/*
 * Method: AddEquip
 *
 * Add an equipment or cargo item to its appropriate equipment slot
 *
 * > num_added = ship:AddEquip(item, count)
 *
 * Parameters:
 *
 *   item - a <Constants.EquipType> string for the item
 *
 *   count - optional. The number of this item to add. Defaults to 1.
 *
 * Return:
 *
 *   num_added - the number of items added. Can be less than count if there
 *               was not enough room.
 *
 * Example:
 *
 * > ship:AddEquip("ANIMAL_MEAT", 10)
 *
 * Availability:
 *
 *   alpha 10
 *
 * Status:
 *
 *   experimental
 */
static int l_ship_add_equip(lua_State *l)
{
    Ship *s = LuaObject<Ship>::CheckFromLua(1);
    Equip::Type e = static_cast<Equip::Type>(LuaConstants::GetConstantFromArg(l, "EquipType", 2));

    int num = luaL_optinteger(l, 3, 1);
    if (num < 0)
        return luaL_error(l, "Can't add a negative number of equipment items.");

    const shipstats_t &stats = s->GetStats();
    if (Equip::types[e].mass != 0)
        num = std::min(stats.free_capacity / (Equip::types[e].mass), num);

    lua_pushinteger(l, s->m_equipment.Add(e, num));
    s->UpdateEquipStats();
    return 1;
}