cave_gen 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b11510609536968e6056d954ddca355949fa5726
4
+ data.tar.gz: 66d0503e91d05df8a9f4bb280791cb6b7db25ed5
5
+ SHA512:
6
+ metadata.gz: e7d8d7d91f70ad28b9810537afec77ab00ea1615fe62fb5a25d26d250cbb8fde5b06effdacd3596167dd67fafb68885e7fe6f3953894f8a54844b8513d575081
7
+ data.tar.gz: 69cc606ed2aa26726ea61ffc38f123ebcec5e22fa946da0938e3fdb48dc9e1270cb475e3a98ed3e6cec5f7e3965aa98aafeb0b26fdd3d6e75195ea88309ae4fe
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/
11
+ .idea/*
12
+
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cave_gen.gemspec
4
+ gem 'rspec'
5
+
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,3 @@
1
+ == GameRuby
2
+
3
+ Put appropriate LICENSE for your project here.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 nikitasmall
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ == GameRuby
2
+
3
+ You should document your project here.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # CaveGen
2
+
3
+ A little gem to create a random caves. For your games (roguelikes or similar) or just for fun.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cave_gen'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cave_gen
20
+
21
+ ## Usage
22
+
23
+ It is very easy to create your own caves!
24
+
25
+ ```ruby
26
+ cave = CaveGen::Cave.new(height, width, error, opacity)
27
+ cave.show_map
28
+ ```
29
+ height and width is a numbers of rows and columns in your array.
30
+ error is a numbers of attempts to continue build cave from another place on map.
31
+ opacity is a number that shows curlicue of your cave. The higher number makes simpler cave.
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/nikitasmall/cave_gen/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cave_gen"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/cave_gen.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cave_gen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cave_gen"
8
+ spec.version = CaveGen::VERSION
9
+ spec.authors = ["nikitasmall"]
10
+ spec.email = ["nikitasosnov@yahoo.com"]
11
+
12
+ spec.summary = "A little gem to create a random caves. For your games (roguelikes or similar) or just for fun."
13
+ spec.homepage = "https://github.com/NikitaSmall/cave_gen.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ #if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
23
+ #end
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.8"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 2.6"
28
+ end
data/lib/cave_gen.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'cave_gen/version'
2
+ require 'cave_gen/map'
3
+
4
+ module CaveGen
5
+ class Cave
6
+ def initialize(height = 20, width = 20, error = 100, opacity = 7)
7
+ @map = Map.new(height, width, error, opacity)
8
+ @map.generate_map
9
+
10
+ @map
11
+ end
12
+
13
+ def show_map
14
+ @map.show_map
15
+ end
16
+
17
+ def get_string
18
+ @map.to_s
19
+ end
20
+
21
+ def to_symbol_array
22
+ @map.to_symbol_array
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,148 @@
1
+ require_relative 'tile'
2
+
3
+ # класс карты, в котором и происходит всё веселье
4
+ class Map
5
+
6
+ # изначально карта принимает два параментра для инициализации - высоты и ширину, образуя массив
7
+ # каждый элемент массива - тайл. Счётчик остановки используется, чтобы не делать бесконечного цикла
8
+ def initialize(height = 20, width = 20, error = 100, opacity = 7)
9
+ @height = height
10
+ @width = width
11
+ @stop = 0
12
+ @error = error
13
+ @opacity_challenge = opacity
14
+
15
+ rows, cols = @width, @height
16
+
17
+ @grid = Array.new(cols)
18
+ cols.times do |y|
19
+ @grid[y] = Array.new(rows)
20
+ rows.times do |x|
21
+ @grid[y][x] = Wall.new(y, x)
22
+ end
23
+ end
24
+ end
25
+
26
+ # показываем всю карту
27
+ def show_map
28
+ @grid.each do |y|
29
+ y.each do |x|
30
+ x.print_tile
31
+ end
32
+ print "\n"
33
+ end
34
+ end
35
+
36
+ # метод содания карты. Берётся случайная первая точка, после чего выбирается направление и осуществляется проверка
37
+ # на возможность проложить пол. Циклично, до тех пор, пока не накопится счётик остановки
38
+ def generate_map
39
+
40
+ @curr_point = get_rand_point
41
+ while end_generation? do
42
+ check_point_to_floor(@curr_point[0], @curr_point[1])
43
+ @curr_point = get_next_point(@curr_point[0], @curr_point[1])
44
+ end
45
+ end
46
+
47
+ # выбор первой случайной точки
48
+ def get_rand_point
49
+ @rand_point = [rand(1..@height-2), rand(1..@width-2)]
50
+ end
51
+
52
+ # проверка точки - может ли она быть полом (считается по окружению)
53
+ # вне зависимости от результата, после проверки точка будет считаться "использованной", т.е. на неё больше не пойдут
54
+ def check_point_to_floor(y_coord, x_coord)
55
+ @grid[y_coord][x_coord].touched = 1
56
+ (y_coord-1..y_coord+1).each do |y|
57
+ (x_coord-1..x_coord+1).each do |x|
58
+ if @grid[y][x].is_floor?
59
+ @grid[y_coord][x_coord].opacity += 1
60
+ end
61
+ end
62
+ end
63
+ if @grid[y_coord][x_coord].opacity < @opacity_challenge
64
+ @grid[y_coord][x_coord] = Floor.new(y_coord, x_coord, @grid[y_coord][x_coord].opacity)
65
+ end
66
+ end
67
+
68
+ # алгоритм выбора следующей точки. Место наибольшей сложности
69
+ def get_next_point(y_coord, x_coord)
70
+ while end_generation? do
71
+ directions = Array.new
72
+ if y_coord - 1 > 0
73
+ if @grid[y_coord - 1][x_coord].touched != 1
74
+ directions << 0
75
+ end
76
+ end
77
+ if x_coord + 1 < @width - 1
78
+ if @grid[y_coord][x_coord + 1].touched != 1
79
+ directions << 1
80
+ end
81
+ end
82
+ if y_coord + 1 < @height - 1
83
+ if @grid[y_coord + 1][x_coord].touched != 1
84
+ directions << 2
85
+ end
86
+ end
87
+ if x_coord - 1 > 0
88
+ if @grid[y_coord][x_coord - 1].touched != 1
89
+ directions << 3
90
+ end
91
+ end
92
+
93
+ unless directions.empty?
94
+ direction = directions.sample
95
+ else
96
+ y_coord, x_coord = get_rand_point
97
+ @stop += 10
98
+ next
99
+ end
100
+ case direction
101
+ when 0
102
+ return [y_coord - 1, x_coord]
103
+ when 1
104
+ return [y_coord, x_coord + 1]
105
+ when 2
106
+ return [y_coord + 1, x_coord]
107
+ when 3
108
+ return [y_coord, x_coord - 1]
109
+ end
110
+
111
+ end
112
+ end
113
+
114
+ # метод проверки генерации. Завершает её, если число остановки (количества ошибок) стало слишком большим.
115
+ def end_generation?
116
+ if @stop < @error
117
+ return true
118
+ else
119
+ return false
120
+ end
121
+ end
122
+
123
+ def is_floor?(y_coord, x_coord)
124
+ return @grid[y_coord][x_coord].is_floor?
125
+ end
126
+
127
+ def to_s
128
+ string = ''
129
+ @grid.each do |y|
130
+ y.each do |x|
131
+ string = string + x.get_tile
132
+ end
133
+ string = string + "\n"
134
+ end
135
+ return string
136
+ end
137
+
138
+ def to_symbol_array
139
+ string = Array.new
140
+ @grid.each_with_index do |y, y_index|
141
+ y.each_with_index do |x, x_index|
142
+ string[y_index][y_index] = x.get_tile
143
+ end
144
+ end
145
+ return string
146
+ end
147
+
148
+ end
@@ -0,0 +1,51 @@
1
+ class BaseTile
2
+ attr_accessor :x
3
+ attr_accessor :y
4
+ attr_accessor :opacity
5
+ attr_accessor :touched
6
+ attr_accessor :symbol
7
+
8
+ def initialize(y, x, opacity = 0, touched = 0)
9
+ @y = y
10
+ @x = x
11
+ @touched = touched
12
+ @opacity = opacity
13
+ @symbol = " "
14
+ end
15
+
16
+ def print_tile
17
+ print @symbol
18
+ end
19
+
20
+ def get_tile
21
+ @symbol
22
+ end
23
+
24
+ def is_floor?
25
+ return false
26
+ end
27
+
28
+ end
29
+
30
+ # Класс Тайла. Описывается двумя возможными свойствами - пол это (или стены) и окружение другими тайлами
31
+ # Число окружения повышается, когда вокруг этого тайла находятся тайлы, которые "полы".
32
+ class Floor < BaseTile
33
+
34
+ def initialize(y, x, opacity = 0, touched = 0)
35
+ super(y, x, opacity, touched)
36
+ @symbol = "."
37
+ end
38
+
39
+ def is_floor?
40
+ return true
41
+ end
42
+
43
+ end
44
+
45
+ class Wall < BaseTile
46
+
47
+ def initialize(y, x, opacity = 0, touched = 0)
48
+ super(y, x, opacity, touched)
49
+ @symbol = "#"
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module CaveGen
2
+ VERSION = "0.1.1"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cave_gen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - nikitasmall
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ description:
56
+ email:
57
+ - nikitasosnov@yahoo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE
66
+ - LICENSE.txt
67
+ - README
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - cave_gen.gemspec
73
+ - lib/cave_gen.rb
74
+ - lib/cave_gen/map.rb
75
+ - lib/cave_gen/tile.rb
76
+ - lib/cave_gen/version.rb
77
+ homepage: https://github.com/NikitaSmall/cave_gen.git
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: A little gem to create a random caves. For your games (roguelikes or similar)
101
+ or just for fun.
102
+ test_files: []