1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_TRACK_MANAGER |
---|
17 | |
---|
18 | #include "track_manager.h" |
---|
19 | |
---|
20 | #include "p_node.h" |
---|
21 | |
---|
22 | #include "track_node.h" |
---|
23 | |
---|
24 | #include <stdarg.h> |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | /** |
---|
29 | \brief initializes a TrackElement (sets the default values) |
---|
30 | */ |
---|
31 | TrackElement::TrackElement(void) |
---|
32 | { |
---|
33 | this->isFresh = true; |
---|
34 | this->isHotPoint = false; |
---|
35 | this->isSavePoint = false; |
---|
36 | this->isFork = false; |
---|
37 | this->isJoined = false; |
---|
38 | this->mainJoin = false; |
---|
39 | this->ID = -1; |
---|
40 | this->startingTime = 0; |
---|
41 | this->duration = TMAN_DEFAULT_DURATION; |
---|
42 | this->endTime = 1; |
---|
43 | this->jumpTime = 0; |
---|
44 | this->nodeCount = 0; |
---|
45 | this->childCount = 0; |
---|
46 | this->name = NULL; |
---|
47 | this->curve = NULL; |
---|
48 | this->children = NULL; |
---|
49 | |
---|
50 | this->history = NULL; |
---|
51 | |
---|
52 | this->condFunc = &TrackElement::random; |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | \brief destroys all alocated memory) |
---|
57 | \todo eventually when deleting a TrackElement you would not like to delete all its preceding TrackElements |
---|
58 | */ |
---|
59 | TrackElement::~TrackElement(void) |
---|
60 | { |
---|
61 | if (this->name) |
---|
62 | delete []name; |
---|
63 | if (this->curve) |
---|
64 | delete this->curve; |
---|
65 | if ((!this->isJoined &&this->childCount > 0) || (this->isJoined && this->mainJoin)) |
---|
66 | { |
---|
67 | for (int i=0; i < this->childCount; i++) |
---|
68 | delete this->children[i]; |
---|
69 | delete this->children; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | \brief Searches through all the TrackElements for trackID. |
---|
75 | \param trackID The ID to search for. |
---|
76 | \returns The TrackElement if Found, NULL otherwise. |
---|
77 | |
---|
78 | \todo make this more modular, to search for different things |
---|
79 | */ |
---|
80 | TrackElement* TrackElement::findByID(unsigned int trackID) |
---|
81 | { |
---|
82 | // return if Found. |
---|
83 | if (this->ID == trackID) |
---|
84 | return this; |
---|
85 | // search on. |
---|
86 | if (this->childCount > 0) |
---|
87 | for (int i=0; i < this->childCount; i++) |
---|
88 | { |
---|
89 | TrackElement* tmpElem; |
---|
90 | if ((tmpElem = this->children[i]->findByID(trackID))) |
---|
91 | return tmpElem; |
---|
92 | } |
---|
93 | else return NULL; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | /** |
---|
98 | \brief checks if there are any BackLoops in the Track |
---|
99 | \param trackElem the trackElement to check about |
---|
100 | it simply does this by looking if the current trackElem is found again somewhere else in the Track |
---|
101 | */ |
---|
102 | bool TrackElement::backLoopCheck(TrackElement* trackElem) |
---|
103 | { |
---|
104 | if (this->childCount == 0) |
---|
105 | return true; |
---|
106 | else |
---|
107 | { |
---|
108 | for (int i = 0; i < this->childCount; i++) |
---|
109 | if(!this->children[i]->backLoopCheck(trackElem)) |
---|
110 | return false; |
---|
111 | |
---|
112 | return true; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | \param name the Name to set. |
---|
118 | */ |
---|
119 | void TrackElement::setName(const char* name) |
---|
120 | { |
---|
121 | // delete the old name |
---|
122 | if (this->name) |
---|
123 | delete []this->name; |
---|
124 | // if a name was given already. |
---|
125 | if (name) |
---|
126 | { |
---|
127 | this->name = new char[strlen(name)+1]; |
---|
128 | strcpy(this->name, name); |
---|
129 | } |
---|
130 | else |
---|
131 | this->name = NULL; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | \returns The name of this TrackElement |
---|
136 | */ |
---|
137 | char* TrackElement::getName(void) const |
---|
138 | { |
---|
139 | return this->name; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | \brief prints out debug information about this TrackElement |
---|
144 | */ |
---|
145 | void TrackElement::debug(void) |
---|
146 | { |
---|
147 | PRINT(0)("--== TrackElement:%i ==--", this->ID); |
---|
148 | if(this->getName()) |
---|
149 | PRINT(0)("Name: %s::", this->getName()); |
---|
150 | if(this->isFresh) |
---|
151 | PRINT(0)(" -- has not jet eddited in any way --\n"); |
---|
152 | PRINT(0)("\n TimeTable: startingTime=%f; endTime=%f; duration=%f; jumpTime=%f\n", this->startingTime, this->endTime, this->duration, this->jumpTime); |
---|
153 | PRINT(0)(" consists of %d Points\n", this->nodeCount); |
---|
154 | if (this->childCount == 0) |
---|
155 | PRINT(0)(" has no child\n"); |
---|
156 | else if (this->childCount == 1) |
---|
157 | PRINT(0)(" has 1 child: =%d=\n", this->children[0]->ID); |
---|
158 | else if (this->childCount > 1) |
---|
159 | { |
---|
160 | PRINT(0)(" has %d children: ", this->childCount); |
---|
161 | for(int i = 0; i < this->childCount; i++) |
---|
162 | PRINT(0)("=%d= ", this->children[i]->ID); |
---|
163 | PRINT(0)("\n"); |
---|
164 | } |
---|
165 | |
---|
166 | if(this->isHotPoint) |
---|
167 | PRINT(0)(" is a special Point:\n"); |
---|
168 | if(this->isSavePoint) |
---|
169 | PRINT(0)(" is a SavePoint\n"); |
---|
170 | if(this->isFork) |
---|
171 | { |
---|
172 | PRINT(0)(" is A Fork with with %d children.\n", this->childCount); |
---|
173 | } |
---|
174 | if(this->isJoined) |
---|
175 | PRINT(0)(" is Joined at the End\n"); |
---|
176 | |
---|
177 | if(!this->backLoopCheck(this)) /* this should not happen */ |
---|
178 | PRINT(2)(" THERE IS A BACKLOOP TO THIS ELEMENT\n"); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | \brief CONDITION that chooses the first child for the decision (static) |
---|
183 | \param nothing Nothing in this function |
---|
184 | \returns the chosen child |
---|
185 | */ |
---|
186 | int TrackElement::lowest(void* nothing) |
---|
187 | { |
---|
188 | return 0; |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | \brief CONDITION that chooses the last child for the decision (static) |
---|
193 | \param nothing Nothing in this function |
---|
194 | \returns the chosen child |
---|
195 | */ |
---|
196 | int TrackElement::highest(void* nothing) |
---|
197 | { |
---|
198 | return this->childCount-1; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | \brief CONDITION that chooses a random child for the decision (static) |
---|
203 | \param nothing Nothing in this function |
---|
204 | \returns the chosen child |
---|
205 | */ |
---|
206 | int TrackElement::random(void* nothing) |
---|
207 | { |
---|
208 | int i = (int)floor ((float)rand()/(float)RAND_MAX * (float)this->childCount); |
---|
209 | if (i >= this->childCount) |
---|
210 | return this->childCount-1; |
---|
211 | else |
---|
212 | return i; |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | \brief CONDITION that chooses child 0, if the node(probably Player) |
---|
217 | is left of its parent (z<0)) and 1/right otherwise. |
---|
218 | \param node The node to act upon. |
---|
219 | \returns the chosen child |
---|
220 | */ |
---|
221 | int TrackElement::leftRight(void* node) |
---|
222 | { |
---|
223 | PNode* tmpNode = (PNode*)node; |
---|
224 | |
---|
225 | if (tmpNode->getRelCoor().z < 0) |
---|
226 | return 0; |
---|
227 | else |
---|
228 | return 1; |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | \brief CONDITION that chooses the child, that has the nearest distance to the node (probably player). |
---|
234 | \param node The node to act upon. |
---|
235 | \returns the chosen child |
---|
236 | |
---|
237 | This is rather dangerous, because one must carefully set the points on the curve. |
---|
238 | The best Way is to set the nodes as wide away of each other as possible, |
---|
239 | but take into consideration, that if the nodes are to far from a center node, the center will be chosen. |
---|
240 | (play with this!!). |
---|
241 | */ |
---|
242 | int TrackElement::nearest(void* node) |
---|
243 | { |
---|
244 | PNode* tmpNode = (PNode*)node; |
---|
245 | |
---|
246 | Vector nodeRelCoord = tmpNode->getRelCoor(); |
---|
247 | float minDist = 100000000; |
---|
248 | int nodeNumber = 0; |
---|
249 | for (int i = 0; i < this->childCount; i++) |
---|
250 | { |
---|
251 | float dist = (nodeRelCoord - this->children[i]->curve->getNode(4)).len(); |
---|
252 | if (dist < minDist) |
---|
253 | { |
---|
254 | minDist = dist; |
---|
255 | nodeNumber = i; |
---|
256 | } |
---|
257 | } |
---|
258 | PRINTF(4)("PathDecision with nearest algorithm: %d\n", nodeNumber); |
---|
259 | return nodeNumber; |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | ///////////////////////////////////// |
---|
264 | ///// TRACKMANAGER ////////////////// |
---|
265 | ///////////////////////////////////// |
---|
266 | /** |
---|
267 | \brief standard constructor |
---|
268 | |
---|
269 | */ |
---|
270 | TrackManager::TrackManager(void) |
---|
271 | { |
---|
272 | this->setClassName ("TrackManager"); |
---|
273 | |
---|
274 | singletonRef = this; |
---|
275 | |
---|
276 | PRINTF(3)("Initializing the TrackManager\n"); |
---|
277 | this->firstTrackElem = new TrackElement(); |
---|
278 | this->firstTrackElem->ID = 1; |
---|
279 | this->currentTrackElem = firstTrackElem; |
---|
280 | this->localTime = 0; |
---|
281 | this->maxTime = 0; |
---|
282 | this->trackElemCount = 1; |
---|
283 | this->bindSlave = this->trackNode = new TrackNode(); |
---|
284 | } |
---|
285 | |
---|
286 | |
---|
287 | /** |
---|
288 | \brief standard destructor |
---|
289 | */ |
---|
290 | TrackManager::~TrackManager(void) |
---|
291 | { |
---|
292 | PRINTF(3)("Destruct TrackManager\n"); |
---|
293 | |
---|
294 | PRINTF(3)("Deleting all the TrackElements\n"); |
---|
295 | delete this->firstTrackElem; |
---|
296 | |
---|
297 | // we do not have a TrackManager anymore |
---|
298 | singletonRef = NULL; |
---|
299 | } |
---|
300 | |
---|
301 | //! Singleton Reference to TrackManager |
---|
302 | TrackManager* TrackManager::singletonRef = NULL; |
---|
303 | |
---|
304 | /** |
---|
305 | \returns The reference on the TrackManager. |
---|
306 | |
---|
307 | If the TrackManager does not exist, it will be created. |
---|
308 | */ |
---|
309 | TrackManager* TrackManager::getInstance(void) |
---|
310 | { |
---|
311 | if (!singletonRef) |
---|
312 | singletonRef = new TrackManager(); |
---|
313 | return singletonRef; |
---|
314 | } |
---|
315 | |
---|
316 | /** |
---|
317 | \brief reserves Space for childCount children |
---|
318 | \param childCount The Count of children to make space for. |
---|
319 | */ |
---|
320 | void TrackManager::initChildren(unsigned int childCount) |
---|
321 | { |
---|
322 | this->currentTrackElem->childCount = childCount; |
---|
323 | this->currentTrackElem->mainJoin = true; |
---|
324 | this->currentTrackElem->children = new TrackElement*[childCount]; |
---|
325 | for (int i=0; i<childCount; i++) |
---|
326 | { |
---|
327 | this->currentTrackElem->children[i] = new TrackElement(); |
---|
328 | this->currentTrackElem->children[i]->ID = ++trackElemCount; |
---|
329 | this->currentTrackElem->children[i]->startingTime = this->currentTrackElem->endTime + this->currentTrackElem->jumpTime; |
---|
330 | this->addPoint(this->currentTrackElem->curve->getNode(this->currentTrackElem->curve->getNodeCount()), this->currentTrackElem->children[i]); |
---|
331 | } |
---|
332 | if (childCount == 1) |
---|
333 | this->currentTrackElem->children[0]->setName(this->currentTrackElem->getName()); |
---|
334 | } |
---|
335 | |
---|
336 | /** |
---|
337 | \brief Searches for a given trackID. |
---|
338 | \param trackID the trackID to search for. |
---|
339 | \returns The TrackElement #trackID if found, NULL otherwise. |
---|
340 | */ |
---|
341 | TrackElement* TrackManager::findTrackElementByID(unsigned int trackID) const |
---|
342 | { |
---|
343 | return firstTrackElem->findByID(trackID); |
---|
344 | } |
---|
345 | |
---|
346 | // INITIALIZE // |
---|
347 | |
---|
348 | /** |
---|
349 | \brief Sets the trackID we are working on. |
---|
350 | \param trackID the trackID we are working on |
---|
351 | */ |
---|
352 | void TrackManager::workOn(unsigned int trackID) |
---|
353 | { |
---|
354 | TrackElement* tmpElem = findTrackElementByID(trackID); |
---|
355 | if (tmpElem) |
---|
356 | this->currentTrackElem = tmpElem; |
---|
357 | else |
---|
358 | printf("TrackElement not Found, leaving unchanged\n"); |
---|
359 | printf("now Working on %d\n", this->currentTrackElem->ID); |
---|
360 | |
---|
361 | } |
---|
362 | |
---|
363 | /** |
---|
364 | \brief Sets the Type of the Curve |
---|
365 | \param curveType The Type to set |
---|
366 | \param trackElem the TrackElement that should get a new Curve. |
---|
367 | */ |
---|
368 | void TrackManager::setCurveType(CurveType curveType, TrackElement* trackElem) |
---|
369 | { |
---|
370 | if (!trackElem->isFresh) |
---|
371 | { |
---|
372 | PRINTF(2)("It is not possible to change the type of a Curve after you have have appended some points to it\n"); |
---|
373 | return; |
---|
374 | } |
---|
375 | this->curveType = curveType; |
---|
376 | switch (curveType) |
---|
377 | { |
---|
378 | case BEZIERCURVE: |
---|
379 | trackElem->curve = new BezierCurve(); |
---|
380 | break; |
---|
381 | case UPOINTCURVE: |
---|
382 | trackElem->curve = new UPointCurve(); |
---|
383 | break; |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | \brief Sets the duration of the current path in seconds. |
---|
389 | \param time The duration in seconds. |
---|
390 | */ |
---|
391 | |
---|
392 | void TrackManager::setDuration(float time) |
---|
393 | { |
---|
394 | this->currentTrackElem->duration = time; |
---|
395 | this->currentTrackElem->endTime = this->currentTrackElem->startingTime + time; |
---|
396 | } |
---|
397 | |
---|
398 | /** |
---|
399 | \brief adds a point to the current TrackElement |
---|
400 | \param newPoint The point to add. |
---|
401 | */ |
---|
402 | bool TrackManager::addPoint(Vector newPoint) |
---|
403 | { |
---|
404 | return this->addPoint(newPoint, this->currentTrackElem); |
---|
405 | } |
---|
406 | |
---|
407 | /** |
---|
408 | \brief adds a point to trackElem |
---|
409 | \param newPoint The point to add. |
---|
410 | \param trackElem The TrackElement to add the Point to |
---|
411 | */ |
---|
412 | bool TrackManager::addPoint(Vector newPoint, TrackElement* trackElem) |
---|
413 | { |
---|
414 | if (trackElem->isFresh) |
---|
415 | { |
---|
416 | this->setCurveType(TMAN_DEFAULT_CURVETYPE, trackElem); |
---|
417 | trackElem->isFresh = false; |
---|
418 | } |
---|
419 | trackElem->curve->addNode(newPoint); |
---|
420 | trackElem->nodeCount++; |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | \brief adds save/splitpoint. |
---|
425 | \param newPoint The point to add. |
---|
426 | \returns A Pointer to a newly appended Curve |
---|
427 | */ |
---|
428 | int TrackManager::addHotPoint(Vector newPoint) |
---|
429 | { |
---|
430 | printf("setting up a HotPoint\n"); |
---|
431 | if (this->currentTrackElem->isFresh) |
---|
432 | { |
---|
433 | this->setCurveType(BEZIERCURVE); |
---|
434 | this->currentTrackElem->isFresh = false; |
---|
435 | } |
---|
436 | |
---|
437 | // \todo HotPoint Handling. |
---|
438 | this->currentTrackElem->curve->addNode(newPoint); |
---|
439 | this->currentTrackElem->nodeCount++; |
---|
440 | this->initChildren(1); |
---|
441 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
442 | } |
---|
443 | |
---|
444 | /** |
---|
445 | \brief Sets the last HotPoint into a savePoint. |
---|
446 | \returns A Pointer to a newly appended Curve |
---|
447 | |
---|
448 | If no HotPoint was defined the last added Point will be rendered into a savePoint. \n |
---|
449 | If the HotPoint was defined as a fork the Point will \b not be set into a savePoint. |
---|
450 | */ |
---|
451 | int TrackManager::setSavePoint(void) |
---|
452 | { |
---|
453 | printf("setting up a SavePoint.\n"); |
---|
454 | if (this->currentTrackElem->isFork || this->currentTrackElem->isSavePoint) |
---|
455 | return this->currentTrackElem->children[1]->ID; |
---|
456 | this->currentTrackElem->isSavePoint = true; |
---|
457 | this->currentTrackElem->isHotPoint = true; |
---|
458 | |
---|
459 | this->initChildren(1); |
---|
460 | this->currentTrackElem = this->currentTrackElem->children[0]; |
---|
461 | } |
---|
462 | |
---|
463 | /** |
---|
464 | \brief adds some interessting non-linear movments through the level. |
---|
465 | \param count The Count of childrens the current HotPoint will have. |
---|
466 | |
---|
467 | If no HotPoint was defined the last added Point will be rendered into a fork. \n |
---|
468 | If the HotPoint was defined as a savePoint the Point will \b not be set into a fork. |
---|
469 | */ |
---|
470 | void TrackManager::fork(unsigned int count, ...) |
---|
471 | { |
---|
472 | int* trackIDs = new int[count]; |
---|
473 | this->forkV(count, trackIDs); |
---|
474 | va_list ID; |
---|
475 | va_start (ID, count); |
---|
476 | for(int i = 0; i < count; i++) |
---|
477 | { |
---|
478 | *va_arg (ID, int*) = trackIDs[i]; |
---|
479 | } |
---|
480 | va_end(ID); |
---|
481 | delete []trackIDs; |
---|
482 | } |
---|
483 | |
---|
484 | /** |
---|
485 | \brief adds some interessting non-linear movments through the level. |
---|
486 | \param count The Count of childrens the current HotPoint will have. |
---|
487 | \param trackIDs A Pointer to an Array of ints which will hold the trackID's (the user will have to reserve space for this). |
---|
488 | |
---|
489 | \see void TrackManager::fork(int count, ...) |
---|
490 | |
---|
491 | \todo initialisation is wrong!! also in setSavePoint. |
---|
492 | */ |
---|
493 | void TrackManager::forkV(unsigned int count, int* trackIDs) |
---|
494 | { |
---|
495 | printf("Forking with %d children\n", count); |
---|
496 | if (this->currentTrackElem->isSavePoint) |
---|
497 | return; |
---|
498 | this->currentTrackElem->isFork = true; |
---|
499 | this->currentTrackElem->isHotPoint = true; |
---|
500 | for(int i = 0; i < count; i++) |
---|
501 | trackIDs[i]=this->trackElemCount+1+i; |
---|
502 | this->initChildren(count); |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | \brief decides under what condition a certain Path will be chosen. |
---|
507 | \param cond the CONDITION of the decision |
---|
508 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
509 | */ |
---|
510 | void TrackManager::condition(CONDITION cond, void* subject) |
---|
511 | { |
---|
512 | this->condition(this->currentTrackElem->ID, cond, subject); |
---|
513 | } |
---|
514 | /** |
---|
515 | \brief decides under what condition a certain Path will be chosen. |
---|
516 | \param groupID the ID on which to choose the preceding move |
---|
517 | \param cond the CONDITION of the decision |
---|
518 | \param subject the Subject that will be decided upon with CONDITION cond. |
---|
519 | */ |
---|
520 | void TrackManager::condition(unsigned int groupID, CONDITION cond, void* subject) |
---|
521 | { |
---|
522 | TrackElement* tmpElem = this->findTrackElementByID(groupID); |
---|
523 | |
---|
524 | switch (cond) |
---|
525 | { |
---|
526 | case LOWEST: |
---|
527 | tmpElem->condFunc = &TrackElement::lowest; |
---|
528 | break; |
---|
529 | case HIGHEST: |
---|
530 | tmpElem->condFunc = &TrackElement::highest; |
---|
531 | break; |
---|
532 | case RANDOM: |
---|
533 | tmpElem->condFunc = &TrackElement::random; |
---|
534 | break; |
---|
535 | case LEFTRIGHT: |
---|
536 | tmpElem->condFunc = &TrackElement::leftRight; |
---|
537 | break; |
---|
538 | case NEAREST: |
---|
539 | tmpElem->condFunc = &TrackElement::nearest; |
---|
540 | break; |
---|
541 | case ENEMYKILLED: |
---|
542 | break; |
---|
543 | } |
---|
544 | tmpElem->subject=subject; |
---|
545 | } |
---|
546 | |
---|
547 | |
---|
548 | /** |
---|
549 | \brief joins some tracks together again. |
---|
550 | \param count The count of Paths to join. |
---|
551 | |
---|
552 | Join will set the localTime to the longest time a Path has to get to this Point. \n |
---|
553 | Join will join all curves to the first curve, meaning that all the tangents will be matched. |
---|
554 | */ |
---|
555 | void TrackManager::join(unsigned int count, ...) |
---|
556 | { |
---|
557 | int* trackIDs = new int [count]; |
---|
558 | va_list ID; |
---|
559 | va_start (ID, count); |
---|
560 | for(int i = 0; i < count; i++) |
---|
561 | { |
---|
562 | trackIDs[i] = va_arg (ID, int); |
---|
563 | } |
---|
564 | va_end(ID); |
---|
565 | this->joinV(count, trackIDs); |
---|
566 | delete []trackIDs; |
---|
567 | } |
---|
568 | |
---|
569 | /** |
---|
570 | \brief joins some tracks together again. |
---|
571 | \param count The count of Paths to join. |
---|
572 | \param trackIDs an Array with the trackID's to join |
---|
573 | |
---|
574 | \see void TrackManager::join(int count, ...) |
---|
575 | */ |
---|
576 | void TrackManager::joinV(unsigned int count, int* trackIDs) |
---|
577 | { |
---|
578 | printf("Joining %d tracks and merging to Track %d\n", count, trackIDs[0]); |
---|
579 | |
---|
580 | // checking if there is a back-loop-connection and ERROR if it is. |
---|
581 | TrackElement* tmpTrackElem = this->findTrackElementByID(trackIDs[0]); |
---|
582 | if (!tmpTrackElem->backLoopCheck(tmpTrackElem)) |
---|
583 | PRINTF(1)("Backloop connection detected at joining trackElements\n"); |
---|
584 | |
---|
585 | // chanching work-on to temporary value. going back at the end. |
---|
586 | int tmpCurrentWorkingID = this->currentTrackElem->ID; |
---|
587 | this->workOn(trackIDs[0]); |
---|
588 | TrackElement* firstJoint = this->currentTrackElem; |
---|
589 | float tmpLatestTime = firstJoint->endTime; |
---|
590 | |
---|
591 | Vector tmpEndPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()); |
---|
592 | Vector tmpTangentPoint = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-1); |
---|
593 | Vector tmpc2Point = firstJoint->curve->getNode(firstJoint->curve->getNodeCount()-2); |
---|
594 | firstJoint->isJoined = true; |
---|
595 | // firstJoint->mainJoin = true; |
---|
596 | if(!firstJoint->isHotPoint) |
---|
597 | this->setSavePoint(); |
---|
598 | // Timing: |
---|
599 | for (int i = 0; i < count; i++) |
---|
600 | { |
---|
601 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
602 | if (tmpJoinElem->childCount == 0 |
---|
603 | && tmpJoinElem->endTime > tmpLatestTime) |
---|
604 | tmpLatestTime = tmpJoinElem->endTime; |
---|
605 | } |
---|
606 | // time the main Join. |
---|
607 | firstJoint->jumpTime = tmpLatestTime - firstJoint->endTime; |
---|
608 | |
---|
609 | // Joining: |
---|
610 | for (int i = 1; i < count; i++) |
---|
611 | { |
---|
612 | TrackElement* tmpJoinElem = this->findTrackElementByID(trackIDs[i]); |
---|
613 | if (tmpJoinElem->childCount > 0) |
---|
614 | printf("!!This Curve has children, and as such will not be joined!!\n You can try joining other childless TrackElements to this one!"); |
---|
615 | else |
---|
616 | { |
---|
617 | this->addPoint(tmpc2Point, tmpJoinElem); |
---|
618 | this->addPoint(tmpTangentPoint, tmpJoinElem); |
---|
619 | this->addPoint(tmpEndPoint, tmpJoinElem); |
---|
620 | // time all other Joins |
---|
621 | tmpJoinElem->jumpTime = tmpLatestTime - tmpJoinElem->endTime; |
---|
622 | |
---|
623 | //Copying Joint-Info |
---|
624 | tmpJoinElem->children = firstJoint->children; |
---|
625 | tmpJoinElem->childCount = firstJoint->childCount; |
---|
626 | tmpJoinElem->isSavePoint = firstJoint->isSavePoint; |
---|
627 | tmpJoinElem->isFork = firstJoint->isFork; |
---|
628 | |
---|
629 | tmpJoinElem->isJoined = true; |
---|
630 | } |
---|
631 | } |
---|
632 | if(firstJoint->childCount > 0) |
---|
633 | for(int i = 0; i < firstJoint->childCount; i++) |
---|
634 | { |
---|
635 | printf("Setting startingTime of %d to %f.\n", firstJoint->children[i]->ID, tmpLatestTime); |
---|
636 | firstJoint->children[i]->startingTime = tmpLatestTime; |
---|
637 | firstJoint->children[i]->endTime = tmpLatestTime+firstJoint->children[i]->duration; |
---|
638 | } |
---|
639 | |
---|
640 | // returning to the TrackElement we were working on. |
---|
641 | this->workOn(tmpCurrentWorkingID); |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | \brief finalizes the TrackSystem. after this it will not be editable anymore |
---|
646 | |
---|
647 | \todo check for any inconsistencies, output errors |
---|
648 | */ |
---|
649 | void TrackManager::finalize(void) |
---|
650 | { |
---|
651 | for (int i = 1; i<= trackElemCount ;i++) |
---|
652 | { |
---|
653 | TrackElement* tmpElem = findTrackElementByID(i); |
---|
654 | if (tmpElem->childCount>0 && tmpElem->mainJoin) |
---|
655 | { |
---|
656 | for (int j = 0; j < tmpElem->childCount; j++) |
---|
657 | { |
---|
658 | |
---|
659 | // c1-continuity |
---|
660 | tmpElem->children[j]->curve->addNode(tmpElem->children[j]->curve->getNode(0) + |
---|
661 | ((tmpElem->children[j]->curve->getNode(0) - |
---|
662 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) |
---|
663 | ),2); |
---|
664 | tmpElem->children[j]->nodeCount++; |
---|
665 | // c2-continuity |
---|
666 | tmpElem->children[j]->curve->addNode((tmpElem->curve->getNode(tmpElem->curve->getNodeCount())- |
---|
667 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-1)) * 4 + |
---|
668 | tmpElem->curve->getNode(tmpElem->curve->getNodeCount()-2), 3); |
---|
669 | tmpElem->children[j]->nodeCount++; |
---|
670 | printf("accelerations: %d-in: count: %d, %f, %f, %f\n %d-out: count: %d %f, %f, %f\n", |
---|
671 | tmpElem->ID, tmpElem->nodeCount, |
---|
672 | tmpElem->curve->calcAcc(0.999).x, tmpElem->curve->calcAcc(0.999).y, tmpElem->curve->calcAcc(0.999).z, |
---|
673 | tmpElem->children[j]->ID, tmpElem->children[j]->nodeCount, |
---|
674 | tmpElem->children[j]->curve->calcAcc(0).x, tmpElem->children[j]->curve->calcAcc(0).y, tmpElem->children[j]->curve->calcAcc(0).z); |
---|
675 | } |
---|
676 | } |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | |
---|
681 | // RUNTIME // |
---|
682 | |
---|
683 | /** |
---|
684 | \brief calculates the Position for the localTime of the Track. |
---|
685 | \returns the calculated Position |
---|
686 | */ |
---|
687 | Vector TrackManager::calcPos() const |
---|
688 | { |
---|
689 | // PRINTF(0)("TrackElement:%d, localTime: %f\n",this->currentTrackElem->ID, this->localTime); |
---|
690 | return this->currentTrackElem->curve->calcPos((this->localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
691 | } |
---|
692 | |
---|
693 | /** |
---|
694 | \brief calculates the Rotation for the localTime of the Track. |
---|
695 | \returns the calculated Rotation |
---|
696 | */ |
---|
697 | Vector TrackManager::calcDir() const |
---|
698 | { |
---|
699 | return this->currentTrackElem->curve->calcDir((this->localTime - this->currentTrackElem->startingTime)/this->currentTrackElem->duration); |
---|
700 | } |
---|
701 | |
---|
702 | /** |
---|
703 | \brief Advances the local-time of the Track around dt |
---|
704 | \param dt The time about which to advance. |
---|
705 | |
---|
706 | This function also checks, if the TrackElement has to be changed. |
---|
707 | */ |
---|
708 | void TrackManager::tick(float dt) |
---|
709 | { |
---|
710 | dt /= 1000; |
---|
711 | PRINTF(4)("CurrentTrackID: %d, LocalTime is: %f, timestep is: %f\n", this->currentTrackElem->ID, this->localTime, dt); |
---|
712 | if (this->localTime <= this->firstTrackElem->duration) |
---|
713 | this->jumpTo(this->localTime); |
---|
714 | this->localTime += dt; |
---|
715 | if (this->localTime > this->currentTrackElem->endTime |
---|
716 | && this->currentTrackElem->children) |
---|
717 | { |
---|
718 | if (this->currentTrackElem->jumpTime != 0.0) |
---|
719 | this->jumpTo(this->localTime + this->currentTrackElem->jumpTime); |
---|
720 | // jump to the next TrackElement and also set the history of the new Element to the old one. |
---|
721 | TrackElement* tmpHistoryElem = this->currentTrackElem; |
---|
722 | this->currentTrackElem = this->currentTrackElem->children[this->choosePath(this->currentTrackElem)]; |
---|
723 | this->currentTrackElem->history = tmpHistoryElem; |
---|
724 | } |
---|
725 | if (this->bindSlave) |
---|
726 | { |
---|
727 | Vector tmp = this->calcPos(); |
---|
728 | Quaternion quat = Quaternion(this->calcDir(), Vector(this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).x,1,this->currentTrackElem->curve->calcAcc((localTime-this->currentTrackElem->startingTime)/this->currentTrackElem->duration).z)); |
---|
729 | |
---|
730 | Vector v(0.0, 1.0, 0.0); |
---|
731 | Quaternion q(-PI/2, v); |
---|
732 | quat = quat * q; |
---|
733 | |
---|
734 | this->bindSlave->setAbsCoor(&tmp); |
---|
735 | this->bindSlave->setAbsDir(&quat); |
---|
736 | } |
---|
737 | } |
---|
738 | |
---|
739 | /** |
---|
740 | \brief Jumps to a certain point on the Track. |
---|
741 | \param time The time on the Track to jump to. |
---|
742 | |
---|
743 | This should be used to Jump backwards on a Track, because moving forward means to change between the Path. (it then tries to choose the default.) |
---|
744 | Max is trackLengthMax. |
---|
745 | */ |
---|
746 | void TrackManager::jumpTo(float time) |
---|
747 | { |
---|
748 | if (time == 0) |
---|
749 | this->currentTrackElem = this->firstTrackElem; |
---|
750 | this->localTime = time; |
---|
751 | } |
---|
752 | |
---|
753 | /** |
---|
754 | \brief a Function that decides which Path we should follow. |
---|
755 | \param trackElem The Path to choose. |
---|
756 | |
---|
757 | */ |
---|
758 | int TrackManager::choosePath(TrackElement* trackElem) |
---|
759 | { |
---|
760 | return (trackElem->*(trackElem->condFunc))(trackElem->subject); |
---|
761 | } |
---|
762 | |
---|
763 | /** |
---|
764 | \brief Sets the PNode, that should be moved along the Tack |
---|
765 | \param bindSlave the PNode to set |
---|
766 | */ |
---|
767 | void TrackManager::setBindSlave(PNode* bindSlave) |
---|
768 | { |
---|
769 | this->bindSlave = bindSlave; |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | \returns the main TrackNode |
---|
774 | */ |
---|
775 | PNode* TrackManager::getTrackNode(void) |
---|
776 | { |
---|
777 | return this->trackNode; |
---|
778 | } |
---|
779 | |
---|
780 | // DEBUG // |
---|
781 | |
---|
782 | /** |
---|
783 | \brief Imports a model of the Graph into the OpenGL-environment. |
---|
784 | \param dt The Iterator used in seconds for Painting the Graph. |
---|
785 | |
---|
786 | This is for testing facility only. Do this if you want to see the Path inside the Level. |
---|
787 | eventually this will all be packed into a gl-list. |
---|
788 | */ |
---|
789 | void TrackManager::drawGraph(float dt) const |
---|
790 | { |
---|
791 | |
---|
792 | for (int i = 1; i <= trackElemCount; i++) |
---|
793 | { |
---|
794 | glBegin(GL_LINE_STRIP); |
---|
795 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
796 | if (tmpElem->curve) |
---|
797 | for(float f = 0.0; f < 1.0; f+=dt) |
---|
798 | { |
---|
799 | // printf("%f, %f, %f\n",trackManager->calcPos().x, trackManager->calcPos().y, trackManager->calcPos().z); |
---|
800 | Vector tmpVector = tmpElem->curve->calcPos(f); |
---|
801 | glVertex3f(tmpVector.x, tmpVector.y, tmpVector.z); |
---|
802 | } |
---|
803 | glEnd(); |
---|
804 | } |
---|
805 | } |
---|
806 | |
---|
807 | /** |
---|
808 | \brief outputs debug information about the trackManager |
---|
809 | \param level how much debug |
---|
810 | */ |
---|
811 | void TrackManager::debug(unsigned int level) const |
---|
812 | { |
---|
813 | PRINT(0)("=========================================\n"); |
---|
814 | PRINT(0)("= CLASS TRACKMANAGER::debug information =\n"); |
---|
815 | PRINT(0)("=========================================\n"); |
---|
816 | // PRINT(0)("Status is: % |
---|
817 | PRINT(0)(" Consists of %d elements\n", this->trackElemCount); |
---|
818 | PRINT(0)(" localTime is: %f\n", this->localTime); |
---|
819 | if (level >= 2) |
---|
820 | { |
---|
821 | for (int i = 1; i <= trackElemCount; i++) |
---|
822 | { |
---|
823 | TrackElement* tmpElem = this->findTrackElementByID(i); |
---|
824 | tmpElem->debug(); |
---|
825 | } |
---|
826 | } |
---|
827 | PRINT(0)("-----------------------------------------\n"); |
---|
828 | } |
---|