-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities_math.go
165 lines (145 loc) · 4.86 KB
/
utilities_math.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
/*
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 (
"errors"
"math"
"math/rand"
blt "bearlibterminal"
)
const (
// Values to catch errors.
WrongIndexValue = -1
)
func RoundFloatToInt(x float64) int {
/* Function RoundFloatToInt takes one float64 number,
rounds it to nearest 1.0, then returns it as a integer. */
return int(math.Round(x))
}
func RandInt(max int) int {
/* Function RandInt wraps rand.Intn function;
instead of returning 0..n-1 it returns 0..n. */
return rand.Intn(max + 1)
}
func RandRange(min, max int) int {
/* Function RandRange returns value between min and max,
both including. */
return RandInt(max-min) + min
}
func OrderToCharacter(i int) string {
/* Function OrderToCharacter takes integer
and converts it to string. Typically,
it will be used with letters, but rune
is alias of int32 and support unicode
well.
Typically, one would like to return
string('a'-1+i)
to convert "1" to "a", but RAWIG will use
it to deal with bare slices that count
from 0.*/
return string('a' + i)
}
func KeyToOrder(key int) int {
/* Function KeyToOrder takes user input as integer
(in BearLibTerminal player input is passed as 0x...)
and return another int that is smaller by
first-key (ie "a" key).
It will need extensive error-checking
(or maybe just LBYL?) for wrong input. */
return key - blt.TK_A
}
func FindObjectIndex(item *Object, arr Objects) (int, error) {
/* Function FindObjectIndex takes object, and slice of objects
as arguments. It returns integer and error.
It is supposed to find index item in arr. If fails,
returns error. */
var err error
index := WrongIndexValue
for i := 0; i < len(arr); i++ {
if arr[i] == item {
index = i
break
}
}
if index == WrongIndexValue {
err = errors.New("*Object not found in []*Object.")
}
return index, err
}
func FindCreatureIndex(creature *Creature, arr Creatures) (int, error) {
/* Function FindCreatureIndex works as FindObjectIndex,
but for monsters. */
var err error
index := WrongIndexValue
for i := 0; i < len(arr); i++ {
if arr[i] == creature {
index = i
break
}
if index == WrongIndexValue {
err = errors.New("*Creature not found in []*Creature.")
}
}
return index, err
}
func DistanceBetween(sourceX, sourceY, targetX, targetY int) int {
/* Function DistanceBetween takes coords of source and target;
it computes distance between these two tiles.
As Go uses float64 for such a computations, it is necessary
to transform ints to float64 then round result to int. */
dx := float64(targetX - sourceX)
dy := float64(targetY - sourceY)
distance := RoundFloatToInt(math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2)))
return distance
}
func (c *Creature) DistanceBetweenCreatures(c2 *Creature) int {
distance := DistanceBetween(c.X, c.Y, c2.X, c2.Y)
return distance
}
func AbsoluteValue(i int) int {
/* Function AbsoluteValue returns absolute (ie "non negative") value. */
if i < 0 {
return -i
}
return i
}
func ReverseIntSlice(arr []int) []int {
/* Function ReverseIntSlice takes slice of int and returns
it in reversed order. It is odd that "battery included"
language like Go does not have built-in functions for it. */
var reversed = []int{}
for i := len(arr) - 1; i >= 0; i-- {
reversed = append(reversed, arr[i])
}
return reversed
}
func CreatureIsInSlice(c *Creature, arr Creatures) bool {
/* Function CreatureIsInSlice takes Creature and slice of Creature
as arguments, and returns true, if c is present in arr.
Otherwise, returns false. */
for _, v := range arr {
if c == v {
return true
}
}
return false
}