Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Inspiration What is programming?

Comments in 'Resources' started by PEMapModder, Mar 25, 2016.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Fundamentally, programming is the process of teaching a machine (I will call it a computer for convenience) things from human language (for convenience of writing, in English) to machine language (for convenience of writing, I overgeneralize them as PHP in this thread).

    Code:
    Concepts in your brain -> (Elaboration of concepts in English) -> Elaboration of concepts in PHP
    
    This flowchart explains all of programming.

    What does a computer know? Or more specifically, what does a CPU, or the processor, the core part of a computer, know? From the perspective of a software engineer, originally, bytes. I would even assume it to be something like the 8 instructions in the Brainfuck programming language, that is basically the editing and reading of data, moving of data pointers and moving of instruction pointers.
    If you have had any experience with electrical circuits (fine, redstone circuits), you would realize that even basic addition of two numbers is difficult enough. We have already invented the expression of numbers with binary form (forget about reading this thread if you don't even know what this is about), but how to add them together? We have to check NOR to set to 0, check XOR to set to 1, and AND to set the next bit.
    Why am I talking about all this basic stuff? I am trying to show that the knowledge of a computer is built level upon level. First we have bytes. Then we have the arithmetic operations of numbers. Then gradually, layer upon layer, we have processors that understand machine code (known as assembled code). Then we have high-level languages like C/C++ that can be parsed by a computer and be converted into machine code. Next, we have PHP, which is written in C to parse strings. And we have PocketMine, which is written in PHP to calculate various things that are understood as a "Minecraft PE server". Then we have plugins that are written upon the PocketMine API. And there are even plugins upon the API of other plugins. Let's put WorldEditArt in this example, since it's the easiest to explain (and I was just coding it).

    Now let's look at it from the opposite direction. The PHP team taught the computer what the PHP language is. The PocketMine team taught the computer what a Minecraft server is, using symbols in the PHP language.

    Still too vague? Let's use WorldEditArt as an example.
    Specifically, how WorldEditArt teaches PHP what a sphere is.

    Apparently, a computer wouldn't know what a sphere is. PocketMine doesn't have it. Nor does any levels I mentioned above have a sphere (actually there is an imagearc() function in the PHP GD extension, but I'm not using it). So how can we explain to the computer what a sphere is?

    How do you explain to a kid what a sphere is? You tell him, a sphere is like a ball. Sadly, a computer doesn't know what a ball is, either.
    As a matter of fact, a computer doesn't need to know what a sphere is. Why am I telling the computer that a sphere has a curved surface? How does it help me creating the WorldEditArt plugin? It may need to know whether a player is in a sphere. It may need to know how to change all blocks in a sphere; or just the ones at the surface. Why am I teaching the sphere how to calculate the total surface area of a sphere? It simply makes no sense. (Meanwhile, teaching it how to estimate the volume of a sphere helps if I want to estimate (small implementation problem: estimate, not calculate, because we are talking about blocks, and it may not be exact) how many blocks I need to change)

    So, I have a method SphereSpace::isInside(Vector3). A Vector3, in a human's concept, is three numbers that represents a three-dimensional vector; or more practically, a position in this situation. But how does PocketMine teach the computer what a Vector3 is? It is simply `class Vector3{ public $x, $y, $z; }`.
    This is a bit complicated with PHP, so let's explain it with a possible Java counterpart instead:
    Code:
    public class Vector3{
      public int x, y, z;
    }
    For those who don't know Java: an int is a 32-bit (4 bytes) representation of an integer (signed, i.e. if the most significant byte is set, the number becomes negative). Too complicated? What you need to know is, an int is 4 bytes that represent an integer.
    So we are telling the computer that a Vector3 is a data structure with 12 bytes (4 x 3 = 12). And in other class methods in the Vector3 class, for example, there is a Vector3::distance() method. It teaches the computer the Pythagorean theorem, so the computer knows that the distance between two Vector3's is calculated by:
    Code:
    √( (Δx)² + (Δy)² + (Δz)² )
    Since PHP has already taught the computer what these arithmetic symbols mean, PocketMine just needs this to teach the computer how to find the distance between two Vector3's.

    Now, how does this help us with the sphere? We have a SphereSpace::isInside(Vector3) method. So we simply tell the computer to check if the distance between the Vector3 and the sphere center <= the sphere radius (PHP taught the computer what <= is),

    If you understand the above, that's what functions are about. Every function teaches the computer a process.

    Now, what is a class? Let's look back at the SphereSpace example. How do you define a sphere? A lot of ways. You can specify the radius and the center point of a sphere (let's neglect multi-world support for convenience). You can also store a GIF or bitmap image that can be parsed by the machine to know the properties of a sphere(this is hilarious). But in the end, you would take the raw form, that is, radius and center.
    This is like how respiration works. When you eat, the carbohydrates can be in different forms, but eventually they get broken down into glucose. Even if some of the blood glucose are converted into glycogen, they are still converted into glucose when used for glycolysis.
    Therefore, a SphereSpace class is a data structure that stores a radius and a center. More precisely, what a computer understands is that it stores a double (radius) and a Vector3 (center), and that we tell it that a sphere is something that can be combined with the equation of circles to fill blocks in a world. What is a world? What is a block? How to set a block? PocketMine defined it. And a sphere is also something that we can check whether something is inside it. And a sphere is also something that we can describe with human language.
    So, what does the SphereSpace contain? It contains class properties, that are $radius and $center. It also contains class methods, which are describe(), fill(Block) and isInside(Vector3).

    I will write more about class inheritance from the same approach when I have time.
    SOFe, Primus, applqpak and 7 others like this.
  2. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Nice tutorial :)
    applqpak and matthww like this.
  3. matthww
    Offline

    matthww Active Member

    Joined:
    May 13, 2014
    Posts:
    91
    Minecraft User:
    Matthww
    This should be a sticky or something. I kinda get it but not everything but thats probably because I don't have enough knowlegde about coding/programming or what they call it...
    applqpak likes this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Although this thread is tagged as Tutorial, it is more like Inspiration.
    matthww and applqpak like this.
  5. matthww
    Offline

    matthww Active Member

    Joined:
    May 13, 2014
    Posts:
    91
    Minecraft User:
    Matthww
    Ask Shoghi to make a INSPIRATION tag xD
    Legoboy0215, applqpak and PEMapModder like this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, @Intyre made it :p
    matthww and applqpak like this.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  8. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Well written, but I think that "forget about reading this thread if you don't even know what this (expressing numbers in binary form) is about" is not appropriate. You can still understand how information is expressed to a machine without understanding the basic binary operations. No offense.

Share This Page

Advertisement