[Ruby Script] Mod Sector Quest Weightings By Owner Race and Neighbor Race

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
User avatar
RoverTX
Posts: 1436
Joined: Wed, 16. Nov 11, 18:37
x4

[Ruby Script] Mod Sector Quest Weightings By Owner Race and Neighbor Race

Post by RoverTX » Wed, 25. Nov 15, 02:45

Written in ruby using the nokogiri XML parser gem(library). Runs through each sector and changes the quest weight based on owner race, or neighbor race. Change OWNER_MOD and NEIGHBOR_MOD to your liking. Uses xpaths to manipulate the xml so should be easily portable to a scripting language of your choice.

x3_universe.xml must be in same folder, results can be found in x3_universe_modded.xml.

Also note I haven't figured out how to stop nokogiri from adding in an xml definition on the first line, just delete it.

Code: Select all

require "nokogiri"

#Easier to type ARGON then to keep remembering 1 stands for Argon ownership
ARGON = 1
BORON = 2
SPLIT = 3
PRANID = 4
TELADI = 5
XENON = 6
KHAAK = 7
PIRATE = 8
GONER = 9
ATF = 17
USC = 18
YAKI = 19

MAP_ATTR_TO_SYM = {
	"qtrade" => :trade,
	"qfight" => :fight,
	"qbuild" => :build,
	"qthink" => :think
}

#Mod based on sector's owner race
OWNER_MOD = {
	ARGON => {
		:trade => 100,
		:fight => 100
	},
	BORON => {
		:build => 100,
		:think => 100
	},
	SPLIT => {
		:fight => 100,
		:build => 100
	},
	PRANID => {
		:fight => 100,
		:think => 100
	},
	TELADI => {
		:trade => 100,
		:build => 100
	},
	USC => {
		:trade => 100,
		:think => 100
	},
	ATF => {
		:fight => 100,
		:think => 100
	},
	YAKI => {
		:fight => 100,
		:build => 100
	},
	PIRATE => {
		:trade => 100,
		:fight => 100
	}
}

#Mod based on neibhor sector's owner race
NEIGHBOR_MOD = {
	PIRATE => {
		:trade => 100,
		:fight => 100
	},
	XENON => {
		:fight => 100,
		:build => 100
	}
}

def add_mods quest, mod
	quest.merge!(mod){|key, val, mod| val + mod} if mod
end


file = File.open(Dir.pwd + "/x3_universe.xml")
xml = Nokogiri::XML(file)

sectors = xml.xpath("/universe/o")

sectors.each do |sector|
	quest = {}	
	MAP_ATTR_TO_SYM.each do |map_attr, sym|
		quest[sym] = sector[map_attr].to_i
	end

	owner = sector['r'].to_i
	
	owner_mods = OWNER_MOD[owner]

	add_mods quest, owner_mods

	x = sector['x'].to_i
	y = sector['y'].to_i
	#Make sure to ignore disabled gates and to only count each race once via uniq
	#All disabled gates have an subtype of 4 and a gid of -1
	#The /.. means this xpath is returning the sector not the gate object, works like file structure
	neightbors = xml.xpath("/universe/o/o[@t='18' and @gx='#{x}' and @gy='#{y}' and @s!='4' and @gid!='-1']/..").map{|s| s['r'].to_i}.uniq.sort
	#You can't be your own neighbor
	neightbors.delete(owner)

	neightbors.each do |neighbor|
		neighbor_mod = NEIGHBOR_MOD[neighbor]
		add_mods quest, neighbor_mod
	end

	MAP_ATTR_TO_SYM.each do |map_attr, sym|
		sector[map_attr] = quest[sym].to_s
	end

end

File.open(Dir.pwd + "/x3_universe_modded.xml", 'w') do |file|
	file.write(xml.to_xml)
end

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”