-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
273 lines (258 loc) · 8.75 KB
/
mem.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
*******************************************************************
*** This software is copyright 1998-2008 by Michael H Riley ***
*** You have permission to use, modify, copy, and distribute ***
*** this software so long as this copyright notice is retained. ***
*** This software may not be used in commercial applications ***
*** without express written permission from the author. ***
*******************************************************************
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "funge.h"
/* ************************************************************************* */
/* ***** Function to find limits of non-space cells, used by y command ***** */
/* ************************************************************************* */
/* FIXME - This routine is a hack, it works but there should be a faster way */
void Find_Limits(VM* vm,INT* minX,INT* minY,INT* maxX,INT* maxY) {
int i,j;
*minX = 99999;
*minY = 99999;
*maxX = -99999;
*maxY = -99999;
for (i=0; i<vm->NumRows; i++)
for (j=0; j<vm->dFspace[i].NumCells; j++)
if (vm->dFspace[i].Cells[j].v != 32) {
if (vm->dFspace[i].Row < *minY) *minY = vm->dFspace[i].Row;
if (vm->dFspace[i].Row > *maxY) *maxY = vm->dFspace[i].Row;
if (vm->dFspace[i].Cells[j].x < *minX) *minX = vm->dFspace[i].Cells[j].x;
if (vm->dFspace[i].Cells[j].x > *maxX) *maxX = vm->dFspace[i].Cells[j].x;
}
}
/* *********************************************** */
/* ***** Searches for a row in dynamic model ***** */
/* *********************************************** */
INT Find_Row(VM* vm,INT y,INT z)
{
int st,en,cr;
if (vm->NumRows == 0) return -1;
st = 0;
en = vm->NumRows - 1;
cr = st+((en-st)/2);
while ((en-st)>1) {
if (vm->dFspace[cr].Row==y && vm->dFspace[cr].z==z) {
return cr;
}
if (vm->dFspace[cr].z < z ||
(vm->dFspace[cr].z==z && vm->dFspace[cr].Row<y)) {
st = cr;
cr = st+((en-st)/2);
}
else {
en = cr;
cr = st+((en-st)/2);
}
}
if (vm->dFspace[st].Row==y && vm->dFspace[st].z==z) return st;
if (vm->dFspace[en].Row==y && vm->dFspace[en].z==z) return en;
return -1;
}
/* ****************************************** */
/* ***** Add new row into dynamic model ***** */
/* ****************************************** */
INT Add_Row(VM* vm,INT y,INT z) {
INT i,j;
i=0;
while (i<vm->NumRows && (vm->dFspace[i].z<z || (vm->dFspace[i].z==z && vm->dFspace[i].Row<y)))
i++;
vm->NumRows++;
if (vm->NumRows == 1) vm->dFspace=(ROWS*)malloc(sizeof(ROWS));
else vm->dFspace=(ROWS*)realloc(vm->dFspace,sizeof(ROWS)*vm->NumRows);
if (vm->dFspace == NULL) {
printf("Could not increase cells in dFspace\n");
exit(1);
}
for (j=vm->NumRows-1;j>i;j--) vm->dFspace[j]=vm->dFspace[j-1];
vm->dFspace[i].Row=y;
vm->dFspace[i].z=z;
vm->dFspace[i].Min=0;
vm->dFspace[i].Max=0;
vm->dFspace[i].Cells = NULL;
vm->dFspace[i].NumCells = 0;
return i;
}
/* *************************************** */
/* ***** Function to add cell to row ***** */
/* *************************************** */
INT Add_Cell(VM* vm,INT row,INT x,INT y,INT z)
{
INT i;
long int addr;
if (x<vm->dFspace[row].Min) vm->dFspace[row].Min=x;
if (x>vm->dFspace[row].Max) vm->dFspace[row].Max=x;
addr=0;
while (addr<vm->dFspace[row].NumCells && vm->dFspace[row].Cells[addr].x<x)
addr++;
vm->dFspace[row].NumCells++;
if (vm->dFspace[row].NumCells == 1)
vm->dFspace[row].Cells=(CELL*)malloc(sizeof(CELL));
else
vm->dFspace[row].Cells=(CELL*)realloc(vm->dFspace[row].Cells,
sizeof(CELL)*vm->dFspace[row].NumCells);
if (vm->dFspace[row].Cells == NULL) {
printf("Could not allocate memory for additional cells\n");
exit(-1);
}
for (i=vm->dFspace[row].NumCells-1;i>addr;i--)
vm->dFspace[row].Cells[i] = vm->dFspace[row].Cells[i-1];
vm->dFspace[row].Cells[addr].x=x;
return addr;
}
/* ************************************* */
/* ***** Get value from fungespace ***** */
/* ************************************* */
INT Get_Funge(VM* vm,INT x,INT y,INT z)
{
INT i;
long int addr;
int st,en,cr;
x += vm->IPs[vm->cip].OffsetX;
y += vm->IPs[vm->cip].OffsetY;
z += vm->IPs[vm->cip].OffsetZ;
if (vm->MapScreen==1 && x>=0 && x<80 && y>=0 && y<25) {
GotoXY(x+1,y+1);
}
i=Find_Row(vm,y,z);
if (i==-1) return 32;
st = 0;
en = vm->dFspace[i].NumCells - 1;
cr = st+((en-st)/2);
while ((en-st)>1) {
if (vm->dFspace[i].Cells[cr].x==x) return vm->dFspace[i].Cells[cr].v;
if (vm->dFspace[i].Cells[cr].x < x) {
st = cr;
cr = st+((en-st)/2);
}
else {
en = cr;
cr = st+((en-st)/2);
}
}
if (vm->dFspace[i].Cells[st].x==x) return vm->dFspace[i].Cells[st].v;
if (vm->dFspace[i].Cells[en].x==x) return vm->dFspace[i].Cells[en].v;
return 32;
}
/* ************************************** */
/* ***** Put value into Funge-Space ***** */
/* ************************************** */
void Put_Funge(VM* vm,INT x,INT y,INT z,INT value) {
INT i;
long int addr;
if (vm->cip >=0) {
x += vm->IPs[vm->cip].OffsetX;
y += vm->IPs[vm->cip].OffsetY;
z += vm->IPs[vm->cip].OffsetZ;
}
if (vm->MapScreen==1 && x>=0 && x<80 && y>=0 && y<25 && value>31 && value<127) {
GotoXY(x+1,y+1); printf("%c",(char)value);
}
if (x>vm->XDim) vm->XDim=x;
if (x<vm->XMin) vm->XMin=x;
if (y>vm->YDim) vm->YDim=y;
if (y<vm->YMin) vm->YMin=y;
if (z>vm->ZDim) vm->ZDim=z;
if (z<vm->ZMin) vm->ZMin=z;
i=Find_Row(vm,y,z);
if (i==-1) {
i=Add_Row(vm,y,z);
addr=Add_Cell(vm,i,x,y,z);
vm->dFspace[i].Cells[addr].v = value;
}
addr = 0;
while (addr < vm->dFspace[i].NumCells) {
if (x==vm->dFspace[i].Cells[addr].x) {
vm->dFspace[i].Cells[addr].v = value;
return;
}
addr++;
}
addr=Add_Cell(vm,i,x,y,z);
vm->dFspace[i].Cells[addr].v = value;
}
void Reset_VM(VM* vm) {
int InFile;
int i;
if (vm->dFspace != NULL) {
for (i=0; i<vm->NumRows; i++) {
if (vm->dFspace[i].Cells != NULL) free(vm->dFspace[i].Cells);
}
if (vm->dFspace != NULL) free(vm->dFspace);
vm->dFspace = NULL;
vm->NumRows = 0;
}
if (vm->isDynamic == 'Y') {
if ((InFile=open(vm->ProgName,O_RDONLY))!=-1) {
Load_Fingerprint_File(vm,InFile,NULL,0);
} else printf("Could not reload fingerprint %s\n",vm->ProgName);
}
else {
Load_File(vm,vm->ProgName,0,0,0);
}
}
void Clear_Fs(VM* vm)
{
vm->dFspace = NULL;
vm->NumRows = 0;
}
/* ***************************************************** */
/* ***** Get cell where the current IP is pointing ***** */
/* ***************************************************** */
INT Get_Cell(VM* vm)
{
INT v;
v=Get_Funge(vm,vm->IPs[vm->cip].x-vm->IPs[vm->cip].OffsetX,
vm->IPs[vm->cip].y-vm->IPs[vm->cip].OffsetY,
vm->IPs[vm->cip].z-vm->IPs[vm->cip].OffsetZ);
return v;
}
/* *************************************************** */
/* ***** Check to see if ip needs to wrap around ***** */
/* *************************************************** */
void Check_Wrap(VM* vm,int dir) {
int cip;
cip = vm->cip;
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* +++++ See if ip went beyond defined fungespace +++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if (vm->IPs[cip].x>vm->XDim ||
vm->IPs[cip].x<vm->XMin ||
vm->IPs[cip].y>vm->YDim ||
vm->IPs[cip].y<vm->YMin ||
vm->IPs[cip].z>vm->ZDim ||
vm->IPs[cip].z<vm->ZMin) {
/* +++++++++++++++++++++++++++++++++++++++++ */
/* +++++ Back IP back into funge-space +++++ */
/* +++++++++++++++++++++++++++++++++++++++++ */
while (vm->IPs[cip].x>vm->XDim || vm->IPs[cip].x<vm->XMin ||
vm->IPs[cip].y>vm->YDim || vm->IPs[cip].y<vm->YMin ||
vm->IPs[cip].z>vm->ZDim || vm->IPs[cip].z<vm->ZMin) {
vm->IPs[cip].x -= vm->IPs[cip].DeltaX*dir;
vm->IPs[cip].y -= vm->IPs[cip].DeltaY*dir;
vm->IPs[cip].z -= vm->IPs[cip].DeltaZ*dir;
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* +++++ Now back through funge-space til other side +++++ */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
while (vm->IPs[cip].x<=vm->XDim && vm->IPs[cip].x>=vm->XMin &&
vm->IPs[cip].y<=vm->YDim && vm->IPs[cip].y>=vm->YMin &&
vm->IPs[cip].z<=vm->ZDim && vm->IPs[cip].z>=vm->ZMin) {
vm->IPs[cip].x -= vm->IPs[cip].DeltaX*dir;
vm->IPs[cip].y -= vm->IPs[cip].DeltaY*dir;
vm->IPs[cip].z -= vm->IPs[cip].DeltaZ*dir;
}
vm->IPs[cip].x += vm->IPs[cip].DeltaX*dir;
vm->IPs[cip].y += vm->IPs[cip].DeltaY*dir;
vm->IPs[cip].z += vm->IPs[cip].DeltaZ*dir;
}
}