Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
33using namespace Ogre;
34using namespace RoR;
35
37 sockfd(-1)
38 , timer(0)
39 , working(false)
40{
41}
42
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
103bool 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}
Central state/object manager and communications hub.
void LOG(const char *msg)
Legacy alias - formerly a macro.
bool ar_parking_brake
Definition Actor.h:467
bool alb_mode
Anti-lock brake state; Enabled? {1/0}.
Definition Actor.h:410
EnginePtr ar_engine
Definition Actor.h:432
float ar_wheel_speed
Physics state; wheel speed in m/s.
Definition Actor.h:453
DashBoardManagerPtr ar_dashboard
Definition Actor.h:484
Ogre::String ar_design_name
Name of the vehicle/machine/object this actor represents.
Definition Actor.h:406
bool tc_mode
Enabled?
Definition Actor.h:504
bool alb_nodash
Anti-lock brake attribute: Hide the dashboard indicator?
Definition Actor.h:413
Ogre::Real ar_brake
Physics state; braking intensity.
Definition Actor.h:452
bool tc_nodash
Hide the dashboard indicator?
Definition Actor.h:507
bool getHeadlightsVisible() const
Definition Actor.h:218
int getInt() const
Definition CVar.h:97
bool _getBool(size_t key)
float getRPM()
Definition Engine.h:94
float getClutch() const
Definition Engine.h:92
int getGear()
Definition Engine.cpp:1038
float getAcc()
Definition Engine.cpp:880
bool hasTurbo() const
Definition Engine.h:72
bool isRunning()
Definition Engine.h:101
float getTurboPSI()
Definition Engine.cpp:857
bool hasContact()
Ignition.
Definition Engine.h:102
void Connect()
Definition OutGauge.cpp:58
bool Update(float dt, ActorPtr truck)
Definition OutGauge.cpp:103
float timer
Definition OutGauge.h:58
CVar * io_outgauge_delay
CVar * io_outgauge_port
CVar * io_outgauge_id
CVar * io_outgauge_ip
@ DD_SIGNAL_WARNING
The warning-blink indicator is lit.
@ DD_SIGNAL_TURNLEFT
Left blinker is lit.
@ DD_SIGNAL_TURNRIGHT
Right blinker is lit.