Jump to content
yamamushi

A LUA "Core Unit Emulator" Tool for New Programmers

Recommended Posts

First, and most importantly, this isn't an official NovaQuark project, and I don't have any idea what the interfaces in the actual game will look like, I'm basing most of this off of the LUA Dev blog and my knowledge of how C/C++ bindings are exposed to LUA. If they want me to change the name of this I'll be happy to. Secondly, I have no idea where this topic belongs so I'm posting it here in General. I think we need a programming-related subforum but that likely won't happen until Alpha at the soonest. 

 

 

Over the Holidays I was thinking about tools I could work on now that might be of some interest to people out there. Things like a ship crew scheduling app, a wiki for programmers, a timeline site keeping track of DU events, a ship delivery scheduling utility, etc. Ultimately, however, I kept coming back to this idea, as it would help me while learning a new language for my job and it would help people who don't know much about LUA (or programming in general) start to wrap their head around what it might be like in DU (as well as help some of those AI-projects out there). 

 

 

I came up with the idea to create a Core Unit "Emulator" as a standalone web service that people can target with LUA scripts. 

 

I'll try to explain what this looks like... 

 

A programmer would set up LUA on their computer, install the LUA sockets module, as well as copy a piece of code into their own projects that would bootstrap them into being able to communicate with a server over the net.

 

From there, function calls into an object would act as calls to a "Core Unit", which under the hood would really just be some socket communications going on to a server doing all of the actual work. 

 

From the perspective of a programmer, they get access to various details about a ship (speed, velocity, pitch, yaw, rotation, etc) that they have to adjust to meet a set of parameters (per round) after a simulation is run. So your scripts would have to account for things like losing an engine, running low on fuel, turbulence, etc. and ultimately get the ship into a given set of passing parameters within a given period of time. 

 

Alternatively, I could write a standalone program that you use to run your scripts against, it doesn't particularly matter to me if it's a remote service or not. 

 

 

 

How does this help new programmers? It would give the ability for people to try writing LUA in a way that resembles what LUA programming in games is actually like. 

 

It won't be what Dual Universe is exactly like, none of us have the ability to write something like that because we can't know what the interfaces actually are. However, it will give you enough experience in LUA to be able to adapt what you learn very quickly. Ultimately it comes down to having a set of elements giving you information via function calls, processing that information, and being able to do things with said elements via other function calls. If you can get around that basic concept then you'll be in a much better position overall. 

 

 

 

 

It would give people an excuse to start learning LUA now, but it won't be very "fun" or realistic, and you'd have to use it knowing that it's not like Dual Universe at all. There probably wouldn't really be graphics either, you'd be flying by sensors only... 

 

I'd even love some help building it on the server side, which I'm currently doing in Golang because I have to learn it for my job. It's not super difficult to program in, but it would certainly be cool to have a helping hand on programming the actual physics side of things. I just don't want to do it all myself, although things like this tend to go that way. 

 

All in all, I'd be willing to help people learn LUA along the way as well, provided you have some knowledge about computers already. You could easily take what you learn and apply it to other games that are scripted through LUA as well (WoW is one of them). 

 

 

Who would be interested in even using something like this?

 

 

I guess I should make a video explaining all of this, as this wall of text is probably too verbose. 

Share this post


Link to post
Share on other sites

+1 for excellent wall building ;) 

 

I would definitely use something like this, and would be willing to lend a hand in whatever way I am able once I finish up a couple of other projects I am working on right now.

Share this post


Link to post
Share on other sites

This sounds excellent and would definitely help incompetent programmers such as me to get into LUA, the mere possibility of receiving feedback from scripting is encouraging. For now though, it is more geographic information systems for me so I can finish my studies but in a month or so I would be willing to help however I can (so, probably be more of a hindrance than help). 

 

You just keep on coming up with awe-inspiring stuff. 

Share this post


Link to post
Share on other sites

I like this idea.

 

For now I'm pretending to have the API just so that I can get used to writing LUA :lol:

-- This function was borrowed from LUA's homepage.
-- It will be used to remove some redundant decimals later on in the script.
-- Feel free to ignore it for now.
function round(num, decimals)
  local mult = 10^(decimals or 0)
  return math.floor(num * mult + 0.5) / mult
end

-- Newton's law:
-- acceleration = newton / mass
-- mass = newton * acceleration
-- newton = mass * acceleration

-- Gravity will, later, be a modifier that the player can determine through a control panel. 
-- The value can be anything, but in this example, the player is going to choose Earth's gravity.
local gravity = 9.80665 -- Earth

-- If gravity returns as a boolean, then gravity has not been set by the player, and gravity should be given a default value.
if gravity == nil then
  gravity = 0
end

-- The amount of force that ONE thruster can perform is assumed to make the script work. Thruster information should be available upon game's release.
local thruster_n = 800000 -- N

-- The ship's weight is assumed to make the script work. Ship's weight should be available upon game's release.
local ship_kg = 135106 -- kg

-- If there is a ship with weight to be calculated, then...
if ship_kg then

    -- The amount of thrusters that this imaginary ship will have is assumed in order to make the script work.
    -- It is also assumed that thrusters are- or can be grouped in this manner.
    local thrusters = {"Thruster 1", "Thruster 2"}
    
    -- The number of thrusters found in the above array.
    local thruster_count = #thrusters
    
    -- The amount of thrust that all thrusters produce in total
    local total_thrust = thruster_n * thruster_count
    
  -- If there is gravity, then...
  if gravity > 0 then
    -- the total thrust needed to repel this gravity is
    thrust_needed = ship_kg * gravity
  else
    -- the ship is not affected by gravity
    thrust_needed = ship_kg
  end
 
    -- If the ship's total thrust is higher than the thrust needed to lift off, then...
    if total_thrust > thrust_needed then
        -- calculate redundant thrust in percent
        local thrust_per = ((total_thrust / thrust_needed) * 100) - 100
        -- calculate the amount of redundant thrusters
        local redundant_thrusters = total_thrust / thrust_needed
    
        print("The ship has",round(thrust_per, 2),"% more thrust than what is required to repel",gravity,"gravity.")
    
        -- I'm a sucker for proper grammar...
        if redundant_thrusters >= 1 and redundant_thrusters < 2 then
            print("The ship has",round(redundant_thrusters,0),"thruster that can safely be removed.") -- 1 thruster
        elseif redundant_thrusters >= 2 then
            print("The ship has",round(redundant_thrusters,0),"thrusters that can safely be removed.") -- 2 thrustersssSSS
        end
    
    print("The ship accelerates",round(redundant_thrusters, 2),"m/s while in",gravity,"gravity.")
    
    else
    
        -- Calculates thrust missing in percent
        local thrust_per = ((total_thrust / thrust_needed) * 100) - 100
        -- Calculates the amount of thrusters missing
        local thrusters_missing = thrust_needed / total_thrust
    
        print("The ship is missing",round(thrust_per, 2),"% of total thrust required to repel",gravity,"gravity.")
    
    -- What? Sue me.
    if thrusters_missing >= 1 and thrusters_missing < 2 then
      print("The ship needs",round(thrusters_missing, 0),"more thruster.") -- 1 thruster
    elseif thrusters_missing >= 2 then
      print("The ship needs",round(thrusters_missing, 0),"more thrusters.") -- 2 or more thrustersssSSS
    end
    
    print("The ship accelerates",round(thrusters_missing, 2),"m/s while in",gravity,"gravity.")
    
    end -- if total_thrust
    
end -- if ship_kg

Edited: Dual Universe will apparently be using LUA 5.3, so getn(thrusters) was deprecated. I had to change it to #thrusters. My bad. I also made sure that the script will run regardless of gravity, and edited the grammar to sound more neutral and coherent. I hope editing doesn't bump my post, but only makes sure that future readers can test out the LUA script if they want and it works as intended. Read CaptainTwerkmotor's post below mine to see what changes I made.

Edited by Hrafna

Share this post


Link to post
Share on other sites

 

I like this idea.

 

For now I'm pretending to have the API just so that I can get used to writing LUA :lol:

-- This function was borrowed from LUA's homepage.
-- It will be used to remove some redundant decimals later on.
-- Feel free to ignore it for now.
function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

-- acceleration = newton / mass
-- mass = newton * acceleration
-- newton = kilograms * acceleration

-- This IS Earth's gravity (relative to the object in question), at sea level. Should be determined by the player.
local earth_g = 9.80665

-- This is the PRESUMED force that ONE thruster can perform in Newton. Should always be available.
local thruster_n = 350000

-- This is the PRESUMED weight of our ship in kilograms. It might not always be available.
local ship_kg = 150000

-- Can we calculate anything?
if ship_kg then

    -- Now let's presume that our ship has 2 thrusters, for the sake of this script to work, and that they are grouped together like this.
    local thrusters = {"Thruster 1", "Thruster 2"}
    
    -- But, since the number of thrusters can change, we will start by finding out how many thrusters we indeed have.
    local thruster_count = table.getn(thrusters)
    
    -- How much thrust does our thrusters produce in total?
    local total_thrust = thruster_n * thruster_count
    
    -- How much thrust do we need in total to repel Earth's gravity?
    local thrust_needed = ship_kg * earth_g
    
    -- If our total thrust is higher than the thrust we need to lift off, then...
    if total_thrust > thrust_needed then
        -- Calculates redundant thrust in percent, and uses the round function to remove redundant decimals
        local thrust_per = round(((total_thrust / thrust_needed) * 100) - 100, 2)
        -- Calculates the amount of redundant thrusters, and uses the round function to remove redundant decimals
        local redundant_thrusters = round(total_thrust / thrust_needed, 2)
        print("You have",thrust_per,"% more thrust than what is required to repel Earth's gravity.")
        -- We don't want them to remove 1 thruster and go into the negative. Besides, I'm a sucker for proper grammar...
        if thrust_per >= 1 and thrust_per < 2 then
            print("You can safely remove",round(redundant_thrusters,0),"thruster.")
        elseif thrust_per >= 2 then
            print("You can safely remove",round(redundant_thrusters,0),"thrusters.")
        end
    else
        -- Calculates thrust missing in percent, and uses the round function to remove redundant decimals
        local thrust_per = round(((total_thrust / thrust_needed) * 100) - 100, 2)
        -- Calculates the amount of thrusters missing, and uses the round function to remove redundant decimals
        local thrusters_missing = round(thrust_needed / total_thrust, 2)
        print("You are missing",thrust_per,"% of total thrust required to repel Earth's gravity.")
        print("You need",round(thrusters_missing,0),"more thrusters.")
    end
    
end

Hopefully, people can see this and realise how easy Lua is. 

Share this post


Link to post
Share on other sites

I was going down a path like that, and then upon reading through the LUA Devblog again I realized that it wouldn't be entirely accurate.

 

I should have named this post "DPU Emulator", as opposed to a Core Unit one...

 

Wish there were more people in the #programming channel on Discord to discuss this with :P 

Share this post


Link to post
Share on other sites

I've been interested in Lua since hearing about how it'll be in DU. Ive looked up some videos on it and have since learned it's a super easy programming language... I've messed with Arduino and PIC so I figured it'd be easiest to learn once there is a game for me to test things in, since otherwise I really have no way to test what I program. Something like this would be awesome, although I'm not sure how you would handle syntax or emulate things happening.

Share this post


Link to post
Share on other sites

I just wanted to update this thread with some of my progress through the last week (I'm not working on this full time).

 

This is a golang based server that accepts JSON messages, processes/parses them and then returns a result to a client in JSON format.

 

Now that the communications part is working, and the message parsing, and the concurrency model for passing messages between the different server threads, I'm ready to start working on the actual "game" part of the "emulator". ie, physics, ship movement, events, etc. This will also require me to build the LUA module for users, which I'll work on later this evening.

 

Here's a link to the github for the server component, although it won't be of much use to anyone without knowing how to deploy a golang based project first. ie, install golang, run $go get github.com/yamamushi/ducuemu , and then run ducuemu-server .

 

https://github.com/yamamushi/ducuemu

Share this post


Link to post
Share on other sites

In hindsight, it may be a better idea for people to learn LUA by downloading ZeroBrane Studio and following the guided tutorial. 

 

https://studio.zerobrane.com/

 

 

There's even love2d for people to try their hand at making their own games with it (ZeroBrane has a demo of a game that uses it). 

 

I'll keep working on this project of mine, but I won't lie it would be way more interesting to learn LUA another way :) 

Share this post


Link to post
Share on other sites

I am excited for this project of yours, just might give me the motivation to learn LUA so that I can work on your emulator and get a basic feeling of possibly how it might work in game, ofcourse it might be completely different inside game but I am sure that if I learn LUA now I can adjust to how it will be used in game pretty easily.
Best of luck with the process, I am not sure how I can help you but best of luck :)

Share this post


Link to post
Share on other sites

This is just a minor math correction, but as you wrote:

-- acceleration = newton / mass
-- mass = newton * acceleration
-- newton = kilograms * acceleration

You got it mostly right. F = m*a becomes m = N/a, not N * a.

 

Think of it as a triangle or pie chart, with N on top and the bottom split between A and M. Cover up the one you solve for to get the equation. To solve for mass, cover it up and you see N / A.

 

One of the tutorial videos covered LUA, but it referenced the codex in game, which you can't access offline. Also, as of 7/19/18, that part of the codex was not active. I'm sure you can transcribe the information if you go through each item one at a time in the game to find out what can be done with it, though that would take some time and resources, and would be subject to any changes they make anyway.

 

I'm a coding noob (only got 2 classes for C++ in for my AA in science/physics, but that was years ago), so I'm trying to learn out of general interest, and because I think creating is more interesting and satisfying that destroying.

 

Oh, another correction. The program wouldn't be for "Earth," so you might want to replace it with something more generic. (I'm not sure what you mean by "repel Earth's gravity, either. Do you mean to program the simulation to determine whether you would be able to achieve stable flight, escape velocity, lift off, or something else?)

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...