The tricky part of OP problem comes down to what the relationships are.
If his friend is employed at a game company and paid the OP to do some of there work for them then the OP would be independent contractor and would in theory own the right to the derived work. The problem being of course that unless their friend got permission from the company to subcontract out to the OP then they didn't have any right to transfer the company's intellectual property to a third party and the OP's work would be illegal as they didn't have permissions from the original owner to make to use of the original code. So while the company can't use the OP work the OP can't either. They have of course opened themselves and their friend up to potential litigation by the company.
If the friend has a company that was hired to do work for the game company then it could be argued that the OP was an employee in which case the friends company would own the work.
This is where it gets a little trickier. The instructions were handed down by the company that my friend has, while my pay came directly from the client, and I didn't use the tools of either to author my code.
But now as I understand it I have the derivative work, but does that mean that, for example, I can choose to not allow my work to be used, and force a reversion to the original work?
It's not quite that simple. What happened is that you and whomever paid you did have a contract. It may not have been written down,but clearly since someone started writing you checks and you started writing code, there was some sort of agreement. You knew what to write, they knew how much to pay you.
Since it was not a written agreement, it gets very hard. If there is a dispute (which reading between the lines, it sounds like there is), then the way disputes are settled in such cases in in the courts. Either you sue them because they're using "your" code, or they sue you for their money back if you're not handing over the code you wrote, etc.
What will then happen is that a Judge will have to create a contract for you, after the fact, based on what you say, what they say and whatever evidence (email thread, logs, notes, etc). And they will decide who owns the code.
Although I would advice contacting a lawyer versed in copyright and contracts law, I have to believe that if someone gave you some instructions on what code to write, wrote checks for you to write it and then you wrote that code, it would be difficult to believe the agreement between you was anything but a "Work for Hire" agreement. A judge would look at all of the facts and then judge what would be a reasonable contract.
Although I suggest you contact an attorney (there-I've said it 4 times:)), if they paid you a reasonable rate (in the normal range for "work for hire" programming) and you accepted their money, I think you'd have an uphill case saying it wasn't a work for hire.
But to answer your question with a much more simple answer: If you write code and get paid for it, but don't have a written contract, that does NOT mean that you automatically own what you've written.
Edit for clarification. What I should have written was: If you write code and get paid for it and don't have a written contract it does not mean that you would automatically win if you were sued (for example, if you then sold/licensed it to someone else). It was a certainly huge error on the part of the person who is paying you to not ensure that you had a contract.
Brian Schmidt Register for GameSoundCon2012 San Francisco Oct 24-25
Thanks for that, pretty clear and concise. I'm sorry if I'm coming off pretty clueless about this, it's just that it is a situation I didn't expect to find myself in, and really need all the advice I can get to make informed decisions. Just to give a couple more details: I'm not using the company's tools, and to make matters even more complex, I'm actually being paid by a third party, not the people who give me instructions on code to write. Also what happens if the pay is far less than the standard work-for-hire rate? To clarify though, this isn't about money as such, I'm just wondering how it affects the issue.
as it has become pretty important recently, for details I can't and won't go into.
To me, this reads a bit like "I now have an offer from a direct competitor and since I wrote that stuff and there's no written contract, I'd reuse what I have, and sell to them". Don't correct me if I'm wrong, and don't tell me if I'm right -- just saying what this looks like.
If it is anything like that, stay away from that idea, for your own good. Don't even talk about it. Seriously.
I know you said don't correct you, but when you posted that I realised that's how it is coming across, and that's REALLY not the case. I've not been approached by anyone, nor do I want to sell the code to another party. Sorry I can't go into details, but let's just keep it vague with it has everything to do with working practices and conditions, and decisions being made about the code.
Thanks for the advice. So it IS as complicated as I thought. But now as I understand it I have the derivative work, but does that mean that, for example, I can choose to not allow my work to be used, and force a reversion to the original work? Also, to complicate matters further, it is only one element of the code that I didn't write from scratch, so does that make the rest of the work isn't derivative?