Adding new Missions

S3rgeus

Emperor
Joined
Apr 10, 2011
Messages
1,270
Location
London, United Kingdom
Missions is a very confusingly named table (IMO) in ../Assets/GamePlay/XML/Units/Civ5Missions.xml (and a corresponding one for G&K). It's basically all the unit actions that just 'do stuff', rather than constructing things. (All of those improvement building actions seem to be in Civ5Builds.xml in the same folder.) When I say these actions just 'do stuff', I mean things like "Spread Religion" or "Fortify". These are actions that change the state of the unit/game in different ways for each mission. (Obviously, building a route (roads, railroads) to somewhere is very different from spreading religion, but they're both missions.)

Now, I'm trying to understand how one goes about adding a new mission that players can use (and the AI too, but I'm not yet sure where it gets its mission-related info). As far as I can tell, which missions are available and which missions a unit can perform (so the button actually shows up) are hardcoded in CvUnitMission.cpp (in the DLL). I'm working on a total conversion mod, so I have no problems with compatibility (in that I'm abandoning the distant hope of compatibility with anything). But I want to make sure I'm not missing something.

Has anyone found a way to add new missions without DLL changes that simply test for that mission case in CvUnitMission.cpp? (I would post in the SDK/Lua subforum, but if there is an XML solution to this, I wouldn't want someone who knows the answer to miss it.)

As far as I've seen it's not possible, but I want to make sure. And, if it does turn out to be impossible, would people be interested in a more modular system that relies properly on the DB instead of a massive enum and a huge else-if block? That way new missions could be added to the DB and their success/capabilities could be exposed through Lua events. (I have ideas about how to do this, but want to make sure I'm not missing some glaringly obvious alternative to rewriting the mission-handling code.)
 
As a side note, when I was writing this I thought about how nice it would be to have a way to add missions via xml and just handle them with an event in lua. And have the button just show up. However, the current API seems a long ways from that, and you'd be looking at a total re-write of UnitPanel.lua, UnitPanel.xml and UnitPanelSmall.xml at a minimum. Plus I think the AI code is buried in the source they haven't released to us yet.
 
SUCCESS! This may actually be simpler than I expected.

You've got a nice way of doing it (much nicer than mine in terms of compatibility, since I'm replacing the DLL). As I understand it, you're rewriting UnitPanel.lua so you can create your own separate events that you can respond to in other Lua code. That's a handy way of keeping track of unit escorting you've made too, my initial thoughts on how to do that would've required a few more massive loops!

I've just managed to get a DLL change working that let me add a new mission via XML without modifying UnitPanel.lua. (I never would've guessed we'd find code that actually adapts to new circumstances in all this.) Using that change as a template, I should be able to make a DLL change that allows new missions to be added via XML and their visibility/functionality controlled by hooking onto some new Lua events. Screenshots to follow in a moment, once I deal with an unrelated CTD.
 
All right, I've attached two screenshots, one showing the new 'custom mission' at the top of the unit's action panel, the second after I've clicked it.

I've also attached a diff of the DLL changes I made to make this work (for interested parties). Bonus points to anyone who knows what kind of mod I'm working on from this information. The attachment wouldn't let me attach a .patch file, so I've changed it to .txt, but if anyone wants to open it in NotePad++ with syntax highlighting, just rename it to have the .patch extension.

Interestingly, the new mission didn't use up the unit's moves (so I need to look into how to do that). The unit's action panel also displayed the "cancel mission" icon (quite prominently) in its action panel after it performed the mission. So I need to work that one out too.

But it does work! It's a proof of concept for replacing the direct functions I've used (shown in the attached diff) with calls to lua events and DB queries.

EDIT: I've just noticed there's a unit standing on water in my second screenshot (the after custom mission one). That's a simple Lua error on my part! Forgot to check if the unit can stand on the tile before spawning it.
 

Attachments

  • Custom Mission Clicked.jpg
    Custom Mission Clicked.jpg
    467 KB · Views: 135
  • Custom Mission Icon.jpg
    Custom Mission Icon.jpg
    391.3 KB · Views: 84
  • new_mission.txt
    new_mission.txt
    7.1 KB · Views: 86
Nice!! Where were you a week ago? :) We can only use one DLL right? If so you and Whoward should definitely talk.

Thanks for the feedback on my code, too.

Ah, I've been quiet on the CiV modding scene for a long time now. I've wanted to come back to it but haven't had time. (didn't make much first time, just one unreleased mod) With one of my other personal projects winding down now, I've been looking at coming back to modding CiV. I've been keeping up with this forum while I've been 'gone' though, and I'd definitely like to ask Whoward about if he'd like to include something like this mission change in his DLL. I would understand if he didn't want to introduce other people's code into his codebase though. I'm hopeful though, the changes I'm working on aren't actually very complicated, it just takes a long time to work out where to look.
 

Nicely done! I'm working on a Wheel of Time total conversion, in which the Horn of Valere is first of many new game systems! So making the Mission system (and hopefully the notifications system too) more flexible and accepting of new additions will save me a lot of work in the long run. As for the bonus points, I'm sure they'll come in handy when playing the mod eventually. :D
 
Awesome sauce, another good sign! I've made a small modification that allows me to decide when the custom mission is 'visible but disabled' (like when you've got the tech to promote a unit, but it's not in your territory). And a nice tooltip and everything! I've attached a screenshot of it. This also seems to have had the side effect of removing the 'cancel mission' icon from the list as well! Two birds, oh yes.

Now, as much as I'd love to implement the whole 'generic' custom mission system (my current changes are still hardcoded to the specific mission I've added, like Firaxis' old ones were) right now, it's 5 AM, and I should sleep. I'll see if I can get the generic system working tomorrow evening.
 

Attachments

  • Capable but Currently Unavailable Mission.jpg
    Capable but Currently Unavailable Mission.jpg
    471.1 KB · Views: 64
Back
Top Bottom