1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819 |
- /*
-
- VISIBLE INFORMATION SET -aka- V I S
- Code based on original code from Valve Software,
- Modified by Sean "Zoner" Cavanaugh ([email protected]) with permission.
- Modified by Tony "Merl" Moore ([email protected])
- Contains code by Skyler "Zipster" York ([email protected]) - Included with permission.
-
- */
- #ifdef SYSTEM_WIN32
- #pragma warning(disable:4996)
- #endif
- //amckern - 64bit - '<' Singed/Unsigned Mismatch
- #pragma warning(disable:4018)
- #include "vis.h"
- #ifdef ZHLT_NETVIS
- #include "zlib.h"
- #endif
- /*
- NOTES
- */
- int g_numportals = 0;
- unsigned g_portalleafs = 0;
- portal_t* g_portals;
- leaf_t* g_leafs;
- // AJM: MVD
- #ifdef HLVIS_MAXDIST
- byte* g_mightsee;
- visblocker_t g_visblockers[MAX_VISBLOCKERS];
- int g_numvisblockers = 0;
- #endif
- //
- static byte* vismap;
- static byte* vismap_p;
- static byte* vismap_end; // past visfile
- static int originalvismapsize;
- byte* g_uncompressed; // [bitbytes*portalleafs]
- unsigned g_bitbytes; // (portalleafs+63)>>3
- unsigned g_bitlongs;
- bool g_fastvis = DEFAULT_FASTVIS;
- bool g_fullvis = DEFAULT_FULLVIS;
- bool g_estimate = DEFAULT_ESTIMATE;
- bool g_chart = DEFAULT_CHART;
- bool g_info = DEFAULT_INFO;
- #ifdef HLVIS_MAXDIST
- // AJM: MVD
- unsigned int g_maxdistance = DEFAULT_MAXDISTANCE_RANGE;
- //bool g_postcompile = DEFAULT_POST_COMPILE;
- //
- #endif
- #ifdef ZHLT_PROGRESSFILE // AJM
- char* g_progressfile = DEFAULT_PROGRESSFILE; // "-progressfile path"
- #endif
- static int totalvis = 0;
- #if ZHLT_ZONES
- Zones* g_Zones;
- #endif
- #ifdef ZHLT_NETVIS
- // -- these are definitions and initializations of C/CPP common variables
- volatile int g_visportalindex = UNINITIALIZED_PORTAL_INDEX; // a client's portal index : current portalindex being worked on
- volatile int g_visportals = 0; // the total portals in the map
- volatile int g_visleafs = 0; // the total portal leafs in the map
- volatile int g_vislocalportal = 0; // number of portals solved locally
- volatile enum vis_states g_visstate = VIS_STARTUP; // current step of execution
- volatile enum vis_modes g_vismode = VIS_MODE_NULL; // style of execution (client or server)
- volatile int g_visleafthread = 0; // control flag (are we ready to leafthread)
- unsigned int g_rate = DEFAULT_NETVIS_RATE;
- volatile double g_starttime = 0; // Start time (from I_FloatTime())
- volatile unsigned long g_idletime = 0; // Accumulated idle time in milliseconds (rolls over after 46.7 days, hopefully a vis client wont run that long)
- volatile unsigned long g_serverindex = 0; // client only variable, server index for calculating percentage indicators on the client
- short g_port = DEFAULT_NETVIS_PORT;
- const char* g_server_addr = NULL;
- volatile bool g_bsp_downloaded = false; // Client variable
- volatile bool g_prt_downloaded = false; // Client variable
- volatile bool g_mightsee_downloaded = false; // Client variable
- char* g_bsp_image = NULL; // Client/Server variable : Server uses it for cache for connecting clients, clients download it to memory to not require filesystem usage
- char* g_prt_image = NULL; // Client/Server variable : Server uses it for cache for connecting clients, clients download it to memory to not require filesystem usage
- unsigned long g_bsp_compressed_size = 0; // Server variable
- unsigned long g_prt_compressed_size = 0; // Server variable
- unsigned long g_bsp_size = 0; // Server variable
- unsigned long g_prt_size = 0; // Server variable
- #endif
- #ifdef ZHLT_INFO_COMPILE_PARAMETERS
- // AJM: addded in
- // =====================================================================================
- // GetParamsFromEnt
- // this function is called from parseentity when it encounters the
- // info_compile_parameters entity. each tool should have its own version of this
- // to handle its own specific settings.
- // =====================================================================================
- void GetParamsFromEnt(entity_t* mapent)
- {
- int iTmp;
- Log("\nCompile Settings detected from info_compile_parameters entity\n");
- // verbose(choices) : "Verbose compile messages" : 0 = [ 0 : "Off" 1 : "On" ]
- iTmp = IntForKey(mapent, "verbose");
- if (iTmp == 1)
- {
- g_verbose = true;
- }
- else if (iTmp == 0)
- {
- g_verbose = false;
- }
- Log("%30s [ %-9s ]\n", "Compile Option", "setting");
- Log("%30s [ %-9s ]\n", "Verbose Compile Messages", g_verbose ? "on" : "off");
- // estimate(choices) :"Estimate Compile Times?" : 0 = [ 0: "Yes" 1: "No" ]
- if (IntForKey(mapent, "estimate"))
- {
- g_estimate = true;
- }
- else
- {
- g_estimate = false;
- }
- Log("%30s [ %-9s ]\n", "Estimate Compile Times", g_estimate ? "on" : "off");
- // priority(choices) : "Priority Level" : 0 = [ 0 : "Normal" 1 : "High" -1 : "Low" ]
- if (!strcmp(ValueForKey(mapent, "priority"), "1"))
- {
- g_threadpriority = eThreadPriorityHigh;
- Log("%30s [ %-9s ]\n", "Thread Priority", "high");
- }
- else if (!strcmp(ValueForKey(mapent, "priority"), "-1"))
- {
- g_threadpriority = eThreadPriorityLow;
- Log("%30s [ %-9s ]\n", "Thread Priority", "low");
- }
- /*
- hlvis(choices) : "HLVIS" : 2 =
- [
- 0 : "Off"
- 1 : "Fast"
- 2 : "Normal"
- 3 : "Full"
- ]
- */
- iTmp = IntForKey(mapent, "hlvis");
- if (iTmp == 0)
- {
- Fatal(assume_TOOL_CANCEL,
- "%s flag was not checked in info_compile_parameters entity, execution of %s cancelled", g_Program, g_Program);
- CheckFatal();
- }
- else if (iTmp == 1)
- {
- g_fastvis = true;
- g_fullvis = false;
- }
- else if (iTmp == 2)
- {
- g_fastvis = false;
- g_fullvis = false;
- }
- else if (iTmp == 3)
- {
- g_fullvis = true;
- g_fastvis = false;
- }
- Log("%30s [ %-9s ]\n", "Fast VIS", g_fastvis ? "on" : "off");
- Log("%30s [ %-9s ]\n", "Full VIS", g_fullvis ? "on" : "off" );
- ///////////////////
- Log("\n");
- }
- #endif
- // =====================================================================================
- // PlaneFromWinding
- // =====================================================================================
- static void PlaneFromWinding(winding_t* w, plane_t* plane)
- {
- vec3_t v1;
- vec3_t v2;
- // calc plane
- VectorSubtract(w->points[2], w->points[1], v1);
- VectorSubtract(w->points[0], w->points[1], v2);
- CrossProduct(v2, v1, plane->normal);
- VectorNormalize(plane->normal);
- plane->dist = DotProduct(w->points[0], plane->normal);
- }
- // =====================================================================================
- // NewWinding
- // =====================================================================================
- static winding_t* NewWinding(const int points)
- {
- winding_t* w;
- int size;
- if (points > MAX_POINTS_ON_WINDING)
- {
- Error("NewWinding: %i points > MAX_POINTS_ON_WINDING", points);
- }
- size = (int)((winding_t*)0)->points[points];
- w = (winding_t*)calloc(1, size);
- return w;
- }
- //=============================================================================
- /////////
- // NETVIS
- #ifdef ZHLT_NETVIS
- // =====================================================================================
- // GetPortalPtr
- // converts a portal index to a pointer
- // =====================================================================================
- portal_t* GetPortalPtr(const long index)
- {
- if (index < (g_numportals * 2))
- {
- return g_portals + index;
- }
- else
- {
- return (NULL);
- }
- }
- // =====================================================================================
- // GetNextPortalIndex
- // This is called by ClientSockets
- // =====================================================================================
- int GetNextPortalIndex()
- {
- int j;
- int best = NO_PORTAL_INDEX;
- portal_t* p;
- portal_t* tp;
- int min;
- ThreadLock();
- min = 99999;
- p = NULL;
- for (j = 0, tp = g_portals; j < g_numportals * 2; j++, tp++)
- {
- if (tp->nummightsee < min && tp->status == stat_none)
- {
- min = tp->nummightsee;
- p = tp;
- best = j;
- }
- }
- if (p)
- {
- p->status = stat_working;
- }
- else
- {
- best = NO_PORTAL_INDEX; // hack to return NO_PORTAL_INDEX to the queue'ing code
- }
- ThreadUnlock();
- return best;
- }
- // =====================================================================================
- // AllPortalsDone
- // returns true if all portals are done...
- // =====================================================================================
- static int AllPortalsDone()
- {
- const unsigned numportals = g_numportals * 2;
- portal_t* tp;
- unsigned j;
- for (j = 0, tp = g_portals; j < numportals; j++, tp++)
- {
- if (tp->status != stat_done)
- {
- return 0;
- }
- }
- return 1;
- }
- #endif
- // NETVIS
- ///////////
- // =====================================================================================
- // GetNextPortal
- // Returns the next portal for a thread to work on
- // Returns the portals from the least complex, so the later ones can reuse the earlier information.
- // =====================================================================================
- static portal_t* GetNextPortal()
- {
- int j;
- portal_t* p;
- portal_t* tp;
- int min;
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_SERVER)
- {
- #else
- {
- if (GetThreadWork() == -1)
- {
- return NULL;
- }
- #endif
- ThreadLock();
- min = 99999;
- p = NULL;
- for (j = 0, tp = g_portals; j < g_numportals * 2; j++, tp++)
- {
- if (tp->nummightsee < min && tp->status == stat_none)
- {
- min = tp->nummightsee;
- p = tp;
- #ifdef ZHLT_NETVIS
- g_visportalindex = j;
- #endif
- }
- }
- if (p)
- {
- p->status = stat_working;
- }
- ThreadUnlock();
- return p;
- }
- #ifdef ZHLT_NETVIS
- else // AS CLIENT
- {
- while (getWorkFromClientQueue() == WAITING_FOR_PORTAL_INDEX)
- {
- unsigned delay = 100;
- g_idletime += delay; // This is the only point where the portal work goes idle, so its easy to add up just how idle it is.
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(1)\n");
- }
- NetvisSleep(delay);
- }
- if (g_visportalindex == NO_PORTAL_INDEX)
- {
- g_visstate = VIS_CLIENT_DONE;
- Send_VIS_GOING_DOWN(g_ClientSession);
- return NULL;
- }
- // convert index to pointer
- tp = GetPortalPtr(g_visportalindex);
- if (tp)
- {
- tp->status = stat_working;
- }
- return (tp);
- }
- #endif
- }
- #ifdef HLVIS_MAXDIST
- // AJM: MVD
- // =====================================================================================
- // DecompressAll
- // =====================================================================================
- void DecompressAll(void)
- {
- int i;
- byte *dest;
- for(i = 0; i < g_portalleafs; i++)
- {
- dest = g_uncompressed + i * g_bitbytes;
- DecompressVis((const unsigned char*)(g_dvisdata + (byte)g_dleafs[i + 1].visofs), dest, g_bitbytes);
- }
- }
- // AJM: MVD
- // =====================================================================================
- // CompressAll
- // =====================================================================================
- void CompressAll(void)
- {
- int i, x = 0;
- byte *dest;
- byte *src;
- byte compressed[MAX_MAP_LEAFS / 8];
- vismap_p = vismap;
-
- for(i = 0; i < g_portalleafs; i++)
- {
- memset(&compressed, 0, sizeof(compressed));
- src = g_uncompressed + i * g_bitbytes;
-
- // Compress all leafs into global compression buffer
- x = CompressVis(src, g_bitbytes, compressed, sizeof(compressed));
- dest = vismap_p;
- vismap_p += x;
- if (vismap_p > vismap_end)
- {
- Error("Vismap expansion overflow");
- }
-
- g_dleafs[i + 1].visofs = dest - vismap; // leaf 0 is a common solid
- memcpy(dest, compressed, x);
- }
- }
- #endif // HLVIS_MAXDIST
- // =====================================================================================
- // LeafThread
- // =====================================================================================
- #ifdef SYSTEM_WIN32
- #pragma warning(push)
- #pragma warning(disable:4100) // unreferenced formal parameter
- #endif
- #ifndef ZHLT_NETVIS
- static void LeafThread(int unused)
- {
- portal_t* p;
- while (1)
- {
- if (!(p = GetNextPortal()))
- {
- return;
- }
- PortalFlow(p);
- Verbose("portal:%4i mightsee:%4i cansee:%4i\n", (int)(p - g_portals), p->nummightsee, p->numcansee);
- }
- }
- #endif //!ZHLT_NETVIS
- #ifdef ZHLT_NETVIS
- static void LeafThread(int unused)
- {
- if (g_vismode == VIS_MODE_CLIENT)
- {
- portal_t* p;
- g_visstate = VIS_BASE_PORTAL_VIS_SERVER_WAIT;
- Send_VIS_LEAFTHREAD(g_visleafs, g_visportals, g_bitbytes);
- while (!g_visleafthread)
- {
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(2)\n");
- }
- NetvisSleep(100);
- }
- g_visstate = VIS_PORTAL_FLOW;
- Send_VIS_WANT_FULL_SYNC();
- while (!g_NetvisAbort)
- {
- if (!(p = GetNextPortal()))
- {
- return;
- }
- PortalFlow(p);
- Send_VIS_DONE_PORTAL(g_visportalindex, p);
- g_vislocalportal++;
- }
- }
- else if (g_vismode == VIS_MODE_SERVER)
- {
- #if 0
- // Server does zero work in ZHLT netvis
- g_visstate = VIS_WAIT_CLIENTS;
- while (!g_NetvisAbort)
- {
- NetvisSleep(1000);
- if (AllPortalsDone())
- {
- g_visstate = VIS_POST;
- return;
- }
- }
- #else
- portal_t* p;
- g_visstate = VIS_WAIT_CLIENTS;
- while (!g_NetvisAbort)
- {
- if (!(p = GetNextPortal()))
- {
- if (AllPortalsDone())
- {
- g_visstate = VIS_POST;
- return;
- }
- NetvisSleep(1000); // No need to churn while waiting on slow clients
- continue;
- }
- PortalFlow(p);
- g_vislocalportal++;
- }
- #endif
- }
- else
- {
- hlassume(false, assume_VALID_NETVIS_STATE);
- }
- }
- #endif
- #ifdef SYSTEM_WIN32
- #pragma warning(pop)
- #endif
- // =====================================================================================
- // LeafFlow
- // Builds the entire visibility list for a leaf
- // =====================================================================================
- static void LeafFlow(const int leafnum)
- {
- leaf_t* leaf;
- byte* outbuffer;
- byte compressed[MAX_MAP_LEAFS / 8];
- unsigned i;
- unsigned j;
- int k;
- int tmp;
- int numvis;
- byte* dest;
- portal_t* p;
- //
- // flow through all portals, collecting visible bits
- //
- memset(compressed, 0, sizeof(compressed));
- outbuffer = g_uncompressed + leafnum * g_bitbytes;
- leaf = &g_leafs[leafnum];
- tmp = 0;
- const unsigned offset = leafnum >> 3;
- const unsigned bit = (1 << (leafnum & 7));
- for (i = 0; i < leaf->numportals; i++)
- {
- p = leaf->portals[i];
- if (p->status != stat_done)
- {
- Error("portal not done (leaf %d)", leafnum);
- }
- {
- byte* dst = outbuffer;
- byte* src = p->visbits;
- for (j=0; j<g_bitbytes; j++, dst++, src++)
- {
- *dst |= *src;
- }
- }
- if ((tmp == 0) && (outbuffer[offset] & bit))
- {
- tmp = 1;
- Warning("Leaf portals saw into leaf");
- Log(" Problem at portal between leaves %i and %i:\n ", leafnum, p->leaf);
- for (k = 0; k < p->winding->numpoints; k++)
- {
- Log(" (%4.3f %4.3f %4.3f)\n", p->winding->points[k][0], p->winding->points[k][1], p->winding->points[k][2]);
- }
- Log("\n");
- }
- }
- outbuffer[offset] |= bit;
- numvis = 0;
-
- for (i = 0; i < (g_portalleafs >> 3); i++)
- {
- byte tmp_byte = outbuffer[i];
- int k;
- for (k=0;k<8;k++)
- {
- if (tmp_byte & (1 << k))
- {
- numvis++;
- }
- }
- }
- //
- // compress the bit string
- //
- Verbose("leaf %4i : %4i visible\n", leafnum, numvis);
- totalvis += numvis;
- i = CompressVis(outbuffer, g_bitbytes, compressed, sizeof(compressed));
- dest = vismap_p;
- vismap_p += i;
- if (vismap_p > vismap_end)
- {
- Error("Vismap expansion overflow");
- }
- g_dleafs[leafnum + 1].visofs = dest - vismap; // leaf 0 is a common solid
- memcpy(dest, compressed, i);
- }
- // =====================================================================================
- // CalcPortalVis
- // =====================================================================================
- static void CalcPortalVis()
- {
- #ifndef ZHLT_NETVIS
- // g_fastvis just uses mightsee for a very loose bound
- if (g_fastvis)
- {
- int i;
- for (i = 0; i < g_numportals * 2; i++)
- {
- g_portals[i].visbits = g_portals[i].mightsee;
- g_portals[i].status = stat_done;
- }
- return;
- }
- #endif
- #ifdef ZHLT_NETVIS
- LeafThread(0);
- #else
- NamedRunThreadsOn(g_numportals * 2, g_estimate, LeafThread);
- #endif
- }
- //////////////
- // ZHLT_NETVIS
- #ifdef ZHLT_NETVIS
- // =====================================================================================
- // CalcVis
- // =====================================================================================
- static void CalcVis()
- {
- unsigned lastpercent = 0;
- int x, size;
- if (g_vismode == VIS_MODE_SERVER)
- {
- g_visstate = VIS_BASE_PORTAL_VIS;
- Log("BasePortalVis: \n");
-
- for (x = 0, size = g_numportals * 2; x < size; x++)
- {
- unsigned percent = (x * 100 / size);
-
- if (percent && (percent != lastpercent) && ((percent % 10) == 0))
- {
- lastpercent = percent;
- printf("%d%%....", percent);
- }
- BasePortalVis(x);
- }
- printf("\n");
- }
- else
- {
- Send_VIS_WANT_MIGHTSEE_DATA();
- while(!g_mightsee_downloaded)
- {
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(3)\n");
- }
- NetvisSleep(100);
- }
- }
- g_visportals = g_numportals;
- g_visleafs = g_portalleafs;
- g_starttime = I_FloatTime();
- StartStatusDisplayThread(g_rate);
- CalcPortalVis();
- if (g_vismode == VIS_MODE_SERVER)
- {
- unsigned int i;
- for (i = 0; i < g_portalleafs; i++)
- {
- LeafFlow(i);
- }
- Log("Average leafs visible: %i\n", totalvis / g_portalleafs);
- }
- }
- #endif
- #ifndef ZHLT_NETVIS
- #ifdef HLVIS_MAXDIST
- // AJM: MVD
- // =====================================================================================
- // GetVisBlock
- // =====================================================================================
- visblocker_t *GetVisBlock(char *name)
- {
- int i;
- visblocker_t *v;
- for(i = 0, v = &g_visblockers[0]; i < g_numvisblockers; i++, v++)
- {
- if(!strcmp(name, v->name))
- return v;
- }
- return NULL;
- }
- // AJM: MVD
- // =====================================================================================
- // InitVisBlock
- // =====================================================================================
- static void InitVisBlock(void)
- {
- char visfile[_MAX_PATH];
- int i;
- int x = 0;
- int num_blocks;
- int num_sides;
- strcpy_s(visfile, g_Mapname);
- DefaultExtension(visfile, ".vis");
- if(!q_exists(visfile))
- return;
- FILE *fp = fopen(visfile, "r");
- if(!fp)
- return;
- while(!feof(fp))
- {
- fscanf_s(fp, "%s\n", g_visblockers[x].name);
- fscanf_s(fp, "%d\n", &num_blocks);
-
- for(i = 0; i < num_blocks; i++)
- {
- fscanf_s(fp, "%s\n", g_visblockers[x].blocknames[i]);
- }
- g_visblockers[x].numnames = num_blocks;
- fscanf_s(fp, "%d\n", &num_sides);
- for(i = 0; i < num_sides; i++)
- {
- fscanf_s(fp, "%f %f %f %f\n", &g_visblockers[x].planes[i].normal[0],
- &g_visblockers[x].planes[i].normal[1],
- &g_visblockers[x].planes[i].normal[2],
- &g_visblockers[x].planes[i].dist);
- }
- g_visblockers[x].numplanes = num_sides;
- g_visblockers[x].numleafs = 0;
- x++;
- }
- g_numvisblockers = x;
- }
- // AJM: MVD
- // =====================================================================================
- // SetupVisBlockLeafs
- // Set up the leafs for the visblocker
- // =====================================================================================
- static void SetupVisBlockLeafs(void)
- {
- int i, j, k, l, q;
- visblocker_t *v;
- leaf_t *leaf;
- portal_t *p;
- plane_t *plane;
- float dist;
- for(i = 0, v = &g_visblockers[0]; i < g_numvisblockers; i++, v++)
- {
- for(j = 0, leaf = &g_leafs[0]; j < g_portalleafs; j++, leaf++)
- {
- for(q = 0, p = leaf->portals[0]; q < leaf->numportals; q++, p++)
- {
- for(k = 0; k < p->winding->numpoints; k++)
- {
- for(l = 0, plane = &v->planes[0]; l < v->numplanes; l++, plane++)
- {
- dist = DotProduct(p->winding->points[k], plane->normal) - plane->dist;
-
- if(dist > ON_EPSILON)
- goto PostLoop;
- }
- }
- }
- PostLoop:
- if(q != leaf->numportals)
- continue;
- // If we reach this point, then the portal is completely inside the visblocker
- v->blockleafs[v->numleafs++] = j;
- }
- }
- }
- // AJM: MVD
- // =====================================================================================
- // SaveVisData
- // =====================================================================================
- void SaveVisData(const char *filename)
- {
- int i;
- FILE *fp = fopen(filename, "wb");
- if(!fp)
- return;
- SafeWrite(fp, g_dvisdata, (vismap_p - g_dvisdata));
- // BUG BUG BUG!
- // Leaf offsets need to be saved too!!!!
- for(i = 0; i < g_numleafs; i++)
- {
- SafeWrite(fp, &g_dleafs[i].visofs, sizeof(int));
- }
- fclose(fp);
- }
- // AJM: MVD
- // =====================================================================================
- // ResetPortalStatus
- // FIX: Used to reset p->status to stat_none; now it justs frees p->visbits
- // =====================================================================================
- void ResetPortalStatus(void)
- {
- int i;
- portal_t* p = g_portals;
- for(i = 0; i < g_numportals * 2; i++, p++)
- {
- //p->status = stat_none;
- free(p->visbits);
- }
- }
- #endif // HLVIS_MAXDIST
- // AJM UNDONE HLVIS_MAXDIST THIS!!!!!!!!!!!!!
- // AJM: MVD modified
- // =====================================================================================
- // CalcVis
- // =====================================================================================
- static void CalcVis()
- {
- unsigned i;
- char visdatafile[_MAX_PATH];
- strcpy_s(visdatafile, g_Mapname);
- DefaultExtension(visdatafile, ".vdt");
- // Remove this file
- _unlink(visdatafile);
- /* if(g_postcompile)
- {
- if(!g_maxdistance)
- {
- Error("Must use -maxdistance parameter with -postcompile");
- }
- // Decompress everything so we can edit it
- DecompressAll();
-
- NamedRunThreadsOn(g_portalleafs, g_estimate, PostMaxDistVis);
- // Recompress it
- CompressAll();
- }
- else
- {*/
- // InitVisBlock();
- // SetupVisBlockLeafs();
- NamedRunThreadsOn(g_numportals * 2, g_estimate, BasePortalVis);
- // if(g_numvisblockers)
- // NamedRunThreadsOn(g_numvisblockers, g_estimate, BlockVis);
- // First do a normal VIS, save to file, then redo MaxDistVis
- CalcPortalVis();
- //
- // assemble the leaf vis lists by oring and compressing the portal lists
- //
- for (i = 0; i < g_portalleafs; i++)
- {
- LeafFlow(i);
- }
- Log("Average leafs visible: %i\n", totalvis / g_portalleafs);
- if(g_maxdistance)
- {
- totalvis = 0;
-
- Log("Saving visdata to %s...\n", visdatafile);
- SaveVisData(visdatafile);
- // We need to reset the uncompressed variable and portal visbits
- free(g_uncompressed);
- g_uncompressed = (byte*)calloc(g_portalleafs, g_bitbytes);
- vismap_p = g_dvisdata;
- // We don't need to run BasePortalVis again
- NamedRunThreadsOn(g_portalleafs, g_estimate, MaxDistVis);
- // No need to run this - MaxDistVis now writes directly to visbits after the initial VIS
- //CalcPortalVis();
-
- for (i = 0; i < g_portalleafs; i++)
- {
- LeafFlow(i);
- }
- // FIX: Used to reset p->status to stat_none; now it justs frees p->visbits
- ResetPortalStatus();
- Log("Average maxdistance leafs visible: %i\n", totalvis / g_portalleafs);
- }
- // }
- }
- #endif //!ZHLT_NETVIS
- // ZHLT_NETVIS
- //////////////
- // =====================================================================================
- // CheckNullToken
- // =====================================================================================
- static INLINE void FASTCALL CheckNullToken(const char*const token)
- {
- if (token == NULL)
- {
- Error("LoadPortals: Damaged or invalid .prt file\n");
- }
- }
- // =====================================================================================
- // LoadPortals
- // =====================================================================================
- static void LoadPortals(char* portal_image)
- {
- int i, j;
- portal_t* p;
- leaf_t* l;
- int numpoints;
- winding_t* w;
- int leafnums[2];
- plane_t plane;
- const char* const seperators = " ()\r\n\t";
- char* token;
- token = strtok(portal_image, seperators);
- CheckNullToken(token);
- if (!sscanf_s(token, "%u", &g_portalleafs))
- {
- Error("LoadPortals: failed to read header: number of leafs");
- }
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- if (!sscanf_s(token, "%i", &g_numportals))
- {
- Error("LoadPortals: failed to read header: number of portals");
- }
-
- Log("Portalleafs: %4i\n", g_portalleafs);
- Log("Numportals: %4i\n", g_numportals);
- g_bitbytes = ((g_portalleafs + 63) & ~63) >> 3;
- g_bitlongs = g_bitbytes / sizeof(long);
- // each file portal is split into two memory portals
- g_portals = (portal_t*)calloc(2 * g_numportals, sizeof(portal_t));
- g_leafs = (leaf_t*)calloc(g_portalleafs, sizeof(leaf_t));
- originalvismapsize = g_portalleafs * ((g_portalleafs + 7) / 8);
- vismap = vismap_p = g_dvisdata;
- vismap_end = vismap + MAX_MAP_VISIBILITY;
- for (i = 0, p = g_portals; i < g_numportals; i++)
- {
- unsigned rval = 0;
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%i", &numpoints);
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%i", &leafnums[0]);
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%i", &leafnums[1]);
- if (rval != 3)
- {
- Error("LoadPortals: reading portal %i", i);
- }
- if (numpoints > MAX_POINTS_ON_WINDING)
- {
- Error("LoadPortals: portal %i has too many points", i);
- }
- if (((unsigned)leafnums[0] > g_portalleafs) || ((unsigned)leafnums[1] > g_portalleafs))
- {
- Error("LoadPortals: reading portal %i", i);
- }
- w = p->winding = NewWinding(numpoints);
- w->original = true;
- w->numpoints = numpoints;
- for (j = 0; j < numpoints; j++)
- {
- int k;
- double v[3];
- unsigned rval = 0;
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%lf", &v[0]);
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%lf", &v[1]);
- token = strtok(NULL, seperators);
- CheckNullToken(token);
- rval += sscanf_s(token, "%lf", &v[2]);
- // scanf into double, then assign to vec_t
- if (rval != 3)
- {
- Error("LoadPortals: reading portal %i", i);
- }
- for (k = 0; k < 3; k++)
- {
- w->points[j][k] = v[k];
- }
- }
- // calc plane
- PlaneFromWinding(w, &plane);
- // create forward portal
- l = &g_leafs[leafnums[0]];
- hlassume(l->numportals < MAX_PORTALS_ON_LEAF, assume_MAX_PORTALS_ON_LEAF);
- l->portals[l->numportals] = p;
- l->numportals++;
- p->winding = w;
- VectorSubtract(vec3_origin, plane.normal, p->plane.normal);
- p->plane.dist = -plane.dist;
- p->leaf = leafnums[1];
- p++;
- // create backwards portal
- l = &g_leafs[leafnums[1]];
- hlassume(l->numportals < MAX_PORTALS_ON_LEAF, assume_MAX_PORTALS_ON_LEAF);
- l->portals[l->numportals] = p;
- l->numportals++;
- p->winding = NewWinding(w->numpoints);
- p->winding->numpoints = w->numpoints;
- for (j = 0; j < w->numpoints; j++)
- {
- VectorCopy(w->points[w->numpoints - 1 - j], p->winding->points[j]);
- }
- p->plane = plane;
- p->leaf = leafnums[0];
- p++;
- }
- }
- // =====================================================================================
- // LoadPortalsByFilename
- // =====================================================================================
- static void LoadPortalsByFilename(const char* const filename)
- {
- char* file_image;
- if (!q_exists(filename))
- {
- Error("Portal file '%s' does not exist, cannot vis the map\n", filename);
- }
- LoadFile(filename, &file_image);
- LoadPortals(file_image);
- free(file_image);
- }
- #if ZHLT_ZONES
- // =====================================================================================
- // AssignPortalsToZones
- // =====================================================================================
- static void AssignPortalsToZones()
- {
- hlassert(g_Zones != NULL);
- UINT32 count = 0;
- portal_t* p;
- UINT32 x;
- UINT32 tmp[20];
- memset(tmp, 0, sizeof(tmp));
- UINT32 numportals = g_numportals * 2;
- for (x=0, p=g_portals; x<numportals; x++, p++)
- {
- BoundingBox bounds;
- winding_t* w = p->winding;
- UINT32 numpoints = w->numpoints;
- UINT32 y;
- for (y=0; y<numpoints; y++)
- {
- bounds.add(w->points[y]);
- }
- p->zone = g_Zones->getZoneFromBounds(bounds);
- tmp[p->zone]++;
- if (p->zone)
- {
- count++;
- }
- }
- for (x=0; x<15; x++)
- {
- Log("Zone %2u : %u\n", x, tmp[x]);
- }
- Log("%u of %u portals were contained in func_vis zones\n", count, numportals);
- }
- #endif
- // =====================================================================================
- // Usage
- // =====================================================================================
- static void Usage()
- {
- Banner();
- Log("\n-= %s Options =-\n\n", g_Program);
- Log(" -full : Full vis\n");
- Log(" -fast : Fast vis\n\n");
- #ifdef ZHLT_NETVIS
- Log(" -connect address : Connect to netvis server at address as a client\n");
- Log(" -server : Run as the netvis server\n");
- Log(" -port # : Use a non-standard port for netvis\n");
- Log(" -rate # : Alter the display update rate\n\n");
- #endif
- Log(" -texdata # : Alter maximum texture memory limit (in kb)\n");
- Log(" -lightdata # : Alter maximum texture memory limit (in kb)\n");
- Log(" -chart : display bsp statitics\n");
- Log(" -low | -high : run program an altered priority level\n");
- Log(" -nolog : don't generate the compile logfiles\n");
- Log(" -threads # : manually specify the number of threads to run\n");
- Log(" -noconcolors : Disable Windows console text coloring\n");
- #ifdef SYSTEM_WIN32
- Log(" -estimate : display estimated time during compile\n");
- #endif
- #ifdef ZHLT_PROGRESSFILE // AJM
- Log(" -progressfile path : specify the path to a file for progress estimate output\n");
- #endif
- #ifdef SYSTEM_POSIX
- Log(" -noestimate : do not display continuous compile time estimates\n");
- #endif
- #ifdef HLVIS_MAXDIST // AJM: MVD
- Log(" -maxdistance # : Alter the maximum distance for visibility\n");
- #endif
- Log(" -verbose : compile with verbose messages\n");
- Log(" -noinfo : Do not show tool configuration information\n");
- Log(" -dev # : compile with developer message\n\n");
- Log(" mapfile : The mapfile to compile\n\n");
- #ifdef ZHLT_NETVIS
- Log("\n"
- "In netvis one computer must be the server and all the rest are the clients.\n"
- "The server should be started with : netvis -server mapname\n"
- "And the clients should be started with : netvis -connect servername\n"
- "\n"
- "The default socket it uses is 21212 and can be changed with -port\n"
- "The default update rate is 60 seconds and can be changed with -rate\n");
- #endif
- exit(1);
- }
- // =====================================================================================
- // Settings
- // =====================================================================================
- static void Settings()
- {
- char* tmp;
- if (!g_info)
- {
- return;
- }
- Log("\n-= Current %s Settings =-\n", g_Program);
- Log("Name | Setting | Default\n" "-------------------|-------------|-------------------------\n");
- // ZHLT Common Settings
- if (DEFAULT_NUMTHREADS == -1)
- {
- Log("threads [ %9d ] [ Varies ]\n", g_numthreads);
- }
- else
- {
- Log("threads [ %9d ] [ %9d ]\n", g_numthreads, DEFAULT_NUMTHREADS);
- }
- Log("verbose [ %9s ] [ %9s ]\n", g_verbose ? "on" : "off", DEFAULT_VERBOSE ? "on" : "off");
- Log("log [ %9s ] [ %9s ]\n", g_log ? "on" : "off", DEFAULT_LOG ? "on" : "off");
- Log("developer [ %9d ] [ %9d ]\n", g_developer, DEFAULT_DEVELOPER);
- Log("chart [ %9s ] [ %9s ]\n", g_chart ? "on" : "off", DEFAULT_CHART ? "on" : "off");
- Log("estimate [ %9s ] [ %9s ]\n", g_estimate ? "on" : "off", DEFAULT_ESTIMATE ? "on" : "off");
- Log("max texture memory [ %9d ] [ %9d ]\n", g_max_map_miptex, DEFAULT_MAX_MAP_MIPTEX);
- #ifdef HLVIS_MAXDIST // AJM: MVD
- Log("max vis distance [ %9d ] [ %9d ]\n", g_maxdistance, DEFAULT_MAXDISTANCE_RANGE);
- //Log("max dist only [ %9s ] [ %9s ]\n", g_postcompile ? "on" : "off", DEFAULT_POST_COMPILE ? "on" : "off");
- #endif
- switch (g_threadpriority)
- {
- case eThreadPriorityNormal:
- default:
- tmp = "Normal";
- break;
- case eThreadPriorityLow:
- tmp = "Low";
- break;
- case eThreadPriorityHigh:
- tmp = "High";
- break;
- }
- Log("priority [ %9s ] [ %9s ]\n", tmp, "Normal");
- Log("\n");
- // HLVIS Specific Settings
- Log("fast vis [ %9s ] [ %9s ]\n", g_fastvis ? "on" : "off", DEFAULT_FASTVIS ? "on" : "off");
- Log("full vis [ %9s ] [ %9s ]\n", g_fullvis ? "on" : "off", DEFAULT_FULLVIS ? "on" : "off");
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_SERVER)
- {
- Log("netvis mode [ Server ]\n");
- }
- else if (g_vismode == VIS_MODE_CLIENT)
- {
- Log("netvis mode [ Client, connected to %s ]\n", g_server_addr);
- }
- Log("netvis port [ %9d ] [ %9d ]\n", g_port, DEFAULT_NETVIS_PORT);
- Log("netvis display rate [ %9d ] [ %9d ]\n", g_rate, DEFAULT_NETVIS_RATE);
- #endif
- Log("\n\n");
- }
- // =====================================================================================
- // main
- // =====================================================================================
- int main(const int argc, char** argv)
- {
- char portalfile[_MAX_PATH];
- char source[_MAX_PATH];
- int i;
- double start, end;
- const char* mapname_from_arg = NULL;
- #ifdef ZHLT_NETVIS
- g_Program = "netvis";
- #else
- g_Program = "hlvis";
- #endif
- if (argc == 1)
- {
- Usage();
- }
- for (i = 1; i < argc; i++)
- {
- if (!strcasecmp(argv[i], "-threads"))
- {
- if (i < argc)
- {
- g_numthreads = atoi(argv[++i]);
- if (g_numthreads < 1)
- {
- Log("Expected value of at least 1 for '-threads'\n");
- Usage();
- }
- }
- else
- {
- Usage();
- }
- }
- #ifdef SYSTEM_WIN32
- else if (!strcasecmp(argv[i], "-estimate"))
- {
- g_estimate = true;
- }
- #endif
- #ifdef SYSTEM_POSIX
- else if (!strcasecmp(argv[i], "-noestimate"))
- {
- g_estimate = false;
- }
- #endif
- #ifdef ZHLT_NETVIS
- else if (!strcasecmp(argv[i], "-server"))
- {
- g_vismode = VIS_MODE_SERVER;
- }
- else if (!strcasecmp(argv[i], "-connect"))
- {
- if (i < argc)
- {
- g_vismode = VIS_MODE_CLIENT;
- g_server_addr = argv[++i];
- }
- else
- {
- Usage();
- }
- }
- else if (!strcasecmp(argv[i], "-port"))
- {
- if (i < argc)
- {
- g_port = atoi(argv[++i]);
- }
- else
- {
- Usage();
- }
- }
- else if (!strcasecmp(argv[i], "-rate"))
- {
- if (i < argc)
- {
- g_rate = atoi(argv[++i]);
- }
- else
- {
- Usage();
- }
- if (g_rate < 5)
- {
- Log("Minimum -rate is 5, setting to 5 seconds\n");
- g_rate = 5;
- }
- if (g_rate > 900)
- {
- Log("Maximum -rate is 900, setting to 900 seconds\n");
- g_rate = 900;
- }
- }
- #endif
- #ifndef ZHLT_NETVIS
- else if (!strcasecmp(argv[i], "-fast"))
- {
- Log("g_fastvis = true\n");
- g_fastvis = true;
- }
- #endif
- else if (!strcasecmp(argv[i], "-full"))
- {
- g_fullvis = true;
- }
- else if (!strcasecmp(argv[i], "-dev"))
- {
- if (i < argc)
- {
- g_developer = (developer_level_t)atoi(argv[++i]);
- }
- else
- {
- Usage();
- }
- }
- else if (!strcasecmp(argv[i], "-verbose"))
- {
- g_verbose = true;
- }
- else if (!strcasecmp(argv[i], "-noinfo"))
- {
- g_info = false;
- }
- else if (!strcasecmp(argv[i], "-chart"))
- {
- g_chart = true;
- }
- else if (!strcasecmp(argv[i], "-low"))
- {
- g_threadpriority = eThreadPriorityLow;
- }
- else if (!strcasecmp(argv[i], "-high"))
- {
- g_threadpriority = eThreadPriorityHigh;
- }
- else if (!strcasecmp(argv[i], "-nolog"))
- {
- g_log = false;
- }
- else if (!strcasecmp(argv[i], "-texdata"))
- {
- if (i < argc)
- {
- int x = atoi(argv[++i]) * 1024;
- if (x > g_max_map_miptex)
- {
- g_max_map_miptex = x;
- }
- }
- else
- {
- Usage();
- }
- }
- else if (!strcasecmp(argv[i], "-lightdata"))
- {
- if (i < argc)
- {
- int x = atoi(argv[++i]) * 1024;
- if (x > g_max_map_miptex)
- {
- g_max_map_miptex = x;
- }
- }
- else
- {
- Usage();
- }
- }
- #ifdef ZHLT_PROGRESSFILE // AJM
- else if (!strcasecmp(argv[i], "-progressfile"))
- {
- if (i < argc)
- {
- g_progressfile = argv[++i];
- }
- else
- {
- Log("Error: -progressfile: expected path to progress file following parameter\n");
- Usage();
- }
- }
- #endif
-
- #ifdef HLVIS_MAXDIST
- // AJM: MVD
- else if(!strcasecmp(argv[i], "-maxdistance"))
- {
- if(i < argc)
- {
- g_maxdistance = abs(atoi(argv[++i]));
- }
- else
- {
- Usage();
- }
- }
- /* else if(!strcasecmp(argv[i], "-postcompile"))
- {
- g_postcompile = true;
- }*/
- #endif
- // SILENCER -->
- else if (!strcasecmp(argv[i], "-noconcolors"))
- {
- g_noConColors = true;
- }
- // <-- SILENCER
- else if (argv[i][0] == '-')
- {
- Log("Unknown option \"%s\"", argv[i]);
- Usage();
- }
- else if (!mapname_from_arg)
- {
- mapname_from_arg = argv[i];
- }
- else
- {
- Log("Unknown option \"%s\"\n", argv[i]);
- Usage();
- }
- }
- #ifdef ZHLT_NETVIS
- threads_InitCrit();
- if (g_vismode == VIS_MODE_CLIENT)
- {
- ConnectToServer(g_server_addr, g_port);
- while (!isConnectedToServer())
- {
- NetvisSleep(100);
- }
- Send_VIS_LOGIN();
- while (!g_clientid)
- {
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(4)\n");
- }
- NetvisSleep(100);
- }
- mapname_from_arg = "proxy";
- }
- else if (g_vismode == VIS_MODE_SERVER)
- {
- StartNetvisSocketServer(g_port);
- if (!mapname_from_arg)
- {
- Log("No mapfile specified\n");
- Usage();
- }
- }
- else
- {
- Log("Netvis must be run either as a server (-server)\n" "or as a client (-connect servername)\n\n");
- Usage();
- }
- #else
- if (!mapname_from_arg)
- {
- Log("No mapfile specified\n");
- Usage();
- }
- #endif
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_CLIENT)
- {
- g_log = false;
- }
- #endif
- safe_strncpy(g_Mapname, mapname_from_arg, _MAX_PATH);
- FlipSlashes(g_Mapname);
- StripExtension(g_Mapname);
- OpenLog(g_clientid);
- atexit(CloseLog);
- ThreadSetDefault();
- ThreadSetPriority(g_threadpriority);
- LogStart(argc, argv);
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_CLIENT)
- {
- Log("ZHLT NETVIS Client #%d\n", g_clientid);
- g_log = false;
- }
- else
- {
- Log("ZHLT NETVIS Server\n");
- }
- #endif
- CheckForErrorLog();
- dtexdata_init();
- atexit(dtexdata_free);
- // END INIT
- // BEGIN VIS
- start = I_FloatTime();
- safe_strncpy(source, g_Mapname, _MAX_PATH);
- safe_strncat(source, ".bsp", _MAX_PATH);
- safe_strncpy(portalfile, g_Mapname, _MAX_PATH);
- safe_strncat(portalfile, ".prt", _MAX_PATH);
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_SERVER)
- {
- LoadBSPFile(source);
- LoadPortalsByFilename(portalfile);
- char* bsp_image;
- char* prt_image;
- g_bsp_size = LoadFile(source, &bsp_image);
- g_prt_size = LoadFile(portalfile, &prt_image);
- g_bsp_compressed_size = (g_bsp_size * 1.01) + 12; // Magic numbers come from zlib documentation
- g_prt_compressed_size = (g_prt_size * 1.01) + 12; // Magic numbers come from zlib documentation
- char* bsp_compressed_image = (char*)malloc(g_bsp_compressed_size);
- char* prt_compressed_image = (char*)malloc(g_prt_compressed_size);
- int rval;
- rval = compress2((byte*)bsp_compressed_image, &g_bsp_compressed_size, (byte*)bsp_image, g_bsp_size, 5);
- if (rval != Z_OK)
- {
- Error("zlib Compression error with bsp image\n");
- }
- rval = compress2((byte*)prt_compressed_image, &g_prt_compressed_size, (byte*)prt_image, g_prt_size, 7);
- if (rval != Z_OK)
- {
- Error("zlib Compression error with prt image\n");
- }
- free(bsp_image);
- free(prt_image);
- g_bsp_image = bsp_compressed_image;
- g_prt_image = prt_compressed_image;
- }
- else if (g_vismode == VIS_MODE_CLIENT)
- {
- Send_VIS_WANT_BSP_DATA();
- while (!g_bsp_downloaded)
- {
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(5)\n");
- }
- NetvisSleep(100);
- }
- Send_VIS_WANT_PRT_DATA();
- while (!g_prt_downloaded)
- {
- if (!isConnectedToServer())
- {
- Error("Unexepected disconnect from server(6)\n");
- }
- NetvisSleep(100);
- }
- LoadPortals(g_prt_image);
- free(g_prt_image);
- }
- #else // NOT ZHLT_NETVIS
- LoadBSPFile(source);
- ParseEntities();
- LoadPortalsByFilename(portalfile);
- # if ZHLT_ZONES
- g_Zones = MakeZones();
- AssignPortalsToZones();
- # endif
- #endif
- Settings();
- g_uncompressed = (byte*)calloc(g_portalleafs, g_bitbytes);
- CalcVis();
- #ifdef ZHLT_NETVIS
- if (g_vismode == VIS_MODE_SERVER)
- {
- g_visdatasize = vismap_p - g_dvisdata;
- Log("g_visdatasize: %i compressed from %i\n", g_visdatasize, originalvismapsize);
- if (g_chart)
- {
- PrintBSPFileSizes();
- }
- WriteBSPFile(source);
- end = I_FloatTime();
- LogTimeElapsed(end - start);
- free(g_uncompressed);
- // END VIS
- #ifndef SYSTEM_WIN32
- // Talk about cheese . . .
- StopNetvisSocketServer();
- #endif
- }
- else if (g_vismode == VIS_MODE_CLIENT)
- {
- #ifndef SYSTEM_WIN32
- // Dont ask . .
- DisconnectFromServer();
- #endif
- end = I_FloatTime();
- LogTimeElapsed(end - start);
- free(g_uncompressed);
- // END VIS
- }
- threads_UninitCrit();
- #else // NOT ZHLT_NETVIS
- g_visdatasize = vismap_p - g_dvisdata;
- Log("g_visdatasize: %i compressed from %i\n", g_visdatasize, originalvismapsize);
- if (g_chart)
- {
- PrintBSPFileSizes();
- }
- WriteBSPFile(source);
- end = I_FloatTime();
- LogTimeElapsed(end - start);
- free(g_uncompressed);
- // END VIS
- #endif // ZHLT_NETVIS
- return 0;
- }
|