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