/
var.c
113 lines (104 loc) · 2.81 KB
/
var.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
#include "var.h"
struct Var* get_var(struct Item* list, char* name)
{
while(list)
{
if(strcmp(((struct Var*)list->val)->name, name) == 0)
return (struct Var*)list->val;
list = list->next;
}
return NULL;
}
int get_var_value(struct Item* list, char* name, int default_value)
{
struct Var* var = get_var(list, name);
return var ? var->value : default_value;
}
struct Var* set_var(struct Item* list, char* name, int value)
{
struct Var* var = get_var(list, name);
if(!var)
{
var = malloc(sizeof(struct Var));
strcpy(var->name, name);
add_after(list, var);
}
var->value = value;
return var;
}
void replace_vars_values(struct Item* vars, char* s)
{
int i, j = 0, k = 0;
char newstring[BUFFER_SIZE];
for(i = 0; s[i] != '\0'; i++)
{
char varname[VAR_NAME_SIZE] = {'\0'};
while(((s[i] >= 'a' && s[i] <= 'z') ||
(s[i] >= 'A' && s[i] <= 'Z') ||
s[i] == '_') && j < VAR_NAME_SIZE - 1)
{
varname[k] = s[i];
k++; i++;
}
if(varname[0] != '\0')
{
struct Var* var;
varname[k] = '\0';
var = get_var(vars, varname);
if(var)
{
sprintf(newstring + j, "%d", var->value);
j += strlen(newstring + j);
}
else
{
sprintf(error, "unknown var \"%s\"", varname);
break;
}
k = 0;
}
newstring[j] = s[i];
j++;
}
newstring[j] = '\0';
strcpy(s, newstring);
}
int* setbind(struct Item* list, char* name, int value)
{
return &(set_var(list, name, value)->value);
}
struct Item* build_special_vars(struct Game* game)
{
struct Var* firstvar = malloc(sizeof(struct Var));
struct Item* list;
strcpy(firstvar->name, "null");
firstvar->value = 0;
list = build_first_item(firstvar);
game->display->width = setbind(list, "_width", 640);
game->display->height = setbind(list, "_height", 480);
game->verbose = setbind(list, "_verbose", 1);
game->display->messagebox->message_color = setbind(list, "_message_color", -1);
game->display->messagebox->choice_color = setbind(list, "_choice_color", -1);
game->display->messagebox->message_bg_color = setbind(list, "_message_bg_color", -1);
game->display->messagebox->choice_bg_color = setbind(list, "_choice_bg_color", -1);
game->display->messagebox->x = setbind(list, "_messagebox_x", 0);
game->display->messagebox->y = setbind(list, "_messagebox_y", 0);
game->display->messagebox->image = setbind(list, "_messagebox_image", -1);
game->display->messagebox->choicecursor = setbind(list, "_choice_cursor_image", -1);
return list;
}
ALLEGRO_COLOR get_color_var(ALLEGRO_COLOR* colors, int id, ALLEGRO_COLOR defaultval)
{
if(id < 0 || id >= COLORS)
return defaultval;
else
return colors[id];
}
void cmd_set(struct Game* game, char* arg)
{
char* valuestr = cut_command(arg);
int value = eval(game->vars, valuestr);
if(*game->verbose)
printf("%s%s%s = %s%d%s", TGREEN, arg, TDEF, TMAGENTA, value, TDEF);
set_var(game->vars, arg, value);
}