Proposal code: autotrader and mouse functions
Posted: Thu Apr 17, 2014 6:41 pm
Author: Darkdesire-- /pioneersp/src/KeyBindings.inc.h+++ /ShipController/KeyBindings.inc.h@@ -21,7 +21,7 @@BINDING_GROUP(Lang::WEAPONS)KEY_BINDING(targetObject, "BindTargetObject", Lang::TARGET_OBJECT_IN_SIGHTS, SDLK_t, 0)-KEY_BINDING(fireLaser, "BindFireLaser", Lang::FIRE_LASER, SDLK_SPACE, 0)+KEY_BINDING(fireLaser, "BindFireLaser", Lang::FIRE_LASER, 0, 0)KEY_BINDING(fireMissile, "BindFireMissile", Lang::FIRE_MISSILE, SDLK_m, 0)BINDING_GROUP(Lang::SHIP_ORIENTATION)@@ -40,6 +40,8 @@KEY_BINDING(thrustLeft, "BindThrustLeft", Lang::THRUSTER_PORT, SDLK_j, 0)KEY_BINDING(thrustRight, "BindThrustRight", Lang::THRUSTER_STARBOARD, SDLK_l, 0)KEY_BINDING(thrustLowPower, "BindThrustLowPower", Lang::USE_LOW_THRUST, SDLK_LSHIFT, 0)+KEY_BINDING(FlightFollowMouse, "BindFlightFollowMouse", Lang::FLIGHT_FOLLOW_MOUSE, SDLK_SPACE, 0)+KEY_BINDING(ActivateAutoTrade, "BindActivateAutoTrade", Lang::ACTIVATE_AUTO_TRADE, SDLK_b, 0)BINDING_GROUP(Lang::SPEED_CONTROL_MODE)KEY_BINDING(increaseSpeed, "BindIncreaseSpeed", Lang::INCREASE_SET_SPEED, SDLK_RETURN, 0)--- /pioneersp/src/LangStrings.inc.h+++ /ShipController/LangStrings.inc.h@@ -218,7 +218,8 @@DECLARE_STRING(QUADRUPLE_SYSTEM)DECLARE_STRING(TRIPLE_SYSTEM)DECLARE_STRING(BINARY_SYSTEM)-DECLARE_STRING(AUTOMATIC_SYSTEM_SELECTION)+DECLARE_STRING(ENABLED_AUTOMATIC_SYSTEM_SELECTION)+DECLARE_STRING(DISABLED_AUTOMATIC_SYSTEM_SELECTION)DECLARE_STRING(FUEL_SCOOP_ACTIVE_N_TONNES_H_COLLECTED)DECLARE_STRING(CARGO_SCOOP_ACTIVE_1_TONNE_X_COLLECTED)DECLARE_STRING(CARGO_BAY_LIFE_SUPPORT_LOST)@@ -430,6 +431,7 @@DECLARE_STRING(HULL_INTEGRITY)DECLARE_STRING(SHIELD_INTEGRITY)DECLARE_STRING(FUEL)+DECLARE_STRING(LAUNCH_PERMISSION_DENIED_BUSY)DECLARE_STRING(HYPERSPACE_JUMP_ABORTED)DECLARE_STRING(LANDED)DECLARE_STRING(DOCKING)@@ -491,3 +493,5 @@DECLARE_STRING(MONTH_DEC)DECLARE_STRING(MISCELLANEOUS)DECLARE_STRING(PAUSED)+DECLARE_STRING(FLIGHT_FOLLOW_MOUSE)+DECLARE_STRING(ACTIVATE_AUTO_TRADE)--- /pioneersp/src/ShipController.cpp+++ /ShipController/ShipController.cpp@@ -11,6 +11,7 @@#include "Space.h"#include "WorldView.h"#include "OS.h"+#include "LuaEvent.h"void ShipController::StaticUpdate(float timeStep){@@ -33,7 +34,8 @@m_setSpeed(0.0),m_flightControlState(CONTROL_MANUAL),m_lowThrustPower(0.25), // note: overridden by the default value in GameConfig.cpp (DefaultLowThrustPower setting)- m_mouseDir(0.0)+ m_mouseDir(0.0),+ m_AutoTradeActivated(false){float deadzone = Pi::config->Float("JoystickDeadzone");m_joystickDeadzone = deadzone * deadzone;@@ -46,12 +48,22 @@m_fireMissileKey = KeyBindings::fireMissile.onPress.connect(sigc::mem_fun(this, &PlayerShipController::FireMissile));+ m_FlightMouseFollowing = KeyBindings::FlightFollowMouse.onPress.connect(+ sigc::mem_fun(this, &PlayerShipController::FlightMouseFollowing));++ m_ActivateAutoTrade = KeyBindings::ActivateAutoTrade.onPress.connect(+ sigc::mem_fun(this, &PlayerShipController::ActivateAutoTrade));++ m_IncreaseSetSpeed = Pi::onMouseWheel.connect(sigc::mem_fun(this, &PlayerShipController::IncreaseSetSpeed));}PlayerShipController::~PlayerShipController(){m_connRotationDampingToggleKey.disconnect();m_fireMissileKey.disconnect();+ m_FlightMouseFollowing.disconnect();+ m_IncreaseSetSpeed.disconnect();+ m_ActivateAutoTrade.disconnect();}void PlayerShipController::Save(Serializer::Writer &wr, Space *space)@@ -171,53 +183,71 @@// have to use this function. SDL mouse position event is bugged in windowsint mouseMotion[2];- SDL_GetRelativeMouseState (mouseMotion+0, mouseMotion+1); // call to flush- if (Pi::MouseButtonState(SDL_BUTTON_RIGHT))+ SDL_GetRelativeMouseState (mouseMotion+0, mouseMotion+1); // call to flush+ // if (Pi::MouseButtonState(SDL_BUTTON_RIGHT))+// if (KeyBindings::FlightFollowMouse.IsActive())+// {+// if (Pi::FlightModel==0) Pi::FlightModel=1;+// else+// Pi::FlightModel=0;+// }+ if (Pi::FlightModel==1){+ int x,y;+ SDL_GetMouseState(&x,&y);+ x-=Pi::renderer->GetWindow()->GetWidth()/2;+ y-=Pi::renderer->GetWindow()->GetHeight()/2;+ if ((x > -5) && (x < 5)) x = 0;+ if ((y > -5) && (y < 5)) y = 0;const matrix3x3d &rot = m_ship->GetOrient();- if (!m_mouseActive) {- m_mouseDir = -rot.VectorZ(); // in world space- m_mouseX = m_mouseY = 0;+// if (!m_mouseActive)+ {+ m_mouseDir = -rot.VectorZ(); // in world space+ // m_mouseX = m_mouseY = 0;m_mouseActive = true;}vector3d objDir = m_mouseDir * rot;const double radiansPerPixel = 0.00002 * m_fovY;- const int maxMotion = std::max(abs(mouseMotion[0]), abs(mouseMotion[1]));- const double accel = Clamp(maxMotion / 4.0, 0.0, 90.0 / m_fovY);-- m_mouseX += mouseMotion[0] * accel * radiansPerPixel;- double modx = clipmouse(objDir.x, m_mouseX);- m_mouseX -= modx;+ const int maxMotion = std::max(abs(x), abs(y));+ const double accel = Clamp(maxMotion / 12.0, 0.0, 90.0 / m_fovY/20.0);++ m_mouseX = x * accel * radiansPerPixel;+ m_mouseX = clipmouse(objDir.x, m_mouseX);const bool invertY = (Pi::IsMouseYInvert() ? !m_invertMouse : m_invertMouse);- m_mouseY += mouseMotion[1] * accel * radiansPerPixel * (invertY ? -1 : 1);- double mody = clipmouse(objDir.y, m_mouseY);- m_mouseY -= mody;-- if(!is_zero_general(modx) || !is_zero_general(mody)) {- matrix3x3d mrot = matrix3x3d::RotateY(modx) * matrix3x3d::RotateX(mody);+ m_mouseY = y * accel * radiansPerPixel * (invertY ? -1 : 1);+ m_mouseY = clipmouse(objDir.y, m_mouseY);++ if(!is_zero_general(m_mouseX) || !is_zero_general(m_mouseY))+ {+ matrix3x3d mrot = matrix3x3d::RotateY(m_mouseX) * matrix3x3d::RotateX(m_mouseY);m_mouseDir = (rot * (mrot * objDir)).Normalized();}}else m_mouseActive = false;- if (m_flightControlState == CONTROL_FIXSPEED) {+ if (m_flightControlState == CONTROL_FIXSPEED)+ {double oldSpeed = m_setSpeed;- if (stickySpeedKey) {- if (!(KeyBindings::increaseSpeed.IsActive() || KeyBindings::decreaseSpeed.IsActive())) {+ if (stickySpeedKey)+ {+ if (!(KeyBindings::increaseSpeed.IsActive() || KeyBindings::decreaseSpeed.IsActive()))+ {stickySpeedKey = false;}}- if (!stickySpeedKey) {+ if (!stickySpeedKey)+ {if (KeyBindings::increaseSpeed.IsActive())m_setSpeed += std::max(fabs(m_setSpeed)*0.05, 1.0);if (KeyBindings::decreaseSpeed.IsActive())m_setSpeed -= std::max(fabs(m_setSpeed)*0.05, 1.0);if ( ((oldSpeed < 0.0) && (m_setSpeed >= 0.0)) ||- ((oldSpeed > 0.0) && (m_setSpeed <= 0.0)) ) {+ ((oldSpeed > 0.0) && (m_setSpeed <= 0.0)) )+ {// flipped from going forward to backwards. make the speed 'stick' at zero// until the player lets go of the key and presses it againstickySpeedKey = true;@@ -233,9 +263,11 @@if (KeyBindings::thrustLeft.IsActive()) m_ship->SetThrusterState(0, -linearThrustPower);if (KeyBindings::thrustRight.IsActive()) m_ship->SetThrusterState(0, linearThrustPower);- if (KeyBindings::fireLaser.IsActive() || (Pi::MouseButtonState(SDL_BUTTON_LEFT) && Pi::MouseButtonState(SDL_BUTTON_RIGHT))) {- //XXX worldview? madness, ask from ship instead- m_ship->SetGunState(Pi::worldView->GetActiveWeapon(), 1);+ // if (KeyBindings::fireLaser.IsActive() || (Pi::MouseButtonState(SDL_BUTTON_LEFT) && Pi::MouseButtonState(SDL_BUTTON_RIGHT)))+ if (Pi::MouseButtonState(SDL_BUTTON_RIGHT))+ {+ //XXX worldview? madness, ask from ship instead+ m_ship->SetGunState(Pi::worldView->GetActiveWeapon(), 1);}if (KeyBindings::yawLeft.IsActive()) wantAngVel.y += 1.0;@@ -254,17 +286,20 @@changeVec.z = KeyBindings::rollAxis.GetValue();// Deadzone more accurate- for (int axis=0; axis<3; axis++) {- if (fabs(changeVec[axis]) < m_joystickDeadzone)- changeVec[axis]=0.0;- else- changeVec[axis] = changeVec[axis] * 2.0;+ for (int axis=0; axis<3; axis++)+ {+ if (fabs(changeVec[axis]) < m_joystickDeadzone)+ changeVec[axis]=0.0;+ else+ changeVec[axis] = changeVec[axis] * 2.0;}wantAngVel += changeVec;- if (wantAngVel.Length() >= 0.001 || force_rotation_damping || m_rotationDamping) {- if (Pi::game->GetTimeAccel()!=Game::TIMEACCEL_1X) {+ if (wantAngVel.Length() >= 0.001 || force_rotation_damping || m_rotationDamping)+ {+ if (Pi::game->GetTimeAccel()!=Game::TIMEACCEL_1X)+ {for (int axis=0; axis<3; axis++)wantAngVel[axis] = wantAngVel[axis] * Pi::game->GetInvTimeAccelRate();}@@ -347,9 +382,36 @@{if (!Pi::player->GetCombatTarget())return;- LuaObject<Ship>::CallMethod<bool>(Pi::player, "FireMissileAt", "any", static_cast<Ship*>(Pi::player->GetCombatTarget()));-}-++ lua_State *l = Lua::manager->GetLuaState();+ int pristine_stack = lua_gettop(l);+ LuaObject<Ship>::PushToLua(Pi::player);+ lua_pushstring(l, "FireMissileAt");+ lua_gettable(l, -2);+ lua_pushvalue(l, -2);+ lua_pushstring(l, "any");+ LuaObject<Ship>::PushToLua(static_cast<Ship*>(Pi::player->GetCombatTarget()));+ lua_call(l, 3, 1);+ lua_settop(l, pristine_stack);+}++void PlayerShipController::FlightMouseFollowing()+{+ switch (Pi::FlightModel)+ {+ case 0:+ {+ Pi::FlightModel = 1;+ break;+ }+ case 1:+ {+ Pi::FlightModel = 0;+ break;+ }+ }+}+Body *PlayerShipController::GetCombatTarget() const{return m_combatTarget;@@ -381,4 +443,38 @@else if (m_setSpeedTarget == m_navTarget)m_setSpeedTarget = 0;m_navTarget = target;-}+}++void PlayerShipController::IncreaseSetSpeed(bool up)+{+ if (Pi::MouseWheel == 0)+ {+ if (up)+ {+ m_setSpeed += std::max(fabs(m_setSpeed)*0.05, 1.0);+ }+ else+ {+ m_setSpeed -= std::max(fabs(m_setSpeed)*0.05, 1.0);+ }+ }+}++void PlayerShipController::ActivateAutoTrade()+{+ switch (m_AutoTradeActivated)+ {+ case true:+ {+ LuaEvent::Queue("onActivateAutoTradeOFF", Pi::player);+ m_AutoTradeActivated = false;+ break;+ }+ case false:+ {+ LuaEvent::Queue("onActivateAutoTradeON", Pi::player);+ m_AutoTradeActivated = true;+ break;+ }+ }+}--- /pioneersp/src/ShipController.h+++ /ShipController/ShipController.h@@ -69,6 +69,9 @@void SetRotationDamping(bool enabled);void ToggleRotationDamping();void FireMissile();+ void FlightMouseFollowing();+ void IncreaseSetSpeed(bool up);+ void ActivateAutoTrade();//targeting//XXX AI should utilize one or more of these@@ -92,6 +95,7 @@bool m_invertMouse; // used for rear view, *not* for invert Y-axis option (which is Pi::IsMouseYInvert)bool m_mouseActive;bool m_rotationDamping;+ bool m_AutoTradeActivated;double m_mouseX;double m_mouseY;double m_setSpeed;@@ -106,6 +110,9 @@sigc::connection m_connRotationDampingToggleKey;sigc::connection m_fireMissileKey;+ sigc::connection m_FlightMouseFollowing;+ sigc::connection m_IncreaseSetSpeed;+ sigc::connection m_ActivateAutoTrade;};#endif