Introduction
After browsing the forums and the spoiler files, I've noticed that many people have asked the question "How does ammo creation work?" or "Does dungeon level affect the ammo created?" So I thought I'd do a little code diving and see if I could come up with a comprehensible answer.
To begin, I should point out that I'm going to start general and work towards the more specifc. Consequently, the first few explanations will not be exactly correct, but will give an idea of how the system works. Also, this data is relevant to ToME 2.3.1. I haven't looked at the code for 3.0 yet, so this may very well be different there.
Summary
All ammo you create will be at least "Good" quality. The probability of getting "Great" arrows (ego or artifact) is approximately
P(Great) = (1 + 4 * Min( (level + 10) / 200, 1 )) / 5
(This is a probability, not a percentage. This value ranges from 0.24 at town level to 0.64 at dungeon level 100, and of course will be 1.0 at dungeon levels 190 or deeper (such as the Nether realm).)
For those that prefer percentages, just multiply that equation by 100:
%(Great) = 20 * (1 + 4 * Min( (level + 10) / 200, 1 ))
1/30 of the items that will be "Great" will actually be artifacts, unless you are in town in which case you cannot make artifact ammo. (So always make it in a dungeon!) Further the plusses to hit and to damage are affected by the dungeon level. They can range from 1 to 10 for "Good" ammo and 1 to 20 for "Great" ammo, but the higher plusses will be more common at lower dungeon levels.
The function Min(x,y) returns the smallest value of either x or y.
The Details
To begin, ammo creation starts with the ability to create [S]hots, [A]rrows and [B]olts. To make shots, you must simply have the Ammo Creation ability. This ability can be gained in two ways. First, and most common, is getting to level two as an Archer. It should be noted that this includes both Archer sub-classes, the straight archer and the ranger. Other classes may "purchase" the Ammo Creation ability for 8 skill points once their Archery skill is >= 10. (To do this, use the 'N' key in the original keyset. In the roguelike keyset, there is no mapping for this function, so you must use the "underlying" keyset by pressing '\' and then 'N'.)
Once you have the Ammo Creation ability, you can immediately make shots, as mentioned above. Also, once your Archery skill is at 10, you can make arrows, and when Archery reaches 20, you can make bolts.
The first thing you need in order to make ammunition is a source of material. For shots, you need rubble. So put yourself next to some rubble in the dungeon and make ammunition and select [S]hots. The game will then prompt you for a direction; simply give the direction of the rubble relative to you and voila, instant shots.
For arrows and bolts, the process is slightly different. For these types of ammunition, you need some junk from the dungeon, such as skeletons, pottery and skulls. Technically speaking you can use any kind of junk or skeletons. This does not, however, include skeletons that are created as a result of you killing monsters, only skeletons that appear on the dungeon floor "naturally."
Once you select a type of ammunition and the materials from which to create it, the rest of the process is very similar whether you are making shots, arrows or bolts. Since I play mostly bow archers, I'll use arrows in my example, but the process can be generalized to shots or bolts quite easily.
To begin the ammo sub-type is selected. For arrows, this is either regular arrow or seeker arrows. Once the type is selected a quantity is made. The quantity is simply a random number between 15 and 30 with a linear distribution.
Once the type and quantity of arrows is determined, the magic gets applied. All arrows made by you will be at least of "Good" quality, that is they will have have plusses to hit and to damage. Theoretically, if the arrows get promoted to artifacts (as discussed below) there is a miniscule chance that they will be cursed, but that chance is very small. (It happens to my hobbit slingers often enough, though. The important thing to know here is that the ancient foul curse and the aggravation and the experience drain and so forth that your new ammo possesses will not have any effect on you unless you actually wield it in your quiver. Just carrying it in the main inventory and then using it has none of those drawbacks. You'll probably have to enchant it up a bit, though, because it can have substantially negative to-hit and to-damage penalties at first. -- GreyCat)
If the ammo does not make it to the "Great" category, it will be at least "Good," and its pluses to hit and to damage will range from 1 to 10. Specifically, Random(1,5) + BellCurve(0, 5, dungeon_level); where Random(1,5) is simply a random number from 1 to 5 and BellCurve(x, y, dungeon_level) is a normal distribution from x to y with the mean being based on the dungeon level. (I know this is an inadequate (and incorrect) description to those with knowledge in statistics, but suffice it to say that lower bonuses are more common at shallow depths, and the higher bonuses are more common at the greater depths; but any number is possible at any depth.)
Once the pluses are determined, the arrows have a 30% chance of being (exploding) arrows. Their splash damage will be one of the following with equal probability:
Electricity, Poison, Acid, Cold, Fire, Plasma, Light, Dark, Shards, Sound, Confusion, Force, Inertia, Mana, Meteor, Ice, Chaos, Nether, Nexus, Time, Gravity, Kill-Wall, Teleport-Away, Turn All, Nuke, Stun and Disintegrate.
That's it for arrows that end up "Good."
In order to determine whether or not an arrow will be great, the first check is a straight up 20% chance that the item will be "*Great*." (The difference between "Great" and "*Great*" will be discussed momentarily.) Failing this check the arrows then have a (dungeon_level + 10 + luck(-15, 15))/2 percent chance of being "Great," where luck(x, y) is a number (not random) from x to y scaled according to your luck, which is a number from -30 to 30. Thus if you have a luck of -30, this will be -15, if your luck is 0, this will be 0 and if your luck is 30, this will be 15, etc.)
Next your arrows will be checked against the existing (non-random) artifact ammo in the game. "*Great*" arrows will have four chances to become such an artifact, while "Great" arrows will only have one. This chance is quite complex, but suffice it to say that it is very very small, but increases as you delve deeper into the dungeons. Essentially, the artifact must exist, not have already exist in the game, be of exactly the right type (i.e. Seeker Bolt) and pass a depth and rarity test. The depth test passes if the depth of the dungeon is deeper that the normal depth for the artifact. If you are shallower than that depth, then you have a 1 in (artifact_level - dungeon_level) * 2 chance of passing the depth test. If this passes then you do a rarity check. This is essentially a 1 in rarity chance. Though the rarity is modified based on luck by a maximum of rarity/2 in either direction. Thus the chances for passing the rarity test range from 1 in rarity/2 for the most lucky to 1 in 3*rarity/2 for the most unlucky. If all these tests pass, the ammo becomes a regular artifact, and the quantity is adjusted back to one.
Most of the time, however, the ammo will fail the normal artifact creation tests. Now the generation of ego or random artifact arrows ensues. First, the to-hit and to-damage bonuses are generated. These are both Random(1,5) + BellCurve( 0, 5, dungeon_level) + BellCurve(0, 10, dungeon_level).
Once the bonuses are in place, the arrows have a 1/30 chance of being a random artifact. This probability is not based on dungeon level. Further, the qualities of the artifact created are also not based on dungeon level.
If the arrows do not become an artifact, then they will be ego arrows. First off, all your ego arrows will be good egos. Thus you won't get any arrows 'of Morgul' or anything else nasty. The details of how the egos are selected belong in a separate spoiler. I'll just say that the egos are selected randomly, with out of depth egos having a much lower probability of occuring.
That about covers it. I hope you find it useful. I wouldn't be surprised if some small errors crept in there, but I did my best to double check my interpretations of the code and my math for the simplifications.
Chatter
TimmyBravo: Which part of the source is this from? I'd like to take a look for myself.
GreyCat: The prompt for which ammo type to create is in do_cmd_archer() in cmd7.c. The rest of that function's pretty straightforward, I think, but the real magic (pun intended) is done in apply_magic() in object2.c.
TimmyBravo: Cool beans, ta very much.
TRM : I've created artifact arrows in Bree at least twice. They weren't particularly great, and it took about 30-40 junk items but it's still an option. (That was on TOME v2.7.7) Never created more than one set of artifact arrows with the one character after playing about 15-20 Rangers... So am assuming it's near-enough impossible??
Orvin: How about statistics for ammo creation generated from a simulation like the one done recently at Spoilers/Sentient Weapons/Statistics?
NeilStevens: No problem. Do feel free to write such a script and publish it, along with the results, right here.
ToME Wiki