1 /*
2 XorGramana Copyright 2009 James W. Morris, james@jwm-art.net
3
4 This file is part of XorGramana.
5
6 XorGramana is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 XorGramana is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XorGramana. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "options.h"
20
21 #include "map.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 const char *map_dir="maps/";
28 const char *xormap_dir="xormaps/";
29 const char *data_dir=DATADIR;
30
31 char* map_name[MAX_LEVEL+1]={0};
32
33 struct xor_options* options=0;
34
35 /*
36 void options_init_user_data_dir();
37 */
38
options_create()39 su_t options_create()
40 {
41 char* tmp;
42 if(!(options=malloc(sizeof(struct xor_options)))){
43 printf("Memory allocation error!\n");
44 exit(1);
45 }
46 options->game=0; /* 0=XorGramana, 1=Xor */
47 options->show_indicators=1;
48 options->scroll_thresh=3;
49 options->good_opt_dir=0;
50 options->replay_speed=7;
51 options->dir_opt=0;
52 options->data_dir=0;
53 options->user_dir=0;
54 options->map_dir=0;
55 if((tmp=getenv("HOME"))){
56 if((options->user_dir=malloc(strlen(tmp)+1)))
57 strcpy(options->user_dir,tmp);
58 }
59 if(options_set_dir_opt(DATA_INST_LOC))
60 return 1;
61 if(options_set_dir_opt(DATA_CWD_LOC))
62 return 1;
63 options_destroy();
64 return 0;
65 }
66
options_destroy()67 void options_destroy()
68 {
69 if(options){
70 if(options->map_dir)
71 free(options->map_dir);
72 if(options->user_dir)
73 free(options->user_dir);
74 free(options);
75 options=0;
76 }
77 }
78
set_game(su_t n)79 su_t set_game(su_t n)
80 {
81 FILE* fp;
82 if(n==1)
83 options->game=1;
84 else
85 options->game=0;
86 if(options->map_dir)
87 free(options->map_dir);
88 if(!(options->map_dir=options_file_path(
89 (options->game?xormap_dir:map_dir),
90 options->data_dir)))
91 return 0;
92 if(!(fp=fopen(options->map_dir,"r"))){
93 free(options->map_dir);
94 options->map_dir=0;
95 return 0;
96 }
97 fclose(fp);
98 return options_create_map_names();
99 }
100
options_set_dir_opt(enum DATA_LOC loc)101 su_t options_set_dir_opt(enum DATA_LOC loc)
102 {
103 switch((options->dir_opt=loc))
104 {
105 case DATA_CWD_LOC: options->data_dir="data/"; break;
106 default: options->data_dir=data_dir; break;
107 }
108 return set_game(options->game);
109 }
110
options_create_map_names()111 su_t options_create_map_names()
112 {
113 su_t i;
114 for(i=MIN_LEVEL;i<=MAX_LEVEL;i++){
115 if(map_name[i])
116 free(map_name[i]);
117 if(!(map_name[i]=xg_map_load_read_name(i)))
118 if(i==MIN_LEVEL)
119 return 0;
120 }
121 return 1;
122 }
123
options_destroy_map_names()124 void options_destroy_map_names()
125 {
126 su_t i;
127 for(i=MIN_LEVEL;i<=MAX_LEVEL;i++)
128 free(map_name[i]);
129 }
130
options_replay_speed(char n)131 long options_replay_speed(char n)
132 {
133 if(n<1)
134 n=1;
135 if(n>9){
136 if(n>='1'&&n<='9'){
137 n=9-('9'-n);
138 }
139 else
140 n=9;
141 }
142 options->replay_speed=n;
143 return (long)(200000000L/(float)n);
144 }
145
options_map_filename(su_t level)146 char* options_map_filename(su_t level)
147 {
148 char* fname;
149 char* dir=0;
150 su_t l;
151 if(level<MIN_LEVEL||level>MAX_LEVEL)
152 return 0;
153 l=strlen(dir=options->map_dir);
154 l+=((level<10?5:6)+1);
155 if(!(fname=malloc(l)))
156 return 0;
157 if(snprintf(fname,l,"%s%d.txt",dir,level)!=l-1){
158 free(fname);
159 return 0;
160 }
161 return fname;
162 }
163
options_file_path(const char * fname,const char * path)164 char* options_file_path(const char* fname, const char *path)
165 {
166 char* fp=0;
167 su_t n;
168 if(!fname)
169 return 0;
170 if(path){
171 if(path[n=strlen(path)]!='/')
172 n++;
173 if(!(fp=calloc(strlen(fname)+n+1,sizeof(char))))
174 return 0;
175 strcpy(fp,path);
176 if(fp[n-1]!='/'){
177 fp[n-1]='/';
178 fp[n]=0;
179 }
180 }
181 else{
182 if(!(fp=calloc(strlen(fname)+1,sizeof(char))))
183 return 0;
184 }
185 strcat(fp,fname);
186 return fp;
187 }
188
189