查看: 2450|回复: 5

[已经解决] 能不能每次进入地图让npc随机出现在指定的区域

[复制链接]
梦石
0
星屑
70
在线时间
5 小时
注册时间
2022-10-30
回帖
4
发表于 2022-10-31 23:12:11 | 显示全部楼层 |阅读模式

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

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

×
做了一个踢人功能,踢掉的队员会出现在一个住宅地图里,然后可以再次招募
现在的问题是每次进入这个地图
npc出现的初始位置都是确定的,感觉比较僵硬
能不能实现每次进入地图npc的位置随机出现在指定区域
梦石
0
星屑
70
在线时间
5 小时
注册时间
2022-10-30
回帖
4
 楼主| 发表于 2022-11-1 00:06:56 来自手机 | 显示全部楼层
有一个想法就是,每次进地图事件触发一个事件,事件把每个npc传送到其他位置,但是传送指令怎么指定指定的可通行区域的随机坐标呢?
回复

使用道具 举报

梦石
0
星屑
70
在线时间
5 小时
注册时间
2022-10-30
回帖
4
 楼主| 发表于 2022-11-1 00:17:26 来自手机 | 显示全部楼层
本帖最后由 leboya 于 2022-11-1 00:19 编辑

每次进入地图触发事件,从一个包含了指定区域的坐标集合中随机取坐标,取npc个数的不重复坐标,然后执行npc传送指令,参数就是这些不重复的坐标。现在的问题就是如何方便的获得指定的坐标集合,一个一个手动输入太麻烦了而且修改地图就会导致要重改。能不能通过刷敌群的区域刷子,直接获取比如43号区域的坐标集合,作为设定的区域的坐标集合?
回复

使用道具 举报

梦石
0
星屑
64
在线时间
3 小时
注册时间
2022-10-28
回帖
7
发表于 2022-11-1 03:56:45 | 显示全部楼层
关于切换地图时NPC的随机位置,我自己的方法是设置一个随机数,1-10,大于小于某些值时,NPC出现在哪些位置。
不过若是不喜欢NPC固定位置的单调性的话,也可以设置NPC随机行走,而有些NPC固定在某些位置。
固定位置的NPC可以根据人物设定灵活设置,比如性格暴烈的可以打沙袋,生性恬静的可以看书,胆小畏生的可以在墙角自闭或装死之类的,,,
歪比八卜,歪比歪比
回复

使用道具 举报

梦石
0
星屑
29901
在线时间
5734 小时
注册时间
2016-3-8
回帖
1653
发表于 2022-11-1 08:38:10 | 显示全部楼层
本帖最后由 alexncf125 于 2022-11-1 08:40 编辑

Give NPCs Random Positions When Entering a Map => 进入地图时给 NPC 随机位置 => 完全符合你的要求 => RM学习交流群:632269521
[pre lang="ruby"]#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#             Give NPCs Random Positions When Entering a Map
#             Author: DiamondandPlatinum3
#=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#  Description:
#
#    This script allows you to set certain NPCs to have random positions
#    when the game is setting up a map, this allows your NPCs to look somewhat
#    less robot like. Whilst this can be done via events, this script also
#    checks tile passability, so your NPCs won't end up on a roof if they're
#    not supposed to.
#
#    Included is an event switch that turns off this feature, the purpose of
#    turning off this feature is for cutscenes, wouldn't want a random NPC to
#    be in your way now would we?
#
#    You may also use Regions to give your NPCs specific tiles they can
#    randomise to.
#
#------------------------------------------------------------------------------
#  Instructions:
#
#  ~  To give an NPC the ability to appear in random locations on the map, all
#     you have to do is edit their event name to include either:
#     <Random>, <RANDOM>, <random>, <RaNdOm>
#
#
#  ~  If you wish to give the NPC a specific Region ID it can only randomise to
#     you must add another tag just after the above with:
#         <Region: ?>                     <= For Singular Regions
#         <Region: [?, ?, ?, etc]>        <= For Multiple Regions
#     Replacing the Question Mark(s) with a Valid Region ID(s).
#
#
#  ~  Similarly you can also use the value of a variable to determine the
#     RegionID if you'd prefer. To do so you must enter:
#         <Region: \v[?]>                 <= For Singular Variables Containing a Region ID
#         <Region: \v[?, ?, ?, etc]>      <= For Multiple Variables Containing a Region ID
#     Replacing the Question Mark(s) with a Variable ID(s).
#
#
#  ~  You can also a Tag to block any region(s).
#       This will alow the event to randomise to any tile that does NOT
#       contain this Region ID. To do so you must enter:
#         <RegionBlock: ?>                <= For Singular Regions to Block
#         <RegionBlock: [?, ?, ?, etc]>   <= For Multiple Regions to Block
#
#       You can also use variables with the RegionBlock the same as above:
#         <RegionBlock: \v[?]>            <= For Singular Variables Containing a Region ID
#         <RegionBlock: \v[?, ?, ?, etc]> <= For Multiple Variables Containing a Region ID
#
#
#  ~  You may also use the first comment in their event list to do the same
#     as above if you do not wish to edit the name.
#
#
#
#  ~  Example ScreenShots:
#       NameTag:     http://i.imgur.com/gIGpPaz.png
#       CommentTag:  http://i.imgur.com/GIxiT08.png
#
#       NameTag with Region:      http://i.imgur.com/HRMpiNx.png
#       CommentTag with Region:   http://i.imgur.com/D2bz6Ed.png
#
#       NameTag with Region Variable:     http://i.imgur.com/3Y4JKT0.png
#       CommentTag with Region Variable:  http://i.imgur.com/kIjN9qb.png
#
#       Multiple Regions:             http://i.imgur.com/mvBykmp.png
#       Multiple Region Variables:    http://i.imgur.com/5yru4Xc.png
#
#       Block Regions:    http://i.imgur.com/bN2zVY3.png
#
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



#==============================================================================
# ** Game_Map
#------------------------------------------------------------------------------
#  This class handles maps. It includes scrolling and passage determination
# functions. The instance of this class is referenced by $game_map.
#==============================================================================

class Game_Map
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * Editable Region
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  
  # Turn This Event Switch Off To Stop Random Positioning, use for cutscenes
  TURN_OFF_RANDOM_POSITIONING_EVENT_SWITCH_ID = 10
  
  # The problem with Randomising is that it will not always get you a good
  # position every single time, or possibly the next few times. This can cause
  # lag if the randomiser keeps picking awkward positions, so here you can set
  # the maximum allowable rejections per NPC. Should the Randomiser exceed this
  # limit, your NPC will stay in its original position
  MAXIMUM_POSITION_REJECTIONS = 50
  #--------------------------------------------------------------------------
  
  
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * Struct Setup
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  DP3MapRegionStruct                  = Struct.new(:region_id, :x, :y)
  DP3RandomRegionRegex                = /<Region:\s?(\[?(?:\d+,?\s?)+\]?)>/im
  DP3RandomRegionBlockRegex           = /<RegionBlock:\s?(\[?(?:\d+,?\s?)+\]?)>/im
  DP3RandomRegionVariableRegex        = /<Region:\s?\\v(\[(?:\s?\d+,?\s?)+\])>/im
  DP3RandomRegionBlockVariableRegex   = /<RegionBlock:\s?\\v(\[(?:\s?\d+,?\s?)+\])>/im
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # *= Alias Listings
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  alias_method(:dp3_gamemap_setup_f98ha,           :setup )
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * Aliased Method: Setup
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def setup( *args )
    # Call Original Method
    dp3_gamemap_setup_f98ha( *args )
   
    # Call New Method
    dp3_randomise_event_positions() if $game_switches[TURN_OFF_RANDOM_POSITIONING_EVENT_SWITCH_ID]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Randomise Event Positions
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_randomise_event_positions()  
    tag = /<RANDOM>/im
    @events.each_key do |key|
      event = @events[key]
      if dp3_get_event_first_comment(event) =~ tag || event.dp3_get_event_name() =~ tag
        dp3_setupregionshash()
        reg_id = dp3_check_event_for_region_tag(event)
        if reg_id[0]
          dp3_randomise_event_position_based_on_blocked_regions( event, reg_id[1] )
        elsif reg_id[1] > 0
          dp3_randomise_event_position_based_on_region( event, reg_id[1] )
        else
          dp3_randomise_event_position_based_on_passability( event )
        end
      end   
    end   
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Setup Regions Hash
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_setupregionshash()
    return if !@dp3_regions_hash.nil? && @dp3_regions_hash[:map_id] == @map_id # No need to do it again if we've already got it
    dp3_create_regions_hash()
    dp3_populate_regions_hash()
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Create Regions Hash
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_create_regions_hash()
    @dp3_regions_hash.clear if @dp3_regions_hash
    @dp3_regions_hash = {}
    @dp3_regions_hash[:map_id] = @map_id
    for i in 1..63
      @dp3_regions_hash = []
    end
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Populate Regions Hash
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_populate_regions_hash()
    for x in 0..(self.width)
      for y in 0..(self.height)
        reg_id = region_id( x, y )
        next if reg_id == 0
        @dp3_regions_hash[reg_id].push(DP3MapRegionStruct.new( reg_id, x, y ))
      end
    end
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Get Active Regions
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_get_active_regions()
    active_regions = Array.new()
    @dp3_regions_hash.each_key do |reg_id|
      if @dp3_regions_hash[reg_id].is_a?(Array)
        active_regions.push(reg_id) unless @dp3_regions_hash[reg_id].empty?
      end
    end
    return active_regions
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method: Get Event All Comments
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_get_event_first_comment( event )
    comment = ""
    unless event.list.nil?
      if event.list[0].code == 108
        event.list.each do |command|
          break if command.code != 108 && command.code != 408
          comment += command.parameters[0]
        end
      end
    end
    return comment
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Check Event For "Region Tag
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_check_event_for_region_tag( event )
    tag         = /<Region/im
    comment_tag = dp3_get_event_first_comment(event)
   
    match_tag = ""
    match_tag = comment_tag                 if comment_tag =~ tag
    match_tag = event.dp3_get_event_name()  if match_tag == "" && event.dp3_get_event_name() =~ tag
   
    if match_tag != ""
      methods = dp3_get_region_regex_checking_methods()
      methods.each do |method|
        result = method.call(match_tag)
        return result if result[1].is_a?(Array) || result[1] != 0
      end
    end
    return [false, 0]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Get Region Regex Checking Methods
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_get_region_regex_checking_methods()
    return [      method(:dp3_check_region_regex),
                  method(:dp3_check_regionvariable_regex),
                  method(:dp3_check_regionblock_regex),
                  method(:dp3_check_regionblockvariable_regex),     ]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Check Region Regex
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_check_region_regex(tag)
    if tag =~ DP3RandomRegionRegex
      region_ids = $1.scan(/\d+/).collect{ |i| i.to_i }
      return [false, region_ids[rand(region_ids.size)]]
    end
    return [false, 0]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Check Region Variable Regex
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_check_regionvariable_regex(tag)
    if tag =~ DP3RandomRegionVariableRegex
      region_ids = $1.scan(/\d+/).collect{ |i| i.to_i }
      return [false, $game_variables[region_ids[rand(region_ids.size)]]]
    end
    return [false, 0]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Check Region Block Regex
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_check_regionblock_regex(tag)
    if tag =~ DP3RandomRegionBlockRegex
      block_region_ids = $1.scan(/\d+/).collect{ |i| i.to_i }
      return [true, block_region_ids]
    end
    return [false, 0]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Check Region Block Variable Regex
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_check_regionblockvariable_regex(tag)
    if tag =~ DP3RandomRegionBlockVariableRegex
      block_region_ids = $1.scan(/\d+/).collect{ |i| $game_variables[i.to_i] }
      return [true, block_region_ids]
    end
    return [false, 0]
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Standable Position?
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_standableposition?( event, x, y )
    return false unless self.valid?(x, y)
    return true if event.through
    return self.airship_land_ok?( x, y ) && !event.collide_with_characters?(x, y)
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Randomise Event Position Based on Passability
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_randomise_event_position_based_on_passability( event )
    x = y = -1;
    rejection_count = 0
    while (!dp3_standableposition?( event, x, y )) do
      return if rejection_count == MAXIMUM_POSITION_REJECTIONS
      rejection_count += 1
      x = rand(self.width)
      y = rand(self.height)
    end
    event.moveto( x, y )
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Randomise Event Position Based on Region
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_randomise_event_position_based_on_region( event, reg_id )
    return if @dp3_regions_hash[reg_id].empty?
   
    x = y = -1;
    rejection_count = 0
    while (!dp3_standableposition?( event, x, y )) do
      return if rejection_count == MAXIMUM_POSITION_REJECTIONS
      rejection_count += 1
      map_pos = @dp3_regions_hash[reg_id][rand(@dp3_regions_hash[reg_id].size)]
      x = map_pos.x
      y = map_pos.y
    end
    event.moveto( x, y )
  end
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method:   Randomise Event Position Based on Blocked Regions
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_randomise_event_position_based_on_blocked_regions( event, blocked_regions )
    x = y = -1;
    current_region_id = 0
    rejection_count = 0
    while (!dp3_standableposition?( event, x, y ) || blocked_regions.include?(current_region_id)) do
      return if rejection_count == MAXIMUM_POSITION_REJECTIONS
      rejection_count += 1
      x = rand(self.width)
      y = rand(self.height)
      current_region_id = region_id(x, y)
    end
    event.moveto( x, y )
  end
end

#==============================================================================
# ** Game_Event
#------------------------------------------------------------------------------
#  This class handles events. Functions include event page switching via
# condition determinants and running parallel process events. Used within the
# Game_Map class.
#==============================================================================

class Game_Event < Game_Character
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # * New Method: Get Original Event Name
  #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def dp3_get_event_name()
    return @event.name
  end
end[/pre]
回复

使用道具 举报

梦石
0
星屑
70
在线时间
5 小时
注册时间
2022-10-30
回帖
4
 楼主| 发表于 2022-11-2 20:31:53 | 显示全部楼层
alexncf125 发表于 2022-11-1 08:38
Give NPCs Random Positions When Entering a Map => 进入地图时给 NPC 随机位置 => 完全符合你的要求 => R ...

谢谢大佬!这时候才看到
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部