Well, I wanted to try this out, and I finally got a chance to give it a shot, so here it is.
I'm not sure what I think of it yet, so I'd like to hear from others.
The first two files are the Ruby script, and the Erb template that gets applied to the YAML data. The last two files are the Input/Output files, they're the ones to look at.
The ruby script:
MB_Dialogs.rb
require 'erb'
require 'yaml'
include YAML
unless ARGV[0]
puts '', "Please provide a filename."
system("pause")
exit
end
source_file = ARGV[0]
data = YAML.load( File.read(source_file) )
if data['file']
lines = []
File.open( "C:/Work/active/shell/mvCompiler/BIN/MB_Dialogs.erb" ) do |fh|
erb = ERB.new( fh.read, nil, ">" )
lines += erb.result( binding ).split(10.chr) # why just 10?
end
File.open(data['file'], 'w') {|f| f.puts lines}
end
The erb template file:
MB_Dialogs.erb
<% data['imports'].each do |import| %>from <%= import.split(' ')[0].strip %> import <%= import.split(' ')[1].strip %>
<% end %>
<%= data['name'] %> = [
<% data['dialogs'].each do |dialog| %>
<% if dialog['actor'] %>
[<%= dialog['actor'] %>, "<%= dialog['start'] %>", <% unless dialog['pre'] %>[],
<% end %>
<% if dialog['pre'] %>[
<% dialog['pre'].each do |cond| %>
(<%= cond %>),
<% end %>
],
<% end %>
"<%= dialog['text'] %>",
"<%= dialog['end'] %>", <% unless dialog['post'] %>[]],<% end %>
<% if dialog['post'] %>[
<% dialog['post'].each do |cond| %>
(<%= cond %>),
<% end %> ]],<% end %>
<% end %>
<% end %>
]
The YAML file (INPUT):
dialog09_map_talks.yaml
## MAP TALKS
---
name: dialog_map_talks
file: dialog08_map_talks_TEST.py
imports:
- header_dialogs *
- header_operations *
- header_parties *
- header_item_modifiers *
- header_skills *
- header_triggers *
- ID_troops *
- ID_party_templates *
- module_constants *
dialogs:
-
actor: party_tpl|pt_peasant
start: start
pre:
text: Greetings traveller.
end: peasant_talk_1
post: play_sound,"snd_encounter_farmers"
-
actor: party_tpl|pt_peasant|plyr
start: peasant_talk_1
pre: eq,"$quest_accepted_zendar_river_pirates"
text: Greetings to you too.
end: close_window
post: assign, "$g_leave_encounter",1
-
actor: party_tpl|pt_peasant|plyr
start: peasant_talk_1
pre:
- neq, "$quest_accepted_zendar_river_pirates"
- eq, "$peasant_misunderstanding_said"
text: I have been charged with hunting down Shadow Knights in this area...
end: peasant_talk_2
post: assign,"$peasant_misunderstanding_said",1
-
actor: party_tpl|pt_peasant|plyr
start: peasant_talk_1
pre:
- neq,"$quest_accepted_zendar_river_pirates
- neq,"$peasant_misunderstanding_said"
text: Greetings. I am hunting Shadow Knights. Have you seen any around here?
end: peasant_talk_2b
post:
The results (OUTPUT):
dialog08_map_talks_TEST.py
from header_dialogs import *
from header_operations import *
from header_parties import *
from header_item_modifiers import *
from header_skills import *
from header_triggers import *
from ID_troops import *
from ID_party_templates import *
from module_constants import *
dialog_map_talks = [
[party_tpl|pt_peasant, "start", [],
"Greetings traveller.",
"peasant_talk_1", [
(play_sound,"snd_encounter_farmers"),
]],
[party_tpl|pt_peasant|plyr, "peasant_talk_1", [
(eq,"$quest_accepted_zendar_river_pirates"),
],
"Greetings to you too.",
"close_window", [
(assign, "$g_leave_encounter",1),
]],
[party_tpl|pt_peasant|plyr, "peasant_talk_1", [
(neq, "$quest_accepted_zendar_river_pirates"),
(eq, "$peasant_misunderstanding_said"),
],
"I have been charged with hunting down Shadow Knights in this area...",
"peasant_talk_2", [
(assign,"$peasant_misunderstanding_said",1),
]],
[party_tpl|pt_peasant|plyr, "peasant_talk_1", [
(neq,"$quest_accepted_zendar_river_pirates),
(neq,"$peasant_misunderstanding_said"),
],
"Greetings. I am hunting Shadow Knights. Have you seen any around here?",
"peasant_talk_2b", []],
]