-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsaving.go
224 lines (204 loc) · 6.6 KB
/
saving.go
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
/*
Copyright (c) 2018, Tomasz "VedVid" Nowakowski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"encoding/gob"
"fmt"
"os"
)
const (
// Constant values for save files manipulation.
MapNameGob = "map.gob"
MapPathGob = "./" + MapNameGob
CreaturesNameGob = "monsters.gob"
CreaturesPathGob = "./" + CreaturesNameGob
ObjectsNameGob = "objects.gob"
ObjectsPathGob = "./" + ObjectsNameGob
)
const (
// Unique name that serves as identifier to values
// that should be converted from nil to object or from object to nil.
ObjectNilPlaceholder = "ObjectNilPlaceholder"
)
func NilToObject() *Object {
/* Function nilToObject returns *Object with >>placeholder<< identifier.
It serves to find data that is nil in game - but format gob does not
work well with nil values (and interfaces).
It is ugly hack, but works. */
placeholder := &Object{BasicProperties{0, 0, "o", ObjectNilPlaceholder,
"black", "black"},
VisibilityProperties{0, false},
CollisionProperties{false, false},
ObjectProperties{false, false, false, 0, 0}}
return placeholder
}
func writeGob(path string, thing interface{}) error {
/* Function writeGob takes path-to-file, and any object (as interface{})
as arguments, then encodes it to gob file. Returns error - unfortunately,
errors that are built in gob package are not very helpful, and whole process
is hard to debug. */
f, err := os.Create(path)
defer f.Close()
if err == nil {
encoder := gob.NewEncoder(f)
encoder.Encode(thing)
}
return err
}
func readGob(path string, thing interface{}) error {
/* Function readGob takes path-to-file, and any object (as interface{})
as arguments, then decodes file to interface. Returns error - Decoding has
as unhelpful errors as Encoding. */
f, err := os.Open(path)
defer f.Close()
if err == nil {
decoder := gob.NewDecoder(f)
err = decoder.Decode(thing)
}
return err
}
func saveBoard(b Board) error {
/* Function saveBoard is helper function that takes game map
as argument and encodes it to save file. */
err := writeGob(MapPathGob, b)
return err
}
func loadBoard(b *Board) error {
/* Function loadBoard is helper function that decodes saved data
to game map. */
err := readGob(MapPathGob, b)
return err
}
func saveCreatures(c Creatures) error {
/* Function saveBoard is helper function that takes monsters
as argument and encodes it to save file.
Unfortunately, gob format/package does not work well with
nil values. To encode it properly, there is placeholder
object created for every nil object; these false objects
should be decode to nil by loadCreatures. */
for i := 0; i < len(c); i++ {
for j := 0; j < len(c[i].Equipment); j++ {
if c[i].Equipment[j] == nil {
c[i].Equipment[j] = NilToObject()
}
}
for k := 0; k < len(c[i].Inventory); k++ {
if c[i].Inventory[k] == nil {
c[i].Inventory[k] = NilToObject()
}
}
}
err := writeGob(CreaturesPathGob, c)
return err
}
func loadCreatures(c *Creatures) error {
/* Function loadCreatures is helper function that decodes saved data
to slice of creatures. Gob package has troubles with handling nil
values, so every nil is represented as placeholder object.
During decoding, every placeholder becomes nil again. */
err := readGob(CreaturesPathGob, c)
for i := 0; i < len(*c); i++ {
objs := (*c)[i].Equipment
for j := 0; j < len(objs); j++ {
if objs[j].Name == ObjectNilPlaceholder {
objs[j] = nil
}
}
inv := (*c)[i].Inventory
for k := 0; k < len(inv); k++ {
if inv[k].Name == ObjectNilPlaceholder {
inv[k] = nil
}
}
}
return err
}
func saveObjects(o Objects) error {
/* Function saveObjects is helper function that takes game objects
as argument and encodes it to save file. */
err := writeGob(ObjectsPathGob, o)
return err
}
func loadObjects(o *Objects) error {
/* Function loadObjects is helper function that decodes saved data
to slice of objects. */
err := readGob(ObjectsPathGob, o)
return err
}
func SaveGame(b Board, c Creatures, o Objects) error {
/* Function SaveGame encodes game map, monsters, objects into
save files, using Go's gob format. This function may need better
error handling - it relies on gob's built-in errors that are
not very helpful. */
var err error
err = saveBoard(b)
if err != nil {
fmt.Println(err)
}
err = saveCreatures(c)
if err != nil {
fmt.Println(err)
}
err = saveObjects(o)
if err != nil {
fmt.Println(err)
}
return err
}
func LoadGame(b *Board, c *Creatures, o *Objects) error {
/* Function LoadGame decoded save files (their names and paths are
specified as constants on the top of this file) into
game map, monsters and objects. As SaveGame, it may need
better error handling due to unhelpful gob's error messages. */
var err error
err = loadBoard(b)
if err != nil {
fmt.Println(err)
}
err = loadCreatures(c)
if err != nil {
fmt.Println(err)
}
err = loadObjects(o)
if err != nil {
fmt.Println(err)
}
return err
}
func DeleteSaves() {
/* Function DeleteSaves sereves, well, deleting saves (mostly upon death).
It checks if certain save file exists. If so, removes it.
Returns the first encountered error. */
var err error
_, err = os.Stat(MapPathGob)
if err == nil {
os.Remove(MapPathGob)
}
_, err = os.Stat(CreaturesPathGob)
if err == nil {
os.Remove(CreaturesPathGob)
}
_, err = os.Stat(ObjectsPathGob)
if err == nil {
os.Remove(ObjectsPathGob)
}
}