Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
feather/server/worldgen/src/voronoi.rs at plugins · feather-rs/feather · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
feather-rs
/
feather
Public
Notifications
You must be signed in to change notification settings
Fork
142
Star
2.7k
Code
Issues
66
Pull requests
38
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
plugins
Breadcrumbs
feather
/
server
/
worldgen
/
src
/
voronoi.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
144 lines (122 loc) · 4.16 KB
plugins
Breadcrumbs
feather
/
server
/
worldgen
/
src
/
voronoi.rs
Copy path
Top
File metadata and controls
Code
Blame
144 lines (122 loc) · 4.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//! Basic Voronoi implementation.
use
rand
::
{
Rng
,
SeedableRng
}
;
use
rand_xorshift
::
XorShiftRng
;
/// Position of a cell.
#
[
derive
(
Debug
,
Clone
,
Copy
,
PartialEq
,
Eq
)
]
struct
CellPos
{
x
:
i32
,
y
:
i32
,
}
/// Position of a seed.
#
[
derive
(
Debug
,
Clone
,
Copy
,
PartialEq
,
Eq
)
]
struct
SeedPos
{
x
:
i32
,
y
:
i32
,
}
/// Representation of a Voronoi grid.
///
/// Seeds around the most recently requested cell are cached.
///
/// # Implementation
/// This Voronoi implementation works using a grid with jitter
/// offsets. A seed is allocated for each cell in the grid with
/// a random offset from the center of the cell based on a hash
/// function of the cell position.
///
/// This allows the grid to be deterministic and efficient compared
/// to when using random cell positions.
pub
struct
VoronoiGrid
{
/// Length and width of each grid square.
length
:
u32
,
/// The seed used for generation.
seed
:
u64
,
/// The currently cached cell position.
cached
:
CellPos
,
/// The positions of the seeds around the cached cell position.
cached_seeds
:
[
[
SeedPos
;
5
]
;
5
]
,
}
impl
VoronoiGrid
{
/// Creates a new voronoi grid with the given
/// length and seed.
///
/// This function does not
/// actually compute any values.
pub
fn
new
(
length
:
u32
,
seed
:
u64
)
->
Self
{
Self
{
length
,
seed
,
cached
:
CellPos
{
x
:
999_999_999
,
y
:
999_999_999
,
}
,
// Use values so that this will be replaced
cached_seeds
:
[
[
SeedPos
{
x
:
0
,
y
:
0
}
;
5
]
;
5
]
,
}
}
/// Returns the position of the seed closest to the given
/// position.
pub
fn
get
(
&
mut
self
,
x
:
i32
,
y
:
i32
)
->
(
i32
,
i32
)
{
let
cell_pos =
CellPos
{
x
:
x /
self
.
length
as
i32
,
y
:
y /
self
.
length
as
i32
,
}
;
self
.
update_cache
(
cell_pos
)
;
// TODO: this is fairly inefficient. There is
// probably a way to optimize this.
let
closest_seed =
self
.
cached_seeds
.
iter
(
)
.
flatten
(
)
.
min_by_key
(
|seed|
{
// Distance squared to cell position
square
(
seed
.
x
- x
)
+
square
(
seed
.
y
- y
)
}
)
.
unwrap
(
)
;
// Safe - iterator is never empty
(
closest_seed
.
x
,
closest_seed
.
y
)
}
/// Updates the currently cached seed positions.
///
/// If the given cell position is equal to the cached
/// cell position, this is a no-op.
fn
update_cache
(
&
mut
self
,
cell
:
CellPos
)
{
if
cell ==
self
.
cached
{
return
;
}
self
.
cached
= cell
;
let
half_length =
(
self
.
length
/
2
)
as
i32
;
for
x
in
-
2
..=
2
{
for
y
in
-
2
..=
2
{
// Calculate center of grid position and then
// apply an offset based on a hash of the cell position.
let
cell_x = cell
.
x
+ x
;
let
cell_y = cell
.
y
+ y
;
let
pos_x = cell_x
*
self
.
length
as
i32
;
let
pos_y = cell_y
*
self
.
length
as
i32
;
let
mut
rng =
XorShiftRng
::
seed_from_u64
(
self
.
seed
^
(
(
(
i64
::
from
(
cell_x
)
)
<<
32
)
|
(
i64
::
from
(
cell_y
)
)
)
as
u64
,
)
;
let
offset = rng
.
gen_range
(
-half_length
,
half_length
)
;
let
center_x = pos_x + half_length
as
i32
;
let
center_y = pos_y + half_length
as
i32
;
let
offsetted_pos =
SeedPos
{
x
:
center_x + offset
,
y
:
center_y + offset
,
}
;
self
.
cached_seeds
[
(
x +
2
)
as
usize
]
[
(
y +
2
)
as
usize
]
= offsetted_pos
;
}
}
}
}
/// Shuffles the given closest_x and closest_y values
/// and returns a deterministic random value in the given range based
/// on those values.
///
/// This can be used to determine a value corresponding to a voronoi seed,
/// for example.
pub
fn
shuffle
(
closest_x
:
i32
,
closest_y
:
i32
,
min
:
usize
,
max
:
usize
)
->
usize
{
let
combined =
(
(
closest_x
as
u64
)
<<
32
)
| closest_y
as
u64
;
let
mut
rng =
XorShiftRng
::
seed_from_u64
(
combined
)
;
rng
.
gen_range
(
min
,
max
)
}
fn
square
(
x
:
i32
)
->
i32
{
x
*
x
}
You can’t perform that action at this time.