-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLmu.rb
More file actions
141 lines (129 loc) · 3.37 KB
/
Lmu.rb
File metadata and controls
141 lines (129 loc) · 3.37 KB
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
# RPGツクール2000のマップデータをRubyオブジェクトに変換する(1)
# テスト環境はRPGツクールVX Ace
# RPGツクール2000の正規ユーザー以外の人が使うと怒られると思います
#
# 16進文字列とバイト列を相互変換する(ruby)
# http://techracho.bpsinc.jp/baba/2011_08_23/4403
# バイナリから配列番号を見るために使っているクラスです
# 値を評価する際には使えないので注意
#
class String
def hex2bin
s = self
raise "Not a valid hex string" unless(s =~ /^[\da-fA-F]+$/)
s = '0' + s if((s.length & 1) != 0)
s.scan(/../).map{ |b| b.to_i(16) }.pack('C*')
end
def bin2hex
self.unpack('C*').map{ |b| "%02X" % b }.join('')
end
# 以下はおまけ 十進法に直したい時(そのまんま)
def bin2dec
self.unpack('C*').join
end
# リトルエンディアンの場合
def bin2dec2
self.unpack("S*").join
end
end
#
# 2000のマップデータのクラス
# 大いに参考にしたもの
# RPG2000/マップデータ
# http://rpg2kdev.sue445.net/?RPG2000%2F%A5%DE%A5%C3%A5%D7%A5%C7%A1%BC%A5%BF
#
class Lmu
attr_reader :header
attr_reader :chipset_id
attr_reader :width
attr_reader :height
attr_reader :ary_map1
attr_reader :ary_map2
def initialize(filename)
@f = File.open(filename, "rb")
# ヘッダ 0x0A("LcfMapUnit"の文字列長) + "LcfMapUnit"
# LMUを正しく読む限りは11バイト固定だが応用のためにこうしようね!
@index = @f.read(1).bin2dec
@header = @f.read(@index.to_i)
# @indexに配列番号を入れる
@index = @f.read(1).bin2hex
# どんどんデータ入れる
@chipset_id = set_chipset(@index)
@width = set_width(@index)
@height = set_height(@index)
@scrolltype = set_scrolltype(@index)
@index = @f.read(1).bin2hex
set_parallax(@index) if @index == "IF"
# 下層マップ
@ary_map1 = set_map(@width * @height)
# 上層マップ
@index = @f.read(1).bin2hex
@ary_map2 = set_map(@width * @height)
# あとは面倒なのでおしまい
@f.close
p "Lmu正常終了"
end
#
# バイナリの先頭からBER整数を取得する
#
def get_ber_int(f)
j = f.read(1).bin2dec.to_i
i = f.read(j).unpack("w").join
return i
end
def set_chipset(index)
if index == "01"
get_ber_int(@f)
@index = @f.read(1).bin2hex
return i.to_i
else
return 1
end
end
def set_width(index)
if index == "02"
i = get_ber_int(@f)
@index = @f.read(1).bin2hex
return i.to_i
else
return 20
end
end
def set_height(index)
if index == "03"
i = get_ber_int(@f)
@index = @f.read(1).bin2hex
return i.to_i
else
return 15
end
end
def set_scrolltype(index)
if index == "0B"
i = get_ber_int(@f)
return i.to_i
else
p @index
p "スクロールタイプ:解析エラー"
return 0
end
end
def set_parallax(index)
# ここで遠景処理書く
end
def set_map(length)
ary_map = []
if @index == "47" || @index == "48"
i = [@width * @height * 2].pack("w").length
@f.read(i)
for i in 0...(length)
str = @f.read(2)
ary_map.push str
end
else
p @index
p "マップ構造:解析エラー"
end
return ary_map
end
end