]> icculus.org git repositories - taylor/freespace2.git/blob - src/mission/missiongoals.cpp
automatic language selection support
[taylor/freespace2.git] / src / mission / missiongoals.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Mission/MissionGoals.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Module for working with Mission goals
16  *
17  * $Log$
18  * Revision 1.4  2003/05/18 03:55:30  taylor
19  * automatic language selection support
20  *
21  * Revision 1.3  2002/06/09 04:41:22  relnev
22  * added copyright header
23  *
24  * Revision 1.2  2002/05/26 22:06:17  relnev
25  * makefile: disable stand_gui for now.
26  *
27  * rest: staticize some globals
28  *
29  * Revision 1.1.1.1  2002/05/03 03:28:09  root
30  * Initial import.
31  *
32  * 
33  * 15    10/27/99 5:22p Jefff
34  * Some coord changes for german ver
35  * 
36  * 14    9/06/99 9:46p Jefff
37  * skip mission support
38  * 
39  * 13    8/28/99 4:54p Dave
40  * Fixed directives display for multiplayer clients for wings with
41  * multiple waves. Fixed hud threat indicator rendering color.
42  * 
43  * 12    8/26/99 8:51p Dave
44  * Gave multiplayer TvT messaging a heavy dose of sanity. Cheat codes.
45  * 
46  * 11    8/03/99 6:21p Jefff
47  * fixed stupid bug with objectives screen key
48  * 
49  * 10    7/29/99 2:58p Jefff
50  * Ingame objective screen icon key now uses normal objective icons and
51  * text is drawn in code.
52  * 
53  * 9     7/24/99 4:19p Dave
54  * Fixed dumb code with briefing bitmaps. Made d3d zbuffer work much
55  * better. Made model code use zbuffer more intelligently.
56  * 
57  * 8     7/10/99 1:44p Andsager
58  * Modified directives listing so that current and recently
59  * satisfied/failed directives stay on screen.
60  * 
61  * 7     2/17/99 2:10p Dave
62  * First full run of squad war. All freespace and tracker side stuff
63  * works.
64  * 
65  * 6     2/02/99 4:35p Neilk
66  * fixed coordinate problem where primary goals was on top of interface in
67  * mission briefing
68  * 
69  * 5     1/30/99 5:08p Dave
70  * More new hi-res stuff.Support for nice D3D textures.
71  * 
72  * 4     11/05/98 5:55p Dave
73  * Big pass at reducing #includes
74  * 
75  * 3     10/13/98 9:28a Dave
76  * Started neatening up freespace.h. Many variables renamed and
77  * reorganized. Added AlphaColors.[h,cpp]
78  * 
79  * 2     10/07/98 10:53a Dave
80  * Initial checkin.
81  * 
82  * 1     10/07/98 10:49a Dave
83  * 
84  * 127   9/15/98 11:44a Dave
85  * Renamed builtin ships and wepaons appropriately in FRED. Put in scoring
86  * scale factors. Fixed standalone filtering of MD missions to non-MD
87  * hosts.
88  * 
89  * 126   6/09/98 10:31a Hoffoss
90  * Created index numbers for all xstr() references.  Any new xstr() stuff
91  * added from here on out should be added to the end if the list.  The
92  * current list count can be found in FreeSpace.cpp (search for
93  * XSTR_SIZE).
94  * 
95  * 125   5/21/98 2:47a Lawrance
96  * Fix some problems with event music
97  * 
98  * 124   4/15/98 9:05a Allender
99  * fix skpping of training mission with branchs
100  * 
101  * 123   4/08/98 10:34p Allender
102  * make threat indicators work in multiplayer.  Fix socket problem (once
103  * and for all???)
104  * 
105  * 122   4/03/98 2:47p Allender
106  * made directives act different when multiple waves of a wing take a long
107  * time to reappear
108  * 
109  * 121   4/01/98 9:21p John
110  * Made NDEBUG, optimized build with no warnings or errors.
111  * 
112  * 120   3/31/98 5:18p John
113  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
114  * bunch of debug stuff out of player file.  Made model code be able to
115  * unload models and malloc out only however many models are needed.
116  *  
117  * 
118  * 119   3/31/98 4:42p Allender
119  * mission objective support for team v. team mode.  Chatbox changes to
120  * make input box be correct length when typing
121  * 
122  * 118   3/23/98 1:39p Hoffoss
123  * Fixed bug where long objects weren't being split at the right point.
124  * 
125  * 117   3/17/98 12:40a Lawrance
126  * Add missing struct members to state
127  * 
128  * 116   3/09/98 4:24p Lawrance
129  * Don't play directive sound effect if gauge is disabled
130  * 
131  * 115   3/04/98 5:51p Hoffoss
132  * Fixed warning
133  * 
134  * 114   3/02/98 9:30p Allender
135  * make sexpression evaluation for arrivals/departures and goals happen
136  * every frame
137  * 
138  * 113   3/02/98 10:08a Hoffoss
139  * Fixed bugs in saving/restoring state.
140  * 
141  * 112   2/27/98 4:37p Hoffoss
142  * Combined Objectives screen into Mission Log screen.
143  * 
144  * 111   2/26/98 10:07p Hoffoss
145  * Rewrote state saving and restoring to fix bugs and simplify the code.
146  * 
147  * 110   2/23/98 3:06p Hoffoss
148  * Made objectives with only one target not show the [1] thing after it.
149  * 
150  * 109   2/22/98 4:30p John
151  * More string externalization classification
152  * 
153  * 108   2/22/98 12:19p John
154  * Externalized some strings
155  * 
156  * 107   2/20/98 8:33p Lawrance
157  * Added mission_goals_incomplete()
158  * 
159  * 106   2/06/98 12:12a Lawrance
160  * Play sound effect when continue is pressed.
161  * 
162  * 105   2/05/98 10:14p Lawrance
163  * Implement Save and Quit
164  * 
165  * 104   2/04/98 4:32p Allender
166  * support for multiple briefings and debriefings.  Changes to mission
167  * type (now a bitfield).  Bitfield defs for multiplayer modes
168  * 
169  * 103   1/30/98 4:24p Hoffoss
170  * Added a 3 second delay for directives before they get displayed.
171  * 
172  * 102   1/29/98 10:26a Hoffoss
173  * Made changes so arrow buttons repeat scrolling when held down.
174  * 
175  * 101   1/28/98 6:19p Dave
176  * Reduced standalone memory usage ~8 megs. Put in support for handling
177  * multiplayer submenu handling for endgame, etc.
178  * 
179  * 100   1/27/98 11:00a Lawrance
180  * Fix bug with showing number of resolved goals in the objective status
181  * popup.
182  * 
183  * 99    1/27/98 10:56a Allender
184  * slight changes to scoring stuff.  Added work for Baranec :-)
185  * 
186  * 98    1/26/98 10:02p Allender
187  * some scoring stuff
188  * 
189  * 97    1/20/98 2:26p Hoffoss
190  * Removed references to timestamp_ticker, used timestamp() instead.
191  * 
192  * 96    1/19/98 9:37p Allender
193  * Great Compiler Warning Purge of Jan, 1998.  Used pragma's in a couple
194  * of places since I was unsure of what to do with code.
195  * 
196  * 95    1/15/98 5:23p Lawrance
197  * Add HUD gauge to indicate completed objectives.
198  * 
199  * 94    1/15/98 1:29p Hoffoss
200  * Made chained events only check next event if that event is also
201  * chained.
202  * 
203  * 93    1/13/98 5:37p Dave
204  * Reworked a lot of standalone interface code. Put in single and
205  * multiplayer popups for death sequence. Solidified multiplayer kick
206  * code.
207  * 
208  * 92    1/12/98 5:17p Allender
209  * fixed primary fired problem and ship warp out problem.  Made new
210  * mission goal info packet to deal with goals more accurately. 
211  * 
212  * 91    1/11/98 10:02p Allender
213  * removed <winsock.h> from headers which included it.  Made psnet_socket
214  * type which is defined just as SOCKET type is.
215  * 
216  * 90    1/10/98 1:14p John
217  * Added explanation to debug console commands
218  * 
219  * 89    1/08/98 10:26a Lawrance
220  * Delay directive success sound effect about 1/2 second.
221  * 
222  * 88    1/07/98 6:46p Lawrance
223  * If a goal is invalid, ignore it when evaluating mission success.
224  * 
225  * 87    1/07/98 11:09a Lawrance
226  * Add sound hook for when directive gets completed.
227  * 
228  * 86    1/02/98 3:07p Hoffoss
229  * Changed sexp evaluation to every half second.
230  * 
231  * 85    12/27/97 8:08p Lawrance
232  * get savegames working again
233  * 
234  * 84    12/26/97 10:02p Lawrance
235  * Add event music for when goals fail.
236  * 
237  * 83    12/22/97 6:07p Hoffoss
238  * Made directives flash when completed, fixed but with is-destroyed
239  * operator.
240  * 
241  * 82    12/21/97 4:33p John
242  * Made debug console functions a class that registers itself
243  * automatically, so you don't need to add the function to
244  * debugfunctions.cpp.  
245  * 
246  * 81    12/19/97 12:43p Hoffoss
247  * Changed code to allow counts in directives.
248  * 
249  * 80    12/15/97 5:26p Allender
250  * temporary code to display for 5 second completion status of objectives
251  * 
252  * 79    12/03/97 4:16p Hoffoss
253  * Changed sound stuff used in interface screens for interface purposes.
254  * 
255  * 78    12/03/97 11:35a Hoffoss
256  * Made changes to HUD messages send throughout the game.
257  * 
258  * 77    12/02/97 10:47a Hoffoss
259  * Changed mention of goals to objectives.
260  * 
261  * 76    12/01/97 12:26a Lawrance
262  * Add flag  MGF_NO_MUSIC to mission_goal struct, to avoid playing music
263  * for certain goals
264  * 
265  * 75    11/21/97 2:16p Allender
266  * debug keys to mark all goals (p/s/b) as satisfied
267  * 
268  * 74    11/17/97 11:45a Johnson
269  * when marking goals false, add log entry.  Be sure that "delay" function
270  * check for equal to delay as well
271  * 
272  * 73    11/13/97 10:16p Hoffoss
273  * Added icons to mission log scrollback.
274  * 
275  * 72    11/13/97 4:05p Hoffoss
276  * Added hiding code for mission log entries.
277  * 
278  * 71    11/05/97 7:11p Hoffoss
279  * Made changed to the hud message system.  Hud messages can now have
280  * sources so they can be color coded.
281  * 
282  * 70    11/02/97 10:09p Lawrance
283  * add missing fields from mission_event to save/restore
284  * 
285  * 69    10/31/97 4:28p Allender
286  * fail all incomplete mission goals when mission is over
287  * 
288  * 68    10/29/97 11:06a Jasen
289  * eval goals/events every 1.75 seconds instead of every 2.5 seconds for
290  * slightly more accurate timing of events
291  * 
292  * 67    10/28/97 10:05a Allender
293  * added function to mark all goals as failed
294  * 
295  * 66    10/28/97 9:33a Lawrance
296  * change 'Alt-Q' in HUD message to use text for bound key
297  * 
298  * 65    10/27/97 5:03p Hoffoss
299  * Fixed comment.
300  * 
301  * 64    10/23/97 2:16p Johnson
302  * make pointers point to NULl after freeing to prevent problems when
303  * tried to free multiple times
304  * 
305  * 63    10/16/97 2:35p Hoffoss
306  * Enhanced the mission goals screen a little.
307  * 
308  * 62    10/16/97 1:28p Hoffoss
309  * New mission goals screen implemented.
310  * 
311  * 61    10/10/97 6:15p Hoffoss
312  * Implemented a training objective list display.
313  * 
314  * 60    10/09/97 4:44p Hoffoss
315  * Dimmed training window glass and made it less transparent, added flags
316  * to events, set he stage for detecting current events.
317  * 
318  * 59    10/06/97 4:11p Lawrance
319  * add missing field from mission_events to save/restore
320  * 
321  * 58    10/03/97 4:14p Hoffoss
322  * Augmented event debug view code.
323  * 
324  * 57    9/30/97 10:01a Hoffoss
325  * Added event chaining support to Fred and FreeSpace.
326  * 
327  * 56    9/29/97 1:58p Hoffoss
328  * Need to check in changes so I can get merged code to continue working
329  * on event chaining code.
330  *
331 */
332
333 #include "freespace.h"
334 #include "object.h"
335 #include "missiongoals.h"
336 #include "missionparse.h"
337 #include "missionlog.h"
338 #include "missiontraining.h"
339 #include "missionscreencommon.h"
340 #include "gamesequence.h"
341 #include "hud.h"
342 #include "key.h"
343 #include "2d.h"
344 #include "timer.h"
345 #include "linklist.h"
346 #include "ship.h"
347 #include "ai.h"
348 #include "parselo.h"
349 #include "sexp.h"
350 #include "eventmusic.h"
351 #include "multi.h"
352 #include "multimsgs.h"
353 #include "stand_gui.h"
354 #include "ui.h"
355 #include "bmpman.h"
356 #include "sound.h"
357 #include "gamesnd.h"
358 #include "alphacolors.h"
359 #include "multi_team.h"
360 #include "localize.h"
361
362 // timestamp stuff for evaluating mission goals
363 #define GOAL_TIMESTAMP                          0                               // make immediately eval
364 #define GOAL_TIMESTAMP_TRAINING 500                     // every half second
365
366 #define MAX_GOALS_PER_LIST                      15
367 #define MAX_GOAL_LINES  200
368
369 // indicies for coordinates
370 #define GOAL_SCREEN_X_COORD 0
371 #define GOAL_SCREEN_Y_COORD 1
372 #define GOAL_SCREEN_W_COORD 2
373 #define GOAL_SCREEN_H_COORD 3
374
375 /*
376 #define GOAL_SCREEN_TEXT_X      81
377 #define GOAL_SCREEN_TEXT_Y      95
378 #define GOAL_SCREEN_TEXT_W      385
379 #define GOAL_SCREEN_TEXT_H      299
380 #define GOAL_SCREEN_ICON_X 45
381 */
382
383 static int Goal_screen_text_coords[GR_NUM_RESOLUTIONS][4] = {
384         {
385                 81,95,385,299           // GR_640
386         },
387         {
388                 130,152,385,299 // GR_1024
389         }
390 };
391
392 static int Goal_screen_icon_xcoord[GR_NUM_RESOLUTIONS] = {
393         45,     // GR_640
394         72              // GR_1024
395 };
396
397
398 // german version gets slightly diff coords
399 static int Objective_key_text_coords_gr[GR_NUM_RESOLUTIONS][3][2] = {
400         {
401                 // GR_640
402                 {175, 344},
403                 {316, 344},
404                 {432, 344}
405         },
406         {
407                 // GR_1024
408                 {310, 546},
409                 {536, 546},
410                 {688, 546}
411         }
412 };
413 static int Objective_key_icon_coords_gr[GR_NUM_RESOLUTIONS][3][2] = {
414         {
415                 // GR_640
416                 {150, 339},
417                 {290, 339},
418                 {406, 339}
419         },
420         {
421                 // GR_1024
422                 {272, 542},
423                 {498, 542},
424                 {650, 542}
425         }
426 };
427
428 static int Objective_key_text_coords[GR_NUM_RESOLUTIONS][3][2] = {
429         {
430                 // GR_640
431                 {195, 344},
432                 {306, 344},
433                 {432, 344}
434         },
435         {
436                 // GR_1024
437                 {310, 546},
438                 {486, 546},
439                 {688, 546}
440         }
441 };
442 static int Objective_key_icon_coords[GR_NUM_RESOLUTIONS][3][2] = {
443         {
444                 // GR_640
445                 {170, 339},
446                 {280, 339},
447                 {406, 339}
448         },
449         {
450                 // GR_1024
451                 {272, 542},
452                 {448, 542},
453                 {650, 542}
454         }
455 };
456
457
458 #define NUM_GOAL_SCREEN_BUTTONS                 3  // total number of buttons
459 #define GOAL_SCREEN_BUTTON_SCROLL_UP    0
460 #define GOAL_SCREEN_BUTTON_SCROLL_DOWN  1
461 #define GOAL_SCREEN_BUTTON_RETURN               2
462
463 struct goal_list {
464         int count;
465         mission_goal *list[MAX_GOALS_PER_LIST];
466         int line_offsets[MAX_GOALS_PER_LIST];
467         int line_spans[MAX_GOALS_PER_LIST];
468
469         goal_list() : count(0) {}
470         void add(mission_goal *m);
471         void set();
472         void icons_display(int y);
473 };
474
475 struct goal_buttons {
476         char *filename;
477         int x, y;
478         int hotspot;
479         UI_BUTTON button;  // because we have a class inside this struct, we need the constructor below..
480
481         goal_buttons(char *name, int x1, int y1, int h) : filename(name), x(x1), y(y1), hotspot(h) {}
482 };
483
484 struct goal_text {
485         int m_num_lines;
486         int m_line_sizes[MAX_GOAL_LINES];
487         char *m_lines[MAX_GOAL_LINES];
488
489         void init();
490         int add(char *text = NULL);
491         void display(int n, int y);
492 };
493
494 int Num_mission_events;
495 int Num_goals = 0;                                                              // number of goals for this mission
496 int Event_index;  // used by sexp code to tell what event it came from
497 int Mission_goal_timestamp;
498
499 mission_event Mission_events[MAX_MISSION_EVENTS];
500 mission_goal Mission_goals[MAX_GOALS];          // structure for the goals of this mission
501 static goal_text Goal_text;
502
503 #define DIRECTIVE_SOUND_DELAY                   500                                     // time directive success sound effect is delayed
504 #define DIRECTIVE_SPECIAL_DELAY         7000                                    // mark special directives as true after 7 seconds
505
506 static int Mission_directive_sound_timestamp;   // timestamp to control when directive succcess sound gets played
507 static int Mission_directive_special_timestamp; // used to specially mark a directive as true even though it's not
508
509 char *Goal_type_text(int n)
510 {
511         switch (n) {
512                 case 0: 
513                         return XSTR( "Primary", 396);
514
515                 case 1:
516                         return XSTR( "Secondary", 397);
517
518                 case 2:
519                         return XSTR( "Bonus", 398);
520         }
521
522         return NULL;
523 };
524
525 static int Goal_screen_text_x;
526 static int Goal_screen_text_y;
527 static int Goal_screen_text_w;
528 static int Goal_screen_text_h;
529 static int Goal_screen_icon_x;
530
531 static goal_list Primary_goal_list;
532 static goal_list Secondary_goal_list;
533 static goal_list Bonus_goal_list;
534
535 static int Scroll_offset;
536 static int Goals_screen_bg_bitmap;
537 static int Goal_complete_bitmap;
538 static int Goal_incomplete_bitmap;
539 static int Goal_failed_bitmap;
540 static UI_WINDOW Goals_screen_ui_window;
541
542 goal_buttons Goal_buttons[NUM_GOAL_SCREEN_BUTTONS] = {
543 //XSTR:OFF
544         goal_buttons("MOB_00", 475, 288, 0),
545         goal_buttons("MOB_01", 475, 336, 1),
546         goal_buttons("MOB_02", 553, 409, 2),
547 //XSTR:ON
548 };
549
550 void goal_screen_button_pressed(int num);
551 void goal_screen_scroll_up();
552 void goal_screen_scroll_down();
553
554 //
555 /// goal_list structure functions
556 //
557
558 void goal_list::add(mission_goal *m)
559 {
560         Assert(count < MAX_GOALS_PER_LIST);
561         list[count++] = m;
562 }
563
564 void goal_list::set()
565 {
566         int i;
567
568         for (i=0; i<count; i++) {
569                 line_offsets[i] = Goal_text.m_num_lines;
570                 line_spans[i] = Goal_text.add(list[i]->message);
571                 
572                 if (i < count - 1)
573                         Goal_text.add();
574         }
575 }
576
577 void goal_list::icons_display(int yoff)
578 {
579         int i, y, ys, bmp, font_height;
580
581         font_height = gr_get_font_height();
582         for (i=0; i<count; i++) {
583                 y = line_offsets[i] - yoff;
584
585                 bmp = -1;                       // initialize for safety.
586                 switch (list[i]->satisfied) {
587                         case GOAL_COMPLETE:
588                                 bmp = Goal_complete_bitmap;
589                                 break;
590
591                         case GOAL_INCOMPLETE:
592                                 bmp = Goal_incomplete_bitmap;
593                                 break;
594
595                         case GOAL_FAILED:
596                                 bmp = Goal_failed_bitmap;
597                                 break;
598                 }
599
600                 if (bmp >= 0) {
601                         bm_get_info(bmp, NULL, &ys, NULL);
602                         y = Goal_screen_text_y                                          // offset of text window on screen
603                                 + y * font_height                                                       // relative line position offset
604                                 + line_spans[i] * font_height / 2       // center of text offset
605                                 - ys / 2;                                                                       // center of icon offest
606
607                         if ((y >= Goal_screen_text_y - ys / 2) && (y + ys <= Goal_screen_text_y + Goal_screen_text_h + ys / 2)) {
608                                 gr_set_bitmap(bmp);
609                                 gr_bitmap(Goal_screen_icon_x, y);
610                         }
611                 }
612         }
613 }
614
615 //
616 /// goal_text structure functions
617 //
618
619 // initializes the goal text struct (empties it out)
620 void goal_text::init()
621 {
622         m_num_lines = 0;
623 }
624
625 // Adds lines of goal text.  If passed NULL (or nothing passed) a blank line is added.  If
626 // the text is too long, it is automatically split into more than one line.
627 // Returns the number of lines added.
628 int goal_text::add(char *text)
629 {
630         int max, count;
631
632         max = MAX_GOAL_LINES - m_num_lines;
633         if (max < 1) {
634                 Error(LOCATION, "Goal text space exhausted");
635                 return 0;
636         }
637
638         if (!text) {
639                 m_lines[m_num_lines] = NULL;
640                 m_line_sizes[m_num_lines++] = 0;
641                 return 1;
642         }
643
644         count = split_str(text, Goal_screen_text_w - Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_X_COORD] + Goal_screen_icon_xcoord[gr_screen.res], m_line_sizes + m_num_lines, m_lines + m_num_lines, max);
645         m_num_lines += count;
646         return count;
647 }
648
649 // Display a line of goal text
650 //   n = goal text line number
651 //   y = y offset to draw relative to goal text area top
652 void goal_text::display(int n, int y)
653 {
654         int y1, w, h;
655         char buf[MAX_GOAL_TEXT];
656
657         if ((n < 0) || (n >= m_num_lines) || (m_line_sizes[n] < 1))
658                 return;  // out of range, don't draw anything
659
660         Assert(m_line_sizes[n] < MAX_GOAL_TEXT);
661         y += Goal_screen_text_y;
662         if (*m_lines[n] == '*') {  // header line
663                 gr_set_color_fast(&Color_text_heading);
664                 strncpy(buf, m_lines[n] + 1, m_line_sizes[n] - 1);
665                 buf[m_line_sizes[n] - 1] = 0;
666
667                 gr_get_string_size(&w, &h, buf);
668                 y1 = y + h / 2 - 1;
669                 gr_line(Goal_screen_icon_x, y1, Goal_screen_text_x - 2, y1);
670                 gr_line(Goal_screen_text_x + w + 1, y1, Goal_screen_icon_x + Goal_screen_text_w, y1);
671
672         } else {
673                 gr_set_color_fast(&Color_text_normal);
674                 strncpy(buf, m_lines[n], m_line_sizes[n]);
675                 buf[m_line_sizes[n]] = 0;
676         }
677
678         gr_printf(Goal_screen_text_x, y, buf);
679 }
680
681 // mission_init_goals: initializes info for goals.  Called as part of mission initialization.
682 void mission_init_goals()
683 {
684         int i;
685
686         Num_goals = 0;
687         for (i=0; i<MAX_GOALS; i++) {
688                 Mission_goals[i].satisfied = GOAL_INCOMPLETE;
689                 Mission_goals[i].flags = 0;
690                 Mission_goals[i].team = 0;
691         }
692
693         Num_mission_events = 0;
694         for (i=0; i<MAX_MISSION_EVENTS; i++) {
695                 Mission_events[i].result = 0;
696                 Mission_events[i].flags = 0;
697                 Mission_events[i].count = 0;
698                 Mission_events[i].satisfied_time = 0;
699                 Mission_events[i].born_on_date = 0;
700                 Mission_events[i].team = -1;
701         }
702
703         Mission_goal_timestamp = timestamp(GOAL_TIMESTAMP);
704         Mission_directive_sound_timestamp = 0;
705         Mission_directive_special_timestamp = timestamp(-1);            // need to make invalid right away
706 }
707
708 // called at the end of a mission for cleanup
709 void mission_event_shutdown()
710 {
711         int i;
712
713         for (i=0; i<Num_mission_events; i++) {
714                 if (Mission_events[i].objective_text) {
715                         free(Mission_events[i].objective_text);
716                         Mission_events[i].objective_text = NULL;
717                 }
718                 if (Mission_events[i].objective_key_text) {
719                         free(Mission_events[i].objective_key_text);
720                         Mission_events[i].objective_key_text = NULL;
721                 }
722         }
723 }
724
725 // called once right before entering the show goals screen to do initializations.
726 void mission_show_goals_init()
727 {
728         int i, type, team_num=0;                // JAS: I set team_num to 0 because it was used without being initialized.
729         goal_buttons *b;
730
731         Scroll_offset = 0;
732         Primary_goal_list.count = 0;
733         Secondary_goal_list.count = 0;
734         Bonus_goal_list.count = 0;
735
736         Goal_screen_text_x = Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_X_COORD];
737         Goal_screen_text_y = Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_Y_COORD];
738         Goal_screen_text_w = Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_W_COORD];
739         Goal_screen_text_h = Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_H_COORD];
740         Goal_screen_icon_x = Goal_screen_icon_xcoord[gr_screen.res];
741
742         // fill up the lists so we can display the goals appropriately
743         for (i=0; i<Num_goals; i++) {
744                 if (Mission_goals[i].type & INVALID_GOAL){  // don't count invalid goals here
745                         continue;
746                 }
747
748                 if ( (Game_mode & GM_MULTIPLAYER) && (The_mission.game_type & MISSION_TYPE_MULTI_TEAMS) && (Mission_goals[i].team != team_num) ){
749                         continue;
750                 }
751
752                 type = Mission_goals[i].type & GOAL_TYPE_MASK;
753                 switch (type) {
754                 case PRIMARY_GOAL:
755                         Primary_goal_list.add(&Mission_goals[i]);
756                         break;
757
758                 case SECONDARY_GOAL:
759                         Secondary_goal_list.add(&Mission_goals[i]);
760                         break;
761
762                 case BONUS_GOAL:
763                         if (Mission_goals[i].satisfied == GOAL_COMPLETE){
764                                 Bonus_goal_list.add(&Mission_goals[i]);
765                         }
766                         break;
767         
768                 default:
769                         Error(LOCATION, "Unknown goal priority encountered when displaying goals in mission\n");
770                         break;
771                 } // end switch
772         } // end for
773
774         Goal_text.init();
775
776         Goal_text.add(XSTR( "*Primary Objectives", 399));
777         Goal_text.add();
778         Primary_goal_list.set();
779
780         if (Secondary_goal_list.count) {
781                 Goal_text.add();
782                 Goal_text.add();
783                 Goal_text.add(XSTR( "*Secondary Objectives", 400));
784                 Goal_text.add();
785                 Secondary_goal_list.set();
786         }
787
788         if (Bonus_goal_list.count) {
789                 Goal_text.add();
790                 Goal_text.add();
791                 Goal_text.add(XSTR( "*Bonus Objectives", 401));
792                 Goal_text.add();
793                 Bonus_goal_list.set();
794         }
795
796         common_set_interface_palette("ObjectivesPalette");  // set the interface palette
797         Goals_screen_ui_window.create(0, 0, gr_screen.max_w, gr_screen.max_h, 0);
798         Goals_screen_ui_window.set_mask_bmap("Objectives-m");
799
800         for (i=0; i<NUM_GOAL_SCREEN_BUTTONS; i++) {
801                 b = &Goal_buttons[i];
802
803                 b->button.create(&Goals_screen_ui_window, "", b->x, b->y, 60, 30, (i < 2), 1);
804                 // set up callback for when a mouse first goes over a button
805                 b->button.set_highlight_action(common_play_highlight_sound);
806                 b->button.set_bmaps(b->filename);
807                 b->button.link_hotspot(b->hotspot);
808         }
809
810         // set up hotkeys for buttons so we draw the correct animation frame when a key is pressed
811         Goal_buttons[GOAL_SCREEN_BUTTON_SCROLL_UP].button.set_hotkey(KEY_UP);
812         Goal_buttons[GOAL_SCREEN_BUTTON_SCROLL_DOWN].button.set_hotkey(KEY_DOWN);
813
814         Goals_screen_bg_bitmap = bm_load("ObjectivesBG");
815         Goal_complete_bitmap = bm_load("ObjComp");
816         Goal_incomplete_bitmap = bm_load("ObjIncomp");
817         Goal_failed_bitmap = bm_load("ObjFail");
818
819
820         // if (Goal_incomplete_bitmap < 0) Int3();
821
822         if (Goals_screen_bg_bitmap < 0) {
823                 Warning(LOCATION, "Could not load the background bitmap: ObjectivesBG.pcx");
824         }
825 }
826
827 // cleanup called when exiting the show goals screen
828 void mission_show_goals_close()
829 {
830         if (Goals_screen_bg_bitmap >= 0)
831                 bm_unload(Goals_screen_bg_bitmap);
832
833         if (Goal_complete_bitmap)
834                 bm_unload(Goal_complete_bitmap);
835         
836         if (Goal_incomplete_bitmap)
837                 bm_unload(Goal_incomplete_bitmap);
838         
839         if (Goal_failed_bitmap)
840                 bm_unload(Goal_failed_bitmap);
841
842         Goals_screen_ui_window.destroy();
843         common_free_interface_palette();                // restore game palette
844         game_flush();
845 }
846         
847 // called once a frame during show goals state to process events and render the screen
848 void mission_show_goals_do_frame(float frametime)
849 {
850         int k, i, y, z;
851         int font_height = gr_get_font_height();
852         
853         k = Goals_screen_ui_window.process();
854         switch (k) {
855                 case KEY_ESC:
856                         mission_goal_exit();                    
857                         break;
858                 
859                 case KEY_DOWN:
860                         goal_screen_scroll_down();
861                         break;
862
863                 case KEY_UP:
864                         goal_screen_scroll_up();
865                         break;
866
867                 default:
868                         // do nothing
869                         break;
870         }       // end switch
871
872         for (i=0; i<NUM_GOAL_SCREEN_BUTTONS; i++){
873                 if (Goal_buttons[i].button.pressed()){
874                         goal_screen_button_pressed(i);
875                 }
876         }
877
878         GR_MAYBE_CLEAR_RES(Goals_screen_bg_bitmap);
879         if (Goals_screen_bg_bitmap >= 0) {
880                 gr_set_bitmap(Goals_screen_bg_bitmap);
881                 gr_bitmap(0, 0);
882         }
883         Goals_screen_ui_window.draw();
884
885         y = 0;
886         z = Scroll_offset;
887         while (y + font_height <= Goal_screen_text_h) {
888                 Goal_text.display(z, y);
889                 y += font_height;
890                 z++;
891         }
892
893         Primary_goal_list.icons_display(Scroll_offset);
894         Secondary_goal_list.icons_display(Scroll_offset);
895         Bonus_goal_list.icons_display(Scroll_offset);
896
897         gr_flip();
898 }
899
900 // Mission Log Objectives subscreen init.
901 // Called once right before entering the Mission Log screen to do initializations.
902 int ML_objectives_init(int x, int y, int w, int h)
903 {
904         int i, type, team_num;
905
906         Primary_goal_list.count = 0;
907         Secondary_goal_list.count = 0;
908         Bonus_goal_list.count = 0;
909
910         Goal_screen_text_x = x - Goal_screen_icon_xcoord[gr_screen.res] + Goal_screen_text_coords[gr_screen.res][GOAL_SCREEN_X_COORD];
911         Goal_screen_text_y = y;
912         Goal_screen_text_w = w;
913         Goal_screen_text_h = h;
914         Goal_screen_icon_x = x;
915
916         team_num = 0;                   // this is the default team -- we will change it if in a multiplayer team v. team game
917         if ( (Game_mode & GM_MULTIPLAYER) && (The_mission.game_type & MISSION_TYPE_MULTI_TEAMS) ){
918                 team_num = Net_player->p_info.team;
919         }
920
921         // fill up the lists so we can display the goals appropriately
922         for (i=0; i<Num_goals; i++) {
923                 if (Mission_goals[i].type & INVALID_GOAL){  // don't count invalid goals here
924                         continue;
925                 }
926
927                 if ( (Game_mode & GM_MULTIPLAYER) && (The_mission.game_type & MISSION_TYPE_MULTI_TEAMS) && (Mission_goals[i].team != team_num) ){
928                         continue;
929                 }
930
931                 type = Mission_goals[i].type & GOAL_TYPE_MASK;
932                 switch (type) {
933                         case PRIMARY_GOAL:
934                                 Primary_goal_list.add(&Mission_goals[i]);
935                                 break;
936
937                         case SECONDARY_GOAL:
938                                 Secondary_goal_list.add(&Mission_goals[i]);
939                                 break;
940
941                         case BONUS_GOAL:
942                                 if (Mission_goals[i].satisfied == GOAL_COMPLETE){
943                                         Bonus_goal_list.add(&Mission_goals[i]);
944                                 }
945                                 break;
946         
947                         default:
948                                 Error(LOCATION, "Unknown goal priority encountered when displaying goals in mission\n");
949                                 break;
950                 } // end switch
951         } // end for
952
953         Goal_text.init();
954
955         Goal_text.add(XSTR( "*Primary Objectives", 399));
956         Goal_text.add();
957         Primary_goal_list.set();
958
959         if (Secondary_goal_list.count) {
960                 Goal_text.add();
961                 Goal_text.add();
962                 Goal_text.add(XSTR( "*Secondary Objectives", 400));
963                 Goal_text.add();
964                 Secondary_goal_list.set();
965         }
966
967         if (Bonus_goal_list.count) {
968                 Goal_text.add();
969                 Goal_text.add();
970                 Goal_text.add(XSTR( "*Bonus Objectives", 401));
971                 Goal_text.add();
972                 Bonus_goal_list.set();
973         }
974
975
976         Goal_complete_bitmap = bm_load("ObjComp");
977         Goal_incomplete_bitmap = bm_load("ObjIncomp");
978         Goal_failed_bitmap = bm_load("ObjFail");
979
980         // if (Goal_incomplete_bitmap < 0) Int3();
981
982         return Goal_text.m_num_lines;
983 }
984
985 // cleanup called when exiting the show goals screen
986 void ML_objectives_close()
987 {
988         if (Goal_complete_bitmap >= 0) {
989                 bm_unload(Goal_complete_bitmap);
990         }
991         
992         if (Goal_incomplete_bitmap >= 0) {
993                 bm_unload(Goal_incomplete_bitmap);
994         }
995         
996         if (Goal_failed_bitmap >= 0) {
997                 bm_unload(Goal_failed_bitmap);
998         }
999 }
1000
1001 void ML_objectives_do_frame(int scroll_offset)
1002 {
1003         int y, z;
1004         int font_height = gr_get_font_height();
1005
1006         y = 0;
1007         z = scroll_offset;
1008         while (y + font_height <= Goal_screen_text_h) {
1009                 Goal_text.display(z, y);
1010                 y += font_height;
1011                 z++;
1012         }
1013
1014         Primary_goal_list.icons_display(scroll_offset);
1015         Secondary_goal_list.icons_display(scroll_offset);
1016         Bonus_goal_list.icons_display(scroll_offset);
1017 }
1018
1019 void ML_render_objectives_key()
1020 {
1021         // display icon key at the bottom
1022         gr_set_bitmap(Goal_complete_bitmap);
1023         if (Lcl_gr) {
1024                 gr_bitmap(Objective_key_icon_coords_gr[gr_screen.res][0][0], Objective_key_icon_coords_gr[gr_screen.res][0][1]);
1025                 gr_set_bitmap(Goal_incomplete_bitmap);
1026                 gr_bitmap(Objective_key_icon_coords_gr[gr_screen.res][1][0], Objective_key_icon_coords_gr[gr_screen.res][1][1]);
1027                 gr_set_bitmap(Goal_failed_bitmap);
1028                 gr_bitmap(Objective_key_icon_coords_gr[gr_screen.res][2][0], Objective_key_icon_coords_gr[gr_screen.res][2][1]);
1029         
1030                 gr_string(Objective_key_text_coords_gr[gr_screen.res][0][0], Objective_key_text_coords_gr[gr_screen.res][0][1] , XSTR("Complete",       1437));
1031                 gr_string(Objective_key_text_coords_gr[gr_screen.res][1][0], Objective_key_text_coords_gr[gr_screen.res][1][1] , XSTR("Incomplete", 1438));
1032                 gr_string(Objective_key_text_coords_gr[gr_screen.res][2][0], Objective_key_text_coords_gr[gr_screen.res][2][1] , XSTR("Failed",         1439));
1033         } else {
1034                 gr_bitmap(Objective_key_icon_coords[gr_screen.res][0][0], Objective_key_icon_coords[gr_screen.res][0][1]);
1035                 gr_set_bitmap(Goal_incomplete_bitmap);
1036                 gr_bitmap(Objective_key_icon_coords[gr_screen.res][1][0], Objective_key_icon_coords[gr_screen.res][1][1]);
1037                 gr_set_bitmap(Goal_failed_bitmap);
1038                 gr_bitmap(Objective_key_icon_coords[gr_screen.res][2][0], Objective_key_icon_coords[gr_screen.res][2][1]);
1039         
1040                 gr_string(Objective_key_text_coords[gr_screen.res][0][0], Objective_key_text_coords[gr_screen.res][0][1] , XSTR("Complete",     1437));
1041                 gr_string(Objective_key_text_coords[gr_screen.res][1][0], Objective_key_text_coords[gr_screen.res][1][1] , XSTR("Incomplete", 1438));
1042                 gr_string(Objective_key_text_coords[gr_screen.res][2][0], Objective_key_text_coords[gr_screen.res][2][1] , XSTR("Failed",               1439));
1043         }
1044 }
1045
1046         
1047 // temporary hook for temporarily displaying objective completion/failure
1048 // extern void message_training_add_simple( char *text );
1049
1050 void mission_goal_status_change( int goal_num, int new_status)
1051 {
1052         int type;
1053
1054         Assert(goal_num < Num_goals);
1055         Assert((new_status == GOAL_FAILED) || (new_status == GOAL_COMPLETE));
1056
1057         // if in a multiplayer game, send a status change to clients
1058         if ( MULTIPLAYER_MASTER ){
1059                 send_mission_goal_info_packet( goal_num, new_status, -1 );
1060         }
1061
1062         type = Mission_goals[goal_num].type & GOAL_TYPE_MASK;
1063         Mission_goals[goal_num].satisfied = new_status;
1064         if ( new_status == GOAL_FAILED ) {
1065                 // don't display bonus goal failure
1066                 if ( type != BONUS_GOAL ) {
1067
1068                         // only do HUD and music is goals are my teams goals.
1069                         if ( (Game_mode & GM_NORMAL) || ((Net_player != NULL) && (Net_player->p_info.team == Mission_goals[goal_num].team)) ) {
1070                                 hud_add_objective_messsage(type, new_status);
1071                                 if ( !Mission_goals[goal_num].flags & MGF_NO_MUSIC ) {  // maybe play event music
1072                                         event_music_primary_goal_failed();
1073                                 }
1074                                 //HUD_sourced_printf(HUD_SOURCE_FAILED, "%s goal failed at time %6.1f!", Goal_type_text(type), f2fl(Missiontime) );
1075                         }
1076                 }
1077                 mission_log_add_entry( LOG_GOAL_FAILED, Mission_goals[goal_num].name, NULL, goal_num );
1078         } else if ( new_status == GOAL_COMPLETE ) {
1079                 if ( (Game_mode & GM_NORMAL) || ((Net_player != NULL) && (Net_player->p_info.team == Mission_goals[goal_num].team))) {
1080                         hud_add_objective_messsage(type, new_status);
1081                         // cue for Event Music
1082                         if ( !(Mission_goals[goal_num].flags & MGF_NO_MUSIC) ) {
1083                                 event_music_primary_goals_met();
1084                         }                       
1085                         mission_log_add_entry( LOG_GOAL_SATISFIED, Mission_goals[goal_num].name, NULL, goal_num );
1086                 }       
1087                 
1088                 if(Game_mode & GM_MULTIPLAYER){
1089                         // squad war
1090                         multi_team_maybe_add_score((int)(Mission_goals[goal_num].score * scoring_get_scale_factor()), Mission_goals[goal_num].team);    
1091                 } else {
1092                         // deal with the score
1093                         Player->stats.m_score += (int)(Mission_goals[goal_num].score * scoring_get_scale_factor());                     
1094                 }
1095         }
1096 }
1097
1098 // return value:
1099 //   EVENT_UNBORN    = event has yet to be available (not yet evaluatable)
1100 //   EVENT_CURRENT   = current (evaluatable), but not yet true
1101 //   EVENT_SATISFIED = event has occured (true)
1102 //   EVENT_FAILED    = event failed, can't possibly become true anymore
1103 int mission_get_event_status(int event)
1104 {
1105         // check for directive special events first.  We will always return from this part of the if statement
1106         if ( Mission_events[event].flags & MEF_DIRECTIVE_SPECIAL ) {
1107
1108                 // if this event is temporarily true, return as such
1109                 if ( Mission_events[event].flags & MEF_DIRECTIVE_TEMP_TRUE ){
1110                         return EVENT_SATISFIED;
1111                 }
1112
1113                 // if the timestamp has elapsed, we can "mark" this directive as true although it's really not.
1114                 if ( timestamp_elapsed(Mission_directive_special_timestamp) ) {
1115                         Mission_events[event].satisfied_time = Missiontime;
1116                         Mission_events[event].flags |= MEF_DIRECTIVE_TEMP_TRUE;
1117                 }
1118
1119                 return EVENT_CURRENT;
1120         } else if (Mission_events[event].flags & MEF_CURRENT) {
1121                 if (!Mission_events[event].born_on_date){
1122                         Mission_events[event].born_on_date = timestamp();
1123                 }
1124
1125                 if (Mission_events[event].result) {
1126                         return EVENT_SATISFIED;
1127                 }
1128
1129                 if (Mission_events[event].formula < 0) {
1130                         return EVENT_FAILED;
1131                 }
1132
1133                 return EVENT_CURRENT;
1134         }
1135
1136         return EVENT_UNBORN;
1137 }
1138
1139 void mission_event_set_directive_special(int event)
1140 {
1141         // bogus
1142         if((event < 0) || (event >= Num_mission_events)){
1143                 return;
1144         }
1145
1146         Mission_events[event].flags |= MEF_DIRECTIVE_SPECIAL;
1147
1148         // start from a known state
1149         Mission_events[event].flags &= ~MEF_DIRECTIVE_TEMP_TRUE;
1150         Mission_directive_special_timestamp = timestamp(DIRECTIVE_SPECIAL_DELAY);
1151 }
1152
1153 void mission_event_unset_directive_special(int event)
1154 {
1155         // bogus
1156         if((event < 0) || (event >= Num_mission_events)){
1157                 return;
1158         }
1159
1160         Mission_events[event].flags &= ~(MEF_DIRECTIVE_SPECIAL);
1161
1162         // this event may be marked temporarily true -- if so, then unmark this value!!!
1163         if ( Mission_events[event].flags & MEF_DIRECTIVE_TEMP_TRUE ){
1164                 Mission_events[event].flags &= ~MEF_DIRECTIVE_TEMP_TRUE;
1165         }
1166         Mission_events[event].satisfied_time = 0;
1167         Mission_directive_special_timestamp = timestamp(-1);
1168 }
1169
1170 // function which evaluates and processes the given event
1171 void mission_process_event( int event )
1172 {
1173         int store_flags = Mission_events[event].flags;
1174         int store_formula = Mission_events[event].formula;
1175         int store_result = Mission_events[event].result;
1176         int store_count = Mission_events[event].count;
1177
1178         int result, sindex;
1179
1180         Directive_count = 0;
1181         Event_index = event;
1182         sindex = Mission_events[event].formula;
1183         result = Mission_events[event].result;
1184
1185         // if chained, insure that previous event is true and next event is false
1186         if (Mission_events[event].chain_delay >= 0) {  // this indicates it's chained
1187                 if (event > 0){
1188                         if (!Mission_events[event - 1].result || ((fix) Mission_events[event - 1].timestamp + i2f(Mission_events[event].chain_delay) > Missiontime)){
1189                                 sindex = -1;  // bypass evaluation
1190                         }
1191                 }
1192
1193                 if ((event < Num_mission_events - 1) && Mission_events[event + 1].result && (Mission_events[event + 1].chain_delay >= 0)){
1194                         sindex = -1;  // bypass evaluation
1195                 }
1196         }
1197
1198         if (sindex >= 0) {
1199                 Sexp_useful_number = 1;
1200                 result = eval_sexp(sindex);
1201
1202                 // if the directive count is a special value, deal with that first.  Mark the event as a special
1203                 // event, and unmark it when the directive is true again.
1204                 if ( (Directive_count == DIRECTIVE_WING_ZERO) && !(Mission_events[event].flags & MEF_DIRECTIVE_SPECIAL) ) {                     
1205                         // make it special - which basically just means that its true until the next wave arrives
1206                         mission_event_set_directive_special(event);
1207
1208                         Directive_count = 0;
1209                 } else if ( (Mission_events[event].flags & MEF_DIRECTIVE_SPECIAL) && Directive_count > 1 ) {                    
1210                         // make it non special
1211                         mission_event_unset_directive_special(event);
1212                 }
1213
1214                 if (Mission_events[event].count || (Directive_count > 1)){
1215                         Mission_events[event].count = Directive_count;
1216                 }
1217
1218                 if (Sexp_useful_number){
1219                         Mission_events[event].flags |= MEF_CURRENT;
1220                 }
1221         }
1222
1223         Event_index = 0;
1224         Mission_events[event].result = result;
1225
1226         // if the sexpression is known false, then no need to evaluate anymore
1227         if ((sindex >= 0) && (Sexp_nodes[sindex].value == SEXP_KNOWN_FALSE)) {
1228                 Mission_events[event].timestamp = (int) Missiontime;
1229                 Mission_events[event].satisfied_time = Missiontime;
1230                 Mission_events[event].repeat_count = -1;
1231                 Mission_events[event].formula = -1;
1232                 return;
1233         }
1234
1235         if (result && !Mission_events[event].satisfied_time) {
1236                 Mission_events[event].satisfied_time = Missiontime;
1237                 if ( Mission_events[event].objective_text ) {
1238                         Mission_directive_sound_timestamp = timestamp(DIRECTIVE_SOUND_DELAY);
1239                 }
1240         }
1241
1242         // decrement the repeat count.  When at 0, don't eval this function anymore
1243         if ( result || timestamp_valid(Mission_events[event].timestamp) ) {
1244                 Mission_events[event].repeat_count--;
1245                 if ( Mission_events[event].repeat_count <= 0 ) {
1246                         Mission_events[event].timestamp = (int)Missiontime;
1247                         Mission_events[event].formula = -1;
1248
1249                         if(Game_mode & GM_MULTIPLAYER){
1250                                 // squad war
1251                                 multi_team_maybe_add_score((int)(Mission_events[event].score * scoring_get_scale_factor()), Mission_events[event].team);
1252                         } else {
1253                                 // deal with the player's score
1254                                 Player->stats.m_score += (int)(Mission_events[event].score * scoring_get_scale_factor());                       
1255                         }
1256                 } else {
1257                         // set the timestamp to time out 'interval' seconds in the future.  We must also reset the
1258                         // value at the sexpresion node to unknown so that it will get reevaled
1259                         Mission_events[event].timestamp = timestamp( Mission_events[event].interval * 1000 );
1260 //                      Sexp_nodes[Mission_events[event].formula].value = SEXP_UNKNOWN;
1261                 }
1262         }
1263
1264         // see if anything has changed  
1265         if(MULTIPLAYER_MASTER && ((store_flags != Mission_events[event].flags) || (store_formula != Mission_events[event].formula) || (store_result != Mission_events[event].result) || (store_count != Mission_events[event].count)) ){
1266                 send_event_update_packet(event);
1267         }       
1268 }
1269
1270 // Maybe play a directive success sound... need to poll since the sound is delayed from when
1271 // the directive is actually satisfied.
1272 void mission_maybe_play_directive_success_sound()
1273 {
1274         if ( timestamp_elapsed(Mission_directive_sound_timestamp) ) {
1275                 Mission_directive_sound_timestamp=0;
1276                 snd_play( &Snds[SND_DIRECTIVE_COMPLETE] );
1277         }
1278 }
1279
1280 void mission_eval_goals()
1281 {
1282         int i, result, goal_changed = 0;
1283
1284         // before checking whether or not we should evaluate goals, we should run through the events and
1285         // process any whose timestamp is valid and has expired.  This would catch repeating events only
1286         for (i=0; i<Num_mission_events; i++) {
1287                 if (Mission_events[i].formula != -1) {
1288                         if ( !timestamp_valid(Mission_events[i].timestamp) || !timestamp_elapsed(Mission_events[i].timestamp) ){
1289                                 continue;
1290                         }
1291
1292                         // if we get here, then the timestamp on the event has popped -- we should reevaluate
1293                         mission_process_event(i);
1294                 }
1295         }
1296         
1297         if ( !timestamp_elapsed(Mission_goal_timestamp) ){
1298                 return;
1299         }
1300
1301         // first evaluate the players goals
1302         for (i=0; i<Num_goals; i++) {
1303                 // don't evaluate invalid goals
1304                 if (Mission_goals[i].type & INVALID_GOAL){
1305                         continue;
1306                 }
1307
1308                 if (Mission_goals[i].satisfied == GOAL_INCOMPLETE) {
1309                         result = eval_sexp(Mission_goals[i].formula);
1310                         if ( Sexp_nodes[Mission_goals[i].formula].value == SEXP_KNOWN_FALSE ) {
1311                                 goal_changed = 1;
1312                                 mission_goal_status_change( i, GOAL_FAILED );
1313
1314                         } else if (result) {
1315                                 goal_changed = 1;
1316                                 mission_goal_status_change(i, GOAL_COMPLETE );
1317                         } // end if result
1318                         
1319                         // tell the player how to end the mission
1320                         //if ( goal_changed && mission_evaluate_primary_goals() != PRIMARY_GOALS_INCOMPLETE ) {
1321                         //      HUD_sourced_printf(HUD_SOURCE_IMPORTANT, "Press %s to end mission and return to base", textify_scancode(Control_config[END_MISSION].key_id) );
1322                         //}
1323                 }       // end if goals[i].satsified != GOAL_COMPLETE
1324         } // end for
1325
1326         // now evaluate any mission events
1327         for (i=0; i<Num_mission_events; i++) {
1328                 if ( Mission_events[i].formula != -1 ) {
1329                         // only evaluate this event if the timestamp is not valid.  We do this since
1330                         // we will evaluate repeatable events at the top of the file so we can get
1331                         // the exact interval that the designer asked for.
1332                         if ( !timestamp_valid( Mission_events[i].timestamp) ){
1333                                 mission_process_event( i );
1334                         }
1335                 }
1336         }
1337
1338         if (The_mission.game_type & MISSION_TYPE_TRAINING){
1339                 Mission_goal_timestamp = timestamp(GOAL_TIMESTAMP_TRAINING);
1340         } else {
1341                 Mission_goal_timestamp = timestamp(GOAL_TIMESTAMP);
1342         }
1343
1344         if ( !hud_disabled() && hud_gauge_active(HUD_DIRECTIVES_VIEW) ) {
1345                 mission_maybe_play_directive_success_sound();
1346         }
1347
1348    // update goal status if playing on a multiplayer standalone server
1349         if (Game_mode & GM_STANDALONE_SERVER){
1350                 std_multi_update_goals();
1351         }
1352 }
1353
1354 //      evaluate_primary_goals() will determine if the primary goals for a mission are complete
1355 //
1356 //      returns 1 - all primary goals are all complete or imcomplete (or there are no primary goals at all)
1357 // returns 0 - not all primary goals are complete
1358 int mission_evaluate_primary_goals()
1359 {
1360         int i, primary_goals_complete = PRIMARY_GOALS_COMPLETE;
1361
1362         for (i=0; i<Num_goals; i++) {
1363
1364                 if ( Mission_goals[i].type & INVALID_GOAL ) {
1365                         continue;
1366                 }
1367
1368                 if ( (Mission_goals[i].type & GOAL_TYPE_MASK) == PRIMARY_GOAL ) {
1369                         if ( Mission_goals[i].satisfied == GOAL_INCOMPLETE ) {
1370                                 return PRIMARY_GOALS_INCOMPLETE;
1371                         } else if ( Mission_goals[i].satisfied == GOAL_FAILED ) {
1372                                 primary_goals_complete = PRIMARY_GOALS_FAILED;
1373                         }
1374                 }
1375         }       // end for
1376
1377         return primary_goals_complete;
1378 }
1379
1380 // return 1 if all primary/secondary goals are complete... otherwise return 0
1381 int mission_goals_met()
1382 {
1383         int i, all_goals_met = 1;
1384
1385         for (i=0; i<Num_goals; i++) {
1386
1387                 if ( Mission_goals[i].type & INVALID_GOAL ) {
1388                         continue;
1389                 }
1390
1391                 if ( ((Mission_goals[i].type & GOAL_TYPE_MASK) == PRIMARY_GOAL) || ((Mission_goals[i].type & GOAL_TYPE_MASK) == SECONDARY_GOAL) ) {
1392                         if ( Mission_goals[i].satisfied == GOAL_INCOMPLETE ) {
1393                                 all_goals_met = 0;
1394                                 break;
1395                         } else if ( Mission_goals[i].satisfied == GOAL_FAILED ) {
1396                                 all_goals_met = 0;
1397                                 break;
1398                         }
1399                 }
1400         }       // end for
1401
1402         return all_goals_met;
1403 }
1404
1405 // function used to actually change the status (valid/invalid) of a goal.  Called externally
1406 // with multiplayer code
1407 void mission_goal_validation_change( int goal_num, int valid )
1408 {
1409         // only incomplete goals can have their status changed
1410         if ( Mission_goals[goal_num].satisfied != GOAL_INCOMPLETE ){
1411                 return;
1412         }
1413
1414         // if in multiplayer, then send a packet
1415         if ( MULTIPLAYER_MASTER ){
1416                 send_mission_goal_info_packet( goal_num, -1, valid );
1417         }
1418
1419         // change the valid status
1420         if ( valid ){
1421                 Mission_goals[goal_num].type &= ~INVALID_GOAL;
1422         } else {
1423                 Mission_goals[goal_num].type |= INVALID_GOAL;
1424         }
1425 }
1426
1427         // the following function marks a goal invalid.  It can only mark the goal invalid if the goal
1428 // is not complete.  The name passed into this funciton should match the name field in the goal
1429 // structure
1430 void mission_goal_mark_invalid( char *name )
1431 {
1432         int i;
1433
1434         for (i=0; i<Num_goals; i++) {
1435                 if ( !stricmp(Mission_goals[i].name, name) ) {
1436                         mission_goal_validation_change( i, 0 );
1437                         return;
1438                 }
1439         }
1440 }
1441
1442 // the next function marks a goal as valid.  A goal may always be marked valid.
1443 void mission_goal_mark_valid( char *name )
1444 {
1445         int i;
1446
1447         for (i=0; i<Num_goals; i++) {
1448                 if ( !stricmp(Mission_goals[i].name, name) ) {
1449                         mission_goal_validation_change( i, 1 );
1450                         return;
1451                 }
1452         }
1453 }
1454
1455 // function to fail all mission goals.  Don't fail events here since this funciton is currently
1456 // called in mission when something bad happens to the player (like he makes too many shots on friendlies).
1457 // Events can still happen.  Things will just be really bad for the player.
1458 void mission_goal_fail_all()
1459 {
1460         int i;
1461
1462         for (i=0; i<Num_goals; i++) {
1463                 Mission_goals[i].satisfied = GOAL_FAILED;
1464                 mission_log_add_entry( LOG_GOAL_FAILED, Mission_goals[i].name, NULL, i );
1465         }
1466 }
1467
1468 // function to mark all incomplete goals as failed.  Happens at the end of a mission
1469 // mark the events which are not currently satisfied as failed as well.
1470 void mission_goal_fail_incomplete()
1471 {
1472         int i;
1473
1474         for (i = 0; i < Num_goals; i++ ) {
1475                 if ( Mission_goals[i].satisfied == GOAL_INCOMPLETE ) {
1476                         Mission_goals[i].satisfied = GOAL_FAILED;
1477                         mission_log_add_entry( LOG_GOAL_FAILED, Mission_goals[i].name, NULL, i );
1478                 }
1479         }
1480
1481         // now for the events.  Must set the formula to -1 and the result to 0 to be a failed
1482         // event.
1483         for ( i = 0; i < Num_mission_events; i++ ) {
1484                 if ( Mission_events[i].formula != -1 ) {
1485                         Mission_events[i].formula = -1;
1486                         Mission_events[i].result = 0;
1487                 }
1488         }
1489 }
1490
1491 // small function used to mark all objectives as true.  Used as a debug function and as a way
1492 // to skip past training misisons
1493 void mission_goal_mark_objectives_complete()
1494 {
1495         int i;
1496
1497         for (i = 0; i < Num_goals; i++ ) {
1498                 Mission_goals[i].satisfied = GOAL_COMPLETE;
1499         }
1500 }
1501
1502 // small function used to mark all events as completed.  Used in the skipping of missions.
1503 void mission_goal_mark_events_complete()
1504 {
1505         int i;
1506
1507         for (i = 0; i < Num_mission_events; i++ ) {
1508                 Mission_events[i].result = 1;
1509                 Mission_events[i].formula = -1;
1510         }
1511 }
1512
1513 // some debug console functions to help list and change the status of mission goals
1514 DCF(show_mission_goals,"List and change the status of mission goals")
1515 {
1516         int i, type;
1517
1518         if (Dc_command)
1519                 Dc_status = 1;
1520
1521         if (Dc_help) {
1522                 dc_printf("Usage: show_mission_goals\n\nList all mission goals and their current status.\n");
1523                 Dc_status = 0;
1524         }
1525
1526         if (Dc_status) {
1527                 for (i=0; i<Num_goals; i++) {
1528                         type = Mission_goals[i].type & GOAL_TYPE_MASK;
1529                         dc_printf("%2d. %32s(%10s) -- ", i, Mission_goals[i].name, Goal_type_text(type));
1530                         if ( Mission_goals[i].satisfied == GOAL_COMPLETE )
1531                                 dc_printf("satisfied.\n");
1532                         else if ( Mission_goals[i].satisfied == GOAL_INCOMPLETE )
1533                                 dc_printf("not satisfied\n");
1534                         else if ( Mission_goals[i].satisfied == GOAL_FAILED )
1535                                 dc_printf("failed\n");
1536                         else
1537                                 dc_printf("\t[unknown goal status].\n");
1538                 }
1539         }
1540 }
1541
1542 //XSTR:OFF
1543 DCF(change_mission_goal, "Change the mission goal")
1544 {
1545         int num;
1546
1547         if ( Dc_command ) {
1548                 dc_get_arg(ARG_INT);
1549                 if ( Dc_arg_int >= Num_goals ) {
1550                         dc_printf ("First parameter must be a valid goal number.\n");
1551                         return;
1552                 }
1553
1554                 num = Dc_arg_int;
1555                 dc_get_arg(ARG_TRUE|ARG_FALSE|ARG_STRING);
1556                 if ( Dc_arg_type & ARG_TRUE )
1557                         Mission_goals[num].satisfied = GOAL_COMPLETE;
1558                 else if ( Dc_arg_type & ARG_FALSE )
1559                         Mission_goals[num].satisfied = GOAL_FAILED;
1560                 else if ( Dc_arg_type & ARG_NONE )
1561                         Mission_goals[num].satisfied = GOAL_INCOMPLETE;
1562                 else if ( Dc_arg_type & ARG_STRING) {
1563                         if ( !stricmp(Dc_arg, "satisfied") )
1564                                 Mission_goals[num].satisfied = GOAL_COMPLETE;
1565                         else if ( !stricmp( Dc_arg, "failed") )
1566                                 Mission_goals[num].satisfied = GOAL_FAILED;
1567                         else if ( !stricmp( Dc_arg, "unknown") )
1568                                 Mission_goals[num].satisfied = GOAL_INCOMPLETE;
1569                         else
1570                                 dc_printf("Unknown status %s.  Use 'satisfied', 'failed', or 'unknown'\n", Dc_arg);
1571                 }
1572         }
1573
1574         if ( Dc_help ) {
1575                 dc_printf("Usage: change_mission_goal <goal_num> <status>\n");
1576                 dc_printf("<goal_num> --  Integer number of goal to change.  See show_mission_goals\n");
1577                 dc_printf("<status>   --  [bool] where a true value makes the goal satisfied,\n");
1578                 dc_printf("               a false value makes the goal failed.\n");
1579                 dc_printf("The <status> field may also be one of 'satisfied', 'failed', or 'unknown'\n");
1580                 dc_printf("\nExamples:\n\n'change_mission_goal 1 true' makes goal 1 successful.\n");
1581                 dc_printf("'change_mission_goal 2' marks goal 2 not complete\n");
1582                 dc_printf("'change_mission_goal 0 satisfied' marks goal 0 as satisfied\n");
1583                 Dc_status = 0;
1584         }
1585 }
1586 //XSTR:ON
1587
1588 // debug functions to mark all primary/secondary/bonus goals as true
1589 #ifndef DEBUG
1590
1591 void mission_goal_mark_all_true(int type)
1592 {
1593         int i;
1594
1595         for (i = 0; i < Num_goals; i++ ) {
1596                 if ( (Mission_goals[i].type & GOAL_TYPE_MASK) == type )
1597                         Mission_goals[i].satisfied = GOAL_COMPLETE;
1598         }
1599
1600         HUD_sourced_printf(HUD_SOURCE_HIDDEN, NOX("All %s goals marked true"), Goal_type_text(type) );
1601 }
1602
1603 #endif
1604
1605 void goal_screen_button_pressed(int num)
1606 {
1607         switch (num) {
1608         case GOAL_SCREEN_BUTTON_SCROLL_UP:
1609                 goal_screen_scroll_up();
1610                 break;
1611
1612         case GOAL_SCREEN_BUTTON_SCROLL_DOWN:
1613                 goal_screen_scroll_down();
1614                 break;
1615
1616         case GOAL_SCREEN_BUTTON_RETURN:
1617                 mission_goal_exit();
1618                 break;
1619         }
1620 }
1621
1622 void goal_screen_scroll_up()
1623 {
1624         if (Scroll_offset) {
1625                 Scroll_offset--;
1626                 gamesnd_play_iface(SND_SCROLL);
1627         } else {
1628                 gamesnd_play_iface(SND_GENERAL_FAIL);
1629         }
1630 }
1631
1632 void goal_screen_scroll_down()
1633 {
1634         int max_lines;
1635
1636         max_lines = Goal_screen_text_h / gr_get_font_height();
1637         if (Scroll_offset + max_lines < Goal_text.m_num_lines) {
1638                 Scroll_offset++;
1639                 gamesnd_play_iface(SND_SCROLL);
1640         } else {
1641                 gamesnd_play_iface(SND_GENERAL_FAIL);
1642         }
1643 }
1644
1645 // Return the number of resolved goals in num_resolved, return the total
1646 // number of valid goals in total
1647 void mission_goal_fetch_num_resolved(int desired_type, int *num_resolved, int *total, int team)
1648 {
1649         int i,type;
1650
1651         *num_resolved=0;
1652         *total=0;
1653
1654         for (i=0; i<Num_goals; i++) {
1655                 // if we're checking for team
1656                 if((team >= 0) && (Mission_goals[i].team != team)){
1657                         continue;
1658                 }
1659
1660                 if (Mission_goals[i].type & INVALID_GOAL) {
1661                         continue;
1662                 }
1663
1664                 type = Mission_goals[i].type & GOAL_TYPE_MASK;
1665                 if ( type != desired_type ) {
1666                         continue;
1667                 }
1668
1669                 *total = *total + 1;
1670
1671                 if (Mission_goals[i].satisfied != GOAL_INCOMPLETE) {
1672                         *num_resolved = *num_resolved + 1;
1673                 }
1674         }
1675 }
1676
1677 // Return whether there are any incomplete goals of the specified type
1678 int mission_goals_incomplete(int desired_type, int team)
1679 {
1680         int i, type;
1681
1682         for (i=0; i<Num_goals; i++) {           
1683                 // if we're checking for team
1684                 if((team >= 0) && (Mission_goals[i].team != team)){
1685                         continue;
1686                 }
1687
1688                 if (Mission_goals[i].type & INVALID_GOAL) {
1689                         continue;
1690                 }
1691
1692                 type = Mission_goals[i].type & GOAL_TYPE_MASK;
1693                 if ( type != desired_type ) {
1694                         continue;
1695                 }
1696
1697                 if (Mission_goals[i].satisfied == GOAL_INCOMPLETE) {
1698                         return 1;
1699                 }
1700         }
1701
1702         return 0;
1703 }
1704
1705 void mission_goal_exit()
1706 {
1707         snd_play( &Snds_iface[SND_USER_SELECT] );
1708         gameseq_post_event(GS_EVENT_PREVIOUS_STATE);
1709
1710