Shadowrun Dialogue System

Shadowrun Dialogue System

Version: 1.4


Introduction

This script is a replica of the Shadowrun's dialogue system.

Features
  • Learn keywords from dialogues
  • Ask about a keyword you have learned
  • Different NPC's reactions to keywords
  • Actor/NPC faces (Optional)
  • BGM in the dialogue scene (Optional)
  • Different bgm depending on the npc
  • Switch operations through dialogues
  • Easy to use!
Screenshots
http://img161.imageshack.us/img161/9384 ... runoo5.jpg[/IMG]

Screenshot from the game:
http://img140.imageshack.us/img140/8785 ... 000vk4.jpg[/IMG]



Demo

None.

Script

Script Spoiler

#==============================================================================
# ** Shadowrun's Dialogue System
#------------------------------------------------------------------------------
#  Author: Dargor
#  Version 1.4
#  25/06/2007
#==============================================================================
# * Instructions
#------------------------------------------------------------------------------
# - Use $scene = Scene_Dialogue.new(npc_id, npc_name, actor_name, bgm_name)
#   to call the dialogue scene
# - \\k[string] will add 'string' to the keywords list
# - \\c[x] is the color code used in the default message system
# - \\s[id][flag] will change switch 'id' to 'flag'
#   (flag: 0 = false, 1 = true)
# - Use \n to jump to a new line of text
#------------------------------------------------------------------------------
# * Note
#------------------------------------------------------------------------------
# - You must keep a -start-, -talk- and -unknown- dialogue for each NPCs.
#==============================================================================
module Shadowrun
  # Faces options
  Faces = true
  Faces_Path = "Graphics/Faces/"
  # BGM options
  BGM = true
  BGM_Name = "054-Negative03"
  # NPCs
  Dialogues = []
  Dialogues[0] = "Hello sir.\n What can I do to help you?"
  Dialogues[1] = "I'm Event001 \n What else do you want to know?"
  Dialogues[2] = "Sorry, I can't help you with that."
  Dialogues[3] = "You should talk to \\c[4]\\k[Aluxes]\\c[0]. \n He is living near this town.  \n I'm sure he can help you."
  Dialogues[4] = "Yo dude \n What's up?"
  Dialogues[5] = "I'm the great Event002! \n I'm strong and sexy! \n But I will switch off switch #1... \\s[1][0]"
  Dialogues[6] = "I dunno..."
  Dialogues[7] = "I don't have any \\k[Weapons]. \n You can purchase it from a \\k[Shop]."
  Dialogues[8] = "There's nothing like that here!"
  Dialogues[9] = "Yeah, he's a nice guy.\\s[1][1]"
  Dialogues[10] = "He's playing with his \\k[Swords] \n all day long."
  Dialogues[11] = "If you can find me one, I'll reward you \n with a \\k[Little Surprise]."
  Dialogues[12] = "Just find me a sword!"
  NPC = []
  NPC[0] = {
    '-start-' => Dialogues[0],
    '-talk-' => Dialogues[1],
    '-unknown-' => Dialogues[2],
    'Weapons' => Dialogues[3],
    'Aluxes' => Dialogues[10],
    'Swords' => Dialogues[3]
    }
  NPC[1] = {
    '-start-' => Dialogues[4],
    '-talk-' => Dialogues[5],
    '-unknown-' => Dialogues[6],
    'Weapons' => Dialogues[7],
    'Shop' => Dialogues[8],
    'Aluxes' => Dialogues[9],
    'Swords' => Dialogues[11],
    'Little Surprise' => Dialogues[12]
    }
  NPC_Faces = []
  NPC_Faces[0] = 'lydia'
  NPC_Faces[1] = 'dude'
end
#==============================================================================
# ** Game_Party
#------------------------------------------------------------------------------
#  This class handles the party. It includes information on amount of gold
#  and items. Refer to "$game_party" for the instance of this class.
#==============================================================================

class Game_Party
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :keywords         # keywords
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias keywords_party_initialize initialize
  def initialize
    @keywords = ['Weapons']
    keywords_party_initialize
  end
  #--------------------------------------------------------------------------
  # * Add Keyword (string)
  #--------------------------------------------------------------------------
  def add_keyword(keyword)
    return if @keywords.include?(keyword)
    @keywords.push(keyword)
    @keywords.sort!
  end
  #--------------------------------------------------------------------------
  # * Remove Keyword (string)
  #--------------------------------------------------------------------------
  def remove_keyword(keyword)
    @keywords.delete(keyword)
    @keywords.sort!
  end
end
#==============================================================================
# ** Window_Base
#------------------------------------------------------------------------------
#  This class is for all in-game windows.
#==============================================================================

class Window_Base < Window
  #--------------------------------------------------------------------------
  # * Draw Face
  #     name  : filename
  #     x     : draw spot x-coordinate
  #     y     : draw spot y-coordinate
  #--------------------------------------------------------------------------
  def draw_face(name, x, y)
    bitmap = Bitmap.new(Shadowrun::Faces_Path+name)
    cw = bitmap.width
    ch = bitmap.height
    src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x, y, bitmap, src_rect)
  end
end
#==============================================================================
# ** Window_Keywords
#------------------------------------------------------------------------------
#  This window deals with general command choices.
#==============================================================================

class Window_Keywords < Window_Command
  #--------------------------------------------------------------------------
  # * Refresh Keywords
  #--------------------------------------------------------------------------
  def refresh_keywords
    @commands = $game_party.keywords
    @item_max = @commands.size
    self.contents = Bitmap.new(width - 32, @item_max * 32)
    refresh
    self.height = 32 + (32 * 5)
  end
end
#==============================================================================
# ** Window_Dialogue
#------------------------------------------------------------------------------
#  This message window is used to display NPC dialogues.
#==============================================================================

class Window_Dialogue < Window_Base
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  #--------------------------------------------------------------------------
  attr_accessor :text       # text
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(text,face, name)
    super(0,0,640,240)
    self.contents = Bitmap.new(width - 32, height - 32)
    @text = text
    @face = face
    @name = name
    refresh
  end
  #--------------------------------------------------------------------------
  # * Refresh Keywords
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.font.color = normal_color
    self.contents.draw_text(32,0,160,32,@name)
    x = 240
    y = 0
    text = @text.clone
    last_text = text.clone
    # Check for keywords, add them to keyword list
    last_text.gsub!(/\\[Kk]\[(.*?)\]/) do
      $game_party.add_keyword($1.to_s) unless $1 == nil
    end
    # Switch operation (Toogle switch)
    last_text.gsub!(/\\[Ss]\[(.*?)\]\[(.*?)\]/) do
      if $2.to_i == 0
        flag = false
      else
        flag = true
      end
      $game_switches[$1.to_i] = flag unless $1 == nil
      echo "Switch #{$1.to_i} = #{flag} \n"
    end
    # Control text processing
    begin
      last_text = text.clone
      text.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
    end until text == last_text
    text.gsub!(/\\[Nn]\[([0-9]+)\]/) do
      $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
    end
    # Change "\\\\" to "\000" for convenience
    text.gsub!(/\\\\/) { "\000" }
    # Change "\\C" to "\001" and "\\K" to "\003"
    text.gsub!(/\\[Cc]\[([0-9]+)\]/) { "\001[#{$1}]" }
    text.gsub!(/\\[Kk]\[(.*?)\]/) {"\003" + $1 + "\003"}
    text.gsub!(/\\[Ss]\[(.*?)\]\[(.*?)\]/) {""}
    # Get 1 text character in c (loop until unable to get text)
    while ((c = text.slice!(/./m)) != nil)
      # If \\
      if c == "\000"
        # Return to original text
        c = "\\"
      end
      # If \C[n]
      if c == "\001"
        # Change text color
        text.sub!(/\[([0-9]+)\]/, "")
        color = $1.to_i
        if color >= 0 and color <= 7
          self.contents.font.color = text_color(color)
        end
        # go to next text
        next
      end
      # If bold and italic
      if c == "\003"
        font = self.contents.font
        font.bold = !font.bold
        font.italic = !font.italic
      end
      # If new line text
      if c == "\n"
        # Add 1 to y
        y += 1
        x = 240 - self.contents.text_size(" ").width
        # go to next text
        next
      end
      # Draw text
      self.contents.draw_text(4 + x, 32 * y, 40, 32, c)
      # Add x to drawn text width
      x += self.contents.text_size(c).width
    end
    # Draw face
    draw_face(@face,32,32) if Shadowrun::Faces
  end
end
#==============================================================================
# ** Window_DialogueActions
#------------------------------------------------------------------------------
#  This message window is used to display dialogue actions.
#==============================================================================

class Window_DialogueActions < Window_Selectable
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize(name = "")
    super(0,240,640,240)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.active = true
    @item_max = 3
    @column_max = 1
    @index = 0
    @name = name
    refresh
  end
  #--------------------------------------------------------------------------
  # * Object Refresh
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    self.contents.draw_text(460,0,160,32,@name)
    # Set action array
    text = ["Talk", "Ask about...","Exit"]
    line = 0
    # Draw text
    text.each do |string|
      line += 1
      x = 32
      y = 20 + (line * 32)
      self.contents.draw_text(x,y,120,32,string.to_s)
    end
    # Draw_face
    draw_face("caleb",460,32) if Shadowrun::Faces
  end
  #--------------------------------------------------------------------------
  # * Update Cursor Rectangle
  #--------------------------------------------------------------------------
  def update_cursor_rect
    x = 24
    y = 52 + (@index % 3) * 32
    self.cursor_rect.set(x, y, 136, 32)
  end
end
#==============================================================================
# ** Scene_Dialogue
#------------------------------------------------------------------------------
#  This class performs dialogue screen processing.
#==============================================================================

class Scene_Dialogue
  #--------------------------------------------------------------------------
  # * Object Initialization
  #     npc_id : npc's id
  #--------------------------------------------------------------------------
  def initialize(npc_id = 0, npc_name = "", actor_name = "",bgm = "")
    @npc_id = npc_id
    @npc_name = npc_name
    @actor_name = actor_name
    @bgm = bgm
  end
  #--------------------------------------------------------------------------
  # * Main Processing
  #--------------------------------------------------------------------------
  def main
    # Play BGM
    Audio.bgm_play("Audio/BGM/" + @bgm) if @bgm != ""
    # Initial npc dialoge
    text = Shadowrun::NPC[@npc_id]['-start-']
    # Initial npc face
    face = Shadowrun::NPC_Faces[@npc_id]
    # Make npc dialogue window
    @dialogue_window = Window_Dialogue.new(text, face, @npc_name)
    # Make actor dialogue window
    @dialogue_actions_window = Window_DialogueActions.new(@actor_name)
    # Make keywords window
    @keywords_window = Window_Keywords.new(260, $game_party.keywords)
    @keywords_window.visible = false
    @keywords_window.active = false
    @keywords_window.height = 32 + (32 * 5)
    @keywords_window.x = 195
    @keywords_window.y = 240 + ((@dialogue_actions_window.height-@keywords_window.height) / 2)
    # Execute transition
    Graphics.transition
    # Main loop
    loop do
      # Update game screen
      Graphics.update
      # Update input information
      Input.update
      # Frame update
      update
      # Abort loop if screen is changed
      if $scene != self
        break
      end
    end
    # Prepare for transition
    Graphics.freeze
    # Dispose of windows
    @dialogue_window.dispose
    @dialogue_actions_window.dispose
    @keywords_window.dispose
  end
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  def update
    # Update windows
    @dialogue_window.update
    @dialogue_actions_window.update
    @keywords_window.update
    # Refresh current map
    $game_map.need_refresh = true
    # If keywords window is active: call update_keywords
    if @keywords_window.active
      update_keywords
      return
    end
    # If action window is active: call update_command
    if @dialogue_actions_window.active
      update_commands
      return
    end
  end
  #--------------------------------------------------------------------------
  # * Update Keywords
  #--------------------------------------------------------------------------
  def update_keywords
    # If B button was pressed
    if Input.trigger?(Input::B)
      @keywords_window.visible = false
      @keywords_window.active = false
      @dialogue_actions_window.active = true
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      key_word = $game_party.keywords[@keywords_window.index]
      if Shadowrun::NPC[@npc_id][key_word] != nil
        @dialogue_window.text = Shadowrun::NPC[@npc_id][key_word]
      else
        @dialogue_window.text = Shadowrun::NPC[@npc_id]['-unknown-']
      end
      @dialogue_window.refresh
      @keywords_window.refresh_keywords
    end
  end
  #--------------------------------------------------------------------------
  # * Update Commands
  #--------------------------------------------------------------------------
  def update_commands
    # If B button was pressed
    if Input.trigger?(Input::B)
      $scene = Scene_Map.new
      $game_map.autoplay
    end
    # If C button was pressed
    if Input.trigger?(Input::C)
      case @dialogue_actions_window.index
      when 0
        @dialogue_window.text = Shadowrun::NPC[@npc_id]['-talk-']
        @dialogue_window.refresh
      when 1
        @keywords_window.visible = true
        @keywords_window.active = true
        @dialogue_actions_window.active = false
      when 2
        $game_map.autoplay
        $scene = Scene_Map.new
      end
    end
  end
end
Expand to see the code.


Instructions

Read the script header.

FAQ

None.

Compatibility

Should be compatible with everything including message systems.

Credits and Thanks

Thanks to Zeriab for helping me with the \\k[string] thing.

Author's Notes

Enjoy!

Terms and Conditions

Just don't forget to credit me!
This looks good. Great script, kupo.
Also, only a foolish fool would try and take this first comment from me.
EDIT : W...wait! I have found an error in this script! And... an line of code that shouldn't be needed in the legal version!
What line, you ask? Line 159.
The line of code is:
    self.contents.font.size = $fontsize
Expand to see the code.

It also appears at lines 171 and 248! And back at line 136. What is the meaning of this, I ask you!
Awesome! This is great for more involved environments in games, in my opinion, and extra points for it being Shadowrun-related. :P (Never cared much for that game, but the Shadowrun franchise itself is made of win.)
can you make this ala NPC Interactions?

can you put the dialoge into a *.rxdata

i have a script to show 2k3 faces.
i think its useful.
It's annoying to write all the texts of the NPC's into the skript...
@Kururu
The reason why I used those lines of code is because I posted (and tested) the script from a friend's house which do not have the legal version of rmxp. (it's an unfortunate shame, I know.) I'm sorry for that but don't worry about it, I'm using the legal version.;)


@hammac, P-Games
Yes, I can change that. I'll be using Trickster's way of reading .rxdata files. :p
that's anythings... you suppose to be using script from a legal version of rmxp here and see nobody care... i really don't get it. Legal Not Legal, legit or not... and here they supposedly don't tolerate this
there is my facescript:
http://www.rmxp.de/forum/datenbanken/rgss-skript-datenbank/sonstige/11952-faceset-skipt/

it can be useful.

can you show the name off the NPC over the Face?

EDIT:

can you kombine with the Keyword Encyclopedia from the Sephs SDK?
i remember this kind of system from FF2... i think it was, at least in the beginning its pretty cool
Ah, I understand. Then, I have a request for this system. Could you make it so that you can activate switches with this dialouge, like you talk to someone, get a quest or story started, then a switch is activated so you can continue out of this dialouge system, or whatever?
Sure, I can do that.
WOW!!

i was just playing this game again an hour ago and i was thinking of how i wanted a diolouge system like this :D
GREAT WORK!!!
Reminds me of FFII's keyword system, although I prefer being able to see the map like with most message systems.
Thankyou, Dargor.
Another request, different BGM for different NPCs.
SCRIPT UPDATED!
See first post for more informations.

[LEFT]When calling the scene, use Scene_Dialogue.new(npc_id, npc_name ,actor_name ,bgm).
[/LEFT]
Seems perfect for a detective-type game, where getting information is an important part of the game.
Thanks, Dargor. This really does suit my game now.
Well i neeed hlep trying to draw the face to the text.It keeps on saying sytax error when i paly my demo of it.
At which line does the error occurs?
The script errors for me too. It doesn't say what's wrong,
It just says "SyntaxError occurred while running script.".
You are a very clever person Dargor and you should be very proud of yourself. I'm a big fan of Final Fantasy II, so I reckon this script is great!
Hi Dargor!
I just wanted to let you know that I get some error message. It says something like:
Uninitialized constant Interpreter::Scene_Dialogue
Looks good, Keep up the good works!
Thanks everybody!

I'll take a look at the script and try to solve this problem.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=22994