Debian Codegraph @

Descent3/levelgoal.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/levelgoal.cpp $
21 * $Revision: 50 $
22 * $Date: 6/23/99 5:34p $
23 * $Author: Jeff $
24 *
25 * Level goal stuff, I guess.
26 *
27 * $Log: /DescentIII/Main/levelgoal.cpp $
28 *
29 * 50 6/23/99 5:34p Jeff
30 * For some reason Linux started complaining about Status function,
31 * renaming it to GetStatus made it quiet.
32 *
33 * 49 5/23/99 8:39p Chris
34 * Now multiple equal priority (primary and secondary) goals are shown on
35 * the level goal tab
36 *
37 * 48 5/18/99 7:33p Jeff
38 * fixed game message log...using out-dated variable that is no longer
39 * used...switched to real buffer. We should remove the out-dated
40 * variable, but I don't want to break any code at this point.
41 *
42 * 47 5/12/99 6:02p Jeff
43 * flip order of saving/loading objects and players
44 *
45 * 46 5/09/99 10:49p Jason
46 * fixed some strings being truncuated in multiplayer games
47 *
48 * 45 5/08/99 4:55p Chris
49 * Only add HUD completion message if the lgoal is enabled
50 *
51 * 44 5/07/99 7:57p Jeff
52 * handle the multisafe condition of clearing the completed flag
53 *
54 * 43 5/02/99 5:09a Jeff
55 * send levelgoal state to joining clients
56 *
57 * 42 5/01/99 1:41a Jeff
58 * made levelgoals multi friendly
59 *
60 * 41 4/14/99 11:49a Matt
61 * Made localizable the goal completed HUD message
62 *
63 * 40 4/05/99 1:07p Chris
64 * Added a goal failed state
65 *
66 * 39 3/16/99 4:17p Matt
67 * Changed goal complete message from "<goal>: Completed!" to "Completed:
68 * <goal>"
69 *
70 * 38 3/10/99 2:25p Kevin
71 * Save/Load and Demo file fixes
72 *
73 * 37 3/04/99 7:39p Matt
74 * Added sound effects to FreeSpace-style persistent HUD messages.
75 *
76 * 36 3/04/99 6:12p Matt
77 * Changed color of goal complete message
78 *
79 * 35 3/03/99 5:34p Matt
80 * Added fade-out for goal complete messages
81 *
82 * 34 3/03/99 3:35a Chris
83 * Fixed.
84 *
85 * 33 3/02/99 6:39p Jeff
86 * level goals working in save/load game
87 *
88 * 32 3/02/99 5:44p Chris
89 * Fixed the save game problem with levelgoals
90 *
91 * 31 2/26/99 8:33a Chris
92 * : Completed! was added
93 *
94 * 30 2/23/99 1:03p Luke
95 * Fixed a bug where all goals where completing automatically
96 *
97 * 29 2/22/99 8:11p Chris
98 * Fixed a bug with saving goals
99 *
100 * 28 2/22/99 3:05p Chris
101 * Complete - minus coop
102 *
103 * 27 2/22/99 1:43a Chris
104 * Version 4 is near complete
105 *
106 * 26 2/21/99 7:49p Chris
107 *
108 * 25 2/21/99 7:47p Chris
109 * Finished up with the anywhere on terrain and anywhere on mine items :)
110 *
111 * 24 2/21/99 7:16p Chris
112 * Added the EVT_LEVEL_GOAL_ITEM_COMPLETE event
113 *
114 * 23 2/21/99 6:37p Chris
115 *
116 * 22 2/21/99 5:44p Nate
117 * Fixed a bug with GoalComplete being called directly from the
118 * Level_goals class. Oops.
119 *
120 * 21 2/21/99 4:47p Chris
121 * More updates
122 *
123 * 20 2/21/99 4:31p Chris
124 * Improving the level goal system... Not done.
125 *
126 * 19 2/20/99 6:19p Chris
127 *
128 * 18 2/20/99 6:18p Chris
129 * Improved levelgoals - not done
130 *
131 * 17 2/20/99 4:29p Chris
132 * SOme updates to the system, not done
133 *
134 * 16 2/20/99 2:59p Matt
135 * Made ShowGoalMessage() add the message to the game message buffer.
136 *
137 * 15 2/19/99 11:32a Chris
138 * Added GoalFindId
139 *
140 * 14 2/18/99 5:29p Matt
141 * Added ShowGoalMessage()
142 *
143 * 13 2/10/99 1:47p Matt
144 * Changed object handle symbolic constants
145 *
146 * 12 1/31/99 7:26p Matt
147 * Renamed a bunch of functions to have HUD capitalized
148 *
149 * 11 1/29/99 5:22p Jeff
150 * localization
151 *
152 * 10 10/15/98 8:54a Matt
153 * Don't call Endlevel() directly, but instead change the game state to
154 * end-of-level
155 *
156 */
157
158 #include "levelgoal.h"
159 #include "mem.h"
160 #include "string.h"
161 #include "hud.h"
162 #include "game.h"
163 #include "gamesequence.h"
164 #include "stringtable.h"
165 #include <stdarg.h>
166 #include "pstring.h"
167 #include "hlsoundlib.h"
168 #include "sounds.h"
169 #include "osiris_dll.h"
170 #include "room.h"
171 #include "object.h"
172 #include "trigger.h"
173 #include "player.h"
174 #include "multi.h"
175 #include "demofile.h"
176 #include "osiris_share.h"
177 #include "multisafe.h"
178 #include "multi.h"
179 #include "multi_world_state.h"
180
181 #define GOAL_MESSAGE_TIME 10.0
182 #define GOAL_MESSAGE_COLOR GR_RGB(0, 242, 148)
183
184 levelgoals Level_goals;
185
186 // This is the load/save version of the level goals
187 #define LEVEL_GOAL_VERSION 4
188
189 void lgoal::GoalComplete(int handle, bool announce) {
190 m_goal_completed = true;
191
192 tOSIRISEventInfo ei;
193 ei.evt_level_goal_complete.level_goal_index = handle;
194
195 Osiris_CallLevelEvent(EVT_LEVEL_GOAL_COMPLETE, &ei);
196
197 if (m_name && announce && (this->m_flags & LGF_ENABLED)) {
198 // Draw on the HUD
199 int y = Game_window_h / 4;
200 // AddPersistentHUDMessage(GOAL_MESSAGE_COLOR,HUD_MSG_PERSISTENT_CENTER,y,GOAL_MESSAGE_TIME,m_completion_message);
201 char message[200];
202 snprintf(message, sizeof(message), "%s %s", TXT_COMPLETED_HUD, m_name);
203 if (Demo_flags == DF_RECORDING) {
204 DemoWritePersistantHUDMessage(GOAL_MESSAGE_COLOR, HUD_MSG_PERSISTENT_CENTER, GOAL_MESSAGE_TIME, GOAL_MESSAGE_TIME,
205 HPF_FADEOUT + HPF_FREESPACE_DRAW, SOUND_GOAL_COMPLETE, message);
206 }
207 if (Demo_flags != DF_PLAYBACK) {
208 AddPersistentHUDMessage(GOAL_MESSAGE_COLOR, HUD_MSG_PERSISTENT_CENTER, y, GOAL_MESSAGE_TIME,
209 HPF_FADEOUT + HPF_FREESPACE_DRAW, SOUND_GOAL_COMPLETE, message);
210 }
211
212 // Add to the game message buffer
213 AddGameMessage(message);
214 }
215 }
216
217 int lgoal::AddItem() {
218 if (m_num_items < MAX_GOAL_ITEMS) {
219 if (m_num_items < 1) {
220 m_item[m_num_items].m_type = LIT_OBJECT;
221 m_item[m_num_items].m_handle = OBJECT_HANDLE_NONE;
222 m_item[m_num_items].m_f_done = false;
223 } else {
224 m_item[m_num_items].m_type = m_item[0].m_type;
225 m_item[m_num_items].m_handle = -1;
226 m_item[m_num_items].m_f_done = false;
227 }
228
229 m_num_items++;
230 return m_num_items - 1;
231 } else {
232 return -1;
233 }
234 }
235
236 bool lgoal::DeleteItem(int index) {
237 int i;
238
239 if (index < 0 || index >= m_num_items)
240 return false;
241
242 for (i = index; i < m_num_items - 1; i++) {
243 m_item[i] = m_item[i + 1];
244 }
245
246 m_num_items--;
247
248 return true;
249 }
250
251 bool lgoal::ItemInfo(int index, char operation, char *type, int *handle, bool *done) {
252 if (index < 0 || index >= m_num_items)
253 return false;
254
255 switch (operation) {
256 case LO_SET_SPECIFIED:
257 if (type) {
258 m_item[index].m_type = *type;
259 }
260 if (handle) {
261 m_item[index].m_handle = *handle;
262 }
263 if (done) {
264 m_item[index].m_f_done = *done;
265 }
266 return true;
267 break;
268
269 case LO_GET_SPECIFIED:
270 if (type) {
271 *type = m_item[index].m_type;
272 }
273 if (handle) {
274 *handle = m_item[index].m_handle;
275 }
276 if (done) {
277 *done = m_item[index].m_f_done;
278 }
279 return true;
280 break;
281 }
282
283 return false;
284 }
285
286 bool lgoal::SetName(int handle, char *name) {
287 if (!name)
288 return false;
289
290 if (m_name)
291 mem_free(m_name);
292
293 m_name = (char *)mem_malloc(strlen(name) + 1);
294 strcpy(m_name, name);
295 m_modified = 1;
296
297 if (Game_mode & GM_MULTI && Netgame.local_role != LR_CLIENT) {
298 // send it off to the clients
299 SendMultiUpdate(handle);
300 }
301
302 return true;
303 }
304
305 void lgoal::SendMultiUpdate(int handle) {
306 ASSERT(Game_mode & GM_MULTI && Netgame.local_role == LR_SERVER);
307
308 msafe_struct mstruct;
309
310 mstruct.index = handle;
311 GetName(mstruct.message, MSAFE_MESSAGE_LENGTH);
312 mstruct.type = m_flags;
313 mstruct.count = m_priority;
314
315 msafe_CallFunction(MSAFE_MISC_LEVELGOAL, &mstruct);
316 }
317
318 bool lgoal::SetCompletionMessage(char *message) {
319 if (!message)
320 return false;
321
322 if (m_completion_message)
323 mem_free(m_completion_message);
324
325 m_completion_message = (char *)mem_malloc(strlen(message) + 1);
326 strcpy(m_completion_message, message);
327
328 return true;
329 }
330
331 bool lgoal::SetItemName(char *iname) {
332 if (!iname)
333 return false;
334
335 if (m_item_name)
336 mem_free(m_item_name);
337
338 m_item_name = (char *)mem_malloc(strlen(iname) + 1);
339 strcpy(m_item_name, iname);
340
341 return true;
342 }
343
344 bool lgoal::Priority(int handle, char operation, int *value) {
345 if (!value)
346 return false;
347
348 switch (operation) {
349 case LO_SET_SPECIFIED:
350 m_priority = *value;
351 m_modified = 1;
352 if (Game_mode & GM_MULTI && Netgame.local_role != LR_CLIENT) {
353 // send it off to the clients
354 SendMultiUpdate(handle);
355 }
356 return true;
357 break;
358 case LO_GET_SPECIFIED:
359 *value = m_priority;
360 return true;
361 break;
362 }
363
364 return false;
365 }
366
367 bool lgoal::GoalList(char operation, char *value) {
368 if (!value)
369 return false;
370
371 switch (operation) {
372 case LO_SET_SPECIFIED:
373 if (*value < 0)
374 *value = 0;
375 else if (*value >= MAX_GOAL_LISTS)
376 *value = MAX_GOAL_LISTS - 1;
377
378 m_g_list = *value;
379 return true;
380 break;
381 case LO_GET_SPECIFIED:
382 *value = m_g_list;
383 return true;
384 break;
385 }
386
387 return false;
388 }
389
390 bool lgoal::GetStatus(int handle, char operation, int *value, bool f_save_load, bool announce) {
391 if (!value)
392 return false;
393
394 switch (operation) {
395 case LO_SET_SPECIFIED:
396 m_flags |= *value;
397 m_modified = 1;
398
399 if (((*value) & LGF_COMPLETED) && !m_goal_completed) {
400 if (!f_save_load)
401 GoalComplete(handle, announce);
402 m_goal_completed = true;
403 }
404
405 if (Game_mode & GM_MULTI && Netgame.local_role != LR_CLIENT) {
406 // send it off to the clients
407 SendMultiUpdate(handle);
408 }
409 return true;
410 break;
411 case LO_GET_SPECIFIED:
412 *value = m_flags;
413 return true;
414 break;
415 case LO_CLEAR_SPECIFIED:
416 // multisafe will pass in 0xFFFFFFFF on a clear before actually setting flags,
417 // so we need to ignore that case
418 if ((*value) != 0xFFFFFFFF && m_goal_completed) {
419 // check to see if we are marking this goal as "Not completed"
420 if ((*value) & LGF_COMPLETED) {
421 // unmark this m_goal_completed variable
422 m_goal_completed = false;
423 }
424 }
425
426 m_flags &= ~(*value);
427 m_modified = 1;
428
429 if (Game_mode & GM_MULTI && Netgame.local_role != LR_CLIENT) {
430 // send it off to the clients
431 SendMultiUpdate(handle);
432 }
433 return true;
434 break;
435 }
436
437 return false;
438 }
439
440 bool lgoal::SetDesc(char *desc) {
441 if (!desc)
442 return false;
443
444 if (m_desc)
445 mem_free(m_desc);
446
447 m_desc = (char *)mem_malloc(strlen(desc) + 1);
448 strcpy(m_desc, desc);
449
450 return true;
451 }
452
453 int lgoal::GetName(char *name, int buffer_size) {
454 if (!name)
455 return 0;
456
457 if (buffer_size <= 0)
458 return strlen(m_name);
459
460 if (m_name == NULL) {
461 name[0] = '\0';
462 return 0;
463 }
464
465 strncpy(name, m_name, buffer_size);
466 name[buffer_size - 1] = '\0';
467
468 return strlen(m_name);
469 }
470
471 int lgoal::GetCompletionMessage(char *message, int buffer_size) {
472 if (!message)
473 return 0;
474
475 if (buffer_size <= 0)
476 return strlen(m_completion_message);
477
478 if (m_completion_message == NULL) {
479 message[0] = '\0';
480 return 0;
481 }
482
483 strncpy(message, m_completion_message, buffer_size);
484 message[buffer_size - 1] = '\0';
485
486 return strlen(m_completion_message);
487 }
488
489 int lgoal::GetItemName(char *iname, int buffer_size) {
490 if (!iname)
491 return 0;
492
493 if (buffer_size <= 0)
494 return strlen(m_item_name);
495
496 if (m_item_name == NULL) {
497 iname[0] = '\0';
498 return 0;
499 }
500
501 strncpy(iname, m_item_name, buffer_size);
502 iname[buffer_size - 1] = '\0';
503
504 return strlen(m_item_name);
505 }
506
507 int lgoal::GetDesc(char *desc, int buffer_size) {
508 if (!desc)
509 return 0;
510
511 if (buffer_size <= 0)
512 return strlen(m_desc);
513
514 if (m_desc == NULL) {
515 desc[0] = '\0';
516 return 0;
517 }
518
519 strncpy(desc, m_desc, buffer_size);
520 desc[buffer_size - 1] = '\0';
521
522 return strlen(m_desc);
523 }
524
525 int lgoal::GetNumItems() { return m_num_items; }
526
527 void lgoal::Reset(bool f_from_editor) {
528 if (m_name)
529 mem_free(m_name);
530
531 if (m_item_name)
532 mem_free(m_item_name);
533
534 if (m_desc)
535 mem_free(m_desc);
536
537 m_name = NULL;
538 m_item_name = NULL;
539 m_desc = NULL;
540 m_num_items = 0;
541 m_priority = 0;
542 m_g_list = 0;
543 m_goal_completed = false;
544
545 if (f_from_editor)
546 m_flags = LGF_ENABLED | LGF_TELCOM_LISTS | LGF_COMP_DALLAS;
547 else
548 m_flags = 0;
549 }
550
551 void lgoal::SendStateToPlayer(int index, int pnum) {
552 if (!(Game_mode & GM_MULTI))
553 return;
554
555 if (m_modified == 0)
556 return;
557
558 ASSERT(Netgame.local_role == LR_SERVER);
559 ASSERT(pnum >= 0 && pnum < MAX_NET_PLAYERS);
560
561 if (!(NetPlayers[pnum].flags & NPF_CONNECTED && NetPlayers[pnum].sequence == NETSEQ_PLAYING))
562 return;
563
564 mprintf(0, "Sending modified LevelGoal %d to player %d\n", index, pnum);
565
566 // now update the buddy handle list of the clients
567 int count = 0;
568 int size_offset;
569 uint8_t data[MAX_GAME_DATA_SIZE];
570
571 size_offset = START_DATA(MP_WORLD_STATES, data, &count);
572
573 MultiAddByte(WS_LEVELGOAL, data, &count);
574 MultiAddUshort(index, data, &count);
575
576 MultiAddInt(m_priority, data, &count);
577
578 char name[256];
579 GetName(name, 256);
580 int len = strlen(name) + 1;
581 MultiAddByte(len, data, &count);
582 memcpy(&data[count], name, len);
583 count += len;
584
585 MultiAddInt(m_flags, data, &count);
586
587 MultiAddByte(WS_END, data, &count);
588 END_DATA(count, data, size_offset);
589
590 // Send it out
591 nw_SendReliable(NetPlayers[pnum].reliable_socket, data, count, false);
592 }
593
594 void lgoal::ResetModified(void) { m_modified = 0; }
595
596 bool levelgoals::GoalStatus(int index, char operation, int *flags, bool announce) {
597 if (index < 0 || index >= m_num_goals)
598 return false;
599
600 return m_goal[index].GetStatus(index, operation, flags, false, announce);
601 }
602
603 bool levelgoals::GoalPriority(int index, char operation, int *flags) {
604 if (index < 0 || index >= m_num_goals)
605 return false;
606
607 return m_goal[index].Priority(index, operation, flags);
608 }
609
610 bool levelgoals::GoalGoalList(int index, char operation, char *value) {
611 if (index < 0 || index >= m_num_goals)
612 return false;
613
614 return m_goal[index].GoalList(operation, value);
615 }
616
617 int levelgoals::AddGoal(bool f_from_editor) {
618 if (m_num_goals < MAX_LEVEL_GOALS) {
619 m_goal[m_num_goals].Reset(f_from_editor);
620 m_num_goals++;
621 return m_num_goals - 1;
622 } else {
623 return -1;
624 }
625 }
626
627 bool levelgoals::DeleteGoal(int index) {
628 int i;
629
630 if (index < 0 || index >= m_num_goals)
631 return false;
632
633 m_goal[index].Reset(false);
634
635 for (i = index; i < m_num_goals - 1; i++) {
636 m_goal[i] = m_goal[i + 1];
637 }
638
639 m_num_goals--;
640
641 return true;
642 }
643
644 int levelgoals::GoalAddItem(int goal_index) {
645 if (goal_index < 0 || goal_index >= m_num_goals)
646 return -1;
647
648 return m_goal[goal_index].AddItem();
649 }
650
651 bool levelgoals::GoalDeleteItem(int goal_index, int item_index) {
652 if (goal_index < 0 || goal_index >= m_num_goals)
653 return false;
654
655 return m_goal[goal_index].DeleteItem(item_index);
656 }
657
658 bool levelgoals::GoalItemInfo(int goal_index, int index, char operation, char *type, int *handle, bool *done) {
659 if (goal_index < 0 || goal_index >= m_num_goals)
660 return false;
661
662 return m_goal[goal_index].ItemInfo(index, operation, type, handle, done);
663 }
664
665 bool levelgoals::GoalSetName(int goal_index, char *name) {
666 if (goal_index < 0 || goal_index >= m_num_goals)
667 return false;
668
669 return m_goal[goal_index].SetName(goal_index, name);
670 }
671
672 bool levelgoals::GoalSetItemName(int goal_index, char *iname) {
673 if (goal_index < 0 || goal_index >= m_num_goals)
674 return false;
675
676 return m_goal[goal_index].SetItemName(iname);
677 }
678
679 bool levelgoals::GoalSetDesc(int goal_index, char *desc) {
680 if (goal_index < 0 || goal_index >= m_num_goals)
681 return false;
682
683 return m_goal[goal_index].SetDesc(desc);
684 }
685
686 bool levelgoals::GoalSetCompletionMessage(int goal_index, char *message) {
687 if (goal_index < 0 || goal_index >= m_num_goals)
688 return false;
689
690 return m_goal[goal_index].SetCompletionMessage(message);
691 }
692
693 int levelgoals::GoalGetName(int goal_index, char *name, int buffer_size) {
694 if (goal_index < 0 || goal_index >= m_num_goals)
695 return -1;
696
697 return m_goal[goal_index].GetName(name, buffer_size);
698 }
699
700 int levelgoals::GoalGetCompletionMessage(int goal_index, char *message, int buffer_size) {
701 if (goal_index < 0 || goal_index >= m_num_goals)
702 return -1;
703
704 return m_goal[goal_index].GetCompletionMessage(message, buffer_size);
705 }
706
707 int levelgoals::GoalGetItemName(int goal_index, char *iname, int buffer_size) {
708 if (goal_index < 0 || goal_index >= m_num_goals)
709 return -1;
710
711 return m_goal[goal_index].GetItemName(iname, buffer_size);
712 }
713
714 int levelgoals::GoalGetDesc(int goal_index, char *desc, int buffer_size) {
715 if (goal_index < 0 || goal_index >= m_num_goals)
716 return -1;
717
718 return m_goal[goal_index].GetDesc(desc, buffer_size);
719 }
720
721 int levelgoals::GoalGetNumItems(int goal_index) {
722 if (goal_index < 0 || goal_index >= m_num_goals)
723 return -1;
724
725 return m_goal[goal_index].GetNumItems();
726 }
727
728 int levelgoals::GetNumGoals() { return m_num_goals; }
729
730 bool levelgoals::SaveLevelGoalInfo(CFILE *fptr) {
731 int i;
732 cf_WriteShort(fptr, LEVEL_GOAL_VERSION);
733
734 cf_WriteShort(fptr, m_num_goals);
735 for (i = 0; i < m_num_goals; i++) {
736 char name[256];
737 char iname[256];
738 char desc[2560];
739 char message[2560];
740 int len;
741 int num_items;
742 int j;
743 int priority = 0;
744 char g_list = 0;
745 int status = 0;
746
747 Level_goals.GoalStatus(i, LO_GET_SPECIFIED, &status);
748 cf_WriteInt(fptr, status);
749
750 Level_goals.GoalPriority(i, LO_GET_SPECIFIED, &priority);
751 cf_WriteInt(fptr, priority);
752
753 Level_goals.GoalGoalList(i, LO_GET_SPECIFIED, &g_list);
754 cf_WriteByte(fptr, g_list);
755
756 Level_goals.GoalGetName(i, name, 256);
757 Level_goals.GoalGetItemName(i, iname, 256);
758 Level_goals.GoalGetDesc(i, desc, 2560);
759 Level_goals.GoalGetCompletionMessage(i, message, 2560);
760
761 len = strlen(name);
762 cf_WriteShort(fptr, len);
763 if (len) {
764 cf_WriteBytes((uint8_t *)name, len, fptr);
765 }
766
767 len = strlen(iname);
768 cf_WriteShort(fptr, len);
769 if (len) {
770 cf_WriteBytes((uint8_t *)iname, len, fptr);
771 }
772
773 len = strlen(desc);
774 cf_WriteShort(fptr, len);
775 if (len) {
776 cf_WriteBytes((uint8_t *)desc, len, fptr);
777 }
778
779 len = strlen(message);
780 cf_WriteShort(fptr, len);
781 if (len) {
782 cf_WriteBytes((uint8_t *)message, len, fptr);
783 }
784
785 num_items = Level_goals.GoalGetNumItems(i);
786 cf_WriteShort(fptr, num_items);
787 for (j = 0; j < num_items; j++) {
788 char type = 0;
789 int handle;
790 bool f_done;
791
792 Level_goals.GoalItemInfo(i, j, LO_GET_SPECIFIED, &type, &handle, &f_done);
793 cf_WriteByte(fptr, type);
794 cf_WriteInt(fptr, handle);
795 cf_WriteByte(fptr, (char)f_done);
796 }
797 }
798
799 cf_WriteInt(fptr, m_flags);
800
801 return true;
802 }
803
804 bool levelgoals::LGStatus(char operation, int *value) {
805 if (!value)
806 return false;
807
808 switch (operation) {
809 case LO_SET_SPECIFIED:
810 m_flags |= *value;
811 return true;
812 break;
813 case LO_GET_SPECIFIED:
814 *value = m_flags;
815 return true;
816 break;
817 case LO_CLEAR_SPECIFIED:
818 m_flags &= ~(*value);
819 return true;
820 break;
821 }
822
823 return false;
824 }
825
826 bool levelgoals::LoadLevelGoalInfo(CFILE *fptr) {
827 CleanupAfterLevel();
828
829 int i;
830 int16_t version = cf_ReadShort(fptr);
831 int num_goals = cf_ReadShort(fptr);
832
833 for (i = 0; i < num_goals; i++) {
834 char name[256];
835 char iname[256];
836 char desc[2560];
837 char message[2560];
838 int len;
839 int num_items;
840 int j;
841 int priority;
842 char g_list;
843 int status;
844
845 int gi = AddGoal(false);
846 ASSERT(gi == i);
847
848 status = cf_ReadInt(fptr);
849 m_goal[i].GetStatus(i, LO_SET_SPECIFIED, &status, true);
850
851 priority = cf_ReadInt(fptr);
852 Level_goals.GoalPriority(i, LO_SET_SPECIFIED, &priority);
853
854 if (version < 4) {
855 g_list = 0;
856 } else {
857 g_list = cf_ReadByte(fptr);
858 }
859 Level_goals.GoalGoalList(i, LO_SET_SPECIFIED, &g_list);
860
861 len = cf_ReadShort(fptr);
862 if (len) {
863 cf_ReadBytes((uint8_t *)name, len, fptr);
864 }
865 name[len] = '\0';
866
867 len = cf_ReadShort(fptr);
868 if (len) {
869 cf_ReadBytes((uint8_t *)iname, len, fptr);
870 }
871 iname[len] = '\0';
872
873 len = cf_ReadShort(fptr);
874 if (len) {
875 cf_ReadBytes((uint8_t *)desc, len, fptr);
876 }
877 desc[len] = '\0';
878
879 if (version < 3) {
880 message[0] = '\0';
881 } else {
882 len = cf_ReadShort(fptr);
883 if (len) {
884 cf_ReadBytes((uint8_t *)message, len, fptr);
885 }
886 message[len] = '\0';
887 }
888
889 Level_goals.GoalSetName(i, name);
890 Level_goals.GoalSetItemName(i, iname);
891 Level_goals.GoalSetDesc(i, desc);
892 Level_goals.GoalSetCompletionMessage(i, message);
893
894 num_items = cf_ReadShort(fptr);
895 for (j = 0; j < num_items; j++) {
896 char type;
897 int handle;
898 bool f_done;
899
900 int item_slot = GoalAddItem(i);
901 ASSERT(item_slot == j);
902
903 type = cf_ReadByte(fptr);
904 handle = cf_ReadInt(fptr);
905 f_done = cf_ReadByte(fptr) != 0;
906 Level_goals.GoalItemInfo(i, j, LO_SET_SPECIFIED, &type, &handle, &f_done);
907 }
908 }
909
910 if (version >= 2)
911 m_flags = cf_ReadInt(fptr);
912 else
913 m_flags = 0;
914
915 ResetModifiedFlags();
916
917 return true;
918 }
919 /*
920 int levelgoals::GetCurrentPrimaryGoalIndex()
921 {
922 int i;
923 int cp;
924 int bestp = -1;
925 int bestg = -1;
926
927 for(i = 0; i < m_num_goals; i++)
928 {
929 int flags;
930 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
931
932 if(!(flags & (LGF_SECONDARY_GOAL | LGF_COMPLETED)))
933 {
934 if(flags & LGF_ENABLED)
935 {
936 m_goal[i].Priority(i,LO_GET_SPECIFIED, &cp);
937
938 if(cp > bestp)
939 {
940 bestp = cp;
941 bestg = i;
942 }
943 }
944 }
945 }
946
947 return bestg;
948 }*/
949
950 // int m_num_active_primaries = 0;
951 // int m_active_primaries[MAX_LEVEL_GOALS];
952 // int m_num_active_secondaries = 0;
953 // int m_active_secondaries[MAX_LEVEL_GOALS];
954
955 int levelgoals::GetNumActivePrimaryGoals() {
956 int x;
957 m_num_active_primaries = 0;
958
959 int i;
960 int bestp[4];
961 int cp; // Current priority
962 char cl; // Current list
963 int temp_p[MAX_GOAL_LISTS][MAX_LEVEL_GOALS];
964 char temp_g[MAX_GOAL_LISTS][MAX_LEVEL_GOALS];
965
966 int temp_count[MAX_GOAL_LISTS];
967
968 for (i = 0; i < MAX_GOAL_LISTS; i++) {
969 bestp[i] = -1;
970 temp_count[i] = 0;
971 }
972
973 // Determine the active number for the current list
974 for (i = 0; i < m_num_goals; i++) {
975 int flags;
976 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
977
978 if (!(flags & (LGF_COMPLETED | LGF_FAILED | LGF_SECONDARY_GOAL))) {
979 if (flags & LGF_ENABLED) {
980 m_goal[i].GoalList(LO_GET_SPECIFIED, &cl);
981 m_goal[i].Priority(i, LO_GET_SPECIFIED, &cp);
982
983 if (bestp[cl] == -1 || cp <= bestp[cl]) {
984 bestp[cl] = cp;
985 temp_p[cl][temp_count[cl]] = cp;
986 temp_g[cl][(temp_count[cl])++] = i;
987 }
988 }
989 }
990 }
991
992 for (x = 0; x < MAX_GOAL_LISTS; x++) {
993 for (i = 0; i < temp_count[x]; i++) {
994 if (temp_p[x][i] == bestp[x]) {
995 m_active_primaries[m_num_active_primaries++] = temp_g[x][i];
996 }
997 }
998 }
999
1000 return m_num_active_primaries;
1001 }
1002
1003 int levelgoals::GetActivePrimaryGoal(int p_index) {
1004 if (p_index < 0 || p_index >= m_num_active_primaries)
1005 return -1;
1006
1007 return m_active_primaries[p_index];
1008 }
1009
1010 int levelgoals::GetNumActiveSecondaryGoals() {
1011 int x;
1012 m_num_active_secondaries = 0;
1013
1014 int i;
1015 int bestp[4];
1016 int cp; // Current priority
1017 char cl; // Current list
1018 int temp_p[MAX_GOAL_LISTS][MAX_LEVEL_GOALS];
1019 char temp_g[MAX_GOAL_LISTS][MAX_LEVEL_GOALS];
1020
1021 int temp_count[MAX_GOAL_LISTS];
1022
1023 for (i = 0; i < MAX_GOAL_LISTS; i++) {
1024 bestp[i] = -1;
1025 temp_count[i] = 0;
1026 }
1027
1028 // Determine the active number for the current list
1029 for (i = 0; i < m_num_goals; i++) {
1030 int flags;
1031 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
1032
1033 if (!(flags & (LGF_COMPLETED | LGF_FAILED))) {
1034 if (flags & LGF_ENABLED) {
1035 if (flags & LGF_SECONDARY_GOAL) {
1036 m_goal[i].GoalList(LO_GET_SPECIFIED, &cl);
1037 m_goal[i].Priority(i, LO_GET_SPECIFIED, &cp);
1038
1039 if (bestp[cl] == -1 || cp <= bestp[cl]) {
1040 bestp[cl] = cp;
1041 temp_p[cl][temp_count[cl]] = cp;
1042 temp_g[cl][(temp_count[cl])++] = i;
1043 }
1044 }
1045 }
1046 }
1047 }
1048
1049 for (x = 0; x < MAX_GOAL_LISTS; x++) {
1050 for (i = 0; i < temp_count[x]; i++) {
1051 if (temp_p[x][i] == bestp[x]) {
1052 m_active_secondaries[m_num_active_secondaries++] = temp_g[x][i];
1053 }
1054 }
1055 }
1056
1057 return m_num_active_secondaries;
1058 }
1059
1060 int levelgoals::GetActiveSecondaryGoal(int s_index) {
1061 if (s_index < 0 || s_index >= m_num_active_secondaries)
1062 return -1;
1063
1064 return m_active_secondaries[s_index];
1065 }
1066
1067 void levelgoals::DoFrame() {
1068 int i;
1069
1070 // Check for auto-completion
1071 for (i = 0; i < m_num_goals; i++) {
1072 int flags;
1073 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
1074
1075 if (!(flags & (LGF_COMPLETED | LGF_FAILED))) {
1076 if (flags & LGF_ENABLED) {
1077 char ctype;
1078 int chandle;
1079 bool cdone;
1080 bool f_done = true;
1081
1082 m_goal[i].ItemInfo(0, LO_GET_SPECIFIED, &ctype, &chandle, &cdone);
1083
1084 if (ctype == LIT_TERRAIN_CELL) {
1085 if (Game_mode & GM_MULTI) {
1086 int j;
1087
1088 for (j = 0; j < MAX_PLAYERS; j++) {
1089 if ((NetPlayers[j].flags & NPF_CONNECTED) && (NetPlayers[j].sequence >= NETSEQ_PLAYING)) {
1090 object *player = &Objects[Players[j].objnum];
1091 if (!ROOMNUM_OUTSIDE(player->roomnum)) {
1092 f_done = false;
1093 }
1094 }
1095 }
1096 } else {
1097 if (!ROOMNUM_OUTSIDE(Player_object->roomnum)) {
1098 f_done = false;
1099 }
1100 }
1101 } else if (ctype == LIT_ANY_MINE) {
1102 if (Game_mode & GM_MULTI) {
1103 int j;
1104
1105 for (j = 0; j < MAX_PLAYERS; j++) {
1106 if ((NetPlayers[j].flags & NPF_CONNECTED) && (NetPlayers[j].sequence >= NETSEQ_PLAYING)) {
1107 object *player = &Objects[Players[j].objnum];
1108 if (ROOMNUM_OUTSIDE(player->roomnum)) {
1109 f_done = false;
1110 }
1111 }
1112 }
1113 } else {
1114 if (ROOMNUM_OUTSIDE(Player_object->roomnum)) {
1115 f_done = false;
1116 }
1117 }
1118 } else {
1119 continue;
1120 }
1121
1122 if (f_done) {
1123 int flags = LGF_COMPLETED;
1124 m_goal[i].GetStatus(i, LO_SET_SPECIFIED, &flags);
1125 }
1126 }
1127 }
1128 }
1129
1130 if (GetNumActivePrimaryGoals() == 0 && !(m_flags & LF_ALL_PRIMARIES_DONE)) {
1131 m_flags |= LF_ALL_PRIMARIES_DONE;
1132
1133 tOSIRISEventInfo ei;
1134 Osiris_CallLevelEvent(EVT_ALL_LEVEL_GOALS_COMPLETE, &ei);
1135 }
1136 }
1137
1138 void levelgoals::CleanupAfterLevel() {
1139 int i;
1140
1141 for (i = m_num_goals; i >= 0; i--) {
1142 DeleteGoal(i);
1143 }
1144 }
1145
1146 void levelgoals::InitLevel() {
1147 int i, j;
1148
1149 for (i = 0; i <= Highest_object_index; i++) {
1150 if (Objects[i].type != OBJ_NONE) {
1151 Objects[i].flags &=
1152 (~(OF_INFORM_PLAYER_COLLIDE_TO_LG | OF_INFORM_PLAYER_WEAPON_COLLIDE_TO_LG | OF_INFORM_DESTROY_TO_LG));
1153 }
1154 }
1155
1156 for (i = 0; i <= Highest_room_index; i++) {
1157 if (Rooms[i].used) {
1158 Rooms[i].flags &= (~(RF_INFORM_RELINK_TO_LG));
1159 }
1160 }
1161
1162 for (i = 0; i < Num_triggers; i++) {
1163 Triggers[i].flags &= (~(TF_INFORM_ACTIVATE_TO_LG));
1164 }
1165
1166 for (i = 0; i < m_num_goals; i++) {
1167 int flags;
1168 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
1169 flags &= LGF_COMP_MASK;
1170
1171 for (j = 0; j < m_goal[i].GetNumItems(); j++) {
1172 char type;
1173 int handle;
1174 bool done;
1175
1176 m_goal[i].ItemInfo(j, LO_GET_SPECIFIED, &type, &handle, &done);
1177
1178 switch (type) {
1179 case LIT_TERRAIN_CELL:
1180 break;
1181 case LIT_INTERNAL_ROOM:
1182 if (flags == LGF_COMP_ENTER) {
1183 if (handle >= 0 && handle <= Highest_room_index && Rooms[handle].used) {
1184 Rooms[handle].flags |= RF_INFORM_RELINK_TO_LG;
1185 }
1186 }
1187 break;
1188 case LIT_OBJECT:
1189 if (flags == LGF_COMP_PLAYER) {
1190 object *obj = ObjGet(handle);
1191 if (obj) {
1192 obj->flags |= OF_INFORM_PLAYER_COLLIDE_TO_LG;
1193 }
1194 } else if (flags == LGF_COMP_PLAYER_WEAPON) {
1195 object *obj = ObjGet(handle);
1196 if (obj) {
1197 obj->flags |= OF_INFORM_PLAYER_WEAPON_COLLIDE_TO_LG;
1198 }
1199 } else if (flags == LGF_COMP_DESTROY) {
1200 object *obj = ObjGet(handle);
1201 if (obj) {
1202 obj->flags |= OF_INFORM_DESTROY_TO_LG;
1203 }
1204 }
1205 break;
1206 case LIT_TRIGGER:
1207 if (handle >= 0 && handle < Num_triggers) {
1208 if (flags == LGF_COMP_ACTIVATE) {
1209 Triggers[handle].flags |= TF_INFORM_ACTIVATE_TO_LG;
1210 }
1211 }
1212 break;
1213 case LIT_ANY_MINE:
1214 break;
1215 }
1216 }
1217 }
1218 }
1219
1220 void levelgoals::Inform(char type, int comp_type, int handle) {
1221 int i, j;
1222
1223 for (i = 0; i < m_num_goals; i++) {
1224 int flags;
1225 m_goal[i].GetStatus(i, LO_GET_SPECIFIED, &flags);
1226
1227 if (!(flags & LGF_ENABLED) && (comp_type == LGF_COMP_ACTIVATE || comp_type == LGF_COMP_ENTER ||
1228 comp_type == LGF_COMP_PLAYER_WEAPON || comp_type == LGF_COMP_PLAYER)) {
1229 continue;
1230 }
1231
1232 int cur_comp_type = flags & LGF_COMP_MASK;
1233 int num_items = m_goal[i].GetNumItems();
1234
1235 if (flags != LGF_COMP_DALLAS && cur_comp_type != comp_type) {
1236 continue;
1237 }
1238
1239 for (j = 0; j < num_items; j++) {
1240 char ctype;
1241 int chandle;
1242 bool cdone;
1243
1244 m_goal[i].ItemInfo(j, LO_GET_SPECIFIED, &ctype, &chandle, &cdone);
1245
1246 if (cdone || type != ctype || chandle != handle) {
1247 continue;
1248 }
1249
1250 bool done = true;
1251 m_goal[i].ItemInfo(j, LO_SET_SPECIFIED, NULL, NULL, &done);
1252
1253 int k;
1254 bool f_done = true;
1255
1256 for (k = 0; k < num_items; k++) {
1257 m_goal[i].ItemInfo(j, LO_SET_SPECIFIED, NULL, NULL, &done);
1258 if (!done) {
1259 f_done = false;
1260 break;
1261 }
1262 }
1263
1264 if (f_done) {
1265 int flags = LGF_COMPLETED;
1266 m_goal[i].GetStatus(i, LO_SET_SPECIFIED, &flags);
1267 }
1268
1269 // Make this after the goal complete is done so that designers can say, if this and not
1270 // goal complete
1271 tOSIRISEventInfo ei;
1272 ei.evt_level_goal_item_complete.level_goal_index = i;
1273
1274 Osiris_CallLevelEvent(EVT_LEVEL_GOAL_ITEM_COMPLETE, &ei);
1275 }
1276 }
1277 }
1278
1279 int levelgoals::GoalFindId(const char *goal_name) {
1280 int i;
1281 char cur_name[200];
1282
1283 for (i = 0; i < m_num_goals; i++) {
1284 GoalGetName(i, cur_name, 200);
1285 if (stricmp(cur_name, goal_name) == 0) {
1286 return i;
1287 }
1288 }
1289
1290 return -1;
1291 }
1292
1293 void levelgoals::ResetModifiedFlags(void) {
1294 for (int i = 0; i < m_num_goals; i++) {
1295 m_goal[i].ResetModified();
1296 }
1297 }
1298
1299 void levelgoals::MultiSendChangedGoals(int pnum) {
1300 for (int i = 0; i < m_num_goals; i++) {
1301 m_goal[i].SendStateToPlayer(i, pnum);
1302 }
1303 }