-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
170 lines (151 loc) · 2.59 KB
/
history.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include "game.h"
#define HISTORY_SIZE 4096
static uint8_t *history;
static int hcap, hpos;
enum {
MOVE_TANK_N,
MOVE_TANK_E,
MOVE_TANK_S,
MOVE_TANK_W,
MOVE_OBJ_N,
MOVE_OBJ_E,
MOVE_OBJ_S,
MOVE_OBJ_W,
BREAK_BRICK,
BREAK_ANTI,
BREAK_ICE,
SINK,
ROTATE_MIRROR,
DIE,
};
static uint8_t pack(int y, int x)
{
return ((y&15)<<4)|(x&15);
}
static void unpack(uint8_t c, struct point *p)
{
p->y = (c>>4)&15;
p->x = c&15;
}
static void record(uint8_t c)
{
if (hpos == hcap) {
hcap *= 2;
history = realloc(history, hcap);
}
history[hpos++] = c;
}
void record_move_tank(int y, int x, int dir)
{
record(pack(y, x));
record(MOVE_TANK_N + dir);
}
void record_move_object(int y, int x, int dir)
{
record(pack(y, x));
record(MOVE_OBJ_N + dir);
}
void record_break_brick(int y, int x)
{
record(pack(y, x));
record(BREAK_BRICK);
}
void record_break_anti(int y, int x)
{
record(pack(y, x));
record(BREAK_ANTI);
}
void record_break_ice(int y, int x)
{
record(pack(y, x));
record(BREAK_ICE);
}
void record_sink(int y, int x, uint8_t obj)
{
record(obj);
record(pack(y, x));
record(SINK);
}
void record_rotate_mirror(int y, int x)
{
record(pack(y, x));
record(ROTATE_MIRROR);
}
void record_die(void)
{
record(DIE);
}
extern int Dx[4], Dy[4];
void init_history(void)
{
hcap = 0x1000;
history = malloc(hcap);
hpos = 0;
}
void clear_history(void)
{
hpos = 0;
}
void undo(void)
{
int p = hpos-1;
if (p < 0) return;
while (p >= 0 && history[p] > MOVE_TANK_W)
p--;
int q = hpos;
while (q > p) {
uint8_t code = history[--q];
struct point pt;
if (code < DIE)
unpack(history[--q], &pt);
int y = pt.y;
int x = pt.x;
int d;
switch (code) {
case MOVE_TANK_N:
case MOVE_TANK_E:
case MOVE_TANK_S:
case MOVE_TANK_W:
tank_y = y;
tank_x = x;
tank_orient = code - MOVE_TANK_N;
// this is necessary to prevent problems with tank
// movers
tank_action = 0;
tank_sliding_dir = -1;
break;
case BREAK_BRICK:
board[y][x].fg = F_BRICK;
break;
case BREAK_ANTI:
board[y][x].fg -= 4;
break;
case BREAK_ICE:
board[y][x].bg = B_THIN_ICE;
break;
case SINK:
board[y][x].fg = history[--q];
board[y][x].bg = B_WATER;
break;
case MOVE_OBJ_N:
case MOVE_OBJ_E:
case MOVE_OBJ_S:
case MOVE_OBJ_W:
d = code - MOVE_OBJ_N;
board[y][x].fg = board[y+Dy[d]][x+Dx[d]].fg;
board[y+Dy[d]][x+Dx[d]].fg = 0;
break;
case ROTATE_MIRROR:
board[y][x].fg = F_ROT_MIRROR_NE | ((board[y][x].fg-1)&3);
break;
case DIE:
tank_alive = true;
break;
}
}
hpos = q;
num_lasers = 0;
}