RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
OutGauge.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "OutGauge.h"
23 
24 #include "Application.h"
25 #include "Actor.h"
26 #include "ActorManager.h"
27 #include "DashBoardManager.h"
28 #include "Engine.h"
29 #include "RoRVersion.h"
30 
31 #include <Ogre.h>
32 
33 using namespace Ogre;
34 using namespace RoR;
35 
36 OutGauge::OutGauge(void) :
37  sockfd(-1)
38  , timer(0)
39  , working(false)
40 {
41 }
42 
43 void OutGauge::Close(void)
44 {
45  if (sockfd != 0)
46  {
47 #ifdef USE_SOCKETW
48 # if _WIN32
49  closesocket(sockfd);
50 # else
51  close( sockfd );
52 # endif
53 #endif // USE_SOCKETW
54  sockfd = 0;
55  }
56 }
57 
59 {
60 #if defined(_WIN32) && defined(USE_SOCKETW)
61  SWBaseSocket::SWBaseError error;
62 
63  // startup winsock
64  WSADATA wsd;
65  if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
66  {
67  LOG("[RoR|OutGauge] Error starting up winsock. OutGauge disabled.");
68  return;
69  }
70 
71  // open a new socket
72  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
73  {
74  LOG(String("[RoR|OutGauge] Error creating socket for OutGauge: ").append(strerror(errno)));
75  return;
76  }
77 
78  // get the IP of the remote side, this function is compatible with windows 2000
79  hostent* remoteHost = gethostbyname(App::io_outgauge_ip->getStr().c_str());
80  char* ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);
81 
82  // init socket data
83  struct sockaddr_in sendaddr;
84  memset(&sendaddr, 0, sizeof(sendaddr));
85  sendaddr.sin_family = AF_INET;
86  sendaddr.sin_addr.s_addr = inet_addr(ip);
87  sendaddr.sin_port = htons(App::io_outgauge_port->getInt());
88 
89  // connect
90  if (connect(sockfd, (struct sockaddr *) &sendaddr, sizeof(sendaddr)) == SOCKET_ERROR)
91  {
92  LOG(String("[RoR|OutGauge] Error connecting socket for OutGauge: ").append(strerror(errno)));
93  return;
94  }
95 
96  LOG("[RoR|OutGauge] Connected successfully");
97  working = true;
98 #else
99  // TODO: fix linux
100 #endif // _WIN32
101 }
102 
103 bool OutGauge::Update(float dt, ActorPtr truck)
104 {
105 #if defined(_WIN32) && defined(USE_SOCKETW)
106  if (!working)
107  {
108  return false;
109  }
110 
111  // below the set delay?
112  timer += dt;
113  if (timer < (0.1f * App::io_outgauge_delay->getFloat()))
114  {
115  return true;
116  }
117  timer = 0;
118 
119  // send a package
120  OutGaugePack gd;
121  memset(&gd, 0, sizeof(gd));
122 
123  // set some common things
124  gd.Time = Root::getSingleton().getTimer()->getMilliseconds();
125  gd.ID = App::io_outgauge_id->getInt();
126  gd.Flags = 0 | OG_KM;
127  sprintf(gd.Car, "RoR");
128 
129  if (!truck)
130  {
131  // not in a truck?
132  sprintf(gd.Display2, "not in vehicle");
133  }
134  else if (truck && !truck->ar_engine)
135  {
136  // no engine?
137  sprintf(gd.Display2, "no engine");
138  }
139  else if (truck && truck->ar_engine)
140  {
141  // truck and engine valid
142  if (truck->ar_engine->hasTurbo())
143  {
144  gd.Flags |= OG_TURBO;
145  }
146  gd.Gear = std::max(0, truck->ar_engine->getGear() + 1); // we only support one reverse gear
147  gd.PLID = 0;
148  gd.Speed = fabs(truck->ar_wheel_speed);
149  gd.RPM = truck->ar_engine->getRPM();
150  gd.Turbo = truck->ar_engine->getTurboPSI() * 0.0689475729f;
151  gd.EngTemp = 0; // TODO
152  gd.Fuel = 0; // TODO
153  gd.OilPressure = 0; // TODO
154  gd.OilTemp = 0; // TODO
155 
156  gd.DashLights = 0;
157  gd.DashLights |= DL_HANDBRAKE;
158  gd.DashLights |= DL_BATTERY;
159  gd.DashLights |= DL_SIGNAL_L;
160  gd.DashLights |= DL_SIGNAL_R;
161  gd.DashLights |= DL_SIGNAL_ANY;
162  if (!truck->tc_nodash)
163  gd.DashLights |= DL_TC;
164  if (!truck->alb_nodash)
165  gd.DashLights |= DL_ABS;
166 
167  gd.ShowLights = 0;
168  if (truck->ar_parking_brake)
169  gd.ShowLights |= DL_HANDBRAKE;
170  if (truck->getHeadlightsVisible())
171  gd.ShowLights |= DL_FULLBEAM;
172  if (truck->ar_engine->hasContact() && !truck->ar_engine->isRunning())
173  gd.ShowLights |= DL_BATTERY;
175  gd.ShowLights |= DL_SIGNAL_L;
177  gd.ShowLights |= DL_SIGNAL_R;
179  gd.ShowLights |= DL_SIGNAL_ANY;
180  if (truck->tc_mode)
181  gd.ShowLights |= DL_TC;
182  if (truck->alb_mode)
183  gd.ShowLights |= DL_ABS;
184 
185  gd.Throttle = truck->ar_engine->getAcc();
186  gd.Brake = truck->ar_brake;
187  gd.Clutch = 1 - truck->ar_engine->getClutch(); // 0-1
188 
189  strncpy(gd.Display1, truck->ar_design_name.c_str(), 15);
190  if (truck->ar_design_name.length() > 15)
191  {
192  strncpy(gd.Display2, truck->ar_design_name.c_str() + 15, 15);
193  }
194  }
195  // send the package
196  send(sockfd, (const char*)&gd, sizeof(gd), NULL);
197 
198  return true;
199 #else
200  // TODO: fix linux
201  return false;
202 #endif // _WIN32
203 }
RoR::OutGauge::DL_FULLBEAM
@ DL_FULLBEAM
Definition: OutGauge.h:76
RoR::OutGauge::DL_SIGNAL_L
@ DL_SIGNAL_L
Definition: OutGauge.h:80
RoR::Actor::ar_parking_brake
bool ar_parking_brake
Definition: Actor.h:414
DashBoardManager.h
RoR::OutGauge::DL_SIGNAL_ANY
@ DL_SIGNAL_ANY
Definition: OutGauge.h:82
RoR::Actor::ar_brake
Ogre::Real ar_brake
Physics state; braking intensity.
Definition: Actor.h:399
RoR::App::io_outgauge_port
CVar * io_outgauge_port
Definition: Application.cpp:202
Engine.h
RoR::Actor::ar_dashboard
DashBoardManager * ar_dashboard
Definition: Actor.h:431
RoR::OutGauge::DL_BATTERY
@ DL_BATTERY
Definition: OutGauge.h:84
RefCountingObjectPtr< Actor >
ActorManager.h
Actor.h
RoR::Actor::tc_nodash
bool tc_nodash
Hide the dashboard indicator?
Definition: Actor.h:453
RoR::Actor::ar_wheel_speed
float ar_wheel_speed
Physics state; wheel speed in m/s.
Definition: Actor.h:400
RoR::Engine::isRunning
bool isRunning()
Definition: Engine.h:101
RoR::Actor::ar_engine
EnginePtr ar_engine
Definition: Actor.h:379
RoR::Engine::getRPM
float getRPM()
Definition: Engine.h:94
RoR::Engine::getClutch
float getClutch() const
Definition: Engine.h:92
RoR::DashBoardManager::_getBool
bool _getBool(size_t key)
Definition: DashBoardManager.h:219
RoR::Actor::getHeadlightsVisible
bool getHeadlightsVisible() const
Definition: Actor.h:196
RoR::OutGauge::OG_KM
@ OG_KM
Definition: OutGauge.h:69
RoR::Engine::getTurboPSI
float getTurboPSI()
Definition: Engine.cpp:857
RoR::DD_SIGNAL_TURNRIGHT
@ DD_SIGNAL_TURNRIGHT
Right blinker is lit.
Definition: DashBoardManager.h:198
RoR::Engine::hasTurbo
bool hasTurbo() const
Definition: Engine.h:72
Application.h
Central state/object manager and communications hub.
RoR::App::io_outgauge_delay
CVar * io_outgauge_delay
Definition: Application.cpp:203
RoR::OutGauge::DL_HANDBRAKE
@ DL_HANDBRAKE
Definition: OutGauge.h:77
RoRVersion.h
RoR::OutGauge::DL_SIGNAL_R
@ DL_SIGNAL_R
Definition: OutGauge.h:81
RoR::OutGauge::OG_TURBO
@ OG_TURBO
Definition: OutGauge.h:68
RoR::Actor::tc_mode
bool tc_mode
Enabled?
Definition: Actor.h:450
RoR::DD_SIGNAL_WARNING
@ DD_SIGNAL_WARNING
The warning-blink indicator is lit.
Definition: DashBoardManager.h:199
RoR::Actor::alb_mode
bool alb_mode
Anti-lock brake state; Enabled? {1/0}.
Definition: Actor.h:357
RoR::DD_SIGNAL_TURNLEFT
@ DD_SIGNAL_TURNLEFT
Left blinker is lit.
Definition: DashBoardManager.h:197
RoR::OutGauge::DL_ABS
@ DL_ABS
Definition: OutGauge.h:85
RoR::OutGauge::Close
void Close()
Definition: OutGauge.cpp:43
RoR::OutGauge::timer
float timer
Definition: OutGauge.h:58
RoR::Actor::alb_nodash
bool alb_nodash
Anti-lock brake attribute: Hide the dashboard indicator?
Definition: Actor.h:360
OutGauge.h
RoR::Engine::getAcc
float getAcc()
Definition: Engine.cpp:880
RoR::App::io_outgauge_id
CVar * io_outgauge_id
Definition: Application.cpp:204
RoR::OutGauge::sockfd
int sockfd
Definition: OutGauge.h:59
RoR::OutGauge::working
bool working
Definition: OutGauge.h:57
Ogre
Definition: ExtinguishableFireAffector.cpp:35
RoR::Actor::ar_design_name
Ogre::String ar_design_name
Name of the vehicle/machine/object this actor represents.
Definition: Actor.h:353
RoR::OutGauge::Update
bool Update(float dt, ActorPtr truck)
Definition: OutGauge.cpp:103
RoR::CVar::getInt
int getInt() const
Definition: CVar.h:97
RoR::OutGauge::DL_TC
@ DL_TC
Definition: OutGauge.h:79
RoR::Engine::getGear
int getGear()
Definition: Engine.cpp:1038
RoR
Definition: AppContext.h:36
RoR::App::io_outgauge_ip
CVar * io_outgauge_ip
Definition: Application.cpp:201
RoR::OutGauge::Connect
void Connect()
Definition: OutGauge.cpp:58