# use WatTCP (WATT-32) for DOS networking
USE_WATT32=1
# gamespy/server browser:
USE_GAMESPY=1
# pci sound card support:
USE_SNDPCI=1
# pci sound card support as dxe3:
SNDPCI_DXE=1
# cd audio support:
USE_CDAUDIO=1
# abrash's asm optimizations:
USE_ASM=1
# dynamic refresh (video) modules:
REF_DXE=1
# compile which renderer if REF_DXE=0 : enable ONLY ONE!
REF_STATIC_SOFT=1
REF_STATIC_GL=0
# compile for which gl driver if REF_DXE=0 and REF_STATIC_GL=1
# mesa, sage, or fxmesa
REFGL_DRIVER=fxmesa
# compile which glide driver,if REF_DXE=0 and REF_STATIC_GL=1
# sst1=Voodoo Graphics, sst96=Voodoo Rush
# cvg=Voodoo2, h5=Banshee and Voodoo3/4/5
FX_GLIDE_HW=h5
# enable evil 3dfx glide hacks for native hardware gamma
# (if REF_DXE=0 and REF_STATIC_GL=1)
USE_3DFXGAMMA=1
# dynamic gamex86 modules:
GAME_DXE=1
# compile 2015 statically instead of base if GAME_DXE=0:
GAME2015=0

# build tools:
CC = gcc
CXX= gpp
NASM = nasm

LINKER  = $(CC)

# build flags:
INCLUDES = -Iclient -Igame -Iqcommon -Iserver -Idos -IGoa -IGoa/CEngine
CPPFLAGS = -DSIN
CPPFLAGS+= -DSPLIT_NETWORK_FRAME=1
# comment this out to enable assertions:
CPPFLAGS+= -DNDEBUG
CPPFLAGS+= $(INCLUDES)

ifeq ($(DEBUG),1)
CFLAGS = -g
else
CFLAGS = -O2
endif
CFLAGS+= -Wall
CFLAGS+= -ffast-math
#CFLAGS+= -fwrapv

CXXFLAGS = $(CFLAGS)
CXXFLAGS+= -fno-exceptions -fno-rtti
CXXFLAGS+= -Wno-sign-compare
CXXFLAGS+= -Wno-overloaded-virtual

LDFLAGS =
CLIBS = -lc -lgcc
#CLIBS += -lm
EXE = sin.exe
SPEECH = speech/libspeech.a

ifeq ($(GAME_DXE),0)
CPPFLAGS+= -DGAME_HARD_LINKED
endif

ifeq ($(USE_SSE),1)
# FS: approx 10-15% speed boost on my P3 550 when running timedemos, but could be dangerous.
CFLAGS+= -march=pentium3
EXE = sin_sse.exe
endif

ifeq ($(DEDICATED_ONLY),1)
CPPFLAGS+= -DDEDICATED_ONLY
USE_ASM = 0
REF_DXE = 1
USE_SNDPCI = 0
USE_CDAUDIO = 0
EXE = sinded.exe
endif

ifneq ($(REF_DXE),1)
CPPFLAGS+= -DREF_HARD_LINKED
endif

ifneq ($(USE_WATT32),1)
NET_OBJ = null/net_null.o
else
NET_OBJ = dos/net_watt.o
WATT32_CFLAGS = -Idos/3rdparty/wattcp/inc -DWATT32_NO_OLDIES
WATT32_LIBS = -Ldos/3rdparty/wattcp/lib -lwatt
endif

ifeq ($(USE_CDAUDIO),1)
CD_OBJ = dos/cd_dos.o
else
CD_OBJ = null/cd_null.o
endif

ifeq ($(USE_ASM),1)
CPPFLAGS+= -Did386=1
else
CPPFLAGS+= -Did386=0
endif

ifeq ($(USE_GAMESPY),1)
CPPFLAGS+= -DGAMESPY
endif

ifeq ($(USE_SNDPCI),1)
CPPFLAGS+= -DUSE_SNDPCI
CFLAGS+= -Idos/3rdparty/include
ifeq ($(SNDPCI_DXE),1)
CPPFLAGS+= -DSNDPCI_DXE
else
LDFLAGS+= -Ldos/3rdparty/lib -lau
endif
endif

ifneq ($(REF_DXE),1)
ifeq ($(REF_STATIC_GL),1)
EXE = sin_fx.exe
INCLUDES += -Iref_gl
INCLUDES += -Idos/3rdparty/include -Idos/3rdparty/include/glide3
LDFLAGS += -Ldos/3rdparty/lib/$(GLDIR) -lgl -Ldos/3rdparty/lib/$(FX_GLIDE_HW) -lglide3x
REF_STATIC_SOFT=0
# compile which gl driver in: mesa, sage, or fxmesa. (only one.)
ifeq ($(REFGL_DRIVER),fxmesa)
CPPFLAGS+= -DREFGL_FXMESA
endif
ifeq ($(REFGL_DRIVER),mesa)
CPPFLAGS+= -DREFGL_MESA
endif
ifeq ($(REFGL_DRIVER),sage)
CPPFLAGS+= -DREFGL_SAGE
endif
GLDIR=$(REFGL_DRIVER)
endif

ifeq ($(REF_STATIC_SOFT),1)
INCLUDES += -Iref_soft
endif
endif

# needed if building against djgpp <= 2.03
#VSNPRINTF = dos/vsnprntf.o

DOS = dos/dos_v2.o \
	$(NET_OBJ) \
	dos/q_shdos.o \
	$(VSNPRINTF) \
	dos/dxe.o \
	dos/sys_dos.o

ifneq ($(DEDICATED_ONLY),1)
DOSCLIENT = dos/vid_dos.o \
	null/cin_null.o \
	$(CD_OBJ) \
	dos/snddma_dos.o \
	dos/snd_gus.o \
	dos/snd_sb.o \
	dos/snd_pci.o \
	dos/in_dos.o

CLIENT = client/cl_bspline.o \
	client/cl_console.o \
	client/cl_ents.o \
	client/cl_fx.o \
	client/cl_hud.o \
	client/cl_input.o \
	client/cl_inv.o \
	client/cl_main.o \
	client/cl_parse.o \
	client/cl_pred.o \
	client/cl_scrn.o \
	client/cl_sincmds.o \
	client/cl_sinfx.o \
	client/cl_surface.o \
	client/cl_tent.o \
	client/cl_view.o \
	client/console3d.o \
	client/keys.o \
	client/ltstyle.o \
	client/menu3d.o \
	client/menu3d_pl.o \
	client/snd_dma.o \
	client/snd_mem.o \
	client/snd_mix.o \
	client/snd_music.o
CL_SPEECH = $(SPEECH)
LIB_SPEECH = -Lspeech -lspeech
else
CLIENT = null/cl_null.o
endif

ifeq ($(USE_GAMESPY),1)
ifneq ($(DEDICATED_ONLY),1)
GAMESPYCLIENT = client/cl_gamespy.o \
	Goa/CEngine/gserverlist.o
endif

GAMESPY = Goa/CEngine/gdarray.o \
	Goa/CEngine/gserver.o \
	Goa/CEngine/gutil.o \
	Goa/CEngine/hashtable.o \
	Goa/nonport.o \
	server/sv_gamespy.o \
	server/sv_gqueryreporting.o
endif

QCOMMON = qcommon/alias.o \
	qcommon/cmd.o \
	qcommon/cmodel.o \
	qcommon/common.o \
	qcommon/crc.o \
	qcommon/cvar.o \
	qcommon/files.o \
	qcommon/glob.o \
	qcommon/md4.o \
	qcommon/net_chan.o \
	qcommon/pmove.o \
	qcommon/sinmdl.o \
	qcommon/wave.o \
	game/q_shared.o

SERVER = server/sv_ccmds.o \
	server/sv_ents.o \
	server/sv_game.o \
	server/sv_init.o \
	server/sv_main.o \
	server/sv_send.o \
	server/sv_user.o \
	server/sv_world.o

ifneq ($(DEDICATED_ONLY),1)
ifneq ($(REF_DXE),1)
ifeq ($(REF_STATIC_SOFT),1)
REFSOFT = ref_soft/r_alias.o \
	ref_soft/r_main.o \
	ref_soft/r_light.o \
	ref_soft/r_misc.o \
	ref_soft/r_model.o \
	ref_soft/r_spritemod.o \
	ref_soft/r_sinmdl.o \
	ref_soft/r_console3d.o \
	ref_soft/r_tesselate.o \
	ref_soft/r_part.o \
	ref_soft/r_poly.o \
	ref_soft/r_polyse.o \
	ref_soft/r_rast.o \
	ref_soft/r_scan.o \
	ref_soft/r_sprite.o \
	ref_soft/r_surf.o \
	ref_soft/r_aclip.o \
	ref_soft/r_bsp.o \
	ref_soft/r_draw.o \
	ref_soft/r_edge.o \
	ref_soft/r_image.o \
	dos/vid_ext.o \
	dos/vregset.o \
	dos/swimp_dos.o
endif

ifeq ($(REF_STATIC_GL),1)
REFGL = ref_gl/gl_arrays.o \
	ref_gl/gl_console3d.o \
	ref_gl/gl_draw.o \
	ref_gl/gl_image.o \
	ref_gl/gl_light.o \
	ref_gl/gl_mesh.o \
	ref_gl/gl_model.o \
	ref_gl/gl_rmain.o \
	ref_gl/gl_rmisc.o \
	ref_gl/gl_rsurf.o \
	ref_gl/gl_sinmdl.o \
	ref_gl/gl_splatter.o \
	ref_gl/gl_sprite.o \
	ref_gl/gl_tesselate.o \
	ref_gl/gl_warp.o \
	dos/qgl_dos.o \
	dos/gl_fxmesa.o \
	dos/gl_dmesa.o \
	dos/gl_sage.o \
	dos/glimp_dos.o
ifeq ($(USE_3DFXGAMMA),1)
REFGL+= dos/fx_gamma.o
CPPFLAGS+= -DUSE_3DFXGAMMA
endif
endif
endif
endif

ifeq ($(USE_ASM),1)
COMMONASM = gas/math.o
CLIENTASM = gas/snd_mixa.o
ifneq ($(REF_DXE),1)
COMMONREFASM = gas/math_ref.o \
	gas/sys_dosa.o
ifeq ($(REF_STATIC_SOFT),1)
REFASM = gas/d_copy.o \
	gas/d_polysa.o \
	gas/d_polysa_16bit.o \
	gas/r_aclipa.o \
	gas/r_draw16.o \
	gas/r_draw16_16bit.o \
	gas/r_drawa.o \
	gas/r_edgea.o \
	gas/r_scana.o \
	gas/r_spr8.o \
	gas/r_surf8.o \
	gas/r_varsa.o
endif
endif
endif

ifeq ($(GAME_DXE),0)
GAMEOBJ= $(GAMEDIR)/actor.o \
	$(GAMEDIR)/ammo.o \
	$(GAMEDIR)/animals.o \
	$(GAMEDIR)/archive.o \
	$(GAMEDIR)/areaportal.o \
	$(GAMEDIR)/armor.o \
	$(GAMEDIR)/assaultrifle.o \
	$(GAMEDIR)/bacrodai.o \
	$(GAMEDIR)/behavior.o \
	$(GAMEDIR)/bouncingbetty.o \
	$(GAMEDIR)/box.o \
	$(GAMEDIR)/bspline.o \
	$(GAMEDIR)/bullet.o \
	$(GAMEDIR)/camera.o \
	$(GAMEDIR)/camgun.o \
	$(GAMEDIR)/chaingun.o \
	$(GAMEDIR)/class.o \
	$(GAMEDIR)/console.o \
	$(GAMEDIR)/ctf.o \
	$(GAMEDIR)/ctf_turret.o \
	$(GAMEDIR)/deadbody.o \
	$(GAMEDIR)/doors.o \
	$(GAMEDIR)/earthquake.o \
	$(GAMEDIR)/entity.o \
	$(GAMEDIR)/eonandpeon.o \
	$(GAMEDIR)/explosion.o \
	$(GAMEDIR)/fists.o \
	$(GAMEDIR)/gamescript.o \
	$(GAMEDIR)/genericbullet.o \
	$(GAMEDIR)/genericrocket.o \
	$(GAMEDIR)/gibs.o \
	$(GAMEDIR)/glowstick.o \
	$(GAMEDIR)/g_main.o \
	$(GAMEDIR)/g_phys.o \
	$(GAMEDIR)/grapple.o \
	$(GAMEDIR)/gravpath.o \
	$(GAMEDIR)/g_spawn.o \
	$(GAMEDIR)/g_utils.o \
	$(GAMEDIR)/hammer.o \
	$(GAMEDIR)/health.o \
	$(GAMEDIR)/heligun.o \
	$(GAMEDIR)/inventoryitem.o \
	$(GAMEDIR)/item.o \
	$(GAMEDIR)/keys.o \
	$(GAMEDIR)/launcher.o \
	$(GAMEDIR)/lensflare.o \
	$(GAMEDIR)/light.o \
	$(GAMEDIR)/listener.o \
	$(GAMEDIR)/magnum.o \
	$(GAMEDIR)/misc.o \
	$(GAMEDIR)/mover.o \
	$(GAMEDIR)/mutanthands.o \
	$(GAMEDIR)/navigate.o \
	$(GAMEDIR)/object.o \
	$(GAMEDIR)/path.o \
	$(GAMEDIR)/peon.o \
	$(GAMEDIR)/player.o \
	$(GAMEDIR)/playerstart.o \
	$(GAMEDIR)/powerups.o \
	$(GAMEDIR)/pulserifle.o \
	$(GAMEDIR)/quantumd.o \
	$(GAMEDIR)/reactiveshields.o \
	$(GAMEDIR)/rocketlauncher.o \
	$(GAMEDIR)/rocket_turret.o \
	$(GAMEDIR)/script.o \
	$(GAMEDIR)/scriptmaster.o \
	$(GAMEDIR)/scriptslave.o \
	$(GAMEDIR)/scriptvariable.o \
	$(GAMEDIR)/secgun.o \
	$(GAMEDIR)/securityturret.o \
	$(GAMEDIR)/sentient.o \
	$(GAMEDIR)/shield.o \
	$(GAMEDIR)/shotgun.o \
	$(GAMEDIR)/shotrocketlauncher.o \
	$(GAMEDIR)/silencer.o \
	$(GAMEDIR)/skeet.o \
	$(GAMEDIR)/sniperrifle.o \
	$(GAMEDIR)/speargun.o \
	$(GAMEDIR)/specialfx.o \
	$(GAMEDIR)/spidermine.o \
	$(GAMEDIR)/steering.o \
	$(GAMEDIR)/stungun.o \
	$(GAMEDIR)/surface.o \
	$(GAMEDIR)/testweapon.o \
	$(GAMEDIR)/thrall.o \
	$(GAMEDIR)/trigger.o \
	$(GAMEDIR)/turret.o \
	$(GAMEDIR)/vehicle.o \
	$(GAMEDIR)/viewthing.o \
	$(GAMEDIR)/weapon.o \
	$(GAMEDIR)/worldspawn.o \
	$(GAMEDIR)/wrench.o
#	$(GAMEDIR)/str.o
2015OBJ= $(GAMEDIR)/bob.o \
	$(GAMEDIR)/ceilingsteering.o \
	$(GAMEDIR)/checkpoints.o \
	$(GAMEDIR)/concussion.o \
	$(GAMEDIR)/crawler.o \
	$(GAMEDIR)/crossbow.o \
	$(GAMEDIR)/dualmagnum.o \
	$(GAMEDIR)/flamethrower.o \
	$(GAMEDIR)/flashlight.o \
	$(GAMEDIR)/goliath.o \
	$(GAMEDIR)/guidedmissile.o \
	$(GAMEDIR)/hoverbike.o \
	$(GAMEDIR)/hoverweap.o \
	$(GAMEDIR)/informergun.o \
	$(GAMEDIR)/jitter.o \
	$(GAMEDIR)/maneroboss.o \
	$(GAMEDIR)/mortician.o \
	$(GAMEDIR)/movecapture.o \
	$(GAMEDIR)/nuke.o \
	$(GAMEDIR)/rocketpack.o \
	$(GAMEDIR)/rope.o \
	$(GAMEDIR)/splitter.o \
	$(GAMEDIR)/spritegun.o \
	$(GAMEDIR)/thug.o
ifeq ($(GAME2015),1)
GAMEDIR = 2015
GAMEOBJ+= $(2015OBJ)
else
GAMEDIR = game
endif
LINKER  = $(CXX)
endif

OBJECTS = $(CLIENT) $(CLIENTASM) $(QCOMMON) $(COMMONASM) $(COMMONREFASM) $(SERVER) $(GAMESPY) $(GAMESPYCLIENT) $(REFSOFT) $(REFGL) $(REFASM) $(GAMEOBJ) $(DOS) $(DOSCLIENT)


.PHONY: clean

all: $(EXE)

$(EXE): $(OBJECTS) $(CL_SPEECH)
	$(LINKER) $(OBJECTS) $(LDFLAGS) $(LIB_SPEECH) $(GLIDE_LIBS) $(WATT32_LIBS) $(CLIBS) -o $(EXE)

$(SPEECH):
	$(MAKE) -C speech

clean:
	rm -f qcommon/*.o
	rm -f client/*.o
	rm -f server/*.o
	rm -f ref_soft/*.o
	rm -f ref_gl/*.o
	rm -f dos/*.o
	rm -f gas/*.o
	rm -f nasm/*.o
	rm -f null/*.o
	rm -f speech/*.o
	rm -f speech/*.a
	rm -f Goa/*.o
	rm -f Goa/CEngine/*.o
	rm -f game/*.o
	rm -f 2015/*.o

DO_AS=$(CC) -x assembler-with-cpp

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(WATT32_CFLAGS) -c $< -o $@

%.o: %.cpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@

%.o: %.s
	$(DO_AS) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
