-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
170 lines (152 loc) · 3.48 KB
/
map.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Map struct {
width, height int
print_width, print_height int
player Player
objs []mapObject
mat [][]mapItem
}
func (m Map) print_map() int {
center_x, center_y := m.player.x, m.player.y
lx, ly := center_x-m.print_width/2, center_y-m.print_height/2
rx, ry := center_x+m.print_width/2+1, center_y+m.print_height/2+1
if m.print_width >= m.width {
lx = 0
rx = m.width
} else {
for lx < 0 {
lx++
rx++
}
for rx >= m.width {
rx--
lx--
}
}
if m.print_height >= m.height {
ly = 0
ry = m.height
} else {
for ly < 0 {
ly++
ry++
}
for ry >= m.height {
ry--
ly--
}
}
for i := ly; i < ry; i++ {
for j := lx; j < rx; j++ {
if i == center_y && j == center_x {
fmt.Printf("%c", '@')
} else {
PrintMapItem(m.mat[i][j])
}
}
fmt.Println()
}
return 0
}
func readMapItemMtrFromFile(filename string) ([][]mapItem, int, int, error) {
file, err := os.Open(filename)
if err != nil {
return nil, 0, 0, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
line := scanner.Text()
dimensions := strings.Fields(line)
if len(dimensions) != 2 {
return nil, 0, 0, fmt.Errorf("некорректный формат размеров матрицы")
}
n, err := strconv.Atoi(dimensions[0])
if err != nil {
return nil, 0, 0, fmt.Errorf("некорректный формат размера матрицы n")
}
m, err := strconv.Atoi(dimensions[1])
if err != nil {
return nil, 0, 0, fmt.Errorf("некорректный формат размера матрицы m")
}
matrix := make([][]mapItem, m)
for i, _ := range matrix {
matrix[i] = make([]mapItem, n)
}
for i := 0; i < m && scanner.Scan(); i++ {
line := scanner.Text()
for j := 0; j < n && j < len(line); j++ {
matrix[i][j] = GetMapItem(line[j])
}
}
if err := scanner.Err(); err != nil {
return nil, 0, 0, err
}
return matrix, n, m, nil
}
return nil, 0, 0, fmt.Errorf("файл пустой")
}
func ReadMapFromFile(filename string) (Map, error) {
m := Map{}
var rc error
m.mat, m.width, m.height, rc = readMapItemMtrFromFile(filename)
return m, rc
}
func ReadMapItems(filename string) ([]mapObject, error) {
chr_id := 1
items := make([]mapObject, 0)
content, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
str := string(content)
lines := strings.Split(str, "\n")
for _, line := range lines {
parts := strings.Split(line, " ")
if len(parts) != 4 {
return nil, fmt.Errorf("Некорректная строка %s", line)
}
x, err := strconv.Atoi(parts[0])
if err != nil {
return nil, err
}
y, err := strconv.Atoi(parts[1])
if err != nil {
return nil, err
}
filepath := parts[2]
typ := parts[3]
if typ == "character" {
var id int = chr_id
for _, v := range items {
if c, ok := v.(*characterObject); ok {
if c.SettingFile == filepath {
id = c.CharacterID
break
}
}
}
obj := characterObject{id, x, y, filepath, nil}
items = append(items, &obj)
if id == chr_id {
chr_id++
}
} else if typ == "clue" {
obj := ClueObject{x, y, filepath}
items = append(items, &obj)
} else if typ == "final" {
obj := FinalObject{X: x, Y: y}
items = append(items, &obj)
} else {
return nil, fmt.Errorf("Некорректный тип %s", typ)
}
}
return items, nil
}