Problem with Pirates Mod + BUG merging

Cybah

Emperor
Joined
Jun 22, 2007
Messages
1,481
Hi! Need help merging Pirates Mod into my Mod. Look at the python error screenshot below.

Here is the code:

CvPiratesModEventManager

PHP:
#
# Pirates
# 

from CvPythonExtensions import *

import CvUtil
import CvEventManager
import sys
import PyHelpers
import CvConfigParser
import math
import time

#import CvAlertManager

			
gc = CyGlobalContext()

PyPlayer = PyHelpers.PyPlayer
PyGame = PyHelpers.PyGame()

localText = CyTranslator()

#alert = CvAlertManager.CvAlertManager()

# Change the value to false if spawned pirate ships should not carry any units.
# Default = True
g_bPirateShipsCanCarryCrew = True

# Increase or decrease the value to change the chance that a spawned pirate 
# ship will carry a crew.
# Default = 50
g_iPirateShipsCarryCrewChance = 50

# Change the value to True if pirate ships can have Unique Units spawned as crew.
# Default = False
g_bPirateShipsCarryUniqueUnitsAsCrew = False

def loadConfigurationValues():
	global g_bPirateShipsCanCarryCrew
	global g_iPirateShipsCarryCrewChance
	global g_bPirateShipsCarryUniqueUnitsAsCrew
		
	config = CvConfigParser.CvConfigParser("Pirates Mod Config.ini")

	if (config != None):
		g_bPirateShipsCanCarryCrew = config.getboolean("Pirates Mod", "Pirate Ships Can Carry Crew", True)
		g_iPirateShipsCarryCrewChance = config.getint("Pirates Mod", "Pirate Ships Carry Crew Chance", 50)
		g_bPirateShipsCarryUniqueUnitsAsCrew = config.getboolean("Pirates Mod", "Pirate Ships Carry Unique Units As Crew", False)


class CvPiratesModEventManager:
	
	def __init__(self, eventManager):

		eventManager.addEventHandler("unitCreated", self.onUnitCreated)
		eventManager.addEventHandler("windowActivation", self.onWindowActivation)
 		#eventManager.addEventHandler("mouseEvent", self.onMouseEvent)
		
		loadConfigurationValues()
	
	def onMouseEvent(self, argsList):
		eventType,mx,my,px,py,interfaceConsumed,screens = argsList

#		gc.getPlayer(18).killUnits()		

		objPlot = gc.getMap().plot(px,py)
	
	def onWindowActivation(self, argsList):
		'Called when the game window activates or deactivates'
		bActive = argsList[0]
		
		if (bActive):
			loadConfigurationValues()	
	
		
	def onUnitCreated(self, argsList):
		'Unit Completed'
		unit = argsList[0]

		# Return immediately if barbarian ships cannot carry crews
		if (not g_bPirateShipsCanCarryCrew):
			return None

		# Return immediately if the unit isn't a barbarian unit.
		if (not unit.isBarbarian()):
			return None
					
		# Return immediately if the unit spawned isn't a barbarian unit
		#if (gc.getPlayer(unit.getOwner()).getCivilizationType() != gc.getInfoTypeForString("CIVILIZATION_BARBARIAN")):
			#return None

		# Return immediately if the barbarian spawned is not a ship
		if (unit.getDomainType() != gc.getInfoTypeForString("DOMAIN_SEA")):
			return None
					
		# Return immediately if barbarian ships cannot carry crews
		if (unit.cargoSpace() == 0):
			return None
			
		# Get the list of possible units that can be created for barbarian ships
		unitInfoList = self.getBuildableUnits(unit.getOwner())
		#alert.debug(0, "unitInfoList", unitInfoList)
                #alert.debug(0, "=", "==========================")
		#for iUnit in unitInfoList:
                        #pInfo = gc.getUnitInfo(iUnit)
                        #sDesc = pInfo.getDescription()
                        #alert.debug(0, "sDesc", sDesc)
			
		# Get the barbarian player
		objPlayer = gc.getPlayer(unit.getOwner())
			
		# Try to fill up the ship with a crew
		for i in range(unit.cargoSpace()):
		
			# Get a random unit info from the list
			iUnitInfoID = gc.getGame().getSorenRandNum(len(unitInfoList), 'Random Test Call')
			
			# Get the test to see if the unit can be created
			iCreationTest = gc.getGame().getSorenRandNum(100, 'Creation Test')
			
			# Create the unit if the test is less than the chance
			if (iCreationTest < g_iPirateShipsCarryCrewChance):
			
				# Create the new barbarian unit
				objNewUnit = objPlayer.initUnit(unitInfoList[iUnitInfoID], unit.plot().getX(), unit.plot().getY(), UnitAITypes.UNITAI_ATTACK, DirectionTypes.NO_DIRECTION)
				
				# Have the barbarian unit load the ship
				#objNewUnit.doCommand(CommandTypes.COMMAND_LOAD, objNewUnit.getOwner(), unit.getID())
				#objNewUnit.getGroup().pushMission(MissionTypes.MISSION_SLEEP, 0, 0, 0, false, true, MissionAITypes.MISSIONAI_LOAD_ASSAULT, unit.plot(), unit)
				objNewUnit.setTransportUnit(unit)
				
	# Returns the list of units that can be built by the player ID passed in.				
	def getBuildableUnits(self, iPlayer):
	
		unitInfoList = []

		# Return an empty list if the player passed in is invalid		
		if(iPlayer < 0):
			return []
						
		# Get the player object
		objPlayer = gc.getPlayer(iPlayer)

		# Get the player's civilization info		
		objCivilizationInfo = gc.getCivilizationInfo(objPlayer.getCivilizationType())
		
		# Go through all of the units in the game
		for iUnitInfoID in range (gc.getNumUnitInfos()):

			# Continue if the player cannot train the current unit
			if (not objPlayer.canTrain(iUnitInfoID, false, false)):
				continue
				
			# Continue if the civilization cannot train the current unit
			if (objCivilizationInfo.getCivilizationUnits(iUnitInfoID) == UnitTypes.NO_UNIT):
				continue
				
			# Get the unit info object
			objUnitInfo = gc.getUnitInfo(iUnitInfoID)
					
			# Continue if the current unit info is an animal
			if (objUnitInfo.isAnimal()):
				continue
					
			# Continue if the current unit info is not a land unit
			if (objUnitInfo.getDomainType() != gc.getInfoTypeForString("DOMAIN_LAND")):
				continue
				
			# Continue if the current unit info does not have an attack UNITAI
			if (not objUnitInfo.getUnitAIType(gc.getInfoTypeForString("UNITAI_ATTACK"))):
				continue

                        # If Unique Units should not be considered the check if the unit is not
                        # the default unit info for the unit class info
			if (not g_bPirateShipsCarryUniqueUnitsAsCrew):
    			        # Get the unit class type
                                iUnitClassID = objUnitInfo.getUnitClassType()

			        # Get the unit class object
			        objUnitClassInfo = gc.getUnitClassInfo(iUnitClassID)

			        # Get the unit class object default unit info index
			        iDefaultUnitInfoID = objUnitClassInfo.getDefaultUnitIndex()

			        # Continue if the unit is not the default unit for the unit class
			        if (iUnitInfoID != iDefaultUnitInfoID):
                                        continue
					
			# Append the unit info ID to the list
			unitInfoList.append(iUnitInfoID)

				
		return unitInfoList



Pirates Mod.xml in Assets\Config:

PHP:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
	Pirates Mod
-->
<mod id="Pirates Mod" 
	 name="Pirates Mod" 
	 author="Grave, TheLopez, Jeckel" 
	 version="0.6" 
	 date="n/a"
	 url="n/a">
	 
	<options id="Pirates Mod" file="ini-Files\Pirates Mod Config.ini">
		<section id="Pirates Mod">
			<option id="Enabled" key="Pirate Ships Can Carry Crew" 
					type="boolean" default="True">
			</option>
			<option id="CrewChance" key="Pirate Ships Carry Crew Chance" 
					type="int" default="100">
			</option>
			<option id="CarryUniqueUnits" key="Pirate Ships Carry Unique Units As Crew" 
					type="boolean" default="False">
			</option>
		</section>
	</options>

	<events module="CvPiratesModEventManager.py"/>
	
</mod>


Pirates Mod Config.ini in ini-Files:

PHP:
[Pirates Mod]

; Change the value to False if spawned pirate ships should not carry any units.
; Default = True
Pirate Ships Can Carry Crew = True


; Increase or decrease the value to change the chance that a spawned pirate 
; ship will carry a crew.
; Default = 50
Pirate Ships Carry Crew Chance = 100


; Change the value to True if pirate ships can have Unique Units spawned as crew.
; Default = False
Pirate Ships Carry Unique Units As Crew = False
 
Damn... I don't get it. There IS a class...

attachment.php
 
Changed Pirates Mod.xml code to:

PHP:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
	Pirates Mod
-->
<mod id="Pirates Mod" 
	 name="Pirates Mod" 
	 author="Grave, TheLopez, Jeckel" 
	 version="0.6" 
	 date="n/a"
	 url="n/a"
	 module="CvPiratesModEventManager">
	 
	<options id="Pirates Mod" file="ini-Files\Pirates Mod Config.ini">
		<section id="Pirates Mod">
			<option id="Enabled" key="Pirate Ships Can Carry Crew" 
					type="boolean" default="True">
			</option>
			<option id="CrewChance" key="Pirate Ships Carry Crew Chance" 
					type="int" default="100">
			</option>
			<option id="CarryUniqueUnits" key="Pirate Ships Carry Unique Units As Crew" 
					type="boolean" default="False">
			</option>
		</section>
	</options>
</mod>


and I don't get a python error screen anymore, but I cannot see any barbarian ship with a crew. :( I made all (of the few) SDK changes.
 
You need to keep this line from the config XML file

Code:
<events module="CvPiratesModEventManager.py"/>

but you need to remove the ".py" from it. The Python module name is "CvPiratesModEventManager", and it is stored in a file called "CvPiratesModEventManager.py". The config XML files should refer to Python modules--not Python files. Maybe I'll make BUG smart enough to remove the ".py" automatically, but not today.
 
Can you post the PythonErr.log file instead of screenshots of the exceptions? It's much easier to help that way.
 
here ya go:

Traceback (most recent call last):
File "BugConfig", line 384, in parse
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 171, in feed
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 280, in goahead
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 668, in parse_starttag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 730, in finish_endtag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 751, in handle_endtag
File "BugConfig", line 588, in end_events
File "BugConfig", line 233, in createEvents
File "BugUtil", line 508, in callFunction
File "BugUtil", line 504, in getFunction
File "BugUtil", line 475, in bind
ConfigError: No such module 'CvPiratesModEventManager'
Traceback (most recent call last):
File "BugConfig", line 384, in parse
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 171, in feed
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 280, in goahead
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 666, in parse_starttag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 703, in finish_starttag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 747, in handle_starttag
File "BugConfig", line 423, in start_load
File "BugConfig", line 116, in loadMod
File "BugInit", line 65, in loadMod
File "BugConfig", line 394, in parse
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 175, in close
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 287, in goahead
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 697, in parse_endtag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 722, in finish_endtag
File "BugConfig", line 397, in syntax_error
ConfigError: error parsing XML: unopened end tag
Traceback (most recent call last):
File "BugConfig", line 384, in parse
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 171, in feed
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 280, in goahead
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 668, in parse_starttag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 730, in finish_endtag
File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 751, in handle_endtag
File "BugConfig", line 588, in end_events
File "BugConfig", line 233, in createEvents
File "BugUtil", line 508, in callFunction
File "BugUtil", line 504, in getFunction
File "BugUtil", line 477, in bind
ConfigError: Module 'CvPiratesModEventManager' must define function or class 'CvPiratesModEventManager'
Traceback (most recent call last):

File "CvAppInterface", line 63, in preGameStart

File "BugEventManager", line 284, in fireEvent

File "BugEventManager", line 294, in _dispatchEvent

File "BugEventManager", line 336, in _handleInitBugEvent

File "BugEventManager", line 413, in initBug

File "BugInit", line 46, in init

File "BugInit", line 65, in loadMod

File "BugConfig", line 394, in parse

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 175, in close

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 280, in goahead

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 666, in parse_starttag

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 703, in finish_starttag

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 747, in handle_starttag

File "BugConfig", line 423, in start_load

File "BugConfig", line 116, in loadMod

File "BugInit", line 65, in loadMod

File "BugConfig", line 394, in parse

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 175, in close

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 287, in goahead

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 697, in parse_endtag

File "e:/main/civilization4/warlords/assets/python/system\xmllib.py", line 722, in finish_endtag

File "BugConfig", line 397, in syntax_error

ConfigError: error parsing XML: unopened end tag
ERR: Python function preGameStart failed, module CvAppInterface
 
ConfigError: No such module 'CvPiratesModEventManager'
...
ConfigError: Module 'CvPiratesModEventManager' must define function or class 'CvPiratesModEventManager'

Make sure the file is named correctly and is installed in your mod. The name of the class looks correct as does the file name you posted, but this is what these errors are saying is the problem.

Another problem:

Code:
<mod id="Pirates Mod" ...>

As per the BUG documentation, all "id" values must be valid Python identifiers (variable names): no spaces allowed.
 
The name is spelled correctly.

And:

Code:
<mod id="Enhanced Tech Conquest" 
	 name="Enhanced Tech Conquest" 
	 author="Bhruic, Grave, Jeckel, TheLopez" 
	 version="0.6" 
	 date="n/a"
	 url="n/a">
	 
	<events module="CvEnhancedTechConquestEventManager"/>
	
</mod>

is working (with space). Therefore I doubt this is the reason.

edit: tried to rename. no success.


It seems, it ignores:

Code:
class CvPiratesModEventManager
in CvPiratesModEventManager.py
 
I think I got it now (splitted the code into two files: eventmanager and another file) but there is still no crew spawning on ships. Hm.
 
I didn't say the space in the ID was the cause of this problem. I said that it is not allowed. You can follow the instructions or not, that's your call. :rolleyes:

Look in the PythonDbg.log file to see if it's loading that module and if BUG is seeing it.
 
I know. I did not want to be rude. I just did not get any error with enhanced tech conquest.

17:04:04 INFO : BugCore - creating uninitialized mod PiratesMod
load_module CvPiratesModEventManager

load_module PiratesMod

I think it works, but the mod seems to be... bad. :D Tried it in history in the making too and did not see there any pirate either. Seems to be the mod's problem. Thanks anyway.
 
Okay, so the module is loading. That's good. And you no longer get any error when it's loading up regarding the class/function name, right?

So my guess is that the events don't actually do anything. You could put BugUtil.alert("message") calls into the event handling functions to see if they are being called by BUG. Or you could turn on event logging in BUG in init.xml:

Code:
<arg name="logging" type="boolean" value="[B][COLOR="Orange"]True[/COLOR][/B]"/>

This will log each event and its arguments to PythonDbg.log.
 
One thing, the pirates w/ crew option needs to be configured via the ini file, I believe by default pirates don't have crews unless an option is set.

"Also if the mod is configured to, some of the pirate ships will carry crews." -grave

Also peeping at thelopez's pirate thread there was some difficulty at some point in the mod with loading crews and unloading crews and whatnot.
 
No, default is "yes" with a chance of 50%. I've set this chance to 100% but without any effect.

You can set the default values yourself.

Code:
# Change the value to false if spawned pirate ships should not carry any units.
# Default = True
g_bPirateShipsCanCarryCrew = True

# Increase or decrease the value to change the chance that a spawned pirate 
# ship will carry a crew.
# Default = 50
g_iPirateShipsCarryCrewChance = 50

# Change the value to True if pirate ships can have Unique Units spawned as crew.
# Default = False
g_bPirateShipsCarryUniqueUnitsAsCrew = False

You just have to remove the config loading code. My solution now works without any ini file (similar to enhanced tech conquest which works without ini too).
 
Back
Top Bottom