Proposal code: autotrader and mouse functions

This forum area is to share and discuss modding projects, models, music and other modding activities of Pioneer. Please use the 'Alpha Mods' or 'Beta Mods' folders to upload your files in the Pioneer section in the download area.
Post Reply
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

Proposal code: autotrader and mouse functions

Post by walterar »

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
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

@Darkdesire You can do a brief overview of what players can expect from these functions?
Vuzz
Private
Posts: 489
Joined: Tue Sep 05, 2017 4:34 pm

RE: Proposal code: autotrader and mouse functions

Post by Vuzz »

Playing under automatic routine ? what is the interest to play like that ? I'm a regular player of multi mmo online and that remind me somes cheat codes called "boot" used by some players to make money , but i don't think it's really "playing game".
ollobrain
Private
Posts: 561
Joined: Tue Jan 04, 2011 9:40 am

RE: Proposal code: autotrader and mouse functions

Post by ollobrain »

??? wtf
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

It is a demonstration of how to turn an idea into code. I agree with @jpab when he says that ideas are cheap, but the code is expensive. But I would like the author expose the original idea that led to his writing. That is, what will see the player. Then everyone can say if you like it or not. Or what would change. We can make a friendly environment here. :victory:
Vuzz
Private
Posts: 489
Joined: Tue Sep 05, 2017 4:34 pm

RE: Proposal code: autotrader and mouse functions

Post by Vuzz »

 We can make a friendly environment here. :victory: Of course Walter , i agree. I just simply explain why i don't like that , friendly ^^ afterall maybe somes players like this sort of gameplay.
Darkdesire
Private
Posts: 10
Joined: Thu Nov 21, 2013 5:37 am

RE: Proposal code: autotrader and mouse functions

Post by Darkdesire »

High everybody the aim is just to add some autopilot function. In this very early version, when invoke, the system goes only in local trade (searching for a station, go to the station in autopilot and give some money to simulate local trade, repeat the operation until the player stop this) BUT i just wanted to see how to code it. It was just to verify if it can be possible to give order to the player vessel. What can be done with this:- Playing pioneer with a little more strategic aspect (what i will try to do it's adding the possibility for the player to own more than one vessel and so giving order to other vessel and go for exploration for example) The next stage will be to add this function in the f3 screen (no need to modify the code, only adding lua function) That is. DD
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

Hi @Darkdesire I wanted to compile (Linux64) and I got this error: SectorView.cpp: In member function ‘void SectorView::InitObject()’: SectorView.cpp:319:26: error: ‘AUTOMATIC_SYSTEM_SELECTION’ is not a member of ‘Lang’ label = (new Gui::Label(Lang::AUTOMATIC_SYSTEM_SELECTION))->Color(255, 255, 255); Do not have much time to research, you take charge? :codemafia: What operating system do you use? Now I kept wanting try it! :sarcastichand:
Darkdesire
Private
Posts: 10
Joined: Thu Nov 21, 2013 5:37 am

RE: Proposal code: autotrader and mouse functions

Post by Darkdesire »

@walterarÎm not to my house until thursday i'll take a look asapThank´s for your interrest
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

@Darkdesire No problem, I can wait until Thursday :nomention:
Darkdesire
Private
Posts: 10
Joined: Thu Nov 21, 2013 5:37 am

RE: Proposal code: autotrader and mouse functions

Post by Darkdesire »

Hello Walterar as promised, this is a link with source code modified [url]http://dl.free.fr/f9VqWFNbw[/url] I've tried it => ok i'm working on Win7 with visual 2013 express
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

@Darkdesire You are a man who keeps his promises. :hi: Thanks! I will test this in Linux :mole:
Marcel
Private
Posts: 1188
Joined: Tue Dec 06, 2016 6:45 pm

RE: Proposal code: autotrader and mouse functions

Post by Marcel »

@DarkdesireThis is the type of behaviour that I would also expect of the AI ships. They should logically be trying to earn a living. That goes for pirates too. It would also be nice if a ship, once encountered by the player, would have a continuing life moving across star systems on its own. A player might become friends or enemies or rivals with the captain of an AI ship for example and possibly encounter it later with relations based on what happened the last time they met.
Vuzz
Private
Posts: 489
Joined: Tue Sep 05, 2017 4:34 pm

RE: Proposal code: autotrader and mouse functions

Post by Vuzz »

@ Marcel, Interstellar travel by spacio temporal jumps can offer opportunities of meetings rehearsals.
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

@Darkdesire I finally had time to try this. Compiles without problems on Linux. The operation is what you have promised. I walked around the solar system, collecting money. I did not feel comfortable with the operation of the mouse. I like how you solved the events. :)
Darkdesire
Private
Posts: 10
Joined: Thu Nov 21, 2013 5:37 am

RE: Proposal code: autotrader and mouse functions

Post by Darkdesire »

@Walterar Thank's for your try, i just don't understand your 2 last sentences: you were not comfortable with my mouse management ? You said everything work as promised, but when i try, it doesn't work as expected for me (if the vessel was docked => asap i launch the autotrade function, the vessel launch but do nothing else), did ou try with your last code ? (if so, i'll donwload it, compile it and give an other try) :preved:
Darkdesire
Private
Posts: 10
Joined: Thu Nov 21, 2013 5:37 am

RE: Proposal code: autotrader and mouse functions

Post by Darkdesire »

@Marcel, asap i can solve the problem encounter with the Walterar version, i will continue to expand my script. :bye:
walterar
Private
Posts: 978
Joined: Fri Nov 25, 2016 8:22 pm

RE: Proposal code: autotrader and mouse functions

Post by walterar »

@Darkdesire I just update and synchronize the master. You want to try now? [url]https://github.com/walterar/pioneer-sp.git [/url] There were many changes in the mouse area.
Post Reply

Return to “Pioneer Mods”