forked from supertuxkart/stk-code
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathitem.cpp
More file actions
573 lines (513 loc) · 19.4 KB
/
item.cpp
File metadata and controls
573 lines (513 loc) · 19.4 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
// SuperTuxKart - a fun racing game with go-kart
//
// Copyright (C) 2004-2015 Steve Baker <sjbaker1@airmail.net>
// Copyright (C) 2006-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "items/item.hpp"
#include "SColor.h"
#include "graphics/irr_driver.hpp"
#include "graphics/lod_node.hpp"
#include "graphics/sp/sp_mesh.hpp"
#include "graphics/sp/sp_mesh_node.hpp"
#include "guiengine/engine.hpp"
#include "items/item_manager.hpp"
#include "karts/abstract_kart.hpp"
#include "modes/world.hpp"
#include "network/packet_types.hpp"
#include "network/network_string.hpp"
#include "network/rewind_manager.hpp"
#include "tracks/arena_graph.hpp"
#include "tracks/drive_graph.hpp"
#include "tracks/drive_node.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
#include <IBillboardSceneNode.h>
#include <IMeshSceneNode.h>
#include <ISceneManager.h>
const float ICON_SIZE = 0.7f;
const int SPARK_AMOUNT = 10;
const float SPARK_SIZE = 0.4f;
const float SPARK_SPEED_H = 1.0f;
// ----------------------------------------------------------------------------
/** Constructor.
* \param type Type of the item.
* \param owner If not NULL it is the kart that dropped this item; NULL
* indicates an item that's part of the track.
* \param id Index of this item in the array of all items.
*/
ItemState::ItemState(ItemType type, const AbstractKart *owner, int id)
{
setType(type);
m_item_id = id;
m_previous_owner = owner;
m_used_up_counter = -1;
if (owner)
setDeactivatedTicks(STKConfig::get()->time2Ticks(1.5f));
else
setDeactivatedTicks(0);
} // ItemState(ItemType)
//-----------------------------------------------------------------------------
/** Constructor to restore item state at current ticks in client for live join
*/
ItemState::ItemState(const ItemStatePacket& packet)
{
m_type = (ItemType)packet.type;
m_original_type = (ItemType)packet.original_type;
m_ticks_till_return = packet.ticks_till_return;
m_item_id = packet.item_id;
m_deactive_ticks = packet.deactive_ticks;
m_used_up_counter = packet.used_up_counter;
m_xyz = packet.original_xyz_rotation.xyz;
m_original_rotation = packet.original_xyz_rotation.rotation;
m_previous_owner = NULL;
int8_t kart_id = packet.previous_owner;
if (kart_id != -1)
m_previous_owner = World::getWorld()->getKart(kart_id);
} // ItemState(const ItemStatePacket& packet)
// ------------------------------------------------------------------------
/** Sets the disappear counter depending on type. */
void ItemState::setDisappearCounter()
{
switch (m_type)
{
case ITEM_BUBBLEGUM:
m_used_up_counter = STKConfig::get()->m_bubblegum_counter; break;
case ITEM_EASTER_EGG:
m_used_up_counter = -1; break;
default:
m_used_up_counter = -1;
} // switch
} // setDisappearCounter
// -----------------------------------------------------------------------
/** Initialises an item.
* \param type Type for this item.
* \param xyz The position for this item.
* \param normal The normal for this item.
*/
void ItemState::initItem(ItemType type, const Vec3& xyz, const Vec3& normal)
{
m_xyz = xyz;
m_original_rotation = shortestArcQuat(Vec3(0, 1, 0), normal);
m_original_type = ITEM_NONE;
m_ticks_till_return = 0;
setDisappearCounter();
} // initItem
// ----------------------------------------------------------------------------
/** Update the state of the item, called once per physics frame.
* \param ticks Number of ticks to simulate. While this value is 1 when
* called during the normal game loop, during a rewind this value
* can be (much) larger than 1.
*/
void ItemState::update(int ticks)
{
if (m_deactive_ticks > 0) m_deactive_ticks -= ticks;
if (m_ticks_till_return>0)
{
m_ticks_till_return -= ticks;
} // if collected
} // update
// ----------------------------------------------------------------------------
/** Called when the item is collected.
* \param kart The kart that collected the item.
*/
void ItemState::collected(const AbstractKart *kart)
{
auto& stk_config = STKConfig::get();
if (m_type == ITEM_EASTER_EGG)
{
// They will disappear 'forever'
m_ticks_till_return = stk_config->time2Ticks(99999);
}
else if (m_used_up_counter > 0)
{
m_used_up_counter--;
// Deactivates the item for a certain amount of time. It is used to
// prevent bubble gum from hitting a kart over and over again (in each
// frame) by giving it time to drive away.
m_deactive_ticks = stk_config->time2Ticks(0.5f);
// Set the time till reappear to -1 seconds --> the item will
// reappear immediately.
m_ticks_till_return = -1;
}
else
{
switch (m_type)
{
case ITEM_BONUS_BOX:
m_ticks_till_return = stk_config->m_bonusbox_item_return_ticks;
break;
case ITEM_NITRO_BIG:
case ITEM_NITRO_SMALL:
m_ticks_till_return = stk_config->m_nitro_item_return_ticks;
break;
case ITEM_BANANA:
m_ticks_till_return = stk_config->m_banana_item_return_ticks;
break;
case ITEM_BUBBLEGUM:
case ITEM_BUBBLEGUM_NOLOK:
m_ticks_till_return = stk_config->m_bubblegum_item_return_ticks;
break;
default:
m_ticks_till_return = stk_config->time2Ticks(2.0f);
break;
}
}
if (RaceManager::get()->isBattleMode())
{
m_ticks_till_return *= 3;
}
} // collected
// ----------------------------------------------------------------------------
/** Returns the graphical type of this item should be using (takes nolok into
* account). */
Item::ItemType ItemState::getGraphicalType() const
{
return m_previous_owner && m_previous_owner->getIdent() == "nolok" &&
getType() == ITEM_BUBBLEGUM ?
ITEM_BUBBLEGUM_NOLOK : getType();
} // getGraphicalType
//-----------------------------------------------------------------------------
/** Save item state at current ticks in server for live join
*/
ItemStatePacket ItemState::saveCompleteState() const
{
ItemStatePacket packet;
packet.type = m_type;
packet.original_type = m_original_type;
packet.ticks_till_return = m_ticks_till_return;
packet.item_id = m_item_id;
packet.deactive_ticks = m_deactive_ticks;
packet.used_up_counter = m_used_up_counter;
packet.original_xyz_rotation.xyz = m_xyz;
packet.original_xyz_rotation.rotation = m_original_rotation;
packet.previous_owner = m_previous_owner ?
m_previous_owner->getWorldKartId() : -1;
return packet;
} // saveCompleteState
// ============================================================================
/** Constructor for an item.
* \param type Type of the item.
* \param xyz Location of the item.
* \param normal The normal upon which the item is placed (so that it can
* be aligned properly with the ground).
* \param mesh The mesh to be used for this item.
* \param owner 'Owner' of this item, i.e. the kart that drops it. This is
* used to deactivate this item for the owner, i.e. avoid that a kart
* 'collects' its own bubble gum. NULL means no owner, and the item
* can be collected immediatley by any kart.
*/
Item::Item(ItemType type, const Vec3& xyz, const Vec3& normal,
scene::IMesh* mesh, scene::IMesh* lowres_mesh,
const std::string& icon, const AbstractKart *owner)
: ItemState(type, owner)
{
m_icon_node = NULL;
m_was_available_previously = true;
// Prevent appear animation at start
m_animation_start_ticks = -9999;
m_distance_2 = 1.2f;
initItem(type, xyz, normal);
m_graphical_type = getGraphicalType();
m_node = NULL;
if (mesh && !GUIEngine::isNoGraphics())
{
LODNode* lodnode =
new LODNode("item", irr_driver->getSceneManager()->getRootSceneNode(),
irr_driver->getSceneManager());
scene::ISceneNode* meshnode =
irr_driver->addMesh(mesh, StringUtils::insertValues("item_%i", (int)type));
lodnode->add(1, meshnode, true);
if (lowres_mesh != NULL)
{
scene::ISceneNode* meshnode =
irr_driver->addMesh(lowres_mesh,
StringUtils::insertValues("item_lo_%i", (int)type));
lodnode->add(2, meshnode, true);
}
// Auto-compute the rendering distance, but use a high scaling factor
// to ensure that even at low settings, on-track items only become invisible
// when already quite far.
lodnode->autoComputeLevel(24); // The distance grows with the square root of the scaling factor
m_node = lodnode;
m_appear_anime_node = irr_driver->getSceneManager()->addEmptySceneNode(m_node);
}
setType(type);
handleNewMesh(getGraphicalType());
if (!m_node)
return;
#ifdef DEBUG
std::string debug_name("item: ");
debug_name += getType();
m_node->setName(debug_name.c_str());
#endif
m_node->setAutomaticCulling(scene::EAC_FRUSTUM_BOX);
m_node->setPosition(xyz.toIrrVector());
Vec3 hpr;
hpr.setHPR(getOriginalRotation());
m_node->setRotation(hpr.toIrrHPR());
m_node->grab();
for (int n = 0; n < SPARK_AMOUNT; n++)
{
scene::ISceneNode* billboard =
irr_driver->addBillboard(core::dimension2df(SPARK_SIZE, SPARK_SIZE),
"item_spark.png", m_appear_anime_node);
#ifdef DEBUG
billboard->setName("spark");
#endif
billboard->setVisible(true);
m_spark_nodes.push_back(billboard);
}
} // Item(type, xyz, normal, mesh, lowres_mesh)
//-----------------------------------------------------------------------------
/** Initialises the item. Note that m_distance_2 must be defined before calling
* this function, since it pre-computes some values based on this.
* \param type Type of the item.
* \param xyz Position of this item.
* \param normal Normal for this item.
*/
void Item::initItem(ItemType type, const Vec3 &xyz, const Vec3&normal)
{
ItemState::initItem(type, xyz, normal);
// Now determine in which quad this item is, and its distance
// from the center within this quad.
m_graph_node = Graph::UNKNOWN_SECTOR;
m_distance_from_center = 9999.9f;
m_avoidance_points[0] = NULL;
m_avoidance_points[1] = NULL;
// Check that Graph exist (it might not in battle mode without navmesh)
if (Graph::get())
{
Graph::get()->findRoadSector(xyz, &m_graph_node);
}
if (DriveGraph::get() && m_graph_node != Graph::UNKNOWN_SECTOR)
{
// Item is on drive graph. Pre-compute the distance from center
// of this item, which is used by the AI (mostly for avoiding items)
Vec3 distances;
DriveGraph::get()->spatialToTrack(&distances, getXYZ(), m_graph_node);
m_distance_from_center = distances.getX();
const DriveNode* dn = DriveGraph::get()->getNode(m_graph_node);
const Vec3& right = dn->getRightUnitVector();
// Give it 10% more space, since the kart will not always come
// parallel to the drive line.
Vec3 delta = right * sqrt(m_distance_2) * 1.3f;
m_avoidance_points[0] = new Vec3(getXYZ() + delta);
m_avoidance_points[1] = new Vec3(getXYZ() - delta);
}
} // initItem
//-----------------------------------------------------------------------------
void Item::setMesh(scene::IMesh* mesh, scene::IMesh* lowres_mesh)
{
#ifndef SERVER_ONLY
if (m_node == NULL)
return;
unsigned i = 0;
for (auto* node : m_node->getAllNodes())
{
scene::IMesh* m = i == 0 ? mesh : lowres_mesh;
if (m == NULL)
{
continue;
}
SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(node);
if (spmn)
{
spmn->setMesh(static_cast<SP::SPMesh*>(m));
}
else
{
((scene::IMeshSceneNode*)node)->setMesh(m);
}
i++;
}
#endif
} // setMesh
//-----------------------------------------------------------------------------
/** Removes an item.
*/
Item::~Item()
{
if (m_node != NULL)
{
for (auto* node : m_spark_nodes)
m_appear_anime_node->removeChild(node);
if (m_icon_node)
m_appear_anime_node->removeChild(m_icon_node);
m_node->removeChild(m_appear_anime_node);
irr_driver->removeNode(m_node);
m_node->drop();
}
if(m_avoidance_points[0])
delete m_avoidance_points[0];
if(m_avoidance_points[1])
delete m_avoidance_points[1];
} // ~Item
//-----------------------------------------------------------------------------
/** Resets before a race (esp. if a race is restarted).
*/
void Item::reset()
{
m_was_available_previously = true;
m_animation_start_ticks = -9999;
ItemState::reset();
if (m_node != NULL)
{
m_node->setScale(core::vector3df(1,1,1));
m_node->setVisible(true);
}
} // reset
// ----------------------------------------------------------------------------
void Item::handleNewMesh(ItemType type)
{
#ifndef SERVER_ONLY
if (m_node == NULL)
return;
setMesh(ItemManager::getItemModel(type),
ItemManager::getItemLowResolutionModel(type));
for (auto* node : m_node->getAllNodes())
{
SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(node);
if (spmn)
spmn->setGlowColor(ItemManager::getGlowColor(type));
}
Vec3 hpr;
hpr.setHPR(getOriginalRotation());
m_node->setRotation(hpr.toIrrHPR());
if (m_icon_node)
m_appear_anime_node->removeChild(m_icon_node);
m_icon_node = NULL;
auto icon = ItemManager::getIcon(type);
if (!icon.empty())
{
m_icon_node = irr_driver->addBillboard(core::dimension2df(1.0f, 1.0f),
icon, m_appear_anime_node);
m_icon_node->setPosition(core::vector3df(0.0f, 0.5f, 0.0f));
m_icon_node->setVisible(false);
((scene::IBillboardSceneNode*)m_icon_node)
->setColor(ItemManager::getGlowColor(type).toSColor());
}
#endif
} // handleNewMesh
// ----------------------------------------------------------------------------
/** Updated the item - rotates it, takes care of items coming back into
* the game after it has been collected.
* \param ticks Number of physics time steps - should be 1.
*/
void Item::updateGraphics(float dt)
{
if (m_node == NULL)
return;
if (m_graphical_type != getGraphicalType())
{
handleNewMesh(getGraphicalType());
m_graphical_type = getGraphicalType();
}
auto& stk_config = STKConfig::get();
float time_till_return = stk_config->ticks2Time(getTicksTillReturn());
bool is_visible = isAvailable() || time_till_return <= 1.0f ||
(getType() == ITEM_BUBBLEGUM &&
getOriginalType() == ITEM_NONE && !isUsedUp());
m_node->setVisible(is_visible);
m_node->setPosition(getXYZ().toIrrVector());
if (!m_was_available_previously && isAvailable())
{
// Play animation when item respawns
m_animation_start_ticks = World::getWorld()->getTicksSinceStart();
m_node->setScale(core::vector3df(0.0f, 0.0f, 0.0f));
}
float time_since_return = stk_config->ticks2Time(
World::getWorld()->getTicksSinceStart() - m_animation_start_ticks);
if (is_visible)
{
if (!isAvailable() && !(getType() == ITEM_BUBBLEGUM &&
getOriginalType() == ITEM_NONE && !isUsedUp()))
{
// Keep it visible so particles work, but hide the model
m_node->setScale(core::vector3df(0.0f, 1.0f, 0.0f));
}
else if (time_since_return <= 1.0f && m_animation_start_ticks)
{
float p = time_since_return, f = (1.0f - time_since_return);
float factor_v = sin(-13.0f * M_PI_2 * (p + 1.0f))
* pow(2.0f, -10.0f * p) + 1.0f;
float factor_h = 1.0f - (f * f * f * f * f - f * f * f * sin(f * M_PI));
m_node->setScale(core::vector3df(factor_h, factor_v, factor_h));
}
else
{
m_node->setScale(core::vector3df(1.0f, 1.0f, 1.0f));
}
// Handle rotation of the item
Vec3 hpr;
if (rotating())
{
// have it rotate
float angle =
fmodf((float)World::getWorld()->getTicksSinceStart() / 40.0f,
M_PI * 2);
btMatrix3x3 m;
m.setRotation(getOriginalRotation());
btQuaternion r = btQuaternion(m.getColumn(1), angle) *
getOriginalRotation();
hpr.setHPR(r);
}
else
hpr.setHPR(getOriginalRotation());
m_node->setRotation(hpr.toIrrHPR());
} // if item is available
bool is_in_appear_anime = time_since_return < 1.0f
|| (!isAvailable() && time_till_return < 1.0f);
m_appear_anime_node->setVisible(is_in_appear_anime);
if (is_in_appear_anime)
{
for (size_t i = 0; i < SPARK_AMOUNT; i++)
{
float t = time_since_return + 0.5f;
float t2 = time_since_return + 1.0f;
float node_angle = !rotating() ? 0.0f :
fmodf((float)World::getWorld()->getTicksSinceStart() / 40.0f,
M_PI * 2);
float x = sin(float(i) / float(SPARK_AMOUNT) * 2.0f * M_PI - node_angle)
* t * SPARK_SPEED_H;
float y = 2.0f * t2 - t2 * t2 - 0.5f;
float z = cos(float(i) / float(SPARK_AMOUNT) * 2.0f * M_PI - node_angle)
* t * SPARK_SPEED_H;
m_spark_nodes[i]->setPosition(core::vector3df(x, y, z));
float factor = std::max(0.0f, 1.0f - t / 2.0f);
m_spark_nodes[i]->setVisible(true);
((scene::IBillboardSceneNode*)m_spark_nodes[i])
->setSize(core::dimension2df(factor * SPARK_SIZE,
factor * SPARK_SIZE));
}
if (m_icon_node)
{
if (!isAvailable())
{
m_icon_node->setVisible(true);
float size = 1.0f / (pow(6.0f, -time_till_return - 0.2f) *
(-time_till_return - 0.2f)) + 7.0f;
((scene::IBillboardSceneNode*)m_icon_node)
->setSize(core::dimension2df(size * ICON_SIZE,
size * ICON_SIZE));
}
else
{
m_icon_node->setVisible(false);
}
}
}
m_was_available_previously = isAvailable();
} // updateGraphics