Hacker News new | past | comments | ask | show | jobs | submit login
SimCity UI Code + DRM (gist.github.com)
113 points by _fs on March 11, 2013 | hide | past | favorite | 61 comments



Good to see people using the Closure Tools. It really is a powerful toolkit.

Just a reminder that I (and some others, hopefully) are in ##closure-tools on Freenode to help you out if you have questions. Make sure to stay in the channel if you ask, as it's low-volume. I don't believe I've let a question go unanswered in well over a year, unless the person left right away and there was no point in answering it.

The channel itself is in no way affiliated with Google, nor am I.


What parts of SimCity were written in Javascript?

(Before you say "The UI, obvs", I'm wondering if it's a common practice to write UI code in games in javascript. I know that Gnome has a lot of JS that's tied into UI, I didn't expect the practice to make itself into game dev).

And if this code is actually from SimCity, does SimCity have some type of v8 or other js engine packaged with it?


I asked one of the SimCity devs a question along those lines this weekend: https://twitter.com/MaxisScott/status/310835756107177984

Q: Is the launcher built on WebKit or some other layout engine? Are there things in game that are WebKit too?

A: The whole UI is done in javascript running a custom version of webkit. :D


>I'm wondering if it's a common practice to write UI code in games in javascript

It's becoming more common. Libraries like Awesomium[1] make it trivial to render browser content to an in-game texture and the V8 engine, while not 'easy' to integrate, is quite empowering in that you can have the UI code call directly into your compiled code.

>does SimCity have some type of v8 or other js engine packaged with it?

Very probably. Most games have some scripting layer in their engines to simplify implementing non-performance-critical game logic. Lua and JS are two of the most popular languages for achieving that these days.

[1] http://awesomium.com/


FWIW, we were much happier with Chromium Embedded [1] than Awesomium. Awesomium is not free for non-commercial use (something like $8,000+ if you wanted source code included, the price of the software isn't even listed on the site now), the support was terrible, and the product seemed to be not nearly as fleshed out at Chromium Embedded. The website is prettier though...

We switched to using Chromium Embedded 6-8 months ago and haven't regretted it a bit.

[1] https://code.google.com/p/chromiumembedded/


We are starting to use Chromium Embedded for all of our in-game UIs, after going through Flash and (for a brief period) CEGUI. No major regrets here. In fact, we like HTML so much that we are considering moving our game tools (level editors, etc) to HTML as well, ala Insomniac [1].

[1] http://www.insomniacgames.com/new-generation-of-insomniacgam...


I am at the moment evaluating various browser components for headless browsing and based on my own brief tests, the two best ones seem to be Chromium Embedded and QtWebKit (I would prefer to have C/C++ solution, so PhantomJS and alike are unfortunately out of question).

Does anyone have any experience / is Chromium Embedded any better than using the WebKit component that is part of Qt 5.0 (QtWebKit)? Both seem to use same JavaScript engine (V8) and it seems that using QtWebKit in headless browser scenarios in Linux environment seems to be a bit easier, but I'm kind of wondering why there seems to be more buzz around CE than QTWK. On obvious reason of course is that using QTWK in app "forces" you to use Qt in other parts of the app that can be a good / bad thing depending on the details.


That was also my experience with Awesomium. It was buggy and support is awful. I actually tried to contact them for a company which was interested in buying a commercial license -- blows my mind that they'd get emails basically saying "we want to give you thousands of dollars" and not even answer. It may have improved since then but I'd be wary of introducing a dependency on it...


Quite some game UI's are done in Flash (check out ScaleForm by Autodesk: http://gameware.autodesk.com/scaleform) so using JavaScript seems plausible as well.


Some older games had the menus fully rendered in openGL. I'm not sure which is better, but I would think our society has more domain experience making menus with flash than with openGL.


exactly, the tooling and skillset for interface design/dev is extremely mature these days, and it probably isn't economical to move people onto custom solutions.



I can't really speak for how common it is, but the Battlefield Play4Free server browser is written in Javascript [1].

[1] http://documentcloud.github.com/backbone/#examples-battlefie...


I learned that a lot of game menus are/were written in flash which was a huge shock to me, js would be as much, if not more, of one


A lot of games use Flash for in-game UIs. See Scaleform.


How is it cool to put someone else's non-free copyrighted work on github?

I'm not a fan of DRM or what they did to Sim City... I've also got nothing against reverse engineering, excerpting code snippets, and that sort of thing. But violating the terms of GitHub, in addition to someone's copyright? Not cool.


The uploader has a decent argument of fair use.

Its uploaded in a nonprofit way, for educational purposes. It is a very small portion of the complete work. The effect of the use upon the potential market is either small or non-existant.

That is three out of the four-factor balancing test regarding fair use. If we guess that this is about 0.3% of the complete work (3MB of a very very large final product), that would be comparable to use 30s clip of a film.


I don't think this is a huge deal, but I'm going to have to go with the GP. This would be an extremely poor example of fair use, and would really only dilute the positive meanings of "fair use".

Fair use is meant to preserve access to copyrighted works by the public that granted copyright in the first place--to maintain certain rights for the public and to ensure that any works built upon copyrighted works (whether that's through inspired creativity or just commentary on the original) aren't themselves held hostage by the original copyright holder.

Nothing in this example is transformative of the original work. The poster just released something that somebody else holds the copyright to. The educational aspect is an extremely weak argument; something merely existing in front of your eyes is not educational. The classic take on educational fair use for this would be taking minimal samples from the source and providing commentary upon them (why they did something the way they did, how they solved a problem that plagues a lot of developers, etc).

It's also certainly not a minimal sampling of the material. The code has been minified, but there appears to be a substantial portion of their UI code intact here, and I wouldn't be surprised if it is largely functional without much more work. It would be dishonest to compare 3MB of scripts to a final game including assets, and even with substantial commentary and insight added to the code, you would not be able to justify redistributing this amount of the source. Again, it isn't quoting from a larger source, it's providing the entire source for large portions of the game engine.

IP law aside, I feel the same way about this as I imagine I would have felt if the Doom 3 source had leaked early: no, you won't be able to play the game for free with it, and it is cool to see, but it's shitty that the people that actually wrote it had no say in its release.


Do you feel confident enough in this assessment that you'd feel comfortable slapping a GPL license on this thing and using it in a project? (Nevermind for a moment that this code isn't at all re-usable.) I feel like that's more or less the standard that should apply to github. It's kind of a disservice to people who are using github for open source but are making effort to avoid this kind of taint. Maybe it's hyperbole but it makes it one step closer to github being "that place where I pirate simcity".


I would feel the same confidence regarding a 30s clip of a movie. That is to say, I would not feel confident at all, but that has nothing to do with the law and all to do how it is interpreted. I do however think that 30s clip of a 3hr movie, or 0.3% of the source code for a game should be small enough that fair use arguments can be made.

While github is often used as a publishing tool, it is also commonly used as a place where work-in-progress is placed. Thus, if someone wanted to create a commentary about a piece of copyrighted material (say, the DRM part of a game), I would personally consider copying the related code into a github repository for refinement to be fair use. So long the four-factor balancing test is fulfilled, why limit the available tools? Should the workplace where such refinement is taking place be limited only as private repositories where each party has a copyright license?

I would also like to note that nowhere in the law does it say that fair use must minimize their usage of the copyrighted work. The law says under its four-factor balancing test:

  the amount and substantiality of the portion used in relation to
  the copyrighted work as a whole.
How it is interpreted has changed widely in the last 15 years. Some talk about the minimal necessary amount for his or her intended use. Other talks about "the heart of the work". For a time, De minimis was considered, through was later reversed in 2005.


Misleading title. This is obviously a formatted version of minified JS, presumably extracted from the shipping game, not a leak.


obviously? care to share for those of us who don't read JS fluently?


In general, the code is dense and difficult to read. If you know what to look for, there are a few idioms scattered around that stand out as awkward, but save a few characters here and there. There are some pretty obvious examples right at the beginning, lines 2 and 4:

  var COMPILED = !0
  goog.DEBUG = !1
As you would expect in a weakly-typed C-style language, !0 == true, and !1 == false. JavaScript has first-class booleans, and there's not much to be gained by using one of these expressions instead of the literal it evaluates to. It only serves to save character count.

Here's another example. Starting at line 91, you get the following:

  !COMPILED && goog.ENABLE_DEBUG_LOADER && (goog.included_ = {}, goog.dependencies_ = { // ... and so forth
The above code uses commas in a particular way that usually means it comes from a minifier. JavaScript has a comma operator, which is basically just a statement separator, but it has the interesting property that it joins two statements as a single expression. As in many C-style languages, control flow operators like if and while don't require curly braces, and apply to the immediately following statement if they are absent. The combination of these two means that

  if(!foo){bar();baz();qux()}
is equivalent to

  if(!foo)bar(),baz(),qux();
which is shorter by a single character. Because the line of code joined by commas is now an expression, this also lets us abuse the short-circuit property of boolean operators, re-writing the above like so:

  foo&&(bar(),baz(),qux());
for a further savings of one more character.

You pretty much never see the comma operator in code written by humans, because it's not useful for anything. Seeing them in production code, especially the way shown above, almost always means the code came out of a minifier. That actually broke the Opera browser a few months back--apparently the parser ran out of stack space when handling a single expression consisting of over 1,400 comma-separated statements.


On the subject of minification, I see a lot of lines that look like this[1]:

  fireToken : 1 === f.tradeTokens.data.fireToken ? !0 : !1,
Why might that be a desired output from a minifier? Why not simply

  fireToken : 1 === f.tradeTokens.data.fireToken,
[1] https://gist.github.com/anonymous/5133829#file-simcityui-js-...


The original code could have been written as "condition ? true : false" and it's just not a pattern the minifier picks up on.


Probably a terrible mistake from the developer, not the minifier.

    fireToken: f.tradeTokens.data.fireToken === 1 ? true : false
In my company I see JS code like the following:

    if (!!someFlag) { /* ... */ }


Cool observations. Thanks!


The fact that the function parameter are (a, b, c) indicates that it was minified code.


Wasn't obvious to me on a first skim through either, but on second viewing all simple variable names are single letters - exactly as one would get through minifying. See for example here and below: https://gist.github.com/anonymous/5133829#file-simcityui-js-...


Curious. Decided to explore.

http://telemetry.simcity.com/simcity/rest/user/1 - user ID 1

http://telemetry.simcity.com/simcity/rest/user/1/friends - their friends

but I thought friends can only seem to see each other on their respective server region. this seems to be storing centralized data?

playing around with other nouns...

http://telemetry.simcity.com/simcity/rest/region/1 - even includes some of their S3 URLs.

http://telemetry.simcity.com/simcity/rest/box/2 - seems list CITY and REGION

i'm trying to think of other nouns to throw in there.

http://telemetry.simcity.com/simcity/rest/user/1027/regions - a list of user ID 1027's regions

http://telemetry.simcity.com/simcity/rest/user/10278/regions - this guy has 4 regions!

(I love RESTful services)


3.5 mb of Javascript. Glug. Looks like just the game ui. I cannot really find references to DRM like the title says.

Lots of nice references to cheats though! They all rely on simcity.DEBUG being true,

  if (simcity.DEBUG)
      switch (a) {
      case scrui.kKeyCodeDigit1:
        c && (e && !d) && scrui.RunCheat("scgameui -unlockalltools");
        break;
      case scrui.kKeyCodeLetterW:
        c && (e && !d) && simcity.CreateUnit("T_RCI_AddHourlyIncomeCheat")
      }
Also pretty nifty: There's a rest api built into the game for communication, (as wel as websockets) and the name and email of one of the programmers are still in the code.

  simcity.TutorialFakeUserData.creator = {
    uri : "/rest/user/0",
    tutorialState : 1,
    id : 0,
    screenName : "Chet Keno",
    email : "mayorkeno@simcity.com",
    nucleusId : 0,
    lastLogin : 0,
    isOnline : "true",
    avatarImage :     "Graphics/Tutorial/tutorial_avatar_keno.png",
    cities_count : 1
  };
Also interesting is that it seems that this is not just a stripped v8 engine. There's things in the code that make me think the whole game UI is actually a pretty decent webkit engine:

  simcity.gEventManager = new simcity.cEventManger;
  simcity.gEventTypes = {
    CLICK : "click",
    MOUSEUP : "mouseUp",
    MOUSEDOWN : "mouseDown",
    MOUSEOVER : "mouseOver",
    MOUSEOUT : "mouseOut",
    ITEMSELECTED : "itemSelected",
    KEYUP : "keyup",
    KEYDOWN : "keydown",
    DATACHANGED : "dataChanged",
    HASHCHANGED : "hashChanged",
    GENERICEVENT : "genericEvent",
    SOCKETEVENT : "socketEvent",
    SOCKETCONNECT : "socketConnect",
    SOCKETDISCONNECT : "socketDisconnect"
  };
  simcity.SocketManager = {};
  simcity.cSocketManager = function () {
    this.mSocketListeners = [];
    this.mSocketRequests = [];

update: It could be leftovers, but this indicates that this is code from a prerelease/debug build.

    simcity.SetUpDemoScreen = function () {
      simcity.persistentLayout = gUIManager.LoadLayout("Layouts/PersistentPreAlpha.js", gUIManager.GetRootWindow());
      simcity.SetTextOnElement(simcity.DemoExitButtonTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bc9", "EXIT"));
      simcity.SetTextOnElement(simcity.DemoAvailableReleaseDateTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bb2", "AVAILABLE FEBRUARY 2013"));
      simcity.SetTextOnElement(simcity.DemoThanksForPlayingTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7b4b",
      "THANKS for PLAYING"));
      simcity.SetTextOnElement(simcity.DemoPreOrderTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7bb1", "PRE-ORDER AT SIMCITY.COM"));
      simcity.SetTextOnElement(simcity.DemoNotFinalSoftwareTxt, new scrui.cLocaleString("Tutorials.json", "0x0eaf7b3c", "Not Final Software"))
    };


Yeah, it's not just v8. It's EAWebKit, a WebKit fork EA has been using in various games (You can find the source releases on their site).



Chet Keno is also the name of one of the mayors of a neighboring town during the tutorial.


The "DRM" that has upset most people is the (allegedly unnecessary) requirement to have a constant connection to the EA servers in order to play. The argument is that the minimal interaction with other cities on the server could easily be done client side, if EA wanted to allow an offline "single player" mode.


It would be an interesting project to offline the game - but I do wonder how hardcore the calculations are that run it. Would it be realistic to run the game itself and the server-side simulation stuff (nulling input from other cities) on an ordinary desktop computer? Or would you definitely need a secondary machine?


People have been saying that they could play for hours after the servers were offline. I think it's pretty safe to say there isn't much running on the server.


I highly doubt there's any significant calculation. If there was, EA would have to rent hundreds of thousands of EC2 instances to cover the running of the game.


On the other hand, if there's no significant calculation on the server, why would EA pretend they were having massive load problems? Sure, maybe they wanted to get some headlines about 'SimCity is so popular it's having launch shortages', but there's got to be a happy medium between "friendly headlines talking about SimCity's popularity" and "seething EA hatred plastered across the Internet".


I think it was just poor scalability engineering. They didn't expect that many users on launch and they didn't implement a system that could scale upwards.


They knew exactly how many users to expect, I'm sure. But you aren't going to pay for a solution to handle all of them when you expect the demand to taper off after a few days.


I'm pretty sure the server is running some, if not all, of the simulation code, in order to prevent cheating. If they just blindly trust client updates, I can simply send an update that gives me another million simoleons.


And this wouldn't matter that much because it's not truly a multiplayer game. You would be cheating at your single player , non-competitive game... big deal.


According to this (http://www.reddit.com/r/SimCity/comments/19xx7d/trying_some_...) the game runs completely fine without being connected to the server, just all the mp features won't work. All the server does is handle trading, leaderboards, regions and validation etc


I think it's worth noting that older versions ran completely fine executing the totality of the required code...


Note also that the last version of SimCity was launched in 2003, running on a completely different engine with a completely different simulation model.

The Maxis team has talked quite a bit about the challenges in making their agent-based simulation engine, Glassbox, performant on low- and medium-spec PCs.


The bit that stands out for me is that the last SimCity was single-threaded so even on modern multi-core machines it chugs.

SimCity is one of those games that would benefit hugely from multi-core computers so I find the whole "We moved core functions to our servers because your computers are too puny" reasoning to be lacking.

It seems obvious that the only reason they moved processing functionality to their servers is because they have to calculate the inter-region stuff which they cannot reliably farm out to untrusted clients.

Basically they decided that the new SimCity would be a "Social" game from the outset and architected around that, the DRM side-effect is just an added "bonus" for Maxis/EA.

Of course many people (Myself included) just want a new single-player SimCity... Hell I would even take the last SimCity with corrected routing and multi-thread support.


Indeed, many people are unhappy that this is not an upgraded SimCity of old. Perhaps that would have been a good thing, perhaps not, but that is certainly a different product than what they shipped. Either way, SimCity (2013) is an entirely different game from SimCity 4, even though the name is shared.

I'm not sure to what degree exactly the new SimCity was optimized for multicore, but just a cursory glance suggests that is making use of all 4 cores in my machine. In terms of what they actually shipped to be calculated in the cloud, my guess would be macro-economic and trade conditions, as well as regional dynamics. These require a view of all the data from the region, and are thus best computed live centrally.


I'm wondering if they are going to do the same with other games. I'm really happy EA decided to use JS to build the UI for the new Sim City game.

At the moment my team is working on a solution that allows other game dev companies to do the same thing and I'm glad to see big players taking a step in that direction.


There's lots of instances like

    if (something....)
        return !0;
    else
        return !1;
is there any reason why they aren't simply using `true` and `false`?

(because !1 is defined to return literally `false` and !0 to return `true`)


Likely was "true" in the original source (and the source in separate files) and was replaced by some minifier. Not clear if these are the files verbatim or if it was automatically formatted from minified js.


It is one of the peephole optimizations that Closure Compiler performs to save some space (read: 2-3 characters) in the final output: http://code.google.com/p/closure-compiler/source/browse/src/.... The compiler actually has a lot of cool little tricks like that to save space (since saving even a few bytes for a property like, say, Gmail, can translate to very large numbers very quickly)


I've created a gist containing a resume of the architecture used for simcity object if anyone is interested. http://goo.gl/eExRF


so there is some browser based simcity or is this ui for the last released(and controversial) game?


Is that all of the server-side logic of the game?


No, that's Javascript that handles the user interface.


At the very least it seems cut off...


Github lists a non-truncated version [1]

[1] https://gist.github.com/anonymous/5133829/raw/9340a6fdc35453...


top link: "This file has been truncated, view the full file [by clicking]."


Perfect example of how JavaScript is not good for app programming. It is great for little tasks, but does not scale well. Please create a standard for a browser VM instead of this JS as ASM workaround that everyone is starting to use.


That's the exact contrary.

What didn't scale was the server-side. The client-side is just fine.

As for the idea of a browser VM, it is a world we live in. Welcome to the wonders of Java applets.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: