
Show HN: Minecraft clone in 2500 lines of C - even supports multiplayer online - fogleman
https://github.com/fogleman/Craft
======
DanielRibeiro
Cool. From the same author of Minecraft clone with Python:
[https://github.com/fogleman/Minecraft](https://github.com/fogleman/Minecraft)

The python version uses only 894 lines of code:
[https://github.com/fogleman/Minecraft/blob/master/main.py](https://github.com/fogleman/Minecraft/blob/master/main.py)

~~~
camus2
Woah, i just began python programming for sysadmin scripts, didnt even know
one could do that with python! impressive.Is there any other game made with
pyglet that are open source?

~~~
eru
I don't know about pyglet, but there are lot of PyGame games to explore.

------
ryandrake
Not to be a party pooper, but there doesn't seem to be much in the way of
error checking, null pointer checking, etc. e.g. Apparently library calls like
calloc() and fopen() never fail. I guess if your goal is "few lines of code"
it's understandable to leave that stuff out.

If your brain has the tendency to automatically go into "code review" mode, I
recommend against browsing the source :)

~~~
papaf
What should you do if calloc fails? Any solution I can think of requires an
architecture that does not belong in a game.

~~~
strictfp
Abort and terminate? Jokes aside, error checking in c is important since if
you don't do it you get "On error resume next" behavior.

~~~
geocar
What does malloc() return if it fails?

    
    
        volatile char *x = 0;
    

And what happens if you write to that address?

    
    
        *x = 69;
    

The answers to these questions, whilst not defined by the C language, are
portable enough.

~~~
clarry
What about the more common situation where the thing to be allocated is a
(potentially large) array? These aren't always accessed sequentially. And the
offset might be (indirectly) controlled by untrusted data from the wild.

------
angersock
CMake Error at CMakeLists.txt:20 (find_package): By not providing
"FindGLEW.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a
package configuration file provided by "GLEW", but CMake did not find one.

    
    
      Could not find a package configuration file provided by "GLEW" with any of
      the following names:
    
        GLEWConfig.cmake
        glew-config.cmake
    
      Add the installation prefix of "GLEW" to CMAKE_PREFIX_PATH or set
      "GLEW_DIR" to a directory containing one of the above files.  If "GLEW"
      provides a separate development package or SDK, be sure it has been
      installed.
    
    

\-- Configuring incomplete, errors occurred!

~

:(

(Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64 GNU/Linux)

~~~
krakensden
Welcome to the pile of busted that is CMake. If you do it for a living, you'll
even get to learn their wonderful psuedo language, so you can parse through
450 lines of Find*.cmake just to figure out that all it does is check ENV_VAR0
and then fall back to a hardcoded string- even though the documentation/header
claims it checks ENV_VAR1.

~~~
csmuk
This.

CMake is another barrier to entry.

what happened to a nice simple makefile? Been using just standard makefiles
for literally 20 years.

The only thing I do is put a checkdeps dependency on which tests for the
presence of dependencies, like autoconf but without the eye poking. Have built
everything from small network servers to Motif behemoths doing only that.

~~~
zurn
You get no (compatible) make on MSVC. More imporantly, whith nontrivial
dependencies to third party libraries, self-coded makefiles just break down.
Cross-platform handling of dependencies is fragile and makes drudgery. You're
liable to do worse than CMake - after wasting a lot of time and energy.

CMake is more an alternative to GNU autoconf and pals, with the difference
that it has first class support for things like MSVC and XCode.

~~~
blueblob
Is nmake not compatible with make? I thought that it was. I haven't programmed
C/C++ on Windows at all though, so I have no experience with this.

~~~
zurn
Microsoft "nmake" is unrelated to the AT&T-originated Unix "nmake", which you
may be thinking of.

(people rarely even attempt nontrivial Makefiles that are portable between BSD
and GNU makes though...)

------
Taylorious
This looks awesome. The code is very clean and well laid out. I am
disappointed that there are literally no comments in the source code though.
Hopefully he will add comments, because a lot of what he is doing is not super
obvious, at least not to me.

~~~
fleitz
Learn to read code instead of comments. It's super straight forward C.

~~~
eru
What the code is doing is clear. The why (and especially the `why not') could
benefit from some commentary.

~~~
DigitalJack
I wish more programmers understood this concept. Yes, we can read the code for
the _what_ , which is why we don't need you to tell us you are adding 2 to X.
_Why_ is a different matter altogether, and that is where a comment can shine
light on the scenario.

~~~
fleitz
To me it ruins the surprise, uncommented code is like an uncommented novel,
once you've grok'd it the whole thing starts to make sense.

Comments are like coles notes.

It's good as professional courtesy but for personal projects... seems like a
waste of time.

~~~
hobbes300
Until you go back to it in a year's time and can't remember any of the reasons
for your design decisions.

~~~
Ntrails
Besides, I genuinely find myself hilarious so the attempts at pithy commentary
tend to make the whole "christ what was I thinking" part of looking at my old
work less painful.

------
ohwp
Some days ago there were comments about Notch being a bad programmer. And when
I see posts like this I think that's unfair.

2500 lines of C doesn't mean a thing. I could do it in 1 line when I removed
all line breaks.

Did the programmer created the concept, interaction design, graphic design?
No, Notch did.

So maybe Notch does write bad code but I think being a programmer is more than
just writing code. In the end Notch got the job done and people are enjoying
the result.

But yeah! great to see another Minecraft clone. It's always nice when people
share there knowledge!

~~~
exadeci
He didn't created the concept but took the ideas from Dwarf Fortress and he
says it :) [http://notch.tumblr.com/post/227922045/the-origins-of-
minecr...](http://notch.tumblr.com/post/227922045/the-origins-of-minecraft)

But still minecraft makes a 2012 macbookpro overheat like no other game, I get
80°C while Minecraft alternative clients/servers barely make it to 70°

~~~
anaccountonhn
If you had bothered to actually read the linked article, he claims to have
been..."inspired" by infiniminer more than any other game.

" But then I found Infiniminer. My god, I realized that that was the game I
wanted to do"

------
aktau
I think an easy visual upgrade to get rid of the jaggies would be to
(optionally) turn on MSAA (multi sample anti-aliasing). It doesn't drop the
lo-fi look IMHO but still looks ways better. MSAA is real easy to enable,
create your window with a MS buffer:

/* I'm not entirely sure if this is the right call for GLFW * I normally use
SDL, you can usually go up to 16 with the * samples */
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);

Enable multisamplimg before drawing anything:

glEnable(GL_MULTISAMPLE);

Thanks for the source, I'll have a read through!

------
jackweirdy
Oops
[https://github.com/fogleman/Craft/issues/15](https://github.com/fogleman/Craft/issues/15)

~~~
guidopallemans
that's the first thing he does on github?

------
simcop2387
It would appear that placing block 15 (the clouds) can cause some problems. If
you disconnect while in a player created cloud it causes you to fall through
the ground permanently.

------
m3mnoch
not to be a parade-rainer (because this is really cool!), but it's not really
a 'clone'. it's really just a cube rendering engine with a handful of textures
and a barebones python socket server.

in my very humble opinion, a minecraft clone would have (at least stubbed
out):

\- ui. if you've never made a game, you'd be amazed at the amount of code ui
takes up. even ugly ui. good lord.

\- mobs. while you can data-drive a lots of your mobs, something like
minecraft still has vast swaths of business logic to drive interactions with
everything from creepers to mating wolves to the ender dragon.

\- crafting. again, you can get a long way with data-driving your crafting
system, but you've still got to build in all the mechanics that control your
crafted items behavior among all the other various items in the game.

and none of that counts any of the polish that publishing a game requires. as
a guy who's shipped games, it's really (even more so than traditional
enterprise software) a case of the last 20% of the work takes 80% of the time
allotted to the entire project.

in my experience, that's where "indie devs" fall down. they solve the hard,
interesting problems (look! i've got an environment rendering! boom! the
combat code works!), but then lose interest and chase other shiny things when
it comes to the rote, painful tasks (wait... i've got to data-bind the hud?
and handle socket disconnects without crashing? and the camera behavior? the
animation has to blend? what about handling a network timeout on the
dynamically loaded textures? you want to deliver "news and updates" to the
client? from what server? and you want a cms with that? oh, and write the
checkbox code for options screen? and qa insists we can apply and skip the
first 30 levels so they can test 31 without playing the entire game? but, but,
but... look! the environment loads on my machine! what kind of video card do
you have?)

anyway.

sorry about the rant.

this really is super-cool. just more of render-plumbing and not really a
clone.

~~~
frik
I found a similar project that fits also your definition of "clone". The guy
coded it in C++ and created a cross platform framework with render path for
OpenGL and DirectX. He even compiled it to JavaScript and WebGL via
Emscripten. And it has some initial multiplayer support and he also wrote his
own GUI framework. Interesting read:

[http://sea-of-memes.com/](http://sea-of-memes.com/)

------
qznc
The minetest project could use a contributor with shader experience. ;)

[http://minetest.net/](http://minetest.net/)

~~~
coolsunglasses
What is minetest trying to do better than Minecraft?

~~~
qznc
Better performance, especially on cheaper machines through use of C++

Easy extensibility via Lua scripting, no need for Java disassembly

Free Software

~~~
coolsunglasses
Cool. I really like the idea. I wonder how a Haskell version would compare in
performance.

I've wanted a leaner and simpler multiplayer Minecraft for some time.

~~~
clarry
During an early stage of development, I ran minetest on OpenBSD on a netbook
with Atom N450. It ran smoothly with a low-ish view distance.

Last I checked, Minecraft was nonfree and depended on some binaries plus did
an OS check, so running it on OpenBSD would've been a nonstarter.

I'm sure performance on N450 would've been miserable too; if it could run at
all with just 1G of RAM.

------
bitwize
This is badass.

Recently I'm discovering how little C it takes to create a decent game with
OpenGL. Well-written, straightforward C is a phenomenally powerful language.

~~~
xmrsilentx
Agreed. Minecraft definitely isn't as smooth as this.

------
tmikaeld
One starts to think - what if minecraft was made in C from the start? Would it
perform better?

~~~
10098
Difficult to say. Writing in C doesn't magically make code run faster, and
it's entirely possible to get abysmal performance compared to a better
implementation in a high-level language.

That being said, it probably wouldn't be too difficult to make something
better in terms of performance than minecraft, because its perceived perf
sucks now as far as I can tell from running it on my machine: sometimes
rendering lags, I regularly see mobs "suspended" in the air while the terrain
renders (and yes, this happens within "old" chunks too, so I can't say that
terrain is being generated or something like that), and I'm not even on weak
hardware, it's a gaming laptop that runs modern AAA titles smoothly. Not sure
what the source of these problems is, maybe my drivers, maybe mojang screwed
up. I didn't notice these things happening on earlier versions which I ran on
a fairly weak linux machine with a crappy intel graphics chip.

~~~
coldtea
> _Difficult to say. Writing in C doesn 't magically make code run faster, and
> it's entirely possible to get abysmal performance compared to a better
> implementation in a high-level language._

That's the theory, but in practice, the kind of programmer that is competent
enough to pull it off in C, is also the kind of programmer that will give it
better performance than the one writing it in some not very game-suitable
higher level language (say Python or Java).

~~~
pkolaczk
It is still difficult to say. IMHO C at scale requires often trading
performance for architectural simplicity. E.g. relying on function pointers
for indirection (or switch) or passing things by value / deep copy to simplify
memory management are techniques which would often degrade performance than
boost it, relative to Java. In real big code, you have to do some higher-level
abstractions or your project is going to be unmaintainable spaghetti mess, and
it is arguable if C lets you build them in the most performant manner.

~~~
coldtea
I've never really seen any Java project for high performance stuff outperform
a C/C++ based on.

Visualization tools, video processing tools, multimedia tools, games, etc --
all Java examples I've seen come worse off that the C/C++ equivalents.

It's true that in large scale C you often trae performance for architectural
simplicity.

But in a higher level you trade performance for everything, even the most
basic operation has some added complexity layer. And you don't even have as
much say as where you'd trade performance for architectural simplicity in, as
you do in C/C++, since some parts you just can't do without.

~~~
erichocean
Same...until the GC kicks in. Then the app locks up for 60+ seconds at a time
every few hours, at which point the developers start moving anything memory
intensive out of Java to avoid the pauses, and/or reduce their severity (c.f.
Cassandra). At that point, is it really Java? You might as well use a
scripting language to integrate with all that C...

~~~
frowaway001
> Then the app locks up for 60+ seconds at a time every few hours

Given that the GC can generally process 1GB of live objects per second, I'd
love to see an example where you have been working with 60GB+ heaps.

~~~
pkolaczk
And this is not taking into account that modern GCs do most or all of the work
either _without stopping the mutator_ (CMS, C4) or incrementally (G1), in
smaller chunks. Times of STW collectors are long gone.

------
tuananh
has anyone test this? how's the performance?

~~~
chandesho
Just tried it and the peformance is fast, it loads in seconds and looks really
good. My friends are already installing it and we are trying get the
multiplayer working.
[http://i.imgur.com/Mzjxhpp.png](http://i.imgur.com/Mzjxhpp.png)

~~~
sherbondy
What desktop environment is that? The icons look like Gnome's but I've never
seen a theme like this.

~~~
tcl16
Looks like Gnome with the Phosphene theme.

[https://github.com/hdni/Phosphene](https://github.com/hdni/Phosphene)

~~~
tbrock
This is easily the best theme I've ever seen. Where/how did you find this?

------
davexunit
Time to plug Minetest. The core is written in C++ and a Lua API is exposed for
extensions.
[https://github.com/minetest/minetest](https://github.com/minetest/minetest)

------
outworlder
Just because it has blocks and you can break them, does not make it a
'minecraft clone'. Like we do not think a rich text editor to be a replacement
for Microsoft Word.

Let's see a redstone implementation in survival mode :)

------
10098
This is amazing. Good job!

------
Yuioup
Kinda getting sick of Minecraft. All those clones and remakes.

Plus I don't understand my Notch is so highly praised. On the one hand he
claims to be an indie dev champion but turns around and makes lucrative
exclusive deals with Microsoft. If he really cared he wouldn't choose his
wallet over his principles. Sorry but that is how I feel.

~~~
driverdan
What exclusive deals? MC is still available for any OS that will run Java.
That doesn't seem very exclusive. Mojang recently said they're going to
release on PS4 too.

------
gravedave
My make also fails due to Glew. Can anyone provide some premade binaries?

~~~
delian66
Here you go:
[http://www.pozdravi.net/tmp/craft.2013_12_10.binary.ubuntu.1...](http://www.pozdravi.net/tmp/craft.2013_12_10.binary.ubuntu.13.04.tar.gz)

------
jburgueno
client.c: 145

struct sockaddr_in address; ... memset(&address, 0, sizeof(address));

Can someone explain me why you need to set memory of sockaddr_in to 0 ? (Even
if &address haven't been touched).

~~~
anywherenotes
it's a local variable, and local variables do not get initialized in C.

~~~
jburgueno
But the initialization of the variable is right after the memset:

    
    
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = ((struct in_addr *)(host->h_addr_list[0]))->s_addr;
        address.sin_port = htons(port);
    

Why is it useful the put 0 before setting those values?

~~~
oh_teh_meows
It's good practice in general. There might be other fields that were not
explicitly set, or the struct may expand in future versions, or you have union
fields/bitfields (e.g. you explicitly set the low order bits of some union
field to some value, thinking the high order bits are all zeroes, but because
you neglected to zero them out first, they're filled with garbage bits. when
you read the union field as a whole you'll get incorrect values).

~~~
jburgueno
Thanks for the explanation!

------
xmrsilentx
Compiles and works amazingly well on Porteus.

------
Todd
Looks like it's under the MIT License.

------
erichocean
Someone should hook up LuaJIT. :)

