-
Notifications
You must be signed in to change notification settings - Fork 0
/
omnisave.c
122 lines (86 loc) · 2.38 KB
/
omnisave.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "md5hash.h"
#include "omnitype.h"
#include "omnimino.def"
/**************************************
SaveGame
**************************************/
static char *StorePtr;
static int StoreFree;
static void Adjust(int Done) {
if (Done >= StoreFree)
Done = StoreFree;
/* Done--; */
StoreFree -= Done;
StorePtr += Done;
}
static void StoreInt(int V, int Delim) {
Adjust(snprintf(StorePtr, StoreFree, "%d%c", V, (char)Delim));
}
static void StoreUnsigned(unsigned int V, int Delim) {
Adjust(snprintf(StorePtr, StoreFree, "%u%c", V, (char)Delim));
}
static void StoreString(char *S) {
Adjust(snprintf(StorePtr, StoreFree, "%s\n", S));
}
void SaveGame(struct Omnimino *GG){
unsigned int *UPtr = (unsigned int *) (&(GG->P));
unsigned int i;
int Used;
char *UserName;
FILE *fout;
if (GameType == 3)
return;
StorePtr = (char *) GlassRow;
StoreFree = StoreBufSize;
do {
for (i = 0; i < PARNUM; i++, UPtr++)
StoreUnsigned(*UPtr, '\n');
if ((GameType == 2) && (FillRatio != 0))
break;
StoreString(ParentName);
StoreInt((int)(LastFigure - Figure), '\n');
StoreInt((int)(NextFigure - Figure), '\n');
for (i = 0; i < FillLevel; i++)
StoreUnsigned(FillBuf[i], ';');
StoreString("");
if (GameType == 2)
break;
struct Coord **F;
for(F = Figure; F <= LastFigure; F++)
StoreInt((int)(*F - Block), ';');
StoreString("");
struct Coord *B;
for(B = Block; B < (*LastFigure); B++) {
StoreInt(B->x, ',');
StoreInt(B->y, ';');
}
StoreString("");
UserName = getenv("USER");
if (!UserName)
UserName = "anonymous";
snprintf(PlayerName,OM_STRLEN,"%s",UserName);
StoreString(PlayerName);
TimeStamp = (unsigned int)time(NULL);
StoreUnsigned(TimeStamp, '\n');
} while(0);
Used = StoreBufSize - StoreFree;
md5hash(GlassRow, Used, GameName);
if (GameType == 2)
strcat(GameName, ".preset");
strcat(GameName, ".mino");
fout = fopen(GameName, "w");
if (fout == NULL) {
snprintf(MsgBuf, OM_STRLEN, "Can not open for write %s.", GameName);
GameType = 3;
} else {
if(fwrite(GlassRow, sizeof(char), Used, fout) != (size_t)Used) {
snprintf(MsgBuf, OM_STRLEN, "Error writing %s.", GameName);
GameType = 3;
}
fclose(fout);
}
}