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