]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/csqcmodel_hooks.qc
fixes for LOD code
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / csqcmodel_hooks.qc
1 .vector glowmod;
2 .float lodmodelindex0;
3 .float lodmodelindex1;
4 .float lodmodelindex2;
5
6 .entity tag_entity;
7 .float tag_index;
8
9 void CSQCModel_Hook_PreDraw(float isplayer)
10 {
11         // auto glowmod from colormap
12         if(isplayer)
13         {
14                 if(self.colormap > 0)
15                         self.glowmod = colormapPaletteColor(((self.colormap >= 1024) ? (self.colormap & 0xFF) : stof(getplayerkeyvalue(self.colormap - 1, "colors"))), TRUE) * 2;
16                 else
17                         self.glowmod = '1 1 1';
18         }
19
20         if(self.modelindex != 0 && isplayer && self.model != "null")
21         {
22                 // LOD
23                 if(self.lodmodelindex0 != self.modelindex)
24                 {
25                         string modelname = self.model;
26                         string s;
27
28                         // set modelindex
29                         self.lodmodelindex0 = self.modelindex;
30                         self.lodmodelindex1 = self.modelindex;
31                         self.lodmodelindex2 = self.modelindex;
32
33                         // FIXME: this only supports 3-letter extensions
34                         s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod1", substring(modelname, -4, 4));
35                         if(fexists(s))
36                         {
37                                 precache_model(s);
38                                 setmodel(self, s);
39                                 if(self.modelindex)
40                                         self.lodmodelindex1 = self.modelindex;
41                         }
42
43                         s = strcat(substring(modelname, 0, strlen(modelname)-4), "_lod2", substring(modelname, -4, 4));
44                         if(fexists(s))
45                         {
46                                 precache_model(s);
47                                 setmodel(self, s);
48                                 if(self.modelindex)
49                                         self.lodmodelindex2 = self.modelindex;
50                         }
51
52                         setmodel(self, modelname); // make everything normal again
53                 }
54
55                 if(autocvar_cl_playerdetailreduction <= 0)
56                 {
57                         if(autocvar_cl_playerdetailreduction <= -2)
58                                 self.modelindex = self.lodmodelindex2;
59                         else if(autocvar_cl_playerdetailreduction <= -1)
60                                 self.modelindex = self.lodmodelindex1;
61                         else
62                                 self.modelindex = self.lodmodelindex0;
63                 }
64                 else
65                 {
66                         float distance = vlen(self.origin - other.origin);
67                         float f = (distance + 100.0) * autocvar_cl_playerdetailreduction;
68                         f *= 1.0 / bound(0.01, view_quality, 1);
69                         if(f > autocvar_cl_loddistance2)
70                                 self.modelindex = self.lodmodelindex2;
71                         else if(f > autocvar_cl_loddistance1)
72                                 self.modelindex = self.lodmodelindex1;
73                         else
74                                 self.modelindex = self.lodmodelindex0;
75                 }
76         }
77
78         if(!isplayer)
79         {
80                 if(self.tag_entity && wasfreed(self.tag_entity))
81                         self.tag_entity = world;
82
83                 if(self.tag_networkentity)
84                 {
85                         // we are ATTACHED!
86                         if(self.tag_entity.entnum == self.tag_networkentity)
87                         {
88                                 // already good
89                                 self.drawmask = MASK_NORMAL;
90                         }
91                         else
92                         {
93                                 // to something NEW NEW NEW NEW!
94                                 self.tag_entity = findfloat(world, entnum, self.tag_networkentity);
95                                 if(self.tag_entity)
96                                 {
97                                         // the best part is: IT EXISTS
98                                         self.drawmask = MASK_NORMAL;
99
100                                         if(substring(self.model, 0, 17) == "models/weapons/v_")
101                                                 if(substring(self.tag_entity.model, 0, 17) == "models/weapons/h_")
102                                                 {
103                                                         self.tag_index = gettagindex(self.tag_entity, "weapon");
104                                                         if(!self.tag_index)
105                                                                 self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
106                                                         if(!self.tag_index)
107                                                         {
108                                                                 // we need to prevent this from 'appening
109                                                                 self.tag_entity = world;
110                                                                 self.drawmask = 0;
111                                                                 dprint("h_ model lacks weapon attachment, but v_ model is attached to it\n");
112                                                         }
113                                                 }
114
115                                         if(substring(self.model, 0, 17) == "models/weapons/v_")
116                                                 if(substring(self.tag_entity.model, 0, 14) == "models/player/")
117                                                 {
118                                                         self.tag_index = gettagindex(self.tag_entity, "tag_weapon");
119                                                         if(!self.tag_index)
120                                                                 self.tag_index = gettagindex(self.tag_entity, "bip01 r hand");
121                                                 }
122
123                                         if(substring(self.tag_entity.model, 0, 17) == "models/weapons/v_")
124                                         {
125                                                 self.tag_index = gettagindex(self.tag_entity, "shot");
126                                                 if(!self.tag_index)
127                                                         self.tag_index = gettagindex(self.tag_entity, "tag_shot");
128                                         }
129                                 }
130                                 else
131                                 {
132                                         // damn, see you next frame
133                                         self.drawmask = 0;
134                                 }
135                         }
136                 }
137                 else
138                 {
139                         // no brain no pain
140                         self.drawmask = MASK_NORMAL;
141                 }
142         }
143 }