Script v1_7_1_0
- AI
- AIDriveStrategy
- AIDriveStrategyBaler
- AIDriveStrategyCollision
- AIDriveStrategyCombine
- AIDriveStrategyConveyor
- AIDriveStrategyStonePicke...
- AIDriveStrategyStraight
- AIJobTypeManager
- AISystem
- AITurnStrategy
- AITurnStrategyBulb1
- AITurnStrategyBulb1Revers...
- AITurnStrategyBulb2
- AITurnStrategyBulb2Revers...
- AITurnStrategyBulb3
- AITurnStrategyBulb3Revers...
- AITurnStrategyDefault
- AITurnStrategyDefaultReve...
- AITurnStrategyHalfCircle
- AITurnStrategyHalfCircleR...
- HelperManager
- NPCManager
- Animals
- Collections
- Contracts
- Debug
- Economy
- Elements
- EnvironmentalScore
- Errors
- Events
- GUI
- Handtools
- Hud
- I3d
- Input
- Jobs
- Maps
- Materials
- Misc
- Objects
- Parameters
- Placeables
- Placement
- Player
- Shop
- Sounds
- Specialization
- Specializations
- StateMachine
- Statistics
- Tasks
- Triggers
- Utils
- Vehicles
Engine v1_7_1_0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- general
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- NoteNode
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- VoiceChat
- XML
Foundation Reference
NPCManager
DescriptionThis class handles all npcsParent
AbstractManagerFunctions
- addNPC
- getNPCByIndex
- getNPCByName
- getRandomIndex
- getRandomNPC
- initDataStructures
- loadDefaultTypes
- loadFromXMLFile
- loadMapData
- loadNPCs
- new
- saveToXMLFile
addNPC
DescriptionAdds a new npcDefinition
addNPC(string name, string title, string imageFilename, string baseDir)Arguments
string | name | npc index name |
string | title | npc real name |
string | imageFilename | npc image filename |
string | baseDir | the base directory |
boolean | true | if added successful else false |
140 | function NPCManager:addNPC(name, title, imageFilename, baseDir, isBaseType) |
141 | if not ClassUtil.getIsValidIndexName(name) then |
142 | print("Warning: '"..tostring(name).."' is not a valid name for a npc. Ignoring npc!") |
143 | return nil |
144 | end |
145 | |
146 | name = name:upper() |
147 | |
148 | if isBaseType and self.nameToIndex[name] ~= nil then |
149 | print("Warning: NPC '"..tostring(name).."' already exists. Ignoring npc!") |
150 | return nil |
151 | end |
152 | |
153 | local npc = self.npcs[name] |
154 | if npc == nil then |
155 | if title == nil or title == "" then |
156 | print("Warning: '"..tostring(title).."' is not a valid title for a npc. Ignoring npc!") |
157 | return nil |
158 | end |
159 | if imageFilename == nil or imageFilename == "" then |
160 | print("Warning: Missing npc image file for npc '"..tostring(name).."'. Ignoring npc!") |
161 | return nil |
162 | end |
163 | |
164 | self.numNpcs = self.numNpcs + 1 |
165 | npc = {} |
166 | npc.name = name |
167 | npc.title = g_i18n:convertText(title) |
168 | npc.index = self.numNpcs |
169 | npc.imageFilename = Utils.getFilename(imageFilename, baseDir) |
170 | npc.finishedMissions = 0 |
171 | |
172 | self.npcs[name] = npc |
173 | self.nameToIndex[name] = self.numNpcs |
174 | self.indexToNpc[self.numNpcs] = npc |
175 | else |
176 | if title ~= nil and title ~= "" then |
177 | npc.title = g_i18n:convertText(title) |
178 | end |
179 | if imageFilename ~= nil and imageFilename ~= "" then |
180 | npc.imageFilename = Utils.getFilename(imageFilename, baseDir) |
181 | end |
182 | end |
183 | |
184 | return npc |
185 | end |
getNPCByIndex
DescriptionGets a npc by indexDefinition
getNPCByIndex(integer index)Arguments
integer | index | the npc index |
table | npc | the npc object |
205 | function NPCManager:getNPCByIndex(index) |
206 | if index ~= nil then |
207 | return self.indexToNpc[index] |
208 | end |
209 | return nil |
210 | end |
getNPCByName
DescriptionGets a npc by index nameDefinition
getNPCByName(string name)Arguments
string | name | the npc index name |
table | npc | the npc object |
216 | function NPCManager:getNPCByName(name) |
217 | if name ~= nil then |
218 | name = name:upper() |
219 | return self.npcs[name] |
220 | end |
221 | return nil |
222 | end |
getRandomIndex
DescriptionGets a random npc indexDefinition
getRandomIndex()Return Values
integer | npcIndex | a random npc index |
197 | function NPCManager:getRandomIndex() |
198 | return math.random(1, self.numNpcs) |
199 | end |
getRandomNPC
DescriptionGets a random npcDefinition
getRandomNPC()Return Values
table | npc | a random npc object |
190 | function NPCManager:getRandomNPC() |
191 | return self.indexToNpc[self:getRandomIndex()] |
192 | end |
initDataStructures
DescriptionInitialize data structuresDefinition
initDataStructures()Code
23 | function NPCManager:initDataStructures() |
24 | self.numNpcs = 0 |
25 | self.npcs = {} |
26 | self.nameToIndex = {} |
27 | self.indexToNpc = {} |
28 | end |
loadDefaultTypes
DescriptionDefinitionloadDefaultTypes()Code
32 | function NPCManager:loadDefaultTypes(missionInfo, baseDirectory) |
33 | local xmlFile = loadXMLFile("npc", "data/maps/maps_npcs.xml") |
34 | if xmlFile ~= 0 then |
35 | self:loadNPCs(xmlFile, missionInfo, baseDirectory, true) |
36 | delete(xmlFile) |
37 | end |
38 | end |
loadFromXMLFile
DescriptionLoad data from xml savegame fileDefinition
loadFromXMLFile(string filename)Arguments
string | filename | xml filename |
100 | function NPCManager:loadFromXMLFile(xmlFilename) |
101 | if xmlFilename == nil then |
102 | return false |
103 | end |
104 | |
105 | local xmlFile = loadXMLFile("npcXML", xmlFilename) |
106 | |
107 | if xmlFile == 0 then |
108 | return false |
109 | end |
110 | |
111 | local i = 0 |
112 | while true do |
113 | local key = string.format("npcs.npc(%d)", i) |
114 | if not hasXMLProperty(xmlFile, key) then |
115 | break |
116 | end |
117 | |
118 | local name = getXMLString(xmlFile, key.."#name") |
119 | local npc = self:getNPCByName(name) |
120 | if npc ~= nil then |
121 | npc.finishedMissions = Utils.getNoNil(getXMLInt(xmlFile, key.."#finishedMissions"), 0) |
122 | else |
123 | print("Warning: Npc '"..tostring(name).."' not found!") |
124 | end |
125 | i = i + 1 |
126 | end |
127 | |
128 | delete(xmlFile) |
129 | |
130 | return true |
131 | end |
loadMapData
DescriptionLoad data on map loadDefinition
loadMapData()Return Values
boolean | true | if loading was successful else false |
43 | function NPCManager:loadMapData(xmlFile, missionInfo, baseDirectory) |
44 | NPCManager:superClass().loadMapData(self) |
45 | |
46 | self:loadDefaultTypes(missionInfo, baseDirectory) |
47 | return XMLUtil.loadDataFromMapXML(xmlFile, "npcs", baseDirectory, self, self.loadNPCs, missionInfo, baseDirectory) |
48 | end |
loadNPCs
DescriptionLoad data on map loadDefinition
loadNPCs()Return Values
boolean | true | if loading was successful else false |
53 | function NPCManager:loadNPCs(xmlFile, missionInfo, baseDirectory, isBaseType) |
54 | |
55 | local i = 0 |
56 | while true do |
57 | local key = string.format("map.npcs.npc(%d)", i) |
58 | if not hasXMLProperty(xmlFile, key) then |
59 | break |
60 | end |
61 | |
62 | local name = getXMLString(xmlFile, key.."#name") |
63 | local title = getXMLString(xmlFile, key.."#title") |
64 | local imageFilename = getXMLString(xmlFile, key.."#imageFilename") |
65 | |
66 | self:addNPC(name, title, imageFilename, baseDirectory, isBaseType) |
67 | |
68 | i = i + 1 |
69 | end |
70 | |
71 | return true |
72 | end |
new
DescriptionCreating managerDefinition
new()Return Values
table | instance | instance of object |
16 | function NPCManager.new(customMt) |
17 | local self = AbstractManager.new(customMt or NPCManager_mt) |
18 | return self |
19 | end |
saveToXMLFile
DescriptionWrite data to savegame fileDefinition
saveToXMLFile(string xmlFilename)Arguments
string | xmlFilename | file path |
boolean | true | if loading was successful else false |
78 | function NPCManager:saveToXMLFile(xmlFilename) |
79 | -- save npcs to xml |
80 | local xmlFile = createXMLFile("npcsXML", xmlFilename, "npcs") |
81 | if xmlFile ~= nil then |
82 | for k, npc in ipairs(self.indexToNpc) do |
83 | local npcKey = string.format("npcs.npc(%d)", k-1) |
84 | setXMLString(xmlFile, npcKey.."#name", npc.name) |
85 | setXMLInt(xmlFile, npcKey.."#finishedMissions", npc.finishedMissions) |
86 | end |
87 | |
88 | saveXMLFile(xmlFile) |
89 | delete(xmlFile) |
90 | |
91 | return true |
92 | end |
93 | |
94 | return false |
95 | end |