-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
150 lines (126 loc) · 3.01 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "options.h"
#include "map.h"
#include "debug.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static const char *map_dir = "maps/";
static const char *data_dir = DATADIR;
struct xor_options *options = 0;
su_t options_create()
{
debug("creating options...\n");
if (!(options = malloc(sizeof(struct xor_options)))) {
err_msg("Memory allocation error!\n");
exit(1);
}
options->oldschool_play = 0;
options->scroll_thresh = 2;
options->good_opt_dir = 0;
options->replay_speed = 4;
options->replay_hyper = 0;
options->replay_step = 0;
options->dir_opt = 0;
options->data_dir = 0;
options->user_dir = 0;
char *tmp = getenv("HOME");
if (tmp) {
if ((options->user_dir = malloc(strlen(tmp) + 1)))
strcpy(options->user_dir, tmp);
}
if (options_set_dir_opt(DATA_INST_LOC))
return 1;
free(options->map_dir);
options->map_dir = 0;
if (options_set_dir_opt(DATA_CWD_LOC))
return 1;
options_destroy();
return 0;
}
void options_destroy()
{
if (options) {
if (options->map_dir)
free(options->map_dir);
if (options->user_dir)
free(options->user_dir);
free(options);
options = 0;
}
}
su_t options_set_dir_opt(enum DATA_LOC loc)
{
switch ((options->dir_opt = loc)) {
case DATA_CWD_LOC:
options->data_dir = 0;
break;
default:
options->data_dir = data_dir;
break;
}
if (!(options->map_dir = options_file_path(map_dir, options->data_dir)))
return 0;
FILE *fp = fopen(options->map_dir, "r");
if (!fp)
return 0;
fclose(fp);
return 1;
}
long options_replay_speed(char n)
{
if (options->replay_hyper)
return 0L;
if (options->replay_step)
return (long) (200000000L / 5.0f );
if (n < 1)
n = 1;
else if (n > 9) {
if (n >= '1' && n <= '9')
n = 9 - ('9' - n);
else
n = 9;
}
options->replay_speed = n;
return (long) (200000000L / (float) n );
}
char *
options_map_filename(su_t level)
{
if (level < MIN_LEVEL || level > MAX_LEVEL)
return 0;
char *dir = options->map_dir;
su_t l = strlen(dir) + 7; /* strlen("01.xcm") */
char *fname = malloc(l);
if (!fname)
return 0;
if (snprintf(fname, l, "%s%02d.xcm", dir, level) != l - 1) {
free(fname);
return 0;
}
return fname;
}
char *
options_file_path(const char *fname, const char *path)
{
if (!fname)
return 0;
char *fp = 0;
if (path) {
su_t n = strlen(path);
if (path[n] != '/')
n++;
if (!(fp = calloc(strlen(fname) + n + 1, sizeof(char))))
return 0;
strcpy(fp, path);
if (fp[n - 1] != '/') {
fp[n - 1] = '/';
fp[n] = 0;
}
}
else {
if (!(fp = calloc(strlen(fname) + 1, sizeof(char))))
return 0;
}
strcat(fp, fname);
return fp;
}