Extracting data from Enemies/Troops database

I've basically completed the RPGMaker Script side of my planned application, and am dicking around with the output in a RDBMS, so I thought I'd share it with the community.

Code:
DataManager.load_normal_database

tf = File.open('Data/_Troops.txt', 'w')
tmf = File.open('Data/_TroopMembers.txt', 'w')
tf.puts(sprintf('Troop_id|Troop_Name'))
tmf.puts(sprintf('Troop id|Enemy id'))
 $data_troops.each do |troop|
   next if troop.nil? || troop.name == ''
  tf.puts(sprintf('%s|%s', troop.id.to_s, troop.name))
  troop.members.each do |troop_member|
   next if troop_member.nil?
   tmf.puts(sprintf('%s|%s', troop.id.to_s, troop_member.enemy_id.to_s))
  end
 end
tf.close
tmf.close

ef = File.open('Data/_EnemyStats.txt', 'w')
edf = File.open('Data/_EnemyDropItems.txt', 'w')
eaf = File.open('Data/_EnemyActions.txt', 'w')
eff = File.open('Data/_EnemyFeatures.txt', 'w')
ef.puts(sprintf('Enemy_id|Name|MHP|MMP|ATK|DEF|MAT|MDF|AGI|LUK|EXP|GOLD|Battler_Name|Battler_Hue|Note'))
edf.puts(sprintf('Enemy_id|Kind|Data id|Denominator'))
eaf.puts(sprintf('Enemy_id|Skill_id|Condition type|Condition param1|Condition param2|Rating'))
eff.puts(sprintf('Enemy_id|Code|Data id|Value'))
$data_enemies.each do |enemy|
  next if enemy.nil? || enemy.name == ''
  ef.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', enemy.id.to_s, enemy.name,
    enemy.params[0].to_s, enemy.params[1].to_s, enemy.params[2].to_s, enemy.params[3].to_s,
    enemy.params[4].to_s, enemy.params[5].to_s, enemy.params[6].to_s, enemy.params[7].to_s,enemy.exp.to_s , enemy.gold.to_s,enemy.battler_name,enemy.battler_hue.to_s,enemy.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
    enemy.drop_items.each do |drop_item|
     next if drop_item.nil? || drop_item.kind == 0
     edf.puts(sprintf('%s|%s|%s|%s', enemy.id.to_s, drop_item.kind.to_s, drop_item.data_id.to_s, drop_item.denominator.to_s))
    end
    enemy.actions.each do |action|
     next if action.nil?
     eaf.puts(sprintf('%s|%s|%s|%s|%s|%s', enemy.id.to_s, action.skill_id.to_s, action.condition_type.to_s, action.condition_param1.to_s, action.condition_param2.to_s, action.rating.to_s))
    end
    enemy.features.each do |enemy_feature|
     next if enemy_feature.nil?
     eff.puts(sprintf('%s|%s|%s|%s', enemy.id.to_s, enemy_feature.code.to_s, enemy_feature.data_id.to_s, enemy_feature.value.to_s))
    end
end
ef.close
edf.close
eaf.close
eff.close

skf = File.open('Data/_Skills.txt', 'w')
skdf = File.open('Data/_SkillsDamage.txt', 'w')
skef = File.open('Data/_SkillsEffects.txt', 'w')
skf.puts(sprintf('Skill_id|Name|Icon_Index|Description|Skill_Type_ID|MP_cost|TP_cost|Scope|Occasion|Speed|Success_Rate|Repeats|TP_Gain|Hit_Type|animation_id|Use_Message_1|Use_Message_2|Required_Weapon_Type_ID1|Required_Weapon_Type_ID2|Note'))
skdf.puts('Skill_id|Type|Element_id|Formula|Variance|Critical')
skef.puts('Skill_id|Code|Data_id|Value1|Value2')
$data_skills.each do |skill|
  next if skill.nil? || skill.name == ''
  skf.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', skill.id.to_s, skill.name, skill.icon_index.to_s, skill.description.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,''), skill.stype_id.to_s, skill.mp_cost.to_s, skill.tp_cost.to_s, skill.scope.to_s, skill.occasion.to_s, skill.speed.to_s, skill.success_rate.to_s, skill.repeats.to_s, skill.tp_gain.to_s, skill.hit_type.to_s, skill.animation_id.to_s, skill.message1.to_s, skill.message2.to_s, skill.required_wtype_id1.to_s, skill.required_wtype_id2.to_s, skill.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
  skdf.puts(sprintf('%s|%s|%s|%s|%s|%s', skill.id.to_s, skill.damage.type.to_s, skill.damage.element_id.to_s, skill.damage.formula, skill.damage.variance.to_s, skill.damage.critical))
  skill.effects.each do |skill_effect|
   next if skill_effect.nil? || skill_effect.code.to_s == 'formula'
   skef.puts(sprintf('%s|%s|%s|%s|%s', skill.id.to_s, skill_effect.code.to_s, skill_effect.data_id.to_s, skill_effect.value1.to_s, skill_effect.value2.to_s))
  end
end
skf.close
skdf.close
skef.close

itf = File.open('Data/_Items.txt', 'w')
itdf = File.open('Data/_ItemsDamage.txt', 'w')
itef = File.open('Data/_ItemsEffects.txt', 'w')
itf.puts(sprintf('Item_id|Name|Icon_Index_Number|Description|Itype_id|Price|Consumable|Scope|Occasion|Speed|Success_Rate|Repeats|TP_Gain|Hit_Type|animation_id|Note'))
itdf.puts('Item_id|Type|Element_id|Formula|Variance|Critical')
itef.puts('Item_id|Code|Data_id|Value1|Value2')
$data_items.each do |item|
  next if item.nil? || item.name == ''
  itf.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', item.id.to_s, item.name, item.icon_index.to_s, item.description.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,''), item.itype_id.to_s, item.price.to_s, item.consumable.to_s, item.scope.to_s, item.occasion.to_s, item.speed.to_s, item.success_rate.to_s, item.repeats.to_s, item.tp_gain.to_s, item.hit_type.to_s, item.animation_id.to_s, item.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
  itdf.puts(sprintf('%s|%s|%s|%s|%s|%s', item.id.to_s, item.damage.type.to_s, item.damage.element_id.to_s, item.damage.formula, item.damage.variance.to_s, item.damage.critical))
  item.effects.each do |item_effect|
     next if item_effect.nil? || item_effect.code.to_s == 'formula'
     itef.puts(sprintf('%s|%s|%s|%s|%s', item.id.to_s, item_effect.code.to_s, item_effect.data_id.to_s, item_effect.value1.to_s, item_effect.value2.to_s))
  end
end
itf.close
itdf.close
itef.close

wf = File.open('Data/_Weapons.txt', 'w')
wff = File.open('Data/_WeaponFeatures.txt', 'w')
wf.puts(sprintf('Weapon_id|Name|Icon_Index_Number|Description|Wtype_id|Price|EType_id|MHP|MMP|ATK|DEF|MAT|MDF|AGI|LUK|animation_id|Note'))
wff.puts(sprintf('Weapon id|Code|Data id|Value'))
$data_weapons.each do |weapon|
  next if weapon.nil? || weapon.name == ''
  wf.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', weapon.id.to_s, weapon.name, weapon.icon_index.to_s, weapon.description.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,''), weapon.wtype_id.to_s, weapon.price.to_s, weapon.etype_id.to_s, weapon.params[0].to_s, weapon.params[1].to_s, weapon.params[2].to_s, weapon.params[3].to_s, weapon.params[4].to_s, weapon.params[5].to_s, weapon.params[6].to_s, weapon.params[7].to_s, weapon.animation_id.to_s, weapon.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
  weapon.features.each do |weapon_feature|
    next if weapon_feature.nil?
    wff.puts(sprintf('%s|%s|%s|%s', weapon.id.to_s, weapon_feature.code.to_s, weapon_feature.data_id.to_s, weapon_feature.value.to_s))
  end
end
wf.close
wff.close

af = File.open('Data/_Armors.txt', 'w')
aff = File.open('Data/_ArmorFeatures.txt', 'w')
af.puts(sprintf('Armor_id|Name|Icon_Index_Number|Description|Atype_id|Price|EType_id|MHP|MMP|ATK|DEF|MAT|MDF|AGI|LUK|Note'))
aff.puts(sprintf('Armor id|Code|Data id|Value'))
$data_armors.each do |armor|
  next if armor.nil? || armor.name == ''
  af.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', armor.id.to_s, armor.name, armor.icon_index.to_s, armor.description.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,''), armor.atype_id.to_s, armor.price.to_s, armor.etype_id.to_s, armor.params[0].to_s, armor.params[1].to_s, armor.params[2].to_s, armor.params[3].to_s, armor.params[4].to_s, armor.params[5].to_s, armor.params[6].to_s, armor.params[7].to_s, armor.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
  armor.features.each do |armor_feature|
    next if armor_feature.nil?
    aff.puts(sprintf('%s|%s|%s|%s', armor.id.to_s, armor_feature.code.to_s, armor_feature.data_id.to_s, armor_feature.value.to_s))
  end
end
af.close
aff.close

stf = File.open('Data/_States.txt', 'w')
stff = File.open('Data/_StateFeaturess.txt', 'w')
stf.puts(sprintf('State_ID|Name|Icon_Index_Number|Restriction|Priority|Remove_At_Battle_End|Remove_By_Restriction|Auto_Removal_Timing|Min_Turns|Max_Turns|Remove_By_Damage|Chance_By_Damage|Remove_By_Walking|Steps_to_Remove|Ally_Message|Enemy_Message|Continuing_Message|Remove_Message|Note'))
stff.puts(sprintf('State id|Code|Data id|Value'))
$data_states.each do |state_object| # "state" is apparently a reserved word...
  next if state_object.nil? || state_object.name == ''
  stf.puts(sprintf('%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s', state_object.id.to_s, state_object.name, state_object.icon_index.to_s, state_object.restriction.to_s, state_object.priority.to_s, state_object.remove_at_battle_end.to_s, state_object.remove_by_restriction.to_s, state_object.auto_removal_timing.to_s, state_object.min_turns.to_s, state_object.max_turns.to_s, state_object.remove_by_damage.to_s, state_object.chance_by_damage.to_s, state_object.remove_by_walking.to_s, state_object.steps_to_remove.to_s, state_object.message1, state_object.message2, state_object.message3, state_object.message4, state_object.note.gsub(/\r\n/,'\\n').gsub(/\t/,'    ').gsub(/\|/,'')))
  state_object.features.each do |state_feature|
    next if state_feature.nil?
    stff.puts(sprintf('%s|%s|%s|%s', state_object.id.to_s, state_feature.code.to_s, state_feature.data_id.to_s, state_feature.value.to_s))
  end
end
stf.close
stff.close

elf = File.open('Data/_Elements.txt', 'w')
elf.puts(sprintf('Element_id|Element_name'))

i = 0
while i < $data_system.elements.length
  
    if $data_system.elements[i] != '' then
      elf.puts(sprintf('%s|%s', i.to_s, $data_system.elements[i]))
    end if
    i = i +1

end

elf.close


sktf = File.open('Data/_SkillTypes.txt', 'w')
sktf.puts(sprintf('Skill_type_id|Skill_type_name'))

i = 0
while i < $data_system.skill_types.length
  
    if $data_system.skill_types[i] != '' then
      sktf.puts(sprintf('%s|%s', i.to_s, $data_system.skill_types[i]))
    end if
    i = i +1

end

sktf.close

wttf = File.open('Data/_WeaponTypes.txt', 'w')
wttf.puts(sprintf('Weapon_type_id|Weapon_type_name'))

i = 0
while i < $data_system.weapon_types.length
  
    if $data_system.weapon_types[i] != '' then
      wttf.puts(sprintf('%s|%s', i.to_s, $data_system.weapon_types[i]))
    end if
    i = i +1

end

wttf.close


artf = File.open('Data/_ArmorTypes.txt', 'w')
artf.puts(sprintf('Armor_type_id|Armor_type_name'))

i = 0
while i < $data_system.armor_types.length
  
    if $data_system.armor_types[i] != '' then
      artf.puts(sprintf('%s|%s', i.to_s, $data_system.armor_types[i]))
    end if
    i = i +1

end

artf.close

Things it does not export:

  • Troop Pages, because the game I'm pointing it at doesn't seem to use them much.
  • Actors or Classes, because the gaming I'm pointing it at has a 1 to 1 correspondence between the two and the features for them are already well documented.
  • Most of the System and Terms objects. (I already know the aliases for the various stats and the vehicles are unused in this game.)
I considered pulling a list of maps and every event with the Battle Processing command (the game in question has a fixed number of battles always called through events), but that seemed to be more effort than it was worth. I should have a nice shiny app capable of making Wikia pages indexing the game's enemies and inventory options in the next few weeks, most likely in LibreOffice Base.

Thanks again for the assistance and hopefully my code can be exapted for someone else's needs.

EDIT: Derp! Obviously, I wiped out any instances of characters that would interfere with the import of the outputted files into an RDBMS engine (Line breaks, tabs, pipes). If you want them to be stored as they were in the original, you need to convert them back on import.

Original thread: https://forums.rpgmakerweb.com/threads/extracting-data-from-enemies-troops-database.88507/