// weightings to use when picking a government type static int l_fac_govtype_weight(lua_State *L) { Faction *fac = l_fac_check(L, 1); const char *typeName = luaL_checkstring(L, 2); const Polit::GovType g = static_cast<Polit::GovType>(LuaConstants::GetConstant(L, "PolitGovType", typeName)); const Sint32 weight = luaL_checkinteger(L, 3); // signed as we will need to compare with signed out of Random.Int32 if (g < Polit::GOV_RAND_MIN || g > Polit::GOV_RAND_MAX) { pi_lua_warn(L, "government type out of range: Faction{%s}:govtype_weight('%s', %d)", fac->name.c_str(), typeName, weight); return 0; } if (weight < 0) { pi_lua_warn(L, "weight must a postive integer: Faction{%s}:govtype_weight('%s', %d)", fac->name.c_str(), typeName, weight); return 0; } fac->govtype_weights.push_back(std::make_pair(g, weight)); fac->govtype_weights_total += weight; lua_settop(L, 1); return 1; }
//police logo //goods/equipment availability (1-per-economy-type: aka agricultural, industrial, tourist, etc) //goods/equipment legality static int l_fac_illegal_goods_probability(lua_State *L) { Faction *fac = l_fac_check(L, 1); const char *typeName = luaL_checkstring(L, 2); const Equip::Type e = static_cast<Equip::Type>(LuaConstants::GetConstant(L, "EquipType", typeName)); const Uint32 probability = luaL_checkunsigned(L, 3); if (e < Equip::FIRST_COMMODITY || e > Equip::LAST_COMMODITY) { pi_lua_warn(L, "argument out of range: Faction{%s}:IllegalGoodsProbability('%s', %d)", fac->name.c_str(), typeName, probability); return 0; } if (probability > 100) { pi_lua_warn(L, "argument (probability 0-100) out of range: Faction{%s}:IllegalGoodsProbability('%s', %d)", fac->name.c_str(), typeName, probability); return 0; } fac->equip_legality[e] = probability; lua_settop(L, 1); return 1; }
/* * Method: GetEquip * * Get a list of equipment in a given equipment slot * * > equip = ship:GetEquip(slot, index) * > equiplist = ship:GetEquip(slot) * * Parameters: * * slot - a <Constants.EquipSlot> string for the wanted equipment type * * index - optional. The equipment position in the slot to fetch. If * specified the item at that position in the slot will be returned, * otherwise a table containing all items in the slot will be * returned instead. * * Return: * * equip - when index is specified, a <Constants.EquipType> string for the * item * * equiplist - when index is not specified, a table of zero or more * <Constants.EquipType> strings for all the items in the slot * * Availability: * * alpha 10 * * Status: * * experimental */ static int l_ship_get_equip(lua_State *l) { Ship *s = LuaObject<Ship>::CheckFromLua(1); const char *slotName = luaL_checkstring(l, 2); Equip::Slot slot = static_cast<Equip::Slot>(LuaConstants::GetConstant(l, "EquipSlot", slotName)); int size = s->m_equipment.GetSlotSize(slot); if (lua_isnumber(l, 3)) { // 2-argument version; returns the item in the specified slot index int idx = lua_tointeger(l, 3) - 1; if (idx >= size || idx < 0) { pi_lua_warn(l, "argument out of range: Ship{%s}:GetEquip('%s', %d)", s->GetLabel().c_str(), slotName, idx+1); } Equip::Type e = (idx >= 0) ? s->m_equipment.Get(slot, idx) : Equip::NONE; lua_pushstring(l, EnumStrings::GetString("EquipType", e)); return 1; } else { // 1-argument version; returns table of equipment items lua_newtable(l); for (int idx = 0; idx < size; idx++) { lua_pushinteger(l, idx+1); lua_pushstring(l, EnumStrings::GetString("EquipType", s->m_equipment.Get(slot, idx))); lua_rawset(l, -3); } return 1; } }
//commodity legality static int l_fac_illegal_goods_probability(lua_State *L) { Faction *fac = l_fac_check(L, 1); const char *typeName = luaL_checkstring(L, 2); const GalacticEconomy::Commodity e = static_cast<GalacticEconomy::Commodity>( LuaConstants::GetConstant(L, "CommodityType", typeName)); const Uint32 probability = luaL_checkunsigned(L, 3); if (probability > 100) { pi_lua_warn(L, "argument (probability 0-100) out of range: Faction{%s}:IllegalGoodsProbability('%s', %d)", fac->name.c_str(), typeName, probability); return 0; } fac->commodity_legality[e] = probability; lua_settop(L, 1); return 1; }
/* * Method: SetEquip * * Overwrite a single item of equipment in a given equipment slot * * > ship:SetEquip(slot, index, equip) * * Parameters: * * slot - a <Constants.EquipSlot> string for the equipment slot * * index - the position to store the item in * * equip - a <Constants.EquipType> string for the item * * Example: * * > -- add a laser to the rear laser mount * > ship:SetEquip("LASER", 1, "PULSECANNON_1MW") * * Availability: * * alpha 10 * * Status: * * experimental */ static int l_ship_set_equip(lua_State *l) { Ship *s = LuaObject<Ship>::CheckFromLua(1); const char *slotName = luaL_checkstring(l, 2); Equip::Slot slot = static_cast<Equip::Slot>(LuaConstants::GetConstant(l, "EquipSlot", slotName)); int idx = luaL_checkinteger(l, 3) - 1; const char *typeName = luaL_checkstring(l, 4); Equip::Type e = static_cast<Equip::Type>(LuaConstants::GetConstant(l, "EquipType", typeName)); // XXX should go through a Ship::SetEquip() wrapper method that checks mass constraints if (idx < 0 || idx >= s->m_equipment.GetSlotSize(slot)) { pi_lua_warn(l, "argument out of range: Ship{%s}:SetEquip('%s', %d, '%s')", s->GetLabel().c_str(), slotName, idx+1, typeName); return 0; } s->m_equipment.Set(slot, idx, e); s->UpdateEquipStats(); return 0; }
/* * Method: SetFuelPercent * * Sets the thruster fuel tank of the ship to the given precentage of its maximum. * * > ship:SetFuelPercent(percent) * * Parameters: * * percent - optional. A number from 0 to 100. Less then 0 will use 0 and * greater than 100 will use 100. Defaults to 100. * * Example: * * > ship:SetFuelPercent(50) * * Availability: * * alpha 20 * * Status: * * experimental */ static int l_ship_set_fuel_percent(lua_State *l) { LUA_DEBUG_START(l); Ship *s = LuaObject<Ship>::CheckFromLua(1); float percent = 100; if (lua_isnumber(l, 2)) { percent = float(luaL_checknumber(l, 2)); if (percent < 0.0f || percent > 100.0f) { pi_lua_warn(l, "argument out of range: Ship{%s}:SetFuelPercent(%g)", s->GetLabel().c_str(), percent); } } s->SetFuel(percent/100.f); LUA_DEBUG_END(l, 0); return 0; }