Dayz
Build 1.29.163047, Scripts Rev. 123548
Dayz Code Explorer by Zeroy
Toggle main menu visibility
Loading...
Searching...
No Matches
rangefinder.c
Go to the documentation of this file.
1
class
Rangefinder
extends
PoweredOptic_Base
2
{
3
static
const
float
RANGEFINDER_MAX_DISTANCE
= 913.4856;
//TODO adjust maximal distance to match real life rangefinder
4
5
protected
ref
Timer
m_Timer
;
6
protected
Widget
m_Root
;
7
protected
TextWidget
m_RangeText
;
8
9
protected
string
m_LayoutPath
;
10
11
void
Rangefinder
()
12
{
13
InitRangeFinderData
();
14
}
15
16
void
~Rangefinder
()
17
{
18
m_IsActionActive =
false
;
19
}
20
21
protected
void
InitRangeFinderData
()
22
{
23
string
path
=
"cfgVehicles "
+
GetType
();
24
if
(
g_Game
.ConfigIsExisting(
path
))
25
{
26
string
layout;
27
if
(
g_Game
.ConfigIsExisting(
path
+
" rangeFinderLayout"
))
28
{
29
g_Game
.ConfigGetText(
path
+
" rangeFinderLayout"
, layout);
30
}
31
32
if
(layout !=
""
&& layout.
Length
() > 0)
33
{
34
m_LayoutPath
= layout;
35
}
36
else
37
{
38
m_LayoutPath
=
"gui/layouts/gameplay/rangefinder_hud.layout"
;
39
}
40
}
41
}
42
43
// How frequently the measurement should be taken
44
float
GetMeasurementUpdateInterval
()
45
{
46
return
0.5;
47
}
48
49
override
void
OnWorkStart
()
50
{
51
if
(
g_Game
.IsServer() && !m_IsActionActive)
// incorrectly synchronized state from EM
52
StopWorkServer();
53
54
if
( !
g_Game
.IsDedicatedServer())
55
{
56
PlayerBase
player_this =
PlayerBase
.Cast(
g_Game
.GetPlayer() );
57
PlayerBase
player_owner =
PlayerBase
.Cast( GetHierarchyRootPlayer() );
58
59
if
( player_this == player_owner )
60
{
61
StartPeriodicMeasurement
();
62
}
63
}
64
}
65
66
override
void
OnWorkStop
()
67
{
68
if
( !
g_Game
.IsDedicatedServer())
69
{
70
PlayerBase
player_this =
PlayerBase
.Cast(
g_Game
.GetPlayer() );
71
PlayerBase
player_owner =
PlayerBase
.Cast( GetHierarchyRootPlayer() );
72
73
if
( player_this == player_owner )
74
{
75
StopPeriodicMeasurement
();
76
}
77
}
78
}
79
80
void
StartPeriodicMeasurement
()
81
{
82
if
( !
m_Timer
)
83
{
84
m_Timer
=
new
Timer
(
CALL_CATEGORY_GAMEPLAY
);
85
}
86
87
m_Root
=
g_Game
.GetWorkspace().CreateWidgets(
m_LayoutPath
);
88
89
// Either use root as text widget directly or find the text as children, arbitrary layout is supported now.
90
m_RangeText
=
TextWidget
.Cast(
m_Root
);
91
if
(!
m_RangeText
)
92
m_RangeText
=
TextWidget
.Cast(
m_Root
.FindAnyWidget(
"RangeText"
));
93
94
m_Timer
.Run(
GetMeasurementUpdateInterval
(),
this
,
"DoMeasurement"
, null,
true
);
95
}
96
97
void
StopPeriodicMeasurement
()
98
{
99
if
(
m_Timer
)
100
{
101
m_Timer
.Stop();
102
}
103
104
if
(
m_Root
)
105
{
106
delete
m_Root
;
107
}
108
}
109
110
protected
void
SetDistanceText
(
TextWidget
text,
float
dist)
111
{
112
dist =
Math
.
Round
(dist);
113
114
if
(dist <
RANGEFINDER_MAX_DISTANCE
)
115
{
116
if
( dist < 10 )
117
text.SetText(
"00"
+ dist.
ToString
() );
118
else
if
( dist < 100 )
119
text.SetText(
"0"
+ dist.
ToString
() );
120
else
121
text.SetText( dist.
ToString
() );
122
}
123
else
124
{
125
SetInvalidText
(text);
126
}
127
}
128
129
protected
void
SetInvalidText
(
TextWidget
text)
130
{
131
text.SetText(
"---"
);
132
}
133
134
// Measures the distance and returns result in formated string
135
void
DoMeasurement
()
136
{
137
PlayerBase
player =
GetPlayer
();
138
139
if
( player )
140
{
141
vector
from =
g_Game
.GetCurrentCameraPosition();
142
vector
fromDirection =
g_Game
.GetCurrentCameraDirection();
143
vector
to = from + (fromDirection *
RANGEFINDER_MAX_DISTANCE
);
144
vector
contact_pos;
145
vector
contact_dir;
146
int
contactComponent;
147
148
bool
hit =
DayZPhysics
.
RaycastRV
( from, to, contact_pos, contact_dir, contactComponent, NULL , NULL, player,
false
,
false
, ObjIntersectIFire);
149
150
// (a)
151
// from -> --- <- horizEnd
152
// (h) \ |
153
// to -> \|
154
155
// Generate result
156
float
h =
vector
.
Distance
( from, contact_pos );
157
158
if
(hit)
159
SetDistanceText
(
m_RangeText
, h );
160
else
161
SetInvalidText
(
m_RangeText
);
162
163
// Horizontal distance
164
TextWidget
angleText =
TextWidget
.Cast(
m_Root
.FindAnyWidget(
"AngleText"
));
165
TextWidget
horizText =
TextWidget
.Cast(
m_Root
.FindAnyWidget(
"RangeHDText"
));
166
167
vector
horizontalTo =
Vector
( contact_pos[0], from[1], contact_pos[2] );
168
float
a =
vector
.
Distance
( from, horizontalTo );
169
170
// Angle between horizontal and actual line
171
float
heightDiff = contact_pos[1] - from[1];
172
float
angle =
Math
.
Atan
( heightDiff / a ) *
Math
.
RAD2DEG
;
173
angle =
Math
.
Round
(angle);
174
175
if
(angleText)
176
{
177
if
(hit)
178
angleText.SetText(
string
.Format(
"%1"
, angle));
179
else
180
SetInvalidText
( angleText );
181
}
182
183
if
(horizText)
184
{
185
if
(hit)
186
SetDistanceText
( horizText, a );
187
else
188
SetInvalidText
( horizText );
189
}
190
}
191
}
192
193
override
void
SetActions
()
194
{
195
super.SetActions();
196
197
RemoveAction
(
ActionViewOptics
);
198
AddAction
(
ActionViewBinoculars
);
199
}
200
201
override
void
OnDebugSpawn
()
202
{
203
GetInventory().CreateInInventory(
"Battery9V"
);
204
}
205
}
GetType
eBleedingSourceType GetType()
Definition
bleedingsource.c:67
AddAction
void AddAction(typename actionName)
Definition
advancedcommunication.c:220
RemoveAction
void RemoveAction(typename actionName)
Definition
advancedcommunication.c:252
ActionViewBinoculars
Definition
actionviewbinoculars.c:2
ActionViewOptics
Definition
actionviewoptics.c:2
DayZPhysics
Definition
dayzphysics.c:124
DayZPhysics::RaycastRV
static proto bool RaycastRV(vector begPos, vector endPos, out vector contactPos, out vector contactDir, out int contactComponent, set< Object > results=NULL, Object with=NULL, Object ignore=NULL, bool sorted=false, bool ground_only=false, int iType=ObjIntersectView, float radius=0.0, CollisionFlags flags=CollisionFlags.NEARESTCONTACT)
Raycasts world by given parameters.
Math
Definition
enmath.c:7
PlayerBase
Definition
playerbaseclient.c:2
PoweredOptic_Base
Definition
nvgoggles.c:2
PoweredOptic_Base::OnDebugSpawn
override void OnDebugSpawn()
Definition
rangefinder.c:201
PoweredOptic_Base::StartPeriodicMeasurement
void StartPeriodicMeasurement()
Definition
rangefinder.c:80
PoweredOptic_Base::m_Root
Widget m_Root
Definition
rangefinder.c:6
PoweredOptic_Base::RANGEFINDER_MAX_DISTANCE
static const float RANGEFINDER_MAX_DISTANCE
Definition
rangefinder.c:3
PoweredOptic_Base::DoMeasurement
void DoMeasurement()
Definition
rangefinder.c:135
PoweredOptic_Base::InitRangeFinderData
void InitRangeFinderData()
Definition
rangefinder.c:21
PoweredOptic_Base::SetDistanceText
void SetDistanceText(TextWidget text, float dist)
Definition
rangefinder.c:110
PoweredOptic_Base::OnWorkStop
override void OnWorkStop()
Definition
rangefinder.c:66
PoweredOptic_Base::Rangefinder
void Rangefinder()
Definition
rangefinder.c:11
PoweredOptic_Base::StopPeriodicMeasurement
void StopPeriodicMeasurement()
Definition
rangefinder.c:97
PoweredOptic_Base::SetInvalidText
void SetInvalidText(TextWidget text)
Definition
rangefinder.c:129
PoweredOptic_Base::GetMeasurementUpdateInterval
float GetMeasurementUpdateInterval()
Definition
rangefinder.c:44
PoweredOptic_Base::OnWorkStart
override void OnWorkStart()
Definition
rangefinder.c:49
PoweredOptic_Base::~Rangefinder
void ~Rangefinder()
Definition
rangefinder.c:16
PoweredOptic_Base::m_RangeText
TextWidget m_RangeText
Definition
rangefinder.c:7
PoweredOptic_Base::m_Timer
ref Timer m_Timer
Definition
rangefinder.c:5
PoweredOptic_Base::m_LayoutPath
string m_LayoutPath
Definition
rangefinder.c:9
PoweredOptic_Base::SetActions
override void SetActions()
Definition
rangefinder.c:193
TextWidget
Definition
enwidgets.c:220
Timer
Definition
dayzplayerimplement.c:39
Widget
Definition
enwidgets.c:190
float::ToString
proto string ToString(bool simple=true)
vector
Definition
enconvert.c:119
vector::Distance
static proto native float Distance(vector v1, vector v2)
Returns the distance between tips of two 3D vectors.
g_Game
DayZGame g_Game
Definition
dayzgame.c:3942
Vector
proto native vector Vector(float x, float y, float z)
Vector constructor from components.
Math::Atan
static proto float Atan(float x)
Returns angle in radians from tangent.
Math::Round
static proto float Round(float f)
Returns mathematical round of value.
Math::RAD2DEG
static const float RAD2DEG
Definition
enmath.c:16
string::Length
proto native int Length()
Returns length of string.
CALL_CATEGORY_GAMEPLAY
const int CALL_CATEGORY_GAMEPLAY
Definition
tools.c:10
GetPlayer
PlayerBase GetPlayer()
Definition
modifierbase.c:51
path
string path
Definition
optionselectormultistate.c:142
Games
Dayz
scripts
4_world
entities
itembase
rangefinder.c
Generated by
1.17.0