blob: 23b27bc40e49910ee916b92b9b8cca071d6175c0 (
plain)
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/*
core/gameinterface.cc
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#include <stdlib.h>
#include <iostream>
#include "core/application.h"
#include "core/cvar.h"
#include "core/func.h"
#include "core/gameinterface.h"
#include "core/player.h"
#include "model/model.h"
#include "sys/sys.h"
namespace core
{
const float MIN_DELTA = 10e-10;
void func_list_model(std::string const &args)
{
model::Model::list();
}
Player GameInterface::game_localplayer;
EntityControlable *localcontrol()
{
if (game()->localplayer())
return game()->localplayer()->control();
else
return 0;
}
Player *localplayer()
{
return game()->localplayer();
}
GameInterface::GameInterface()
{
clear();
game_localplayer.clear();
if (Cvar::sv_dedicated->value()) {
game_localplayer.player_name.assign("Console");
} else {
game_localplayer.player_name.assign("Player");
game_localplayer.update_info();
}
core::Func::add("list_model", (core::FuncPtr) func_list_model);
}
GameInterface::~GameInterface()
{
core::Func::remove("list_model");
game_localplayer.clear();
clear();
}
// clear all game related objects
void GameInterface::clear()
{
//con_debug << "Clearing game data\n";
// remove all entities
for (std::map<unsigned int, Entity*>::iterator it = Entity::registry.begin(); it != Entity::registry.end(); it++) {
delete (*it).second;
}
Entity::registry.clear();
// remove all game functions
for (std::map<std::string, Func*>::iterator it = Func::registry.begin(); it != Func::registry.end(); it++) {
if ( ((*it).second->flags() & Func::Game) == Func::Game) {
delete (*it).second;
Func::registry.erase(it);
}
}
// remove all game cvars
for (std::map<std::string, Cvar*>::iterator it = Cvar::registry.begin(); it != Cvar::registry.end(); it++) {
if ( ((*it).second->flags() & Cvar::Game) == Cvar::Game) {
delete (*it).second;
Cvar::registry.erase(it);
}
}
// remove all models
model::Model::clear();
game_previousframetime = 0;
game_serverframetime = 0;
game_clientframetime = 0;
}
void GameInterface::reset_clientstate(float timestamp, float prevtimestamp)
{
game_previousframetime = prevtimestamp;
game_serverframetime = timestamp;
std::map<unsigned int, core::Entity *>::iterator it;
for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) {
core::Entity *entity = (*it).second;
if (entity->state() && !(entity->flags() & core::Entity::Static))
entity->state()->assign(entity);
}
if ( game_clientframetime < game_previousframetime)
game_clientframetime = game_previousframetime;
else if ( game_clientframetime > game_serverframetime)
game_clientframetime = game_serverframetime;
}
void GameInterface::update_entity_clientstate(Entity *entity)
{
if (!entity->state()) {
entity->entity_clientstate = new core::ClientState(entity);
entity->entity_clientstate->assign(entity);
}
if(Cvar::cl_prediction->value() == 0) {
entity->state()->assign(entity);
return;
}
if (!(entity->flags() & core::Entity::Static)) {
// clientstate location
entity->state()->state_location = entity->state()->previouslocation() +
(entity->location() - entity->state()->previouslocation()) * timeoffset();
if (game_clientframetime < game_serverframetime) {
//FIXME this is hopelessly broken
// http://local.wasp.uwa.edu.au/~pbourke/geometry/planeline/
float cosangle;
float angle;
float side;
float u;
float l;
math::Vector3f n;
math::Vector3f p;
entity->state()->state_axis.assign(entity->state()->previousaxis());
// clientstate axis: pitch
// project entity->axis().up() into the plane with entity->state()->axis()->left() normal
n = entity->state()->axis().left();
p = entity->axis().up();
u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
p = entity->axis().up() + u * n;
side = entity->state()->axis().forward().x * p.x +
entity->state()->axis().forward().y * p.y +
entity->state()->axis().forward().z * p.z;
l = p.length();
if ((fabs(side) - MIN_DELTA > 0)) {
cosangle = math::dotproduct(p, entity->state()->axis().up());
if (fabs(cosangle) + MIN_DELTA < 1 ) {
angle = acos(cosangle) * 180.0f / M_PI;
angle = math::sgnf(side) * angle * timeoffset();
entity->state()->state_axis.change_pitch(-angle);
}
}
// clientstate axis: direction
// project entity->axis().forward() into the plane with entity->state()->axis().up() normal
n = entity->state()->axis().up();
p = entity->axis().forward();
u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
p = entity->axis().forward() + u * n;
side = entity->state()->axis().left().x * p.x +
entity->state()->axis().left().y * p.y +
entity->state()->axis().left().z * p.z;
l = p.length();
if ((fabs(side) - MIN_DELTA > 0)) {
cosangle = math::dotproduct(p, entity->state()->axis().forward());
if (fabs(cosangle) + MIN_DELTA < 1 ) {
angle = acos(cosangle) * 180.0f / M_PI;
angle = math::sgnf(side) * angle * timeoffset();
entity->state()->state_axis.change_direction(angle);
}
}
// clientstate axis: roll
// project entity->axis().up() into the plane with entity->state()->axis().forward() normal
n = entity->state()->axis().forward();
p = entity->axis().up();
u = p[0]*n[0] + p[1]*n[1] + p[2]*n[2] / (-n[0]*n[0] - n[1]*n[1] - n[2] * n[2]);
p = entity->axis().up() + u * n;
side = entity->state()->axis().left().x * p.x +
entity->state()->axis().left().y * p.y +
entity->state()->axis().left().z * p.z;
l = p.length();
if ((fabs(side) - MIN_DELTA > 0)) {
cosangle = math::dotproduct(p, entity->state()->axis().up());
if (fabs(cosangle) + MIN_DELTA < 1 ) {
angle = acos(cosangle) * 180.0f / M_PI;
angle = math::sgnf(side) * angle * timeoffset();
entity->state()->state_axis.change_roll(angle);
}
}
} else {
entity->state()->state_axis.assign(entity->axis());
}
} else {
entity->state()->assign(entity);
}
}
void GameInterface::update_clientstate(float seconds)
{
game_clientframetime += seconds;
std::map<unsigned int, core::Entity *>::iterator it;
for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) {
update_entity_clientstate((*it).second);
}
}
float GameInterface::timeoffset() {
if (game_clientframetime > game_serverframetime)
return 1;
float d = game_serverframetime - game_previousframetime;
if (d <= 0)
return 0;
float t = game_clientframetime - game_previousframetime;
return t/d;
}
}
|