Jump to content

BG2: Creating Forced Encounters


Recommended Posts

Note: Apparantly the scripting command I use here is only available in Bg2 - ToB


If you've played BG2, you'll probably remember the one-off encounter with everyone's (least?) favourite drow ranger Drizzt shortly after leaving the Underdark... Would you like to be able to force similar encounters in your mod? I wanted to, and I've figured out a method for doing, and here it is.


Firstly, the resources I used (which you'll probably have anyway) were:


WeiDU http://www.WeiDU.org

Near Infinity http://www.idi.ntnu.no/~joh/ni

The IESDP http://dragonlance.teambg.net/iesdp/

A calculator with a 'hex' function (thoughtfully provided by Windows :D)


I'll start by describing what I myself was trying to do. I was trying to add an encounter which would trigger once when the player had a certain NPC in their party and left the Umar Hills (AR1100), taking them to a custom area of my design (which I called A#book). I did not want it to be possible to return to A#book once you had been there.


The file WORLDMAP.WMP controls random encounter areas and probabilities. Open it up in Near Infinity, and click on the Edit tab. The bottom field should be called Map Entry 0- click on Edit again.


You should be looking at (well, in my copy of BG2 at least) 13 fields, then 24 named Area Entry x and 164 named Area Link x. The IESDP explains each worldmap area has an Area Entry, and each exit from the area has an Area Link. Looking in the Area Entry for AR1100 (number 7), I see this uses Area Link fields 57-66 (it shows which ones are exits North, South, East and West).


Go examine one of them, and look in the IESDP too. You'll notice that all the Area Link entries for AR1100 have None specified in the Random Encounter section: So firstly, we need to fix that. What I'm going to do is use WeiDU to change Random Encounter Area 1 for each area link to read A#book. The IESDP tells us this field is stored at offset 2c in the Area Link, so we add this to the offset of the start of the Area Link and use WRITE_ASCII to fill in our custom area. I did this for all area links from AR1100- I want to make sure the encounter is accessible from wherever you try to go. A couple of points to note:


1. You don't need the .ARE extension for the custom area when you WRITE_ASCII.

2. WeiDU doesn't seem to want to install if you use the number in hexadecimal, so convert your offset back to decimal.

3. The offset between each area link is D8, or 216, so you can simply add this value to work out the offset of the next area link you need to patch.

4. The worldmap file is saved in your save games too. So, to see the changes you make, you need to start a new game after patching WORLDMAP.WMP. I made the mistake of not doing so a few times and was most dismayed why it didn't appear to have worked.


My .TP2 code then looked like this. Note the comments (//text) are purely for my visual aid:


//Creating random encounters from Umar Hills (AR1100, Area links 57-76) to custom area A#book.

COPY_EXISTING ~Worldmap.WMP~ ~override/Worldmap.wmp~

WRITE_ASCII ~18556~ ~A#book~ //link 57

WRITE_ASCII ~18772~ ~A#book~ //link 58

WRITE_ASCII ~18988~ ~A#book~ //link 59

WRITE_ASCII ~19204~ ~A#book~ //link 60

WRITE_ASCII ~19gibberlings3.netgibberlings3.net420~ ~A#book~ //link 61

WRITE_ASCII ~19636~ ~A#book~ //link 62

WRITE_ASCII ~19852~ ~A#book~ //link 63

WRITE_ASCII ~20068~ ~A#book~ //link 64

WRITE_ASCII ~20284~ ~A#book~ //link 65

WRITE_ASCII ~20500~ ~A#book~ //link 66

WRITE_ASCII ~20716~ ~A#book~ //link 67

WRITE_ASCII ~20932~ ~A#book~ //link 68

WRITE_ASCII ~21148~ ~A#book~ //link 69

WRITE_ASCII ~21364~ ~A#book~ //link 70

WRITE_ASCII ~21580~ ~A#book~ //link 71

WRITE_ASCII ~21796~ ~A#book~ //link 72

WRITE_ASCII ~22012~ ~A#book~ //link 73

WRITE_ASCII ~22228~ ~A#book~ //link 74

WRITE_ASCII ~22444~ ~A#book~ //link 75

WRITE_ASCII ~22660~ ~A#book~ //link 76


Note that since there were originally no random encounters from AR1100 the probability of one was set to 0: This is good for us. You may need to set it to 0 yourself if you plan to use an area with existing random encounters, though.


So, now there is an encounter area to link to. You just need to use scripting to trigger it. As I said before, I wanted my encounter to happen the first time party left the Umar Hills with my NPC certain conditions, so I added the following to my NPC's override script. Note I've removed the conditions but left in a global variable to remind you you need to set one to make sure it only happens once!



(Some Conditions)































This sets the probability of encounters from AR1100 to basically anywhere else on the worldmap (as these areas are the other places with Area Entry entries in WORLDMAP.WMP) to 100. And since we've set the custom area A#book as the random encounter area, this is where they'll go. Simple! :D


Finally, you need to at some point set the probabilities of encounters back to 0 to ensure you don't keep revisiting the area. This is again simple:



(Some other conditions)































Thanks to SimDing0 and MaxTeamBG for helping me figure some of this stuff out. :D

Link to comment

One thing I've been meaning to add to this discussion is the idea that I believe the engine "reads ahead" for additional information for random encounters. AR2601, Drizzt's random encounter area, is set at a 100% probability when traveling from the Underdark Exit to the City Gates. (Excluding the three wilderness areas, going anywhere from the Underdark Exit necessitates traveling this link due to the setup of the worldmap.) However, you only encounter Drizzt once, the first time you travel this link. Nothing ever reduces the random encounter probability down from 100%, so there must be another reason why you only visit AR2601 once.


My theory, consistent with my observations but in no way rigorously tested, is that the engine reads ahead to the random encounter area. If it evaluates the probability of an encounter to true, it checks the area script of the random encounter area. If it can find a script block with a true OnCreation() block, then an actual random encounter occurs. If no OnCreation() blocks are true, then no random encounter occurs--this is why you only have a set number of random encounters, even travelling around Athkatla, and why you only encounter Drizzt once.

Link to comment

We were talking about this in chat, and mr. Ding0 said:


(19:32:28) (SimDing0) You don't need to fuck around with SetEncounterProbability. If you set the encounter probability to 101, it occurs once, then never again.


EDIT: What's with all these double-posts today?

Link to comment

Okay, well. Just checked my coding, it works like a charm.


I can't be bothered to explain every little detail in it now, all the other tp2-ninjas will understand what I'm doing, and the rest can ask and I will answer. :undecided:


EDIT: Oh, yeah, I better put in the disclaimer I did somewhere else:


Note that I coded this before I actually realised that there is a "# of area entries" inside the .wmp, and I was too proud over my own code for keeping track on the number of area entries to change it. I did add an additional check, though. Also note that I added a check for the area from G3A. I dunno if there's any other mods adding areas to the worldmap which doesn't use AR****.are, but I'll add them if needed.


// Update the worldmap to make sure the random encounter in between Waukeen and Slums happens
COPY_EXISTING ~worldmap.wmp~ ~override~
 READ_LONG 0xc "map_off"
 SET "entry"       = ("%map_off%" + 0xb8)
 SET "outer_check" = 0
 SET "inner_check" = 0
 SET "num_ent"     = 0
 WHILE ("%outer_check%" = 0) BEGIN
   READ_ASCII ("%entry%" + 0x8) "area" (2)
   READ_ASCII ("%entry%" + 0x8) "spec_area" (6)
   WHILE (("%spec_area%" STRING_COMPARE_CASE "AR0700" = 0) AND ("%inner_check%" = 0)) BEGIN
     READ_SHORT ("%entry%" + 0x50)                   "nlink"
     READ_SHORT ("%entry%" + 0x50 + 0x8)             "wlink"
     READ_SHORT ("%entry%" + 0x50 + 0x8 + 0x8)       "slink"
     READ_SHORT ("%entry%" + 0x50 + 0x8 + 0x8 + 0x8) "elink"
     SET "inner_check" = 1
   PATCH_IF (("%area%" STRING_COMPARE_CASE "AR" = 0) OR ("%area%" STRING_COMPARE_CASE "G3" = 0)) BEGIN
     SET "num_ent" = ("%num_ent%" + 1)
     SET "outer_check" = 1
   SET "entry" = ("%entry%" + 0xf0)
 SET "patching_done" = 0
 READ_LONG  0x30 "area_num"
 WHILE (("%patching_done%" = 0) AND ("%num_ent%" = "%area_num%")) BEGIN
   SET "1stlink" = (("%map_off%" + 0xb8) + ("%num_ent%" * 0xf0) + ("%nlink%" * 0xd8))
   SET "2ndlink" = (("%map_off%" + 0xb8) + ("%num_ent%" * 0xf0) + ("%wlink%" * 0xd8))
   SET "3rdlink" = (("%map_off%" + 0xb8) + ("%num_ent%" * 0xf0) + ("%slink%" * 0xd8))
   SET "4thlink" = (("%map_off%" + 0xb8) + ("%num_ent%" * 0xf0) + ("%elink%" * 0xd8))
   PATCH_IF ("1stlink" > 0) BEGIN
     WRITE_ASCII ("%1stlink%" + 0x2c) ~ARSC#0~
     WRITE_SHORT ("%1stlink%" + 0x54) 101
   PATCH_IF ("2ndlink" > 0) BEGIN
     WRITE_ASCII ("%2ndlink%" + 0x2c) ~ARSC#0~
     WRITE_SHORT ("%2ndlink%" + 0x54) 101
   PATCH_IF ("3rdlink" > 0) BEGIN
     WRITE_ASCII ("%3rdlink%" + 0x2c) ~ARSC#0~
     WRITE_SHORT ("%3rdlink%" + 0x54) 101
   PATCH_IF ("4thlink" > 0) BEGIN
     WRITE_ASCII ("%4thlink%" + 0x2c) ~ARSC#0~
     WRITE_SHORT ("%4thlink%" + 0x54) 101
   SET "patching_done" = 1

Link to comment

Since spam bumped this post, I should point out that the engine handles random encounter probability by (continuously?) subtracting 100 from the encounter probability when the probability is greater than 100. The first time you encounter Drizzt, the random encounter fires and the probability is reduced to 1% (so technically, it should be possible to revisit the encounter area even in the default game).

Link to comment

I have some questions about Forced Encounters. And believe, that this is the best topic to post them.


1)Nightmare asked:

Is it possible to force a random enounter from another random encounter area?

But how technically is it possible?

encounters are controlled by Worldmap. And technically encounter-areas aren't placed on worldmap. So, we can't set encounter probability from encounter area to destination area.


2)There are four places for setting encounter areas. If I add for example three to one link, what determines, whitch area will be used to encounter?

I can set using weidu probability for each encounter. But can I in-game change probability for only one of thoose encounters?

Link to comment


This topic is now archived and is closed to further replies.

  • Create New...