Algorithms 101

Pft... the slogan for this page is Things you won't find anywhere else or some shit like that, and what have I been done? Talking about coding and coding... what everyone and their grandmas are already doing.

Hell, even some no-dick pseudo periodists[1] have dared to talk about coding!

He he he! Today we'll learn about polymorphism.

It's trivial to find whatever you want about classes, inheritance, design patterns[2], high order functions, closures[3], combinators... and all that kind of shit that makes you look sophisticated.

But you know what's actually hard to find? HOW TO FUCKING MAKE AN ALGORITHM.
No big deal, really. It's just THE MOST FUCKING BASIC AND IMPORTANT THING FOR PROGRAMMERS.

Remember I said that shitty coders were the ones that give scripts a bad name?
Well, they are so awfully incompetent because they don't know how to create algorithms.
And how can I blame them, if no one ever talks about the topic?

Here I'll share with you my favorite method for making them.
It's easy, none convoluted at all and has served me well over the years.

Don't let its simplicity to fool you.
When I was studying computer programming at university, this technique consistently produced programs that were way faster and stable[4] than those made by my yellowish underwear clad fellow students who thought they were Von Neumann's reincarnation.

But in practice, those guys barely qualified as Region 4 Urkels.

Algorithms

Let's first translate that word to the language people with clean underwear speaks.

What's an algorithm

A list of instructions for the computer to follow.

That's it. That's all there's to that big word.
Algorithms are basically the same as cooking recipes.

Here's a more detailed list of what an algorithm is:

  1. Instructions are ordered.
  2. They are executed sequentially; that is, one after another.
  3. All instructions must finish. That is, if an instruction never finishes, you have no algorithm.

Mmmmh... that list above looks suspiciously like an algorithm, actually.

Now that we now what an algorithm is, we can learn a technique to make them.

Top-Down Refinement (or something)

Of course I didn't invent this technique.

I don't even remember if that's the correct name because I learned about it many years ago and I can't even remember if the book was written in spanish (my mother language), but that is quite unlikely[5].
If I'm not mistaken, I learned it from the Deitel & Deitel C++ book[6].

The method goes like this:

  1. Put in simple words what you want to do.
  2. Break that idea into simple steps.
  3. Repeat steps 1 and 2 until you have a program.

That's it.
I told you it was deceptively simple.

How to apply it

Let's make a simple algorithm in pseudocode.
The magic of pseudocode is that you can easily translate it to any single language with almost no effort.

You always start with a one liner overview of what you want to accomplish.
The more succint, the better.

I'll make an algorithm for an upcoming mod I want to do in a near future.
I usually write this kind of things in a txt file or some scrap piece of paper.

Make an NPC to suicide after blackmailing him/her

That's quite simple. Let's refine it.

Make an NPC to suicide after blackmailing him/her
  Show some incriminating proof
  Suicide

As you can see, I didn't jump straight into complex implementations right away.
I just put a simple overview of how I'm going to accomplish my goal.

Let's refine the suicide part.

Suicide
  Select a suicide method
  Suicide

I'm actually doing this algorithm from zero at the moment of writing this article, so you are actually watching in real time how to do an actual working algorithm.

Sometimes ideas just flow when you are doing things.
In this case, I never had in mind having many different suiciding methods[7] in my original envision of making NPCs kill themselves, but I'm liking that idea, so I'll go along with that.

Anyway, we can refine the suicide method selection:

Select a suicide method
  Randomly select between:
  - Pick an unarmed fight with a guard
  - Jump from some high place
  - Eat some poison

And each one of those methods can be further refined.
I'll do that in place for the sake of saving space. This article is getting too long for my liking.

Select a suicide method
  Randomly select between:
  - Pick an unarmed fight with a guard
      Unequip current weapon
      Attack nearest guard

  - Jump from some high place
      Put some marker in some high place in the CK
      Make the NPC run to it
      Make the NPC to jump down the abyss when the marker is reached

  - Eat some poison
      Add jarrin root to the NPC
      Consume it

As you can see, at this point now we are doing actual implementations.
Now we are dealing with things that can be coded and be done in the Creation Kit.

You can see this is still quite dumb because I haven't take into account what would happen if guards aren't near or there's no marker to be found[8] (I'll most likely make them poison themselves when that happens).
BUT HAVING AN OVERVIEW AT ALL TIMES IS WHAT EASILY MADE ME REALIZE THIS.

Warning

The most common source of bugs is letting unmanaged situations roam free.

"Unmanaged situations" are things like what you have seen: things you didn't take into account they could happen when designing your program.

Warning

If your scripts have unintended side effects, unmanaged situations are the most likely culprit.

Of course, it a rare ocurrence that you can foresee every single possibility right at the design stage, but that's what debugging is for.

Tip

Don't be afraid to make changes to your code once you have written it.

Think programs are like statues: you carefully chip small parts of it until you reveal the final form.

It's quite rare for a program to work flawlessly at first release[9].

Let's put together what we have done so far.

Make an NPC to suicide after blackmailing him/her
==================================================
  Show some incriminating proof
  Suicide

Suicide
=========
  Select a suicide method
  Suicide

Select a suicide method
========================
  Randomly select between:
  - Pick an unarmed fight with a guard
      Unequip current weapon
      Attack nearest guard

  - Jump from some high place
      Put some marker in some high place in the CK
      Make the NPC run to it
      Make the NPC to jump down the abyss when the marker is reached

  - Eat some poison
      Add jarrin root to the NPC
      Consume it

At this point I don't know if any of these particular implementations will work, but that doesn't matter too much, since the overview is already there.

You can also see I have everything inside a single *.txt file because that way I always have an overview of what I'm doing, so it's quite easy to find and correct bugs when things aren't working as expected.

Of course, since this isn't a programming language you have quite a lot of freedom on how you can write your algorithm.
My own style may not suit you and you will want to do it your own way.

Eventually develop your own writing style

It's perfectly valid if you do so.
It means you are making this technique your own and you actually UNDERSTAND it.

As long as you follow the rule of refining step by step, you will be fine.

As a fun excercise, you can further refine both Select a suicide method and Show some incriminating proof so you can do all kind of crazy and funny things[10].

Wrapping up

Now you should have a good insight of how to do algorithms.

If you need more examples, here's and algorithm I did for an actually working mod that I haven't yet released at the time of writing this.
As you would expect, I'll move it to Skyrim Platform.

You will see some ideas for my designed algorithm are different from their implementations, but that's quite normal and depends on the limitations of whatever language[11] you implement your algorithm on.

Anyway, good luck with your mods.
Hopefully now you actually have a real tool to make them.


  1. Almost ALL "periodists" have been (and will continue to be) no-dick, no-brain "periodists", actually.

    It's quite an imbecile state of affairs I have to make these kind of clarifications, but such is this over-politized world we live in.

    I don't give a shit about "lefts and rights" because that idiocy doesn't even exist in my country and if it were, both sides are full of loud, annyoing idiots I don't want to have anything to do with, anyway. ↩︎

  2. If you can get a hold on this book, you really should.
    EVERYONE can understand it. ↩︎

  3. I'll talk about some of those here.
    In my opinion, those are more useful and easy to use for us, Skyrim modders, than all Object Oriented stuff. ↩︎

  4. I remember the first time I learned about a technique called "backtracking".
    Turned out I was already using it regularly thanks to the algorithm design technique I'm about to share with you.
    I usually don't care to learn the names of such kind of things because I simply just have no need to. ↩︎

  5. Most of the really useful info never gets translated to spanish from english. ↩︎

  6. What? Did you think I hated C++ and Java with a passion for no reason?
    Yeah, I've actually used both to make programs and hated every single second of it (C kinda felt old fashioned with its "I don't know what the fuck a string is" policy, but was an otherwise ok dude).

    And... what??? almost 20 years since I bought that book??? That explains why I haven't been able to find it around the house. ↩︎

  7. It's a damn shame I don't have site counters here, so I don't know who came looking for what, but if you came here expecting to find how to kill yourself, from a suicidal person (me) to other I have just one thing to tell you: lol, git gud at web searching, noob. ↩︎

  8. I'm also not too fond on randomly selecting a method, but I can easily change it to some context aware one. ↩︎

  9. Still, that's no excuse to write sloppy, shitty code. ↩︎

  10. Imagine Ahlam "coincidentally" entering the room where Nazeem is having sex with dogs or some preposterous shit like that. ↩︎

  11. Every single language has its own limitations because most of them were designed with different purposes in mind.
    Don't you mind when Java code monkeys lash at you when you tell them about your favorite language. If a perfect programming language existed, no one would have ever invented hundreds and hundreds of them. ↩︎