查看: 3325|回复: 1

[已经过期] 自动将图像转为RM地图

[复制链接]
梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
发表于 2020-4-26 09:34:09 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 alexncf125 于 2024-1-25 01:46 编辑

<自动将图像转为RM地图>

  1. # PNG 保存 (CACAO [url]http://cacaosoft.web.fc2.com/[/url])
  2. class Bitmap
  3.   def save_png(filename, alpha = false)
  4.     sgnt = "\x89PNG\r\n\x1a\n"
  5.     ihdr = _chunk('IHDR', [width,height,8,(alpha ? 6 : 2),0,0,0].pack('N2C5'))
  6.     data = []
  7.     height.times do |y|
  8.       data << 0
  9.       width.times do |x|
  10.         color = self.get_pixel(x, y)
  11.         data << color.red << color.green << color.blue
  12.         data << color.alpha if alpha
  13.       end
  14.     end
  15.     idat = _chunk('IDAT', Zlib::Deflate.deflate(data.pack('C*')))
  16.     iend = _chunk('IEND', "")
  17.     File.open(filename, 'wb') do |file|
  18.       file.write(sgnt)
  19.       file.write(ihdr)
  20.       file.write(idat)
  21.       file.write(iend)
  22.     end
  23.   end
  24. private
  25.   def _chunk(name, data)
  26.     return [data.size, name, data, Zlib.crc32(name + data)].pack('NA4A*N')
  27.   end
  28. end

  29. mapid = 1
  30. # 地图 ID

  31. filename = "pic"
  32. # 转换图片文件名

  33. power = 12
  34. # 初始压缩质量(值越大速度越快,生成的图像质量越差)

  35. speed = 1
  36. # 压缩粗糙程度(值越大速度越快,生成的图像质量越差)

  37. tilesetid = 6
  38. # 新地图使用的图块,为0新建图块,否则修改原图块

  39. tilename = ["tileB","tileC","tileD","tileE"]
  40. # 图块素材文件名

  41. def data_reset(map)
  42.   $tilemap = map.data
  43.   $color_list = [Color.new(255,255,255,0)]
  44. end
  45. def add_color(color)
  46.   index = $color_list.index(color)
  47.   return index if index
  48.   $color_list.push color
  49.   return $color_list.size - 1
  50. end
  51. def change_color(red,green,blue,power)
  52.   return Color.new( (red.to_i / power) * power, ( green.to_i / power ) * power,
  53.     ( blue.to_i / power) * power, 255)
  54. end
  55. def get_bitmap_data(map,bitmap,need_change = false,power = 2,speed = 2)
  56.   data_reset(map)
  57.   error = false
  58.   catch :get_data_normal do
  59.     map.width.times do |x|
  60.       map.height.times do |y|
  61.         color = bitmap.get_pixel(x, y)
  62.         if need_change
  63.           color = change_color(color.red, color.green, color.blue, power)
  64.         end
  65.         index = add_color(color)
  66.         if index >= 1024
  67.           p "处理失败,对画质进行压缩后重新开始"
  68.           error = true
  69.           throw :get_data_normal
  70.         end
  71.         $tilemap[x,y,2] = index
  72.         $tilemap[x,y,0],$tilemap[x,y,1],$tilemap[x,y,3] = 2048,0,16
  73.       end
  74.       p "正在处理 #{x} / #{map.width}"
  75.     end
  76.   end #catch
  77.   get_bitmap_data(map, bitmap, true, power + speed, speed ) if error
  78.   msgbox("转换结束,正在生成地图,当前压缩度为#{power}") unless error
  79. end
  80. def save_bitmap_to_file(name)
  81.   tile_bitmap1 = Bitmap.new(512,512)
  82.   tile_bitmap2 = Bitmap.new(512,512)
  83.   tile_bitmap3 = Bitmap.new(512,512)
  84.   tile_bitmap4 = Bitmap.new(512,512)
  85.   $color_list.each_with_index do |color,index|
  86.     tile_id = index / 256
  87.     index = index % 256
  88.     x = index % 8
  89.     y = index / 8
  90.     if index >= 128
  91.       x += 8; y -= 16
  92.     end
  93.     bitmap = [tile_bitmap1,tile_bitmap2,tile_bitmap3,tile_bitmap4]
  94.     bitmap[tile_id].fill_rect(x * 32, y * 32, 32, 32, color)
  95.   end
  96.   tile_bitmap1.save_png("Graphics/Tilesets/#{name[0]}.png", true)
  97.   tile_bitmap2.save_png("Graphics/Tilesets/#{name[1]}.png", true)
  98.   tile_bitmap3.save_png("Graphics/Tilesets/#{name[2]}.png", true)
  99.   tile_bitmap4.save_png("Graphics/Tilesets/#{name[3]}.png", true)
  100. end
  101. map = load_data(sprintf("Data/Map%03d.rvdata2", mapid))
  102. tileset = load_data("Data/Tilesets.rvdata2")
  103. bitmap = Bitmap.new(filename)
  104. get_bitmap_data(map, bitmap, false, power.to_i, speed.to_i)
  105. save_bitmap_to_file(tilename)
  106. if !tileset[tilesetid]
  107.   tilesetid = tileset.size
  108.   tileset[tilesetid] = tileset[1].clone
  109. end
  110. tileset[tilesetid].tileset_names[5, 4] = tilename
  111. map.data = $tilemap
  112. map.tileset_id = tilesetid
  113. save_data(map,sprintf("Data/Map%03d.rvdata2", mapid))
  114. save_data(tileset,"Data/Tilesets.rvdata2")
  115. exit
复制代码

比如现在这張图
map.jpg
重命名成pic.png后,新建一个工程
把它扔去和Game.exe同样的位置
結果生成了以下的東西
tileB.png tileC.png tileD.png tileE.png

把power = 12和speed = 1和tilesetid = 6全改成0都是一样的結果

评分

参与人数 1赞 +1 收起 理由
PLeaseS + 1 tilename = [&amp;quot;tileB&amp;quot;,&amp;quot;tile

查看全部评分

梦石
0
星屑
30103
在线时间
5741 小时
注册时间
2016-3-8
回帖
1662
 楼主| 发表于 2020-4-28 11:41:36 | 显示全部楼层
本帖最后由 alexncf125 于 2024-1-5 22:41 编辑

经过一轮实测,脚本的用途是
把图像的每1x1像素转为RM地图的1格图块
简单来说,这脚本好像沒啥大用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部