]> icculus.org git repositories - btb/d2x.git/blob - main/editor/macro.c
4a7b855e6f4a3a5777cff9a0231e82491c55f199
[btb/d2x.git] / main / editor / macro.c
1 /* $Id: macro.c,v 1.4 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 recording/playing/saving macros
18  *
19  */
20
21 #ifdef RCS
22 static char rcsid[] = "$Id: macro.c,v 1.4 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 <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <math.h>
33 #include <string.h>
34
35 #include "inferno.h"
36 #include "segment.h"
37 #include "editor.h"
38
39 #include "gr.h"
40 #include "ui.h"
41 #include "key.h"
42 #include "fix.h"
43 #include "mono.h"
44 #include "3d.h"
45 #include "mouse.h"
46 #include "bm.h"
47 #include "error.h"
48 #include "medlisp.h"
49 #include "cflib.h"
50
51 #include "kdefs.h"
52
53 #include "u_mem.h"
54
55 #define MAX_NUM_EVENTS 10000
56
57 UI_EVENT * RecordBuffer;
58
59 int MacroNumEvents = 0;
60 int MacroStatus = 0;
61
62 static char filename[128] = "*.MIN";
63
64 int MacroRecordAll()
65 {
66         if ( MacroStatus== UI_STATUS_NORMAL )
67         {
68                 if (RecordBuffer) free( RecordBuffer );
69                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
70                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_MOUSE | UI_RECORD_KEYS );
71                 MacroStatus = UI_STATUS_RECORDING;
72         }
73         return 1;
74 }
75
76 int MacroRecordKeys()
77 {
78         if ( MacroStatus== UI_STATUS_NORMAL )
79         {
80                 if (RecordBuffer) free( RecordBuffer );
81                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
82                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_KEYS );
83                 MacroStatus = UI_STATUS_RECORDING;
84         }
85         return 1;
86 }
87
88 int MacroPlayNormal()
89 {
90         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
91         {
92                 ui_set_playback_speed( 1 );
93                 ui_play_events_realtime(MacroNumEvents, RecordBuffer);
94                 MacroStatus = UI_STATUS_PLAYING;
95         }
96         return 1;
97 }
98
99 int MacroPlayFast()
100 {
101         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
102         {
103                 ui_mouse_hide();
104                 ui_play_events_fast(MacroNumEvents, RecordBuffer);
105                 MacroStatus = UI_STATUS_FASTPLAY;
106         }
107         return 1;
108 }
109
110 int MacroSave()
111 {
112
113         if (MacroNumEvents < 1 )
114         {
115                 MessageBox( -2, -2, 1, "No macro has been defined to save!", "Oops" );
116                 return 1;
117         }
118
119         if (ui_get_filename( filename, "*.MAC", "SAVE MACRO" ))   {
120                 RecordBuffer[0].type = 7;
121                 RecordBuffer[0].frame = 0;
122                 RecordBuffer[0].data = MacroNumEvents;
123                 WriteFile(  filename, RecordBuffer, sizeof(UI_EVENT)*MacroNumEvents );
124         }
125         return 1;
126 }
127
128 int MacroLoad()
129 {
130         int length;
131
132         if (ui_get_filename( filename, "*.MAC", "LOAD MACRO" ))   {
133                 if (RecordBuffer) free( RecordBuffer );
134                 RecordBuffer = (UI_EVENT *)ReadFile( filename, &length );
135                 MacroNumEvents = RecordBuffer[0].data;
136         }
137         return 1;
138 }
139
140 void macro_free_buffer()
141 {
142         if ( RecordBuffer ) 
143                 free(RecordBuffer);
144 }
145
146 int MacroMenu()
147 {
148         int x;
149         char * MenuItems[] = { "Play fast",
150                                            "Play normal",
151                                            "Record all",
152                                            "Record keys",
153                                            "Save macro",
154                                            "Load macro" };
155
156         x = MenuX( -1, -1, 6, MenuItems );
157
158         switch( x )
159         {
160         case 1:
161                 MacroPlayFast();
162                 break;
163         case 2:
164                 MacroPlayNormal();
165                 break;
166         case 3:
167                 MacroRecordAll();
168                 break;
169         case 4:
170                 MacroRecordKeys();
171                 break;
172         case 5:     // Save
173                 MacroSave();
174                 break;
175         case 6:     // Load
176                 MacroLoad();
177                 break;
178         }
179         return 1;
180 }
181