Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //========================================================================
- //
- // DOOMBRINGER : CSQC code
- //
- // Copyright (C) 2015 - 2018 Kristian Kaell
- // Copyright (C) 2015 Andrew Apted
- //
- // This code is free software; you can redistribute it and/or modify
- // it under the terms of the GNU GPL (General Public License); either
- // version 2 of the License, or (at your option) any later version.
- //
- //========================================================================
- /* Player entity handling */
- float torso_first_bone = 5;
- float torso_last_bone = 73;
- float last_time;
- float current_viewzoom;
- float view_quality = 1;
- .float torso_frame;
- .float torso_lerpfrac;
- .float torso_frame1time;
- .float torso_frame2;
- .float torso_frame2time;
- .float back_bone;
- .float back_bone2;
- .float fx_time;
- .float fx_flags;
- .vector fx_dest;
- .float fx_owner; // numeric value of engine-drawn ent, can only use with getentity()
- .float fx_enemy;
- .float team;
- .float deadflag;
- .float fx_along;
- float autocvar_cl_playeravatar;
- string autocvar_cl_playercountry;
- float autocvar_cl_forcemodels;
- float autocvar_cl_forceskins;
- float autocvar_cl_forcecolors;
- float autocvar_cl_forceglow;
- float autocvar_cl_forcecorpsecolor;
- float autocvar_snd_clientsndvol;
- float autocvar_snd_warningsndvol;
- float autocvar_cl_playerdetailreduction;
- float surface;
- string autocvar_enemymodel;
- float autocvar_enemyskin;
- float autocvar_enemycolor;
- float autocvar_corpsecolor;
- string autocvar_enemyglow;
- string autocvar_glow;
- float localteam;
- float teamcolor;
- float teamskin;
- vector view_origin, view_forward, view_right, view_up;
- // detect changes in 'enemymodel' cvar, load new model on changes
- string enemymodel;
- string teammodel;
- string zs_enemymodel_latch;
- string zs_defaultmodel_latch;
- void CheckClientCountry(float force_send);
- void CheckClientAvatar(float force_send);
- void startanim(entity e, float f)
- {
- e.frame2 = e.frame;
- e.frame2time = e.frame1time;
- e.lerpfrac = 1.0;
- e.frame = f;
- //e.frame1time = time;
- }
- float fexists(string f)
- {
- float fh = fopen(f, FILE_READ);
- if (fh < 0) return false;
- fclose(fh);
- return true;
- }
- // FEATURE: LOD from Xonotic
- .float lodmodelindex0;
- .float lodmodelindex1;
- .float lodmodelindex2;
- void CSQCPlayer_LOD_Apply(entity this)
- {
- // LOD model loading
- if(this.lodmodelindex0 != this.modelindex)
- {
- string modelname = this.model;
- string s;
- vector mi = this.mins;
- vector ma = this.maxs;
- // set modelindex
- this.lodmodelindex0 = this.modelindex;
- this.lodmodelindex1 = this.modelindex;
- this.lodmodelindex2 = this.modelindex;
- // FIXME: this only supports 3-letter extensions
- s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod1", substring(modelname, -4, 4));
- if(fexists(s))
- {
- precache_model(s);
- setmodel(this, s);
- if(this.modelindex)
- this.lodmodelindex1 = this.modelindex;
- }
- s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod2", substring(modelname, -4, 4));
- if(fexists(s))
- {
- precache_model(s);
- setmodel(this, s);
- if(this.modelindex)
- this.lodmodelindex2 = this.modelindex;
- }
- setmodel(this, modelname); // make everything normal again
- setsize(this, mi, ma);
- }
- // apply LOD
- if(autocvar_cl_playerdetailreduction <= 0)
- {
- if(autocvar_cl_playerdetailreduction <= -2)
- this.modelindex = this.lodmodelindex2;
- else if(autocvar_cl_playerdetailreduction <= -1)
- this.modelindex = this.lodmodelindex1;
- else
- this.modelindex = this.lodmodelindex0;
- }
- else
- {
- float distance = vlen(this.origin - view_origin);
- float f = (distance * current_viewzoom + 100.0) * autocvar_cl_playerdetailreduction;
- f *= 1.0 / bound(0.01, view_quality, 1);
- if(f > 2048)
- this.modelindex = this.lodmodelindex2;
- else if(f > 768)
- this.modelindex = this.lodmodelindex1;
- else
- this.modelindex = this.lodmodelindex0;
- }
- }
- void PL_TorsoAnim(entity e, float animframe)
- {
- e.torso_frame2 = e.torso_frame;
- e.torso_frame2time = e.torso_frame1time;
- e.torso_lerpfrac = 1.0;
- e.torso_frame = animframe;
- e.torso_frame1time = time;
- }
- void PL_UpdateWeapon(entity player)
- {
- if(player == world)
- return;
- /* if(!player)
- remove(player.pl_extweap);
- if not(player.pl_extweap)
- return;*/
- local string name = "";
- local string hand = "Weapon_R";
- float tag_found;
- //print(va("DEBUG: Tag found1: %d\n", player.weapon));
- if (player.deadflag > 0 || player.weapon == WEAPON_FISTS)
- {
- // nothing if spectating or dead
- setmodel(player.pl_extweap, "null");
- PL_TorsoAnim (player, PLANIM_hold_fists);
- return;
- }
- //print(va("DEBUG: Tag found2: %d\n", player.weapon));
- switch (player.weapon)
- {
- case WEAPON_BOLTER:
- name = "models/weapons/w_bolter.obj";
- hand = "Weapon_R";
- PL_TorsoAnim (player, PLANIM_hold_bolter);
- break;
- case WEAPON_SHOTGUN:
- name = "models/weapons/w_sg.obj";
- hand = "Weapon_R";
- PL_TorsoAnim (player, PLANIM_hold_shotgun);
- break;
- case WEAPON_SUPER_SHOTGUN:
- name = "models/weapons/w_ssg.obj";
- hand = "Weapon_R";
- PL_TorsoAnim (player, PLANIM_hold_superbolter);
- break;
- case WEAPON_GOTLUNG:
- name = "models/weapons/w_gotlung.obj";
- hand = "Weapon_L";
- PL_TorsoAnim (player, PLANIM_hold_chaingun);
- break;
- case WEAPON_GRENADE:
- name = "models/weapons/v_gl.obj";
- hand = "Weapon_L";
- PL_TorsoAnim (player, PLANIM_hold_grenade);
- break;
- case WEAPON_ROCKET:
- name = "models/weapons/v_rl.obj";
- hand = "Weapon_L";
- PL_TorsoAnim (player, PLANIM_hold_rocket);
- break;
- case WEAPON_TESLA:
- name = "models/weapons/v_shaft.obj";
- hand = "Weapon_R";
- PL_TorsoAnim (player, PLANIM_hold_tesla);
- break;
- case WEAPON_RAILGUN:
- name = "models/weapons/v_rail.obj";
- hand = "Weapon_R";
- PL_TorsoAnim (player, PLANIM_hold_railgun);
- break;
- }
- eprint(player);
- // print(va("DEBUG: Tag found3: %d\n", player.weapon));
- //print ("DEBUG: Gettagindex: Weapon\n");
- if(player.skeletonindex)
- if((tag_found = gettagindex(player, hand)))
- {
- //print(va("DEBUG: Tag found4: %d\n", tag_found));
- //if (self.pl_extweap.weapon != name)
- //{
- //print(va("DEBUG: Tag found5: %d\n", tag_found));
- //self.pl_extweap.weapon = name;
- setmodel(player.pl_extweap, name);
- //print(va("DEBUG: WeapExt model: %s\n", self.pl_extweap.model));
- //print(va("DEBUG: WeapExt hand: %s\n", hand));
- setattachment(player.pl_extweap, player, hand);
- //eprint(player);
- //}
- }
- else
- setmodel(player.pl_extweap, "null");
- }
- void PL_WeaponModel_Create(entity player)
- {
- //if (player != world)
- if (!player.pl_extweap)
- {
- player.pl_extweap = spawn();
- print("DEBUG: Spawned Ext Weapon:\n");
- eprint(player);
- //PL_WeaponModel_Init(self.pl_extweap);
- player.pl_extweap.drawmask = MASK_ENGINE;
- player.pl_extweap.renderflags = 0;
- setorigin(player.pl_extweap, '0 0 0');
- setsize (player.pl_extweap, '0 0 0', '0 0 0');
- player.pl_extweap.angles = '0 0 0';
- player.pl_extweap.scale = 0.60;
- player.pl_extweap.model = "";
- setattachment(player.pl_extweap, player, "");//Weapon_R, Weapon_L
- // eprint(player);
- PL_UpdateWeapon(player);
- }
- }
- void player_predraw()
- {
- if ( self.fx_owner == player_localentnum )
- {
- self.movetype = MOVETYPE_WALK;
- // Prepare rollback
- vector vOldOrigin = self.origin;
- vector vOldVelocity = self.velocity;
- float fOldPMoveFlags = self.pmove_flags;
- local float i;
- // Apply physics for every single input-frame that has not yet been
- // acknowledged by the server (servercommandframe = last acknowledged frame)
- for ( i = servercommandframe + 1; i <= clientcommandframe; i++ ) {
- getinputstate( i );
- // runstandardplayerphysics( self );
- }
- vPlayerVelocity = self.velocity;
- //addentity( self );
- print("Walking round?\n");
- // Time to roll back
- self.origin = vOldOrigin;
- setorigin( self, self.origin );
- self.velocity = vOldVelocity;
- self.pmove_flags = fOldPMoveFlags;
- self.movetype = MOVETYPE_NONE;
- }
- self.lerpfrac = max(0, self.lerpfrac - frametime * 10);
- self.torso_lerpfrac = max(0, self.torso_lerpfrac - frametime * 10);
- local float frametime = time - last_time;
- last_time = time;
- if (intermission == 1)
- return;
- /*if (frametime <= 0)
- return;*/
- if (frametime > 0.1)
- frametime = 0.1;
- //self.drawmask = 0;
- if (! self.modelindex)
- return;
- // never draw for the same client (unless chase-cam is on)
- if (self.fx_owner == player_localentnum && !chase_active)
- {
- self.renderflags = RF_EXTERNALMODEL;
- self.pl_extweap.renderflags = RF_EXTERNALMODEL;//FIXME : Make pl_extmodel show in mirrors.
- }
- else
- {
- self.renderflags = 0;
- self.pl_extweap.renderflags = 0;
- }
- self.drawmask = MASK_ENGINE;
- //self.origin = getentityvec(self.fx_owner, E_ORIGINANDVECTORS);
- local float old_yangles = self.angles_y;
- self.angles = csqc_calcangles(v_forward, v_up);
- //Save the forward looking direction.
- local float torso_aim = self.angles_y;
- //Limit how far up or down the spine will bend
- if (self.angles_x > 70)
- self.angles_x = 70;
- else if (self.angles_x < -70)
- self.angles_x = -70;
- //print(va("%f\n", self.w_direction));
- //print(va("angles %f\n", old_yangles));
- if (self.w_direction < 130 && self.w_direction > 50)
- self.w_direction = (self.w_direction - 50)/2;
- else if (self.w_direction > -130 && self.w_direction < -50)
- self.w_direction = (self.w_direction + 50)/2;
- else if (self.w_direction <= -120)
- self.w_direction = self.w_direction + 180;
- else if (self.w_direction >= 120)
- self.w_direction = self.w_direction - 180;
- else if (self.w_direction > 50 || self.w_direction < -50)
- self.w_direction = self.w_olddir;
- self.w_olddir = self.w_direction;
- self.angles_y = self.angles_y + self.w_direction;
- /*if (old_yangles > self.angles_y+100 || old_yangles < self.angles_y - 100)
- {
- print(va("w_angles%f\n", self.angles_y));
- print(va("old_angles %f\n", old_yangles));
- }*/
- //Blend rotation of the legs.
- if ((old_yangles > 270 && self.angles_y < 90) || (old_yangles < 90 && self.angles_y > 270))
- {
- if (old_yangles > self.angles_y) //Old angle is over 270 degrees
- {
- self.angles_y = self.angles_y - ((old_yangles-360-self.angles_y)/10);
- //print("Turn Right\n");
- }
- else if (old_yangles < self.angles_y) //New angle is over 270
- {
- self.angles_y = self.angles_y - ((old_yangles+360-self.angles_y)/10);
- //print("Turn Left\n");
- }
- }
- else
- self.angles_y = old_yangles - ((old_yangles-self.angles_y)/10);
- // build skeleteon for leg animation
- skel_build(self.skeletonindex, self, self.modelindex, 0 /* retain */,
- 1, 999 /* <-- all bones */ );
- // if not mixing LEG + TORSO animations, do nothing else here
- if (self.torso_frame < 0)
- return;
- /* MIX IT */
- local float save_frame = self.frame;
- local float save_time = self.frame1time;
- local float save_lerpfrac = self.lerpfrac;
- local float save_frame2 = self.frame2;
- local float save_time2 = self.frame2time;
- self.frame = self.torso_frame;
- self.frame1time = self.torso_frame1time;
- self.lerpfrac = self.torso_lerpfrac;
- self.frame2 = self.torso_frame2;
- self.frame2time = self.torso_frame2time;
- skel_build(self.skeletonindex, self, self.modelindex, 0 /* retain */,
- torso_first_bone, torso_last_bone /* <-- torso bones */ );
- self.frame = save_frame;
- self.frame1time = save_time;
- self.lerpfrac = save_lerpfrac;
- self.frame2 = save_frame2;
- self.frame2time = save_time2;
- //print(va("frame %d %d %d %d lerpfrac * %d %d %f\n", self.frame, self.frame2, self.frame3, self.frame4, self.lerpfrac, self.lerpfrac3, self.lerpfrac4));
- local float back = skel_find_bone(self.skeletonindex, "Bip002 Spine1");
- local vector org = skel_get_bonerel(self.skeletonindex, back);
- local vector ang = skel_get_boneabs(self.skeletonindex, back);
- ang_y = ang_y-self.angles_x-5;
- ang_x = 0;
- //Make the torso always face forward.
- torso_aim = torso_aim - self.angles_y;
- ang_z = ang_z+torso_aim;
- makevectors(ang);
- skel_set_bone(self.skeletonindex, back, org);
- /*
- back = skel_find_bone(self.skeletonindex, "Bip002 Spine2");
- org = skel_get_bonerel(self.skeletonindex, back);
- ang = skel_get_boneabs(self.skeletonindex, back);
- ang_y = ang_y-(self.angles_x/2)-5;
- makevectors(ang);
- skel_set_bone(self.skeletonindex, back, org);*/
- //print(va("%f\n", self.angles_x));
- //print(va("%f\n", org_y));
- //print(va("%f\n", self.angles_z));
- self.angles_x = 0;
- //self.angles_z = self.w_direction;
- // Pred_PlayerUpdated(self);
- PL_WeaponModel_Create(self);
- }
- void Ent_Remove_Player()
- {
- print("DEBUG: Ent_Remove_Player\n");
- //Free Skeleton
- if (self.skeletonindex)
- {
- skel_delete(self.skeletonindex);
- self.skeletonindex = 0;
- //print("DEBUG: Skeleton freed\n");
- }
- remove(self.pl_extweap);
- remove(self);
- }
- void Ent_Update_Player(float is_new)
- {
- self.origin_x = ReadCoord();
- self.origin_y = ReadCoord();
- self.origin_z = ReadCoord();
- self.angles_x = ReadCoord();
- self.angles_y = ReadCoord();
- self.angles_z = ReadCoord();
- self.velocity_x = ReadShort();
- self.velocity_y = ReadShort();
- self.velocity_z = ReadShort();
- self.flags = ReadLong();
- setsize( self, '-16 -16 -24', '16 16 32' );
- setorigin( self, self.origin );
- //print(va("DEBUG: Ent_Update_Player: is_new %d\n", is_new));
- //print(va("frame %d %d %d %d lerpfrac * %d %d %f\n", self.frame, self.frame2, self.frame3, self.frame4, self.lerpfrac, self.lerpfrac3, self.lerpfrac4));
- /* this MUST mirror the SendEntity_Player in game code */
- local float sendflags = ReadShort();
- //print(va("DEBUG: Ent_Update_Player: sendflags %d\n", sendflags));
- if (sendflags & SENDFLAG_MODEL)
- {
- setmodel(self, ReadString());
- self.fx_owner = ReadEntity();
- self.team = ReadByte();
- //player = ftoe(self.fx_owner);
- //print(va ("DEBUG: Player model is %s\n", self.model));
- }
- if (sendflags & SENDFLAG_FRAME)
- {
- local float legs_frame = ReadByte();
- startanim(self, legs_frame);
- //Don't update frametime for running/walking animations
- if (legs_frame < PLANIM_turn || legs_frame > PLANIM_runB)
- self.frame1time = time;
- }
- if (sendflags & SENDFLAG_TORSO)
- {
- self.torso_frame2 = self.torso_frame;
- self.torso_frame2time = self.torso_frame1time;
- self.torso_lerpfrac = 1.0;
- self.torso_frame = ReadByte();
- self.torso_frame1time = time;
- //print(va("DEBUG: Anim found: %d\n", self.torso_frame));
- }
- if (sendflags & SENDFLAG_SKIN)
- {
- self.skin = ReadByte();
- }
- if (sendflags & SENDFLAG_COLORS)
- {
- self.colormap = ReadByte() | 1024;
- //print(va ("DEBUG: Player color is %d\n", self.colormap));
- }
- if (sendflags & SENDFLAG_WEAPON)
- {
- self.weapon = ReadByte();
- PL_UpdateWeapon(self);
- //print(va("DEBUG: Weapon found: %d\n", self.weapon));
- //eprint(self);
- }
- if (sendflags & SENDFLAG_DEAD)
- {
- self.deadflag = ReadByte();
- //print(va ("DEBUG: Player is dead? %d\n", self.deadflag));
- local entity plweap = self.pl_extweap;
- remove(plweap);
- }
- if (sendflags & SENDFLAG_ANGLES)
- {
- self.w_direction = ReadShort();
- //print(va ("DEBUG: Player is dead? %d\n", self.deadflag));
- }
- if (is_new || !self.skeletonindex)
- {
- // print(va("DEBUG: Ent_Update_Player: is_new_skeleton %d\n", is_new));
- self.classname = "player";
- self.solid = SOLID_SLIDEBOX;
- self.predraw = player_predraw;
- self.skeletonindex = skel_create(self.modelindex);
- self.drawmask = MASK_ENGINE;
- /*if (self.skeletonindex)
- self.back_bone = skel_find_bone(self.skeletonindex, "Bip002 Spine");*/
- }
- //is it a spectator? Then no need to do any of this.
- if (self.team == 0)
- return;
- //Check what is yours
- if (self.fx_owner == player_localentnum)
- {
- localteam = self.team;
- teamskin = self.skin;
- teamcolor = self.colormap;
- teammodel = self.model;
- if(self.deadflag == 1)
- localcmd("set _togglezoom +; -button4\n"); //Make sure you don't respawn with togglezoom on.
- }
- surface = cvar("r_showsurfaces");
- /*if(!self.modelindex)
- {
- setmodel(self, zs_defaultmodel_latch);
- }*/
- if (((self.fx_owner != player_localentnum && self.team != localteam) || ((zs_gametype == "ffa" || zs_gametype == "duel") && self.fx_owner != player_localentnum)) && self.team != 0)
- {
- if ((autocvar_cl_forcemodels) && ((self.model != "models/gore/gib_db_head.obj" && self.deadflag > 0) || (self.deadflag == 0)))
- {
- enemymodel = strcat("models/player/", autocvar_enemymodel, "/player.iqm");
- setmodel(self, enemymodel);
- self.skeletonindex = skel_create(self.modelindex);
- }
- else if (self.model == "models/gore/gib_db_head.obj" && self.deadflag > 0)
- return;
- if (autocvar_cl_forceskins && surface == 0)
- self.skin = autocvar_enemyskin;
- else if(!autocvar_cl_forceskins && zs_gametype == "tdm")
- self.skin = 2;
- if(self.deadflag > 0 && autocvar_cl_forcecorpsecolor)
- {
- self.colormap = autocvar_corpsecolor;
- self.skin = 2;
- self.glowmod = '0 0 0';
- return;
- }
- else
- {
- if (autocvar_cl_forcecolors)
- self.colormap = autocvar_enemycolor;
- else if(!autocvar_cl_forcecolors && self.team == 1 && zs_gametype == "tdm")
- self.colormap = 198;
- else if(!autocvar_cl_forcecolors && self.team == 2 && zs_gametype == "tdm")
- self.colormap = 35;
- if (autocvar_cl_forceglow)
- self.glowmod = stov(autocvar_enemyglow);
- else
- self.glowmod = '1 1 1';
- }
- }
- // Friends adjustments
- if ((self.fx_owner != player_localentnum && self.team == localteam && zs_gametype != "ffa" && zs_gametype != "duel" && self.team != 0))
- {
- if ((autocvar_cl_forcemodels) && ((self.model != "models/gore/gib_db_head.obj" && self.deadflag > 0) || (self.deadflag == 0)))
- setmodel(self, teammodel);
- else if (self.model == "models/gore/gib_db_head.obj" && self.deadflag > 0)
- return;
- if (autocvar_cl_forceskins && surface == 0)
- self.skin = teamskin;
- else if(autocvar_cl_forceskins && surface > 0)
- self.skin = 6;
- else if(!autocvar_cl_forceskins && zs_gametype == "tdm")
- self.skin = 2;
- if(self.deadflag > 0 && autocvar_cl_forcecorpsecolor)
- {
- self.colormap = autocvar_corpsecolor;
- self.glowmod = '0 0 0';
- return;
- }
- else
- {
- if (autocvar_cl_forcecolors)
- self.colormap = teamcolor;
- else if(!autocvar_cl_forcecolors && self.team == 1 && zs_gametype == "tdm")
- self.colormap = 198;
- else if(!autocvar_cl_forcecolors && self.team == 2 && zs_gametype == "tdm")
- self.colormap = 35;
- if (autocvar_cl_forceglow)
- self.glowmod = stov(autocvar_glow);
- else
- self.glowmod = '1 1 1';
- }
- //print(va("teammember got skin %d\n", self.skin));
- }
- }
- //------------------------------------------------------------------------
- void TempEnt_WClientSound()
- {
- local float kind = ReadByte();
- // print(va("DEBUG: Extra: %f\n", extra));
- local vector org = ReadVector();
- local string snd_name = "";
- local float attn = ATTN_NORM;
- switch (kind)
- {
- case 1:
- snd_name = "";
- default:
- print(va("^1Unknown client sound #%d\n", kind));
- return;
- }
- pointsound(org, snd_name, autocvar_snd_clientsndvol, attn);
- }
- void TempEnt_ClientSound()
- {
- local float kind = ReadByte();
- local float extra = ReadByte();
- local float extra2 = ReadByte();
- // print(va("DEBUG: Extra: %f\n", extra));
- local float ent_num = ReadEntity();
- local vector org = ReadVector();
- local string snd_name = "";
- local float attn = ATTN_NORM;
- switch (kind)
- {
- case CSOUND_FOOTSTEP:
- {
- local float r = rand_int(6);
- local string mat = "normal";
- /* if (extra == )
- else*/ if (ent_num != player_localentnum && extra2 != localteam)
- {
- mat = "metal";
- snd_name = va("sound/player/default/step_%s_%d.wav", mat, r);
- pointsound(org, snd_name, autocvar_snd_clientsndvol, attn);
- }
- else if (ent_num != player_localentnum && extra2 == localteam)
- {
- snd_name = va("sound/player/default/step_%s_%d.wav", mat, r);
- pointsound(org, snd_name, autocvar_snd_clientsndvol, attn);
- }
- else
- {
- snd_name = va("sound/player/default/step_%s_%d.wav", mat, r);
- sound(world,CHAN_AUTO, snd_name, autocvar_snd_clientsndvol, ATTN_NONE);
- }
- return;
- }
- case CSOUND_AMWARNING:
- {
- if (ent_num != player_localentnum)
- return;
- snd_name = va("sound/hud/ammo_warning.wav");
- sound(world, CHAN_AUTO, snd_name, autocvar_snd_warningsndvol, ATTN_NONE);
- return;
- }
- default:
- print(va("^1Unknown client sound #%d\n", kind));
- return;
- }
- }
- //--- editor settings ---
- // vi:ts=4:sw=4:expandtab
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement