1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#include "common.h"
#include "patcher.h"
#include "Timer.h"
#include "WaterLevel.h"
#include "ModelIndices.h"
#include "Physical.h"
#include "Vehicle.h"
#include "Floater.h"
cBuoyancy &mod_Buoyancy = *(cBuoyancy*)0x8F2674;
//static float fVolMultiplier = 1.0f;
static float &fVolMultiplier = *(float*)0x601394;
// amount of boat volume in bounding box
// 1.0-volume is the empty space in the bbox
static float fBoatVolumeDistribution[9] = {
// rear
0.75f, 0.9f, 0.75f,
0.95f, 1.0f, 0.95f,
0.3f, 0.7f, 0.3f
// bow
};
bool
cBuoyancy::ProcessBuoyancy(CPhysical *phys, float buoyancy, CVector *impulse, CVector *point)
{
m_numSteps = 2.0f;
if(!CWaterLevel::GetWaterLevel(phys->GetPosition(), &m_waterlevel, phys->m_flagD8))
return false;
m_matrix = phys->GetMatrix();
PreCalcSetup(phys, buoyancy);
SimpleCalcBuoyancy();
float f = CalcBuoyancyForce(phys, impulse, point);
if(m_isBoat)
return true;
return f != 0.0f;
}
void
cBuoyancy::PreCalcSetup(CPhysical *phys, float buoyancy)
{
CColModel *colModel;
m_isBoat = phys->IsVehicle() && ((CVehicle*)phys)->IsBoat();
colModel = phys->GetColModel();
m_dimMin = colModel->boundingBox.min;
m_dimMax = colModel->boundingBox.max;
if(m_isBoat){
if(phys->GetModelIndex() == MI_PREDATOR){
m_dimMax.y *= 0.9f;
m_dimMin.y *= 0.9f;
}else if(phys->GetModelIndex() == MI_SPEEDER){
m_dimMax.y *= 1.1f;
m_dimMin.y *= 0.9f;
}else if(phys->GetModelIndex() == MI_REEFER){
m_dimMin.y *= 0.9f;
}else{
m_dimMax.y *= 0.9f;
m_dimMin.y *= 0.9f;
}
}
m_step = (m_dimMax - m_dimMin)/m_numSteps;
if(m_step.z > m_step.x && m_step.z > m_step.y){
m_stepRatio.x = m_step.x/m_step.z;
m_stepRatio.y = m_step.y/m_step.z;
m_stepRatio.z = 1.0f;
}else if(m_step.y > m_step.x && m_step.y > m_step.z){
m_stepRatio.x = m_step.x/m_step.y;
m_stepRatio.y = 1.0f;
m_stepRatio.z = m_step.z/m_step.y;
}else{
m_stepRatio.x = 1.0f;
m_stepRatio.y = m_step.y/m_step.x;
m_stepRatio.z = m_step.z/m_step.x;
}
m_haveVolume = false;
m_numPartialVolumes = 1.0f;
m_volumeUnderWater = 0.0f;
m_impulse = CVector(0.0f, 0.0f, 0.0f);
m_position = phys->GetPosition();
m_positionZ = CVector(0.0f, 0.0f, m_position.z);
m_buoyancy = buoyancy;
m_waterlevel += m_waterLevelInc;
}
void
cBuoyancy::SimpleCalcBuoyancy(void)
{
float x, y;
int ix, i;
tWaterLevel waterPosition;
// Floater is divided into 3x3 parts. Process and sum each of them
ix = 0;
for(x = m_dimMin.x; x <= m_dimMax.x; x += m_step.x){
i = ix;
for(y = m_dimMin.y; y <= m_dimMax.y; y += m_step.y){
CVector waterLevel(x, y, 0.0f);
FindWaterLevel(m_positionZ, &waterLevel, &waterPosition);
fVolMultiplier = m_isBoat ? fBoatVolumeDistribution[i] : 1.0f;
if(waterPosition != FLOATER_ABOVE_WATER)
SimpleSumBuoyancyData(waterLevel, waterPosition);
i += 3;
}
ix++;
}
m_volumeUnderWater /= (m_dimMax.z - m_dimMin.z)*sq(m_numSteps+1.0f);
}
float
cBuoyancy::SimpleSumBuoyancyData(CVector &waterLevel, tWaterLevel waterPosition)
{
static float fThisVolume;
static CVector AverageOfWaterLevel;
static float fFraction;
static float fRemainingSlice;
float submerged = Abs(waterLevel.z - m_dimMin.z);
// subtract empty space from submerged volume
fThisVolume = submerged - (1.0f - fVolMultiplier);
if(fThisVolume < 0.0f)
return 0.0f;
if(m_isBoat){
fThisVolume *= fVolMultiplier;
if(fThisVolume < 0.5f)
fThisVolume = 2.0f*sq(fThisVolume);
if(fThisVolume < 1.0f)
fThisVolume = sq(fThisVolume);
fThisVolume = sq(fThisVolume);
}
m_volumeUnderWater += fThisVolume;
AverageOfWaterLevel.x = waterLevel.x * m_stepRatio.x;
AverageOfWaterLevel.y = waterLevel.y * m_stepRatio.y;
AverageOfWaterLevel.z = (waterLevel.z+m_dimMin.z)/2.0f * m_stepRatio.z;
if(m_flipAverage)
AverageOfWaterLevel = -AverageOfWaterLevel;
fFraction = 1.0f/m_numPartialVolumes;
fRemainingSlice = 1.0f - fFraction;
m_impulse = m_impulse*fRemainingSlice + AverageOfWaterLevel*fThisVolume*fFraction;
m_numPartialVolumes += 1.0f;
m_haveVolume = true;
return fThisVolume;
}
void
cBuoyancy::FindWaterLevel(const CVector &zpos, CVector *waterLevel, tWaterLevel *waterPosition)
{
*waterPosition = FLOATER_IN_WATER;
// waterLevel is a local x,y point
// m_position is the global position of our floater
// zpos is the global z coordinate of our floater
CVector xWaterLevel = Multiply3x3(m_matrix, *waterLevel);
CWaterLevel::GetWaterLevel(xWaterLevel.x + m_position.x, xWaterLevel.y + m_position.y, m_position.z,
&waterLevel->z, true);
waterLevel->z -= xWaterLevel.z + zpos.z; // make local
if(waterLevel->z > m_dimMax.z){
waterLevel->z = m_dimMax.z;
*waterPosition = FLOATER_UNDER_WATER;
}else if(waterLevel->z < m_dimMin.z){
waterLevel->z = m_dimMin.z;
*waterPosition = FLOATER_ABOVE_WATER;
}
}
bool
cBuoyancy::CalcBuoyancyForce(CPhysical *phys, CVector *impulse, CVector *point)
{
if(!m_haveVolume)
return false;
*impulse = Multiply3x3(m_matrix, m_impulse);
*point = CVector(0.0f, 0.0f, m_volumeUnderWater*m_buoyancy*CTimer::GetTimeStep());
return true;
}
STARTPATCHES
InjectHook(0x546270, &cBuoyancy::ProcessBuoyancy, PATCH_JUMP);
InjectHook(0x546360, &cBuoyancy::PreCalcSetup, PATCH_JUMP);
InjectHook(0x5466F0, &cBuoyancy::SimpleCalcBuoyancy, PATCH_JUMP);
InjectHook(0x546820, &cBuoyancy::SimpleSumBuoyancyData, PATCH_JUMP);
InjectHook(0x546620, &cBuoyancy::FindWaterLevel, PATCH_JUMP);
InjectHook(0x5465A0, &cBuoyancy::CalcBuoyancyForce, PATCH_JUMP);
ENDPATCHES
|