]> icculus.org git repositories - btb/d2x.git/blob - main/editor/ehostage.c
include conf.h in new editor files
[btb/d2x.git] / main / editor / ehostage.c
1 /* $Id: ehostage.c,v 1.3 2004-12-19 15:21:11 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Routines for placing hostages, etc...
18  *
19  */
20
21 #ifdef RCS
22 static char rcsid[] = "$Id: ehostage.c,v 1.3 2004-12-19 15:21:11 btb Exp $";
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #include "conf.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifndef __LINUX__
32 #include <conio.h>
33 #include <dos.h>
34 #include <direct.h>
35 #endif 
36 #include <string.h>
37 #include <math.h>
38
39 #include "screens.h"
40 #include "inferno.h"
41 #include "segment.h"
42 #include "editor.h"
43
44 #include "timer.h"
45 #include "objpage.h"
46 #include "fix.h"
47 #include "mono.h"
48 #include "error.h"
49 #include "kdefs.h"
50 #include        "object.h"
51 #include "polyobj.h"
52 #include "game.h"
53 #include "powerup.h"
54 #include "ai.h"
55 #include "hostage.h"
56 #include "eobject.h"
57 #include "medwall.h"
58 #include "eswitch.h"
59 #include "medrobot.h"
60 #include "key.h"
61 #include "bm.h"
62 #include "sounds.h"
63 #include "centers.h"
64 #include "piggy.h"
65
66 //-------------------------------------------------------------------------
67 // Variables for this module...
68 //-------------------------------------------------------------------------
69 static UI_WINDOW                                *MainWindow = NULL;
70 static UI_GADGET_USERBOX        *HostageViewBox;
71 static UI_GADGET_INPUTBOX       *HostageText;
72 static UI_GADGET_BUTTON         *QuitButton;
73 static int                                              CurrentHostageIndex=-1;
74 static int                                              LastHostageIndex=-1;
75
76 static fix                      Vclip_animation_time=0;                 // How long the rescue sequence has been playing
77 static fix                      Vclip_playback_speed=0;                         // Calculated internally.  Frames/second of vclip.
78 static vclip            *Vclip_ptr = NULL;                              // Used for the vclip on monitor
79
80 void vclip_play( vclip * vc, fix frame_time )   
81 {
82         int bitmapnum;
83
84         if ( vc == NULL )
85                 return;
86
87         if ( vc != Vclip_ptr )  {
88                 // Start new vclip
89                 Vclip_ptr = vc;
90                 Vclip_animation_time = 1;
91
92                 // Calculate the frame/second of the playback
93                 Vclip_playback_speed = fixdiv(i2f(Vclip_ptr->num_frames),Vclip_ptr->play_time);
94         }
95
96         if ( Vclip_animation_time <= 0 )
97                 return;
98
99         // Find next bitmap in the vclip
100         bitmapnum = f2i(Vclip_animation_time);
101
102         // Check if vclip is done playing.
103         if (bitmapnum >= Vclip_ptr->num_frames)         {
104                 Vclip_animation_time    = 1;                                                                                    // Restart this vclip
105                 bitmapnum = 0;
106         }
107
108         PIGGY_PAGE_IN( Vclip_ptr->frames[bitmapnum] );
109         gr_bitmap(0,0,&GameBitmaps[Vclip_ptr->frames[bitmapnum].index] );
110         
111         Vclip_animation_time += fixmul(frame_time, Vclip_playback_speed );
112 }
113
114
115
116 static char HostageMessage[]  = "  ";
117
118 static fix Time;
119
120 int SelectPrevHostage() {
121         int start=0;
122
123         do      {
124                 CurrentHostageIndex--;
125                 if ( CurrentHostageIndex < 0 ) CurrentHostageIndex = MAX_HOSTAGES-1;
126                 start++;
127                 if ( start > MAX_HOSTAGES ) break;
128         } while ( !hostage_is_valid( CurrentHostageIndex ) );
129
130         if (hostage_is_valid( CurrentHostageIndex ) )   {
131                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
132         } else {
133                 CurrentHostageIndex =-1;
134         }
135         
136         return CurrentHostageIndex;
137 }
138
139
140 int SelectNextHostage() {
141         int start=0;
142
143         do      {
144                 CurrentHostageIndex++;
145                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
146                 start++;
147                 if ( start > MAX_HOSTAGES ) break;
148         } while ( !hostage_is_valid( CurrentHostageIndex ) );
149
150         if (hostage_is_valid( CurrentHostageIndex ) )   {
151                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
152         } else {
153                 CurrentHostageIndex =-1;
154         }
155         
156         return CurrentHostageIndex;
157 }
158
159
160 int SelectClosestHostage()      {
161         int start=0;
162
163         while ( !hostage_is_valid( CurrentHostageIndex ) )      {
164                 CurrentHostageIndex++;
165                 if ( CurrentHostageIndex >= MAX_HOSTAGES ) CurrentHostageIndex = 0;
166                 start++;
167                 if ( start > MAX_HOSTAGES ) break;
168         }
169
170         if (hostage_is_valid( CurrentHostageIndex ) )   {
171                 Cur_object_index = Hostages[CurrentHostageIndex].objnum;
172         } else {
173                 CurrentHostageIndex =-1;
174         }
175         
176         return CurrentHostageIndex;
177 }
178
179
180 int PlaceHostage()      {
181         int ctype,i;
182         vms_vector      cur_object_loc;
183
184         //update_due_to_new_segment();
185         compute_segment_center(&cur_object_loc, Cursegp);
186
187         ctype = -1;
188         for (i=0; i<Num_total_object_types; i++ )       {
189                 if (ObjType[i] == OL_HOSTAGE )  {
190                         ctype = i;      
191                         break;
192                 }
193         }
194
195         Assert( ctype != -1 );
196
197         if (place_object(Cursegp, &cur_object_loc, ctype )==0)  {
198                 Int3();         // Debug below
199                 i=place_object(Cursegp, &cur_object_loc, ctype );
200                 return 1;
201         }
202
203         if (hostage_object_is_valid( Cur_object_index ) )       {
204                 CurrentHostageIndex     = Objects[Cur_object_index].id;
205         } else {
206                 Int3();         // Get John! (Object should be valid)
207                 i=hostage_object_is_valid( Cur_object_index );  // For debugging only
208         }
209
210         return 0;
211 }
212
213 int CompressHostages()
214 {
215         hostage_compress_all();
216
217         return 0;
218 }
219
220 //@@int SelectPrevVclip()       {
221 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
222 //@@            return 0;
223 //@@
224 //@@    if ( Hostages[CurrentHostageIndex].type == 0 )
225 //@@            Hostages[CurrentHostageIndex].type = N_hostage_types-1;
226 //@@    else
227 //@@            Hostages[CurrentHostageIndex].type--;
228 //@@    
229 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
230 //@@            Hostages[CurrentHostageIndex].type = 0;
231 //@@    
232 //@@    return 1;
233 //@@}
234 //@@
235 //@@int SelectNextVclip()       {
236 //@@    if (!hostage_is_valid( CurrentHostageIndex ) )  
237 //@@            return 0;
238 //@@
239 //@@    Hostages[CurrentHostageIndex].type++;
240 //@@    if ( Hostages[CurrentHostageIndex].type >= N_hostage_types )
241 //@@            Hostages[CurrentHostageIndex].type = 0;
242 //@@
243 //@@    return 1;
244 //@@}
245
246 int SelectNextFace()
247 {
248         int start = Hostages[CurrentHostageIndex].vclip_num;
249         
250         if (!hostage_is_valid( CurrentHostageIndex ) )  
251                 return 0;
252
253         do {
254                 Hostages[CurrentHostageIndex].vclip_num++;
255                 if ( Hostages[CurrentHostageIndex].vclip_num >= MAX_HOSTAGES)
256                         Hostages[CurrentHostageIndex].vclip_num = 0;
257
258                 if (Hostages[CurrentHostageIndex].vclip_num == start)
259                         return 0;
260
261         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
262
263         return 1;
264 }
265
266 int SelectPrevFace()
267 {
268         int start = Hostages[CurrentHostageIndex].vclip_num;
269         
270         if (!hostage_is_valid( CurrentHostageIndex ) )  
271                 return 0;
272
273         do {
274                 Hostages[CurrentHostageIndex].vclip_num--;
275                 if ( Hostages[CurrentHostageIndex].vclip_num < 0)
276                         Hostages[CurrentHostageIndex].vclip_num = MAX_HOSTAGES-1;
277
278                 if (Hostages[CurrentHostageIndex].vclip_num == start)
279                         return 0;
280
281         } while (Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].num_frames == 0);
282
283         return 1;
284 }
285
286 int PlayHostageSound()  {
287         int sound_num;
288
289         if (!hostage_is_valid( CurrentHostageIndex ) )  
290                 return 0;
291
292         sound_num = Hostage_face_clip[Hostages[CurrentHostageIndex].vclip_num].sound_num;
293
294         if ( sound_num > -1 )   {
295                 digi_play_sample( sound_num, F1_0 );
296         }
297
298         return 1;       
299 }
300
301 //@@int find_next_hostage_sound()       {
302 //@@    int start=0,n;
303 //@@
304 //@@    n = Hostages[CurrentHostageIndex].sound_num;
305 //@@    do      {
306 //@@            n++;
307 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
308 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
309 //@@            start++;
310 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
311 //@@    } while ( Sounds[n] == NULL );
312 //@@
313 //@@    if ( Sounds[n] == NULL )
314 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
315 //@@    else    {
316 //@@            Hostages[CurrentHostageIndex].sound_num = n;
317 //@@            PlayHostageSound();
318 //@@    }
319 //@@    return 1;
320 //@@}
321 //@@
322 //@@int find_prev_hostage_sound()       {
323 //@@    int start=0,n;
324 //@@
325 //@@    n = Hostages[CurrentHostageIndex].sound_num;
326 //@@    do      {
327 //@@            n--;
328 //@@            if ( n < SOUND_HOSTAGE_VOICES ) n = SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS-1;
329 //@@            if ( n >= SOUND_HOSTAGE_VOICES+MAX_HOSTAGE_SOUNDS ) n = SOUND_HOSTAGE_VOICES;
330 //@@            start++;
331 //@@            if ( start > MAX_HOSTAGE_SOUNDS ) break;
332 //@@    } while ( Sounds[n] == NULL );
333 //@@
334 //@@    if ( Sounds[n] == NULL )
335 //@@            Hostages[CurrentHostageIndex].sound_num = -1;
336 //@@    else    {
337 //@@            Hostages[CurrentHostageIndex].sound_num = n;
338 //@@            PlayHostageSound();
339 //@@    }
340 //@@    return 1;
341 //@@}
342
343
344 //-------------------------------------------------------------------------
345 // Called from the editor... does one instance of the hostage dialog box
346 //-------------------------------------------------------------------------
347 int do_hostage_dialog()
348 {
349         int i;
350
351         // Only open 1 instance of this window...
352         if ( MainWindow != NULL ) return 0;
353         
354         // Close other windows
355         close_all_windows();
356
357         CurrentHostageIndex = 0;
358         SelectClosestHostage();
359
360         // Open a window with a quit button
361         MainWindow = ui_open_window( TMAPBOX_X+10, TMAPBOX_Y+20, 765-TMAPBOX_X, 545-TMAPBOX_Y, WIN_DIALOG );
362         QuitButton = ui_add_gadget_button( MainWindow, 20, 222, 48, 40, "Done", NULL );
363
364         ui_wprintf_at( MainWindow, 10, 32,"&Message:" );
365         HostageText = ui_add_gadget_inputbox( MainWindow, 10, 50, HOSTAGE_MESSAGE_LEN, HOSTAGE_MESSAGE_LEN, HostageMessage );
366
367         // The little box the hostage vclip will play in.
368         HostageViewBox = ui_add_gadget_userbox( MainWindow,10, 90+10, 64, 64 );
369
370         // A bunch of buttons...
371         i = 90;
372 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Type", SelectPrevVclip );
373 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Type >>", SelectNextVclip );i += 29;         
374 //@@    ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Sound",  find_prev_hostage_sound );
375 //@@    ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Sound >>", find_next_hostage_sound );i += 29;                
376
377         ui_add_gadget_button( MainWindow,155,i,70, 26, "<< Face", SelectPrevFace );
378         ui_add_gadget_button( MainWindow,155+70,i,70, 26, "Face >>", SelectNextFace );i += 29;          
379         ui_add_gadget_button( MainWindow,155,i,140, 26, "Play sound", PlayHostageSound );i += 29;               
380         ui_add_gadget_button( MainWindow,155,i,140, 26, "Next Hostage", SelectNextHostage );    i += 29;                
381         ui_add_gadget_button( MainWindow,155,i,140, 26, "Prev Hostage", SelectPrevHostage ); i += 29;           
382         ui_add_gadget_button( MainWindow,155,i,140, 26, "Compress All", CompressHostages ); i += 29;            
383         ui_add_gadget_button( MainWindow,155,i,140, 26, "Delete", ObjectDelete );       i += 29;                
384         ui_add_gadget_button( MainWindow,155,i,140, 26, "Create New", PlaceHostage );   i += 29;                
385         
386         Time = timer_get_fixed_seconds();
387
388         LastHostageIndex = -2;          // Set to some dummy value so everything works ok on the first frame.
389         
390 //      if ( CurrentHostageIndex == -1 )
391 //              SelectNextHostage();
392
393         return 1;
394
395 }
396
397 void hostage_close_window()
398 {
399         if ( MainWindow!=NULL ) {
400                 ui_close_window( MainWindow );
401                 MainWindow = NULL;
402         }
403 }
404
405 void do_hostage_window()
406 {
407         fix DeltaTime, Temp;
408
409         if ( MainWindow == NULL ) return;
410
411         SelectClosestHostage();
412
413         //------------------------------------------------------------
414         // Call the ui code..
415         //------------------------------------------------------------
416         ui_button_any_drawn = 0;
417         ui_window_do_gadgets(MainWindow);
418
419         //------------------------------------------------------------
420         // If we change objects, we need to reset the ui code for all
421         // of the radio buttons that control the ai mode.  Also makes
422         // the current AI mode button be flagged as pressed down.
423         //------------------------------------------------------------
424         if (LastHostageIndex != CurrentHostageIndex )   {
425
426                 if ( CurrentHostageIndex > -1 ) 
427                         strcpy( HostageText->text, Hostages[CurrentHostageIndex].text );
428                 else
429                         strcpy(HostageText->text, " " );
430
431                 HostageText->position = strlen(HostageText->text);
432                 HostageText->oldposition = HostageText->position;
433                 HostageText->status=1;
434                 HostageText->first_time = 1;
435
436         }
437
438         //------------------------------------------------------------
439         // If any of the radio buttons that control the mode are set, then
440         // update the cooresponding AI state.
441         //------------------------------------------------------------
442         if ( CurrentHostageIndex > -1 ) 
443                 strcpy( Hostages[CurrentHostageIndex].text, HostageText->text );
444
445         //------------------------------------------------------------
446         // A simple frame time counter for spinning the objects...
447         //------------------------------------------------------------
448         Temp = timer_get_fixed_seconds();
449         DeltaTime = Temp - Time;
450         Time = Temp;
451
452         //------------------------------------------------------------
453         // Redraw the object in the little 64x64 box
454         //------------------------------------------------------------
455         if (CurrentHostageIndex > -1 )  {
456                 int vclip_num;
457                 
458                 vclip_num = Hostages[CurrentHostageIndex].vclip_num;
459
460                 Assert(vclip_num != -1);
461
462                 gr_set_current_canvas( HostageViewBox->canvas );
463
464                 if ( vclip_num > -1 )   {
465                         vclip_play( &Hostage_face_clip[vclip_num], DeltaTime ); 
466                 } else {
467                         gr_clear_canvas( CGREY );
468                 }
469         } else {
470                 // no hostage, so just blank out
471                 gr_set_current_canvas( HostageViewBox->canvas );
472                 gr_clear_canvas( CGREY );
473         }
474
475         //------------------------------------------------------------
476         // If anything changes in the ui system, redraw all the text that
477         // identifies this robot.
478         //------------------------------------------------------------
479         if (ui_button_any_drawn || (LastHostageIndex != CurrentHostageIndex) )  {
480                 if ( CurrentHostageIndex > -1 ) {
481                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: %d   Object: %d", CurrentHostageIndex, Hostages[CurrentHostageIndex].objnum );
482                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type: %d   Sound: %d   ", Hostages[CurrentHostageIndex].type, Hostages[CurrentHostageIndex].sound_num );
483                         ui_wprintf_at( MainWindow, 10, 73, "Face: %d   ", Hostages[CurrentHostageIndex].vclip_num);
484                 }       else {
485                         ui_wprintf_at( MainWindow, 10, 15, "Hostage: none " );
486                         //@@ui_wprintf_at( MainWindow, 10, 73, "Type:    Sound:       " );
487                         ui_wprintf_at( MainWindow, 10, 73, "Face:         " );
488                 }
489                 Update_flags |= UF_WORLD_CHANGED;
490         }
491
492         if ( QuitButton->pressed || (last_keypress==KEY_ESC))   {
493                 hostage_close_window();
494                 return;
495         }               
496
497         LastHostageIndex = CurrentHostageIndex;
498 }
499
500
501