-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
142 lines (132 loc) · 4.02 KB
/
utils.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
/*
* utils.go
*
* Copyright (C) 2020-2023 Dariusz Sikora <ds@isangeles.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"strings"
"github.com/isangeles/flame/character"
flamedata "github.com/isangeles/flame/data"
flameres "github.com/isangeles/flame/data/res"
"github.com/isangeles/flame/item"
"github.com/isangeles/flame/objects"
"github.com/isangeles/flame/serial"
"github.com/isangeles/flame/useaction"
"github.com/isangeles/fire/config"
"github.com/isangeles/fire/request"
)
// transferItems transfer items between specified objects.
// Items are in the form of a map with IDs as keys and serial values as values.
func transferItems(from, to item.Container, items map[string][]string) error {
for id, serials := range items {
for _, serial := range serials {
item := from.Inventory().Item(id, serial)
if item == nil {
return fmt.Errorf("Item not found: %s %s",
id, serial)
}
from.Inventory().RemoveItem(item)
to.Inventory().AddItem(item)
}
}
return nil
}
// removeItems removes items from specified container.
// Items are in the form of a map with IDs as keys and serial values as values.
func removeItems(container item.Container, items map[string][]string) error {
for id, serials := range items {
for _, serial := range serials {
item := container.Inventory().Item(id, serial)
if item == nil {
return fmt.Errorf("Item not found: %s %s",
id, serial)
}
container.Inventory().RemoveItem(item)
}
}
return nil
}
// charSkillRecipe returns skill or recipe with specified ID from the character,
// or nil if character does not have skill or recipe with such ID.
func charSkillRecipe(char *character.Character, id string) useaction.Usable {
for _, s := range char.Skills() {
if s.ID() == id {
return s
}
}
for _, r := range char.Crafting().Recipes() {
if r.ID() == id {
return r
}
}
return nil
}
// inRange checks if specified objects are in the minimum range between each other.
// The minimum range value is specified in config package.
// The function always returns true if at least one of the specified objects have
// no position.
func inRange(ob1, ob2 serial.Serialer) bool {
pos1, ok := ob1.(objects.Positioner)
if !ok {
return true
}
pos2, ok := ob2.(objects.Positioner)
if !ok {
return true
}
return objects.Range(pos1, pos2) <= config.ActionMinRange
}
// equip inserts item to specified slots in character equipment.
func equip(eq *character.Equipment, it item.Equiper, slots []request.EquipmentSlot) error {
for _, slot := range slots {
for _, eqSlot := range eq.Slots() {
if string(eqSlot.Type()) != slot.Type || eqSlot.ID() != slot.ID {
continue
}
for _, itSlot := range it.Slots() {
if itSlot == eqSlot.Type() {
eqSlot.SetItem(it)
}
}
}
}
for _, itSlot := range it.Slots() {
equiped := false
for _, eqSlot := range eq.Slots() {
if eqSlot.Type() != itSlot || eqSlot.Item() != it {
continue
}
equiped = true
break
}
if !equiped {
eq.Unequip(it)
return fmt.Errorf("Item is was not inserted in all required slots: %s %s: %s",
it.ID(), it.Serial(), string(itSlot))
}
}
return nil
}
// importModule imports module data from module directory or module file.
func importModule(path string) (flameres.ModuleData, error) {
if strings.HasSuffix(path, flamedata.ModuleFileExt) {
return flamedata.ImportModule(path)
}
return flamedata.ImportModuleDir(path)
}