Mechanics: Projectiles involving a ball shot out of a cannon, moving in the opposite direction of the shot 
A child is playing with a toy cannon on the floor of a long railway carriage. The carriage is moving horizontally in a northerly direction with acceleration $a$. The child points the cannon southward at an angle $\theta$ to the horizontal and fires a toy shell which leaves the cannon at speed $V$. Find, in terms of $a$ and $g$, the value of $\tan 2\theta$ for which the cannon has maximum range (in the carriage).

I'm already quite familiar with projectiles and how to go about with most of those 
questions, but this is the first time I've seen a question in which the point from which the object is thrown from is already moving in the opposite direction, and I'm unsure about how I take this into consideration.
My attempt at this question has been to look at the SUVAT equations for the toy shell both horizontally and vertically, at the point where the shell lands back on the ground.
Horizontally:
S=?
U=$V\cos\theta$
V=?
A=$-a$? since the movement of the cannon in the opposite direction would reduce it's acceleration
T=?
Vertically:
S=$0$
U=$V\sin\theta$
V=?
A=$-g$
T=?
I'm not sure what step I should take next as there are too many unknowns when looking at the shells projectile horizontally. I'm aware that the cannon would have maximum range when $\theta=\frac{\pi}{4}$, but is the question simply asking to work out $\tan(2(\frac{\pi}{4}))$ or is there more to it. Any help would be greatly appreciated.

Let the point at which the child fires the toy cannon from be $P$. Let us consider the situation relative to point $P$. Let $d$ be the distance the shell travels horizontally. Let $T$ be the time for the shell to land after being fired.
Vertical motion:
$s = ut + \frac{1}{2} at^2$, we get $$ 0 = VT\sin \theta - \frac{g}{2}T^2 \implies T = \dfrac{2V\sin \theta}{g}$$
Horizontal motion:
$$d = VT\cos \theta + \frac{a}{2}T^2 \implies gd = V^2\sin 2\theta + \dfrac{2aV^2\sin^2 \theta}{g} $$
Differentiating with respect to $\theta$ yields $$ g\dfrac{\mathrm{d}d}{\mathrm{d}\theta} = 2V^2\cos 2\theta + \dfrac{2aV^2}{g}\sin 2\theta$$
For maximum range, we maximise $d$ by setting $\frac{\mathrm{d}d}{\mathrm{d}\theta} = 0$
$$ \begin{align}\therefore 2V^2\cos 2\theta + \dfrac{2aV^2}{g}\sin 2\theta &= 0 \\ \iff \cos 2\theta + \frac{a}{g} \sin 2\theta &= 0 \\ \iff \tan 2\theta = -\frac{g}{a} \end{align} $$
