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