Jump to content

[1.5.1][WIP] Automation mod - computers


MarcinS

Recommended Posts

I have started and am at a quite advanced state of working on an "Automation" mod. This mod introduces computers and programming to your world of Minecraft.

 

Mod is currently in pre-alpha stage and soon I will add an alpha download to allow testing.

 

Why did I start working on this mod?

Being computer programmer myself, I was very excited when I saw Guude use ComputerCraft in his series. I've decided to start using turtles, when playing on my old laptop (single-player mode), but after starting just one turtle with "excavate", the frame rate dropped to 1-2, so I had to quickly abandon this idea.

I've decided to help fix the problems ComputerCraft has. Unfortunately, due to the fact that it is not open-source, I was unable to provide input to it. Also, I'm afraid that a lot of issues it has, are a result of some architectural choices made early during the project, and cannot be fixed without major changes. Therefore, I've decided to write my own mod.

 

Goals for this mod:

- be open-source - I can't understand non-commercial software being any other way,

- badly written programs should not cause a deadly lag (everything stops for couple of seconds) of the server/world,

- mod should work comfortably on servers with many users (i.e. public), and should not be just limited to private/white-listed servers,

- mod should not allow for dead-locks or server crashes (quite obvious),

- running computer programs should not start and run in new threads, as this consumes a lot of server resources and does not scale well above hundred computers running programs on a server,

- writing/editing a program should be user friendly and not impacted by client-server communication speed,

- syntax checking and error finding during program editing is a must, to avoid spending hours trying to find a typo in code after trying to run the program,

- if possible, mod should use a popular widely-known computer language,

- built-in objects should be extendable to allow adding new functions in new versions,

- mod should gracefully handle multiple players using the same computer at the same time,

- provide easy to use configuration and monitoring tools for server owners, to allow throttling of computer speed for players running many computers and other throttling and configuration options,

- provide an easy to use public API for adding new computer modules (see below).

 

Mod description:

Mod introduces several levels of computers, starting with a simple computer with limited capabilities, going up to "super-computer" class. The more advanced the computer is, the faster it runs, can house more modules, allows for larger program memory usage, etc.

 

Each computer has 1-5 (not finalized yet) slots for modules. Modules (items) are added to a computer via inventory interface of the computer. These add capabilities to the computer, as a computer without modules cannot really interact with the world. There will be several modules released with the core Automation mod (list not yet finalized):

- Location - module that allows the program to query the computer's location and which side it faces,

- Storage - module that allows to store items in the computer, as well as interacting with adjacent inventories,

- Mobility - module that allows the computer to change its location and facing (move in the world),

- Harvest - module that allows to harvest blocks in the vicinity of the computer,

- Redstone - module that allows to measure received and emit redstone signal.

 

In addition to modules, one extra item will be added to the game - Terminal. This item allows to connect to the computer, by right-clicking on it while holding Terminal in hand, to start a new computer session.

 

What's left to do:

- textures and icons,

- Mobility module testing,

- Harvest and Redstone modules - code and testing,

- configuration and monitoring tools to allow throttling; throttling part is actually very easy to do as mod was designed with this in mind,

- loads of testing by others.

 

Future plans:

1. Requirement for computers to consume energy (duh!). I would like to make the mod compatible with both Buildcraft and IC2 energy, and which one will be used, will be based on mod configuration and the other mod (BC, IC2) presence.

2. Computer user access, inter-computer communication security - this opens a nice field for extra-layer of game-play, where players can attempt to hack each other systems, if they manage to find a security whole in someone's program.

3. Peripherals and connecting/communicating with them using a cable, or if possible - using existing networks (for example glass-fibre cable from IC2).

Link to comment
Share on other sites

  • 2 weeks later...

I'm kind of interested in this for a certain project of mine. I propose Python for the programming language.

Reasoning: easy to learn, has a native Java implementation (Jython), great Java interoperability, lots of standard library features, it's not Lua.

Link to comment
Share on other sites

I'm kind of interested in this for a certain project of mine. I propose Python for the programming language.

Reasoning: easy to learn, has a native Java implementation (Jython), great Java interoperability, lots of standard library features, it's not Lua.

Actually I already implemented JavaScript syntax (a bit stricter version of it).

 

Also, I'm not sure if I would be able to use any existing language implementations, as these probably do not offer too much of a control over how many operations are executed in one go, and if a "busy wait" was done in a program, might introduce the same problem ComputerCraft has with world freezing.

 

I should have an alpha version in about 2 weeks, just need to figure out, what kind of options I want to give for the Redstone module, write and test it.

Link to comment
Share on other sites

I don't think it's a good idea. A custom language is basically useless outside the mod, and the implementation is quite tricky to get right. Jython shouldn't have the freezing issue, as long as it has at least a thread of its own. ComputerCraft does everything on the main thread and uses Lua coroutines for scheduling, which is a massive hack. I was thinking of a transaction based design for world changes, with the main interpreter running on a different thread, and the game commiting transactions every redstone tick (because no world state can change faster).

Link to comment
Share on other sites

I don't think it's a good idea. A custom language is basically useless outside the mod, and the implementation is quite tricky to get right. Jython shouldn't have the freezing issue, as long as it has at least a thread of its own. ComputerCraft does everything on the main thread and uses Lua coroutines for scheduling, which is a massive hack. I was thinking of a transaction based design for world changes, with the main interpreter running on a different thread, and the game commiting transactions every redstone tick (because no world state can change faster).

It's not really a custom language, as it's basically a JavaScript, by "stricter version" I mean - I enforce definition of variables (with "var") before the variable can be accessed, which allows me to detect any problems during parsing - editing the program and quickly highlight the problem. If need be, I can turn off that check.

 

Having one thread per each running program is also a scaling issue (ignore this comment if you mean having one thread for all the programs). With potentially hundreds of computers running programs in game, this poses a threat of exhausting Java resources by the mere volume of threads.

 

However, I do not understand your approach. What gain would that be, having interpreter on a separate thread, if it always has to run in sync with world ticks. Please keep in mind, that AFAIK Minecraft is not thread-safe and utilizes single-thread model when operating on World, therefore you shouldn't access, both read and/or write, it's data from any other thread than the main thread itself.

Link to comment
Share on other sites

Is your implementation ECMAScript compliant (assuming you turn off that check)? No? Then it's not JavaScript. I wanted to use something like this as an educational tool, and I don't think a subset of JavaScript is good for this use case. As for threading, Jython should be able to handle that with greenlets/coroutines, and if that doesn't cut it, there's always PyPy, which scales way better with implementation-native greenlets. Having it on a separate thread means that we don't have to care about scheduling it, but we can still have it run at lower priority. Synchronization needs testing, but I think a context switch every N ticks should be OK.

 

Edit: you also gave me another reason why an existing language should be easier to work with: tools. You'll want an editor with good syntax highlighting, and maybe even basic refactoring support, and that takes quite a lot of coding. For, say, Python or (compliant) JavaScript, there are lots of already existing open source tools and components that can be integrated into the game.

Link to comment
Share on other sites

Running a "busy waiting" thread (in case of badly written program) on a lower-priority does not prevent thread starvation (on main Minecraft thread), therefore I think it's a mistake to run a program on a separate thread. It only introduces complexity and potential for thread-safety issues when done wrong. If an implementation does not offer me complete control over scheduling it's a "no go" for me.

 

Thank you for your comments, but for the time being I will stick with what I have.

Link to comment
Share on other sites

As for conformance with ECMAScript, I've run the examples for Wikipedia page, and they get the results that are required for it. I do not intend to have full ECMAScript conformance, as I see little to no value in doing that. As an example, why have more than one way of defining an object?

 

Anyway, here are the tests I've written that give the expected results:


    @Test
    public void recursiveFunctionCall() throws IllegalSyntaxException, IOException, ExecutionException {
        final Variable variable = executeScript("function factorial(n) { if (n == 0) { return 1; } return n * factorial(n - 1); } return factorial(3);");
        assertEquals(6, ((Number) variable.getValue()).intValue());
    }

    @Test
    public void recursiveFunctionCall2() throws IllegalSyntaxException, IOException, ExecutionException {
        final Variable variable = executeScript("var factorial = function(n) { if (n == 0) { return 1; } return n * factorial(n - 1); }; return factorial(3);");
        assertEquals(6, ((Number) variable.getValue()).intValue());
    }

    @Test
    public void testClosure() throws IllegalSyntaxException, IOException, ExecutionException {
        final Variable variable = executeScript("var displayClosure = function() { var count = 0; return function () { return ++count; }; };"
                +" var inc = displayClosure(); return \"\"+inc()+\" \"+inc()+\" \"+inc();");
        assertEquals("1.0 2.0 3.0", variable.getValue());
    }

    @Test
    public void anonymousFunction() throws IllegalSyntaxException, IOException, ExecutionException {
        final Variable variable = executeScript("var v; v = 1; var getValue = (function(v) { return function() {return v;}; }(v)); v = 2; return getValue();");
        assertEquals(1, ((Number) variable.getValue()).intValue());
    }

Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.