Is it legal to make an open-source version of a closed-source game (and expansion pack) to distribute to other users? I want to do that for an old game called Age of Empires 2.
Disclosure: I am not a lawyer.
As for the general question, if your goal is to create free open-source software that facilitates playing a proprietary game, and you have programming skills, typically there is no legal impediment to writing a replacement game engine. (You may potentially fall afoul of software patents, but no more so than in any other major programming project to produce software for performing tasks that existing software already performs.) If you do this, then someone can install your engine, and then, if they have the artistic data (maps, "worlds", sounds, and so forth) from the game, they can play the game.
Developing a game engine for an existing game is a non-trivial task, and usually there will be differences between the way it behaves and the way the original game ran, though they might be slight. But the main thing you should realize is that the game data, representing the "world" of the game, will still be proprietary, and generally unlawful to redistribute. So people will still have to have a copy of the game (or at least certain files from it), to play it using your engine.
Another example (where it was actually the original developer that released the GNU/Linux version of the game engine as free open source software) is DOOM (and since then, most of Id Software's game engines have eventually been released as FOSS, sometimes with slight modifications for patent reasons). There are a number of community projects that have enhanced the original DOOM engine, allowing for more complex maps, enhanced graphics, and a more three-dimensional gameplay experience.
Given the limitations and that writing a game engine from scratch (plus testing it and fixing bugs) is a significant task, you might decide to make your own game instead.
Disclaimer: This answer is for informational purposes only and not for the purpose of providing legal advice. You should contact an attorney to obtain advice with respect to any particular issue or problem.
You can't copy it to be open-source. Even if the source were leaked, that is illegal. You can, however, follow in the footsteps of FreeCiv or Doom3. FreeCiv is based on Civilization, and is an open-source version. Doom3 is also open-source but requires the use of official game data for full operation or you must provide your own assets for models. Either way the engine is free, open source, and available to all. FreeCiv includes no game data since both the original and FreeCiv generate maps dynamically. The requirements to release your version are as follows:
Any patents related to the original must have expired. You can use a patent search such as Google Patent Search. Simply type your query as if it were a regular Google search, and under one or two web results, patents will be shown.
The program you are writing does not use strong cryptography that is restricted by the US government.(Or if it were downloaded in some countries, you could be considered an arms dealer) You may also register with the US Bureau of Industry and Security, which is a part of the US Department of Commerce, although the legal paperwork may be very difficult to file without professional legal counsel. Also see the BIS FAQ on encryption.
Your version does not use images or other files (such as sound files) from the original version.
Your version includes some (at least minor) changes.
You did not use, directly, or as an idea on how to write your version, any of the source of the original if it had been leaked.
You do not utilize the source's trademarks.
Do note that you have to code it, package it, and put it in a PPA or the repositories. This may be very difficult. There is no way to just copy-paste some magic from the old version. Please read the disclaimer below prior to doing anything to make your version, and for more guidance about the actual stages of coding, visit Game Development and Stack Overflow.
Legal disclaimer: This is not meant to be legal advice. The author of this answer, Ask Ubuntu, Stack Exchange, Inc, their representatives, agents, and/or any affiliated parties will not be responsible for any damages, incidental, or consequential, that may arise due to the use of these guidelines. These guidelines are based on US laws and regulations. Laws and regulations in your area may differ so check with a lawyer or local authorities.
Why not just join in with the development team here?
0 A.D is a fantastic AOE2 spin off, and could do with some devs. Also, you save yourself some of the horrors of undertaking such a large project like that yourself.
While the other answers adequately cover copyright and patent considerations, you should also be aware of trademark.
In particular, you asked:
Is it legal to make an open-source version of a closed-source game
Under trademark law, this depends on what you mean by "version". A trademark grants its owner the exclusive right to use a protected mark (such as "Age of Empires") in commerce. It forbids others from using the mark or a similar brand in a related area of commerce (like Age of Empires brand computers), as well as in unrelated areas in a way that could "dilute" the value of the mark (e.g. Age of Empires brand flush toilets).
So, you should not call your game "Age of Empires" or something similar, and you should make clear that it is a 3rd-party product not made by, endorsed by, or affiliated with the owners of the mark (I think Microsoft). However, it is fine to mention Age of Empires, e.g. to say your game is similar to or inspired by Age of Empires, since that would be a purely factual statement.
(Of course to be sure what you want is legal, consult a lawyer.)