]> icculus.org git repositories - btb/d2x.git/blob - main/editor/macro.c
remove rcs tags
[btb/d2x.git] / main / editor / macro.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Routines for recording/playing/saving macros
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "conf.h"
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <math.h>
28 #include <string.h>
29
30 #include "inferno.h"
31 #include "segment.h"
32 #include "editor.h"
33
34 #include "gr.h"
35 #include "ui.h"
36 #include "key.h"
37 #include "fix.h"
38 #include "mono.h"
39 #include "3d.h"
40 #include "mouse.h"
41 #include "bm.h"
42 #include "error.h"
43 #include "medlisp.h"
44
45 #include "kdefs.h"
46
47 #include "u_mem.h"
48
49 #define MAX_NUM_EVENTS 10000
50
51 UI_EVENT * RecordBuffer;
52
53 int MacroNumEvents = 0;
54 int MacroStatus = 0;
55
56 static char filename[PATH_MAX] = "*.MIN";
57
58 int MacroRecordAll()
59 {
60         if ( MacroStatus== UI_STATUS_NORMAL )
61         {
62                 if (RecordBuffer) d_free( RecordBuffer );
63                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
64                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_MOUSE | UI_RECORD_KEYS );
65                 MacroStatus = UI_STATUS_RECORDING;
66         }
67         return 1;
68 }
69
70 int MacroRecordKeys()
71 {
72         if ( MacroStatus== UI_STATUS_NORMAL )
73         {
74                 if (RecordBuffer) d_free( RecordBuffer );
75                 MALLOC( RecordBuffer, UI_EVENT, MAX_NUM_EVENTS );
76                 ui_record_events( MAX_NUM_EVENTS, RecordBuffer, UI_RECORD_KEYS );
77                 MacroStatus = UI_STATUS_RECORDING;
78         }
79         return 1;
80 }
81
82 int MacroPlayNormal()
83 {
84         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
85         {
86                 ui_set_playback_speed( 1 );
87                 ui_play_events_realtime(MacroNumEvents, RecordBuffer);
88                 MacroStatus = UI_STATUS_PLAYING;
89         }
90         return 1;
91 }
92
93 int MacroPlayFast()
94 {
95         if (MacroStatus== UI_STATUS_NORMAL && MacroNumEvents > 0 && RecordBuffer )
96         {
97                 ui_mouse_hide();
98                 ui_play_events_fast(MacroNumEvents, RecordBuffer);
99                 MacroStatus = UI_STATUS_FASTPLAY;
100         }
101         return 1;
102 }
103
104 int MacroSave()
105 {
106
107         if (MacroNumEvents < 1 )
108         {
109                 MessageBox( -2, -2, 1, "No macro has been defined to save!", "Oops" );
110                 return 1;
111         }
112
113         if (ui_get_filename( filename, "*.MAC", "SAVE MACRO" ))
114         {
115                 PHYSFS_file *fp = PHYSFS_openWrite(filename);
116
117                 if (!fp) return 0;
118                 RecordBuffer[0].type = 7;
119                 RecordBuffer[0].frame = 0;
120                 RecordBuffer[0].data = MacroNumEvents;
121                 PHYSFS_write(fp, RecordBuffer, sizeof(UI_EVENT), MacroNumEvents);
122                 PHYSFS_close(fp);
123         }
124         return 1;
125 }
126
127 int MacroLoad()
128 {
129         if (ui_get_filename( filename, "*.MAC", "LOAD MACRO" ))
130         {
131                 PHYSFS_file *fp = PHYSFS_openRead(filename);
132                 int length;
133
134                 if (!fp)
135                         return 0;
136                 if (RecordBuffer)
137                         d_free( RecordBuffer );
138                 length = PHYSFS_fileLength(fp);
139                 RecordBuffer = d_malloc(length);
140                 if (!RecordBuffer)
141                         return 0;
142
143                 PHYSFS_read(fp, RecordBuffer, length, 1);
144                 PHYSFS_close(fp);
145                 MacroNumEvents = RecordBuffer[0].data;
146         }
147         return 1;
148 }
149
150 void macro_free_buffer()
151 {
152         if ( RecordBuffer ) 
153                 d_free(RecordBuffer);
154 }
155
156 int MacroMenu()
157 {
158         int x;
159         char * MenuItems[] = { "Play fast",
160                                            "Play normal",
161                                            "Record all",
162                                            "Record keys",
163                                            "Save macro",
164                                            "Load macro" };
165
166         x = MenuX( -1, -1, 6, MenuItems );
167
168         switch( x )
169         {
170         case 1:
171                 MacroPlayFast();
172                 break;
173         case 2:
174                 MacroPlayNormal();
175                 break;
176         case 3:
177                 MacroRecordAll();
178                 break;
179         case 4:
180                 MacroRecordKeys();
181                 break;
182         case 5:     // Save
183                 MacroSave();
184                 break;
185         case 6:     // Load
186                 MacroLoad();
187                 break;
188         }
189         return 1;
190 }
191