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