Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NPC Interactions
#1
NPC Interactions
by Near Fantastica
Jul 12, 2005

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


Features:

First off I know this script is going to confuse more of you then I would like but that’s the way it has to be just sit down and think about it for a second... About what it is, what it dose and how it works... I am sure you will get it think of Morrowind NPC interaction system...

This script works in the following way...
Quote:1. The script starts a scene...

2. It opens the text file you tell it too or if you leave it blank opens the file with the event name...

3. Displays the text in the window and add any keywords to the menus list key words are defined in the text file and display as a different color...

4. When you select and new item form the list the script opens and displays that text file repeating step 3 over again...


Instructions:

Keywords:
Keywords are linking words the name of another text file you want to link to for more information about a topic a keyword is placed with square brackets and any spaces are replaced by underscores...
Quote:[Key_Words]
[NPC]
[Interactions]

Text Files
The text files need to be saved with the ending .rxdata and the name should be in all CAPS... As well is the keyword uses an underscore the file name dose not it uses a space... Also to open and read .rxdata file do an open with notepad... Don’t forget to make an NPC Interactions folder which the text files are placed in...

Commands:
Code:
$scene = Scene_NPC_Interaction.new(self, "START")


Self is a most so don't remove it
"Start" is the name of the first text you want to call if start is left out then it takes the event name and displays that file¦
Code:
$scene = Scene_Player_Log.new


Script:

Code:
#======================================
# ¦ NPC Interactions
#======================================
#  By: Near Fantastica
#   Date: 17.06.05
#   Version: 1
#======================================
#
#================================
# ¦ Player Log
#================================

class Player_Log
#--------------------------------------------------------------
attr_accessor :keys
#--------------------------------------------------------------
def initialize
   @keys = []
end
#--------------------------------------------------------------
end

#================================
# ¦ Scene Player Log
#================================

class Scene_Player_Log
#--------------------------------------------------------------
def initialize
   @keys = []
   @replaces = []
   @text = ""
   @list = $player_log.keys
   file = $player_log.keys[0]
   file.upcase!
   setup(file)
end
#--------------------------------------------------------------
def setup(file)
   @list.push(file) if not @list.include?(file)
   @keys = []
   @replaces = []
   @file = file + ".rxdata"
   @text = IO.readlines("NPC Interactions/#{@file}")
   find_keys
   find_text
end
#--------------------------------------------------------------
def find_keys
   for line in @text
     x = @text.index(line)
     words = line.split
     for word in words
       if word.include?("[") and word.include?("]")
         @replaces.push(x)
         word.delete!("[")
         word.delete!("]")
         word.gsub!(/_/, ' ')
         @keys.push(word)
         if not @list.include?(word)
           @list.push(word)
         end
       end
     end
   end
end
#--------------------------------------------------------------
def find_text
   for replace in @replaces
     line = @text[replace]
     line.delete!("[")
     line.delete!("]")
     line.gsub!(/_/, ' ')
     @text[replace] = line
   end
end
#--------------------------------------------------------------
def main
   @spriteset = Spriteset_Map.new
   @command_window = Window_Command.new(180, @list)
   @command_window.height = 480
   @command_window.opacity = 100
   @scroll_window = Window_Scroll.new(@keys, @replaces, @text)
   Graphics.transition
   loop do
     Graphics.update
     Input.update
     update
     if $scene != self
       break
     end
   end
   Graphics.freeze
   @spriteset.dispose
   @command_window.dispose
   @scroll_window.dispose
end
#--------------------------------------------------------------
def update
   @command_window.update
   @scroll_window.update
   if @command_window.active
     update_command
     return
   end
   if @scroll_window.active
     update_scroll
     return
   end
end
#--------------------------------------------------------------
def update_command
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     $scene = Scene_Map.new
     return
   end
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.decision_se)
     setup(@list[@command_window.index])
     @command_window.setup(@list)
     @scroll_window.y = 0
     @scroll_window.setup(@keys, @replaces, @text)
     @command_window.active = false
     @scroll_window.active = true
     return
   end
end
#--------------------------------------------------------------
def update_scroll
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     @command_window.active = true
     @scroll_window.active = false
     return
   end
   if Input.repeat?(Input::UP)
     return if @scroll_window.y == 0
     @scroll_window.y+=16
   end  
   if Input.repeat?(Input::DOWN)
     return if (@scroll_window.sy - 480) <= (@scroll_window.y * -1)
     @scroll_window.y-=16
   end  
end
end

#================================
# ¦ Scene Interaction
#================================

class Scene_NPC_Interaction
#--------------------------------------------------------------
def initialize(interpreter, file = nil)
   @interpreter = interpreter
   @name = @interpreter.event.name
   file = @name if file == nil
   @keys = []
   @replaces = []
   @text = ""
   @list = []
   file.upcase!
   setup(file)
end
#--------------------------------------------------------------
def setup(file)
   @list.push(file) if not @list.include?(file)
   @keys = []
   @replaces = []
   @file = file + ".rxdata"
   @text = IO.readlines("NPC Interactions/#{@file}")
   find_keys
   find_text
end
#--------------------------------------------------------------
def find_keys
   for line in @text
     x = @text.index(line)
     words = line.split
     for word in words
       if word.include?("[") and word.include?("]")
         @replaces.push(x)
         word.delete!("[")
         word.delete!("]")
         word.gsub!(/_/, ' ')
         @keys.push(word)
         if not @list.include?(word)
           @list.push(word)
         end
       end
     end
   end
end
#--------------------------------------------------------------
def find_text
   for replace in @replaces
     line = @text[replace]
     line.delete!("[")
     line.delete!("]")
     line.gsub!(/_/, ' ')
     @text[replace] = line
   end
end
#--------------------------------------------------------------
def main
   @spriteset = Spriteset_Map.new
   @name_window = Window_Name.new(@name)
   @command_window = Window_Command.new(180, @list)
   @command_window.y = 64
   @command_window.height = 416
   @command_window.opacity = 100
   @scroll_window = Window_Scroll.new(@keys, @replaces, @text)
   Graphics.transition
   loop do
     Graphics.update
     Input.update
     update
     if $scene != self
       break
     end
   end
   player_keys = $player_log.keys
   @list.shift
   npc_keys = @list
   temp = player_keys | npc_keys
   $player_log.keys = temp
   Graphics.freeze
   @spriteset.dispose
   @name_window.dispose
   @command_window.dispose
   @scroll_window.dispose
end
#--------------------------------------------------------------
def update
   @command_window.update
   @scroll_window.update
   if @command_window.active
     update_command
     return
   end
   if @scroll_window.active
     update_scroll
     return
   end
end
#--------------------------------------------------------------
def update_command
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     $scene = Scene_Map.new
     return
   end
   if Input.trigger?(Input::C)
     $game_system.se_play($data_system.decision_se)
     setup(@list[@command_window.index])
     @command_window.setup(@list)
     @scroll_window.y = 0
     @scroll_window.setup(@keys, @replaces, @text)
     @command_window.active = false
     @scroll_window.active = true
     return
   end
end
#--------------------------------------------------------------
def update_scroll
   if Input.trigger?(Input::B)
     $game_system.se_play($data_system.cancel_se)
     @command_window.active = true
     @scroll_window.active = false
     return
   end
   if Input.repeat?(Input::UP)
     return if @scroll_window.y == 0
     @scroll_window.y+=16
   end  
   if Input.repeat?(Input::DOWN)
     return if (@scroll_window.sy - 480) <= (@scroll_window.y * -1)
     @scroll_window.y-=16
   end  
end
end

#================================
# ¦ Window Scroll
#================================

class Window_Scroll < Window_Base
#--------------------------------------------------------------
attr_reader   :sy
#--------------------------------------------------------------
def initialize(keys, replaces, text)
   @sx = 460
   if text.size * 32 >= 480
     @sy = (text.size+1) * 32
   else
     @sy = 480
   end
   @keys = keys
   @replaces = replaces
   @text= text
   super(180, 0, @sx, @sy)
   self.opacity = 100
   self.contents = Bitmap.new(@sx - 32, @sy - 32)
   self.contents.font.name = $defaultfonttype
   self.contents.font.size = $defaultfontsize
   refresh
end
#--------------------------------------------------------------
def refresh
   self.contents.clear
   y=0
   for i in 0...@text.size
     if @replaces.include?(i)
       id = @replaces.index(i)
       words = @text[i].split
       lenght = 0
       for word in words
         key_words = @keys[id].split
         if key_words.include?(word)#if word == @keys[id]
           self.contents.font.color = text_color(6)
           self.contents.draw_text(lenght, y, @sx, 32, word)
           temp = self.contents.text_size(word)
           lenght += temp.width
           lenght += 8
         else
           self.contents.font.color = normal_color
           self.contents.draw_text(lenght, y, @sx, 32, word)
           temp = self.contents.text_size(word)
           lenght += temp.width
           lenght += 8
         end
       end
     else
       self.contents.font.color = normal_color
       self.contents.draw_text(0, y, @sx, 32, @text[i])
     end
     y+=32
   end
end
#--------------------------------------------------------------
def setup(keys, replaces, text)
   @keys = keys
   @replaces = replaces
   @text= text
   if @text.size * 32 >= 480
     @sy = (text.size+1) * 32
   else
     @sy = 480
   end
   self.height = @sy
   self.contents.dispose
   self.contents = Bitmap.new(@sx - 32, @sy - 32)
   self.contents.font.name = $defaultfonttype
   self.contents.font.size = $defaultfontsize
   refresh
end
end

#================================
# ¦ Window Name
#================================

class Window_Name < Window_Base
#--------------------------------------------------------------------------
def initialize(name)
   @name = name
   super(0, 0, 180, 64)
   self.contents = Bitmap.new(width - 32, height - 32)
   self.contents.font.name = $defaultfonttype  # "Gold" window font
   self.contents.font.size = $defaultfontsize
   self.opacity = 100
   refresh
end
#--------------------------------------------------------------------------
def refresh
   self.contents.clear
   self.contents.draw_text(0, 0, 160, 32, @name, 1)
end
end


#================================
# ¦ Window Command
#================================

class Window_Command < Window_Selectable
  #--------------------------------------------------------------
def setup(commands)
   @commands = commands
   @item_max = commands.size
   self.contents.dispose
   self.contents = Bitmap.new(width - 32, @item_max * 32)
   self.contents.font.name = $defaultfonttype
   self.contents.font.size = $defaultfontsize
   refresh
end
end

#======================================
# ¦ Interpreter
#======================================

class Interpreter
def event
   return $game_map.events[@event_id]
end
end

#======================================
# ¦ Game Event
#======================================

class Game_Event < Game_Character
#--------------------------------------------------------------------------
def name
   return @event.name
end
end

#======================================
# ¦ Scene Title
#======================================

class Scene_Title
#--------------------------------------------------------------------------
alias npc_scene_title_cng command_new_game
#--------------------------------------------------------------------------
def command_new_game
   $player_log = Player_Log.new
   npc_scene_title_cng
end
end

#======================================
# ¦ Scene Load
#======================================

class Scene_Load < Scene_File
#--------------------------------------------------------------------------
alias npc_scene_load_read_save_data read_save_data
#--------------------------------------------------------------------------
def read_save_data(file)
   npc_scene_load_read_save_data(file)
   $player_log = Marshal.load(file)
end
end

#======================================
# ¦ Scene Save
#======================================

class Scene_Save < Scene_File
#--------------------------------------------------------------------------
alias npc_scene_save_write_save_data write_save_data
#--------------------------------------------------------------------------
def write_save_data(file)
   npc_scene_save_write_save_data(file)
   Marshal.dump($player_log, file)
end
end

Take Care,
Near
}




Users browsing this thread: