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
Pirates Mod.xml in Assets\Config:
Pirates Mod Config.ini in ini-Files:
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
I made all (of the few) SDK changes.
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.