Talk:Pig

This talk page is for discussing Pig.
  • Sign and date your posts by typing four tildes (~~~~).
  • Put new text below old text.
  • Be polite.
  • Assume good faith.
  • Don't delete discussions.

Mood

Does the Pig's mood affect the quality of generated truffles? XaqNautilus (talk) 13:13, 4 February 2018 (UTC)

Extra Truffles

Every time a pig produces a truffle, there is a 2/3 chance of it producing another. The first is guaranteed. We can work out the chances of second and subsequent truffles and add them up. The first ten terms in the infinite sum are

1 + (2/3) + (4/9) + (8/27) + (16/81) + (32/243) + (64/729) + (128/2187) + (256/6561) + (512/19683) + ……

Simplifying to the same denominator as the tenth term

1 + (13122 + 8748 + 5832 + 3888 + 2592 + 1728 + 1152 + 768 + 512 + ……)/19683

which is

1 + (38342/19683)

or

1 + 1.94797……

As you can see, the second to tenth terms add up to almost two, and the sum to infinity is indeed 2. And remember, that's in addition to the guaranteed first truffle.

so, an optimal pig will produce an average of three truffles per day, in the long run

One More Day (talk) 21:39, 10 November 2018 (UTC)

Ok. The article had not said that the first one was guaranteed. I was figuring there was only a 2/3 chance of a first one, putting my count off by one. Butterbur (talk) 05:50, 11 November 2018 (UTC)
Something is still incorrect about the mechanics here. Despite my best efforts, I am unable to replicate an average of 3 truffles per day. With 12 pigs over 28 days, maxed friendship and the Shepherd profession, and grass-free pens (grassy tiles can significantly reduce the truffles found each day) I get about 2.35 truffles/day per pig. If this math is even correct about how extra truffle probability is determined (what's the source?), the probability of getting that first truffle at all and/or pathfinding and time considerations may also play significant roles.Farmonymous (talk) 16:03, 10 January 2020 (UTC)


Well, I have yet to be convinced that one truffle is guaranteed, experience being contrary. However, I've never optimized production as One More Day speaks of. I tend to think that the amount of grass-free space affects the count, up to who knows how many pigs in whatever number of open tiles. I'm not sure the pig pen needs to be grass-free if there's enough room total. I've never selected the Shepherd Profession.
And that's because truffle count isn't the only factor to consider. I turn all or almost all truffles into truffle oil, an excellent profit center, especially while one is still developing the farm and in need of cash. Which is why I generally choose Tiller / Artisan professions. The truffle oil then sells for 40% more, which requires only 5/7 of the truffles to produce equal profits. I haven't kept tabs on max or average truffle production, because there are other affected profits that figure higher in my strategy. But I can say that I often get more than 2 per pig per day, probably over 2 on average. In truffle oil, these 2 truffles are as good as oil from 2.8 truffles in non-artisan oil. Perhaps I might even reach that 3/day equivalency?
There's more than one way to skim a profit.  ;) Butterbur (talk) 02:10, 14 January 2020 (UTC)
Here's an update to these old numbers. I'm not sure why precisely my older 2.35/day estimate was inaccurate, but I've attempted the experiment again. In practice I think it's probable that area boundaries and the presence of other truffles on the ground inhibits the production of more truffles. With that in mind I setup an enclosure with no grass, a larger area, my barn located at the top and center of the enclosure (attempting to increase the space between barn door and pen boundaries), and the barn door open at all times (maximizing time pigs spend outside). I was able to boost the average truffle production to ~2.66 truffles/day per pig. (Interestingly, I did NOT use the Shepherd profession this time.) That's still not 3/day however. A question I've been interested in answering is what amount of space is necessary for a pig to maximize its potential truffle production.
Our probability series is indeed (1 * 2^1)/(3^2) + (2 * 2^2)/(3^3) ... (x * 2^x)/(3^[x+1]) and its sum from x=0 to infinity should indeed = 2 extra truffles on average. But how would we actually fully reach an average of two in practice? We would in practice need to observe rare situations where a pig produces 5, 6, 7, 10... even 20 extra truffles in a given day. In fact we would need to observe the entire spectrum from 0 to an infinite number of extra truffles. In practice, a single pig's ability to find a large number of truffles in a given day is going to be limited by the finite number of open tiles available to that pig and the finite amount of time in which a pig can engage in searches from 6am to 5pm. Thus the real average number of truffles will be something less than 3. The question is how much less.
To further estimate the real limits to daily truffle finding, I would like to know more about how the truffle search behavior works. Is a random check done to initiate searching behavior every game tick or perhaps every 10 in-game minutes? Farmonymous (talk) 19:21, 17 October 2020 (UTC)
The basic answer is that the chance is 0.0002 every game tick. Which is probably as important in limiting the truffle rate as friendship/66% chance of multiple truffles.
The relevant code is:
  • FarmAnimal::findTruffle actually generates the truffle. This is where Animal Friendship/1500 is used to decide whether another truffle is possible that day.
  • FarmAnimal::behaviors calls FarmAnimal::findTruffle, but only if all the conditions are met; this is where the 0.0002 chance is imposed. (It also handles the condition that the pig's bounding box cannot overlap with any terrain features or objects -- grass and flooring are both terrain features).
  • FarmAnimal::updateWhenCurrentLocation and FarmAnimal::updateWhenNotCurrentLocation call FarmAnimal::behaviors -- and those functions eventually lead back to the primary game update loop, i.e., once per game tick not once per 10 in-game minutes.
I'm not sure what the ticks/second rate is in the game (is it hardware dependent?), but I'm surprised even 2.66 truffles/day are possible. Assuming 20 ticks/second gives 10080 ticks in 12 game hours; 10080*0.0002 implies only ~2 truffles on average. With the potential for pretty large fluctuations -- I'm guessing there might be no real difference between your 2.35/day test and your 2.66/day tests, but just random variations. At 10080 ticks there's also a 13% chance of no truffles, i.e., 1 truffle is not guaranteed.
Plus, some fraction of the ticks will inevitably fail to meet the necessary conditions. Truffle-finding is only possible if the pig isn't eating or trying to find food. Whenever the pig's fullness is less than 195, there's a 0.002 chance (10 times more likely than truffle-finding) that the pig will go in search of grass -- using up time and leaving the pig standing in a grassy spot where truffles aren't possible. If there's no grass in the enclosure, I'm guessing the pig still heads in the direction of grass outside the enclosure, then maybe gives up when it hits a fence?
Other limitations are time spent in the barn, the chance that any time after 5pm the pig will head to the barn, etc. Fraction of ground covered in grass/flooring. I don't think the ground filling up with truffles directly prevents more truffles -- but indirectly it would limit the places where a pig can stand and probably increase the chances that pigs go back in the barn.
Hope this helps. Nebulous Maestress (talk) 23:46, 22 October 2020 (UTC)
Many thanks! This matches my experience much better, especially with regards to the variability and distribution of truffle finds. Average daily yield approaches 2 per pig only when friendship is high and the animal is reasonably happy, can go a bit higher at max levels, but can also drop to half that or less on poor yield days. It is fairly common for truffles to appear in clumps by the end of the day, sometimes as many as five adjacent (with enough pigs). (Maybe they feel cramped?) And it is also not uncommon to have them appear outside the pig pen enclosure, on tiles adjacent to the barriers, be they fences, tea bushes, trees, Lightning Rods, or the like - and this even when there's another truffle directly opposite on the inside of the barrier. A pig's bounding box never seems to reach far enough to produce truffles two tiles outside the pen. Butterbur (talk) 16:01, 23 October 2020 (UTC)

Truffles on Pathing?

Perhaps it is a trick of my memory, but I think I have seen truffles appear on top of paved tiles. I can't say for sure about paved tiles inside the animal enclosure, but it's another thing outside the enclosure, where I often place walkways. At any rate, I've never been sure about why the game places any truffle where it does, and that is especially so for those that appear on the opposite sides of obstacles like fences. The pig can not be present on such a tile when digging, neither can it dig on the tile where an obstacle is. Why does the truffle appear then two tiles away from the nearest possible digging point? For that matter, why do they appear on grass tiles inside a pen?

My guess is that truffles are (sometimes?) placed adjacent to the digging location. On grass, they always seem to be at the borders between grass and clear tiles. Perhaps a tile on the inside of a pen next to its border is dug and the game first decides it should appear on a border tile, which is found to be obstructed, and is then moved outside the pen? And the game decides that if that tile is paved, there is no reason why the truffle can't be there (just as a torch or piece of equipment can be placed on pathing)?

This entry is a reminder to myself to look further, but perhaps someone else already has noticed something related.(?) Butterbur (talk) 16:29, 25 October 2020 (UTC)

The basic answer is that pathing (plus grass, tilled dirt, etc.) prevents pigs from digging for truffles, but it's irrelevant when deciding where a dug-up truffle is placed -- and a truffle is never placed in the tile where it was dug up.
There are two separate location-related checks when trying to spawn a truffle, which handle terrain features completely differently.
  • First question: is the pig standing in a location where it can dig for truffles?
  • Digging is cancelled if the pig is on a tile containing any terrain feature or object.
  • Specifics: FarmAnimal::behavior has a check that looks at each of the four corners of the pig's bounding box; if just one corner is on a terrain feature/object tile, truffle-spawning is cancelled.
  • Second question: where should a dug-up truffle be placed?
  • The code effectively spirals outwards from the pig's location. The truffle is placed in the first valid tile that is found. Multiple criteria are relevant here, including:
  • A tile is invalid if it contains any object.
  • A tile is invalid if any farm animal, monster, or player is standing in it (specifically, if any corner of the character bounding box is in the tile).
  • Terrain features only make a tile invalid if:
    • The terrain feature makes the tile impassable (e.g., bushes, trees, stumps, equipment)
    • On tilled soil, if the tile contains a seed or crop.
  • Specifics: FarmAnimal::findTruffle calls Utility::spawnObjectAround, which tests a requested tile, then keeps testing all tiles adjacent to any previously-tested tile (resulting a spiral-type pattern). The tests require a tile to be non-water and not fail GameLocation::isTileOccupiedForPlacement. The truffle is spawned in the first valid tile that is found. Up to 100 tiles will be tested (creating a diamond-shape region up to 7 tiles away in every direction), otherwise truffle-spawning fails (theoretically reducing the truffle-production rate, but probably only in unusual cases).
  • GameLocation::isTileOccupiedForPlacement is the same basic test used when you try to place items such as torches and equipment. (As always, details get muddy, with a host of special conditions for various items, covered in Object.canBePlacedHere, but the default/fallback test is GameLocation::isTileOccupiedForPlacement).
One key implication is that the truffle always appears at least one tile away from where it was dug up. At least if you assume the truffle is dug up on one of the tiles where the pig is standing (which is what is implied by check #1). The fact that the pig is standing there makes truffle placement impossible, based on the any-farm-animal test in check #2.
In general, clutter could force truffles to appear outside the fence (or any location accessible to the player but not the pig). Or if many farm animals are clustered near the pig, the truffle might be forced to spawn far away -- even though the enclosure looks relatively uncluttered when you later go to collect the truffles. Nebulous Maestress (talk) 19:13, 25 October 2020 (UTC)
Ah! So a pig must be standing at least two tiles away from fence or enclosure or it cannot dig because of the obstruction. And I have seen a truffle spawned outside the fence, at least three tiles away from that. You say it can spawn up to 7 tiles away in the right circumstances. But in hundreds of spawns in my experience, I have never seen a truffle two tiles outside a fence or enclosure, so it would seem that three tiles is the limit one is at all likely to find in play. But now I know to watch for that "outside" chance. Thanks! You are indeed Maestress of the Nebulous! Butterbur (talk) 05:59, 26 October 2020 (UTC)
Return to "Pig" page.