void Ship::SetGunState(int idx, int state) { std::string slot(idx?"laser_rear":"laser_front"); if (ScopedTable(m_equipSet).CallMethod<int>("OccupiedSpace", slot)) { m_gun[idx].state = state; } }
// returns direction in ship's frame from this ship to target lead position vector3d Ship::AIGetLeadDir(const Body *target, const vector3d& targaccel, int gunindex) { assert(target); if (ScopedTable(m_equipSet).CallMethod<int>("OccupiedSpace", "laser_front") == 0) return target->GetPositionRelTo(this).Normalized(); const vector3d targpos = target->GetPositionRelTo(this); const vector3d targvel = target->GetVelocityRelTo(this); // todo: should adjust targpos for gunmount offset double projspeed = 0; Properties().Get(gunindex?"laser_rear_speed":"laser_front_speed", projspeed); vector3d leadpos; // avoid a divide-by-zero floating point exception (very nearly zero is ok) if( !is_zero_exact(projspeed) ) { // first attempt double projtime = targpos.Length() / projspeed; leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime; // second pass projtime = leadpos.Length() / projspeed; leadpos = targpos + targvel*projtime + 0.5*targaccel*projtime*projtime; } else { // default leadpos = targpos; } return leadpos.Normalized(); }
void CargoBody::Init() { m_hitpoints = 1.0f; SetLabel(ScopedTable(m_cargo).CallMethod<std::string>("GetName")); SetMassDistributionFromModel(); m_hasSelfdestruct = true; std::vector<Color> colors; //metallic blue-orangeish color scheme colors.push_back(Color(255, 198, 64)); colors.push_back(Color(0, 222, 255)); colors.push_back(Color(255, 255, 255)); SceneGraph::ModelSkin skin; skin.SetColors(colors); skin.SetDecal("pioneer"); skin.Apply(GetModel()); GetModel()->SetColors(colors); Properties().Set("type", ScopedTable(m_cargo).CallMethod<std::string>("GetName")); }
static void registerEquipChangeListener(Player *player) { lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); LuaObject<Player>::PushToLua(player); lua_pushcclosure(l, onEquipChangeListener, 1); LuaRef lr(Lua::manager->GetLuaState(), -1); ScopedTable(player->GetEquipSet()).CallMethod("AddListener", lr); lua_pop(l, 1); LUA_DEBUG_END(l, 0); }
void Player::SetShipType(const ShipType::Id &shipId) { Ship::SetShipType(shipId); lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); LuaObject<Player>::PushToLua(this); lua_pushcclosure(l, onEquipChangeListener, 1); LuaRef lr(Lua::manager->GetLuaState(), -1); ScopedTable(m_equipSet).CallMethod("AddListener", lr); lua_pop(l, 1); LUA_DEBUG_END(l, 0); }
Player::Player(ShipType::Id shipId): Ship(shipId) { SetController(new PlayerShipController()); InitCockpit(); lua_State *l = Lua::manager->GetLuaState(); LUA_DEBUG_START(l); LuaObject<Player>::PushToLua(this); lua_pushcclosure(l, onEquipChangeListener, 1); LuaRef lr(Lua::manager->GetLuaState(), -1); ScopedTable(m_equipSet).CallMethod("AddListener", lr); lua_pop(l, 1); LUA_DEBUG_END(l, 0); }
void Ship::SetShipType(const ShipType::Id &shipId) { // clear all equipment so that any relevant capability properties (or other data) is wiped ScopedTable(m_equipSet).CallMethod("Clear", this); SetShipId(shipId); SetModel(m_type->modelName.c_str()); m_skin.SetDecal(m_type->manufacturer); m_skin.Apply(GetModel()); Init(); onFlavourChanged.emit(); if (IsType(Object::PLAYER)) Pi::game->GetWorldView()->SetCamType(Pi::game->GetWorldView()->GetCamType()); InitEquipSet(); LuaEvent::Queue("onShipTypeChanged", this); }
void Ship::InitEquipSet() { lua_State * l = Lua::manager->GetLuaState(); PropertyMap & p = Properties(); LUA_DEBUG_START(l); pi_lua_import(l, "EquipSet"); LuaTable es_class(l, -1); LuaTable slots = LuaTable(l).LoadMap(GetShipType()->slots.begin(), GetShipType()->slots.end()); m_equipSet = es_class.Call<LuaRef>("New", slots); p.Set("equipSet", ScopedTable(m_equipSet)); UpdateEquipStats(); { ScopedTable es(m_equipSet); int usedCargo = es.CallMethod<int>("OccupiedSpace", "cargo"); int totalCargo = std::min(m_stats.free_capacity + usedCargo, es.CallMethod<int>("SlotSize", "cargo")); p.Set("usedCargo", usedCargo); p.Set("totalCargo", totalCargo); } lua_pop(l, 2); LUA_DEBUG_END(l, 0); }
bool Ship::OnCollision(Object *b, Uint32 flags, double relVel) { // hitting space station docking surfaces shouldn't do damage if (b->IsType(Object::SPACESTATION) && (flags & 0x10)) { return true; } // hitting cargo scoop surface shouldn't do damage int cargoscoop_cap = 0; Properties().Get("cargo_scoop_cap", cargoscoop_cap); if (cargoscoop_cap > 0 && b->IsType(Object::CARGOBODY) && !dynamic_cast<Body*>(b)->IsDead()) { LuaRef item = dynamic_cast<CargoBody*>(b)->GetCargoType(); if (LuaObject<Ship>::CallMethod<int>(this, "AddEquip", item) > 0) { // try to add it to the ship cargo. Pi::game->GetSpace()->KillBody(dynamic_cast<Body*>(b)); if (this->IsType(Object::PLAYER)) Pi::game->log->Add(stringf(Lang::CARGO_SCOOP_ACTIVE_1_TONNE_X_COLLECTED, formatarg("item", ScopedTable(item).CallMethod<std::string>("GetName")))); // XXX SfxManager::Add(this, TYPE_SCOOP); UpdateEquipStats(); return true; } if (this->IsType(Object::PLAYER)) Pi::game->log->Add(Lang::CARGO_SCOOP_ATTEMPTED); } if (b->IsType(Object::PLANET)) { // geoms still enabled when landed if (m_flightState != FLYING) return false; else { if (GetVelocity().Length() < MAX_LANDING_SPEED) { m_testLanded = true; return true; } } } if ( b->IsType(Object::CITYONPLANET) || b->IsType(Object::SHIP) || b->IsType(Object::PLAYER) || b->IsType(Object::SPACESTATION) || b->IsType(Object::PLANET) || b->IsType(Object::STAR) || b->IsType(Object::CARGOBODY)) { LuaEvent::Queue("onShipCollided", this, b->IsType(Object::CITYONPLANET) ? dynamic_cast<CityOnPlanet*>(b)->GetPlanet() : dynamic_cast<Body*>(b)); } return DynamicBody::OnCollision(b, flags, relVel); }
Widget *Context::CallTemplate(const char *name) { return CallTemplate(name, ScopedTable(m_lua->GetLuaState())); }
Widget *Context::CallTemplate(const char *name, const LuaTable &args) { return ScopedTable(m_templateStore).Call<UI::Widget *>(name, args); }