Algorithms sample compendium

Here I'll put some quick and easy algorithms I made for mods I'm working on/published.
I used the Top-Down Refinement method to do all of these, as usual.

All these algorithms are intended to be implemented as real, working mods.
I made them public because I hope this helps you make better mods; better mods I will benefit from, as well.

If you think you can do better than me, I ENCOURAGE YOU TO DO IT.
Modifying things because we think we can do things better than others is what modding is all about[1], no? 😃

Table of contents

▶︎
all
running...

Auto unequip ammo

Auto (un)equip ammo on player
==============================
React when player changes weapon
  Get what kind of weapon it was
  Remember and unequip ammo if unequipped bow or crossbow
  Restore ammo if equipped bow or crossbow


Get what kind of weapon it was
===============================
  It's a bow if it has keyword "Bow" or has the "bow" word somewhere in its name
  It's a crossbow if it has keyword "Crossbow" or has the "crossbow" word somewhere in its name
  I don't care about other types of weapons


Remember and unequip ammo if unequipped bow or crossbow
========================================================
  Save id of bow ammo if unequipped weapon was a bow
  Save id of crossbow ammo if unequipped weapon was a crossbow
  Unequip ammo


Restore ammo if equipped bow or crossbow
=========================================
  Equip remembered bow ammo id if weapon is a bow
  Equip remembered crossbow ammo id if weapon is a crossbow


Equip remembered (cross)bow ammo id if weapon is a (cross)bow
==============================================================
  If ammo id was not found in inventory, take other
  If ammo id was found, use it


If ammo id was not found in inventory, take other
==================================================
  Either the strongest or more abundant (let player configure it)

Notes

When doing this algorithm I didn't know Weapon.GetWeaponType() existed, so you will see some steps are more complicated here than in implementation.

I still left my old ideas so you can see how implementation can change compared to pseudocode.

Implementation changes

Note

I left paragraph above intact to show you how an algorithm may evolve as you start implementing things.

I wrote the "Notes" section above before starting doing the actual implementation.
That's why next ideas are disconnected from that section.

Once I started implementing the algorithm above, I decided it was better to Keep It Simple and let Skyrim or other mods to decide which ammo to equip, so the actual algorithm ended up being this:

Auto unequip ammo on player
==============================
React when player changes weapon
  Get what kind of weapon it was
  Remember equiped ammo is equipped weapon was Ammo
  Remove remembered ammo if unequipped weapon was a (cross)bow

Get what kind of weapon it was
===============================
  if Form it's no Weapon
    return "ammo" if it's Ammo
    else return "None"

  return "bow" if getWeaponType == 7
  return "crossbow" if getWeaponType == 9
  else return "other"

Yeah that's it.

In my own algorithm writing style I seldom use if, while, for, etc in the same way you would use them in real code[2].
As you have seen, I mostly use indentations to refine ideas rather than show something belongs to a "block of code".

You know, most of the time I actually like my algorithms to be at a higher (conceptual) level than dealing with the specific language implementation.

Still, I made an exception with Get what kind of weapon it was because I thought specifics needed to be elaborated for illustration purposes for this article.

Personally, I would have left it as this:

Auto unequip ammo on player
==============================
React when player changes weapon
  Get what kind of weapon it was
  Remember equiped ammo is equipped weapon was Ammo
  Remove remembered ammo if unequipped weapon was a (cross)bow

Wrapping up

You have seen how:

Tip

Modern computing is so versatile because programs (software) are living, evolving things, unlike first computers ever.

Your algorithms should also be living and evolving things.


  1. And isn't creating new and better things is precisely what it makes it a so pleasurable activity? ↩︎

  2. Unless the algorithm concept is so complex I really need to do that to get a good idea of what I need to do. ↩︎