Guest User

component_ai

a guest
Sep 22nd, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 36.83 KB | None | 0 0
  1. import bge,ast,math, time,bpy
  2. from mathutils import Vector
  3. from mathutils import Matrix
  4. cont = bge.logic.getCurrentController()
  5. own = cont.owner
  6. def stick():
  7.    
  8.     scene = bge.logic.getSceneList()['Scene']
  9.     for obj in scene.objects:
  10.         if 'Stick' in obj:
  11.             obj.localLinearVelocity.x=0
  12.             obj.localLinearVelocity.y=0
  13.             obj.localLinearVelocity.z=0
  14.            
  15.            
  16. def navigate(data):
  17.     self = data[0]
  18.     self = own.childrenRecursive[self]
  19.     topSpeed = data[1]
  20.     if own['Target']=="None" and 'Player' not in own:
  21.        
  22.         own['X']*=.999
  23.         own['Y']*=.999
  24.          
  25.        
  26.     if 'nav_Point' in own and own['Impacted']==0:
  27.         #print('engaged')
  28.            
  29.         v2 = own.getVectTo(own['nav_Point'])
  30.         if 'Reverse' not in own:
  31.             own.worldPosition = own.worldPosition + (v2[1]*(topSpeed*.125))
  32.         else:
  33.             own.worldPosition = own.worldPosition + (v2[1]*(topSpeed*-.06125))
  34.         target = None    
  35.         if 'KDTree' in own.scene.objects['Plane']:
  36.             kd = own.scene.objects['Plane']['KDTree']
  37.             near = kd.find(own.worldPosition)
  38.             point = own.scene.objects['Plane']['KDList'][near[1]]
  39.            
  40.             z = point.z
  41.             #print(z-own.worldPosition.z)
  42.        
  43.             own.alignAxisToVect(own.getVectTo(own['nav_Point'])[1],1,.1)
  44.             own.alignAxisToVect([0,0,1],2,1)
  45.            
  46.             own.worldPosition.z =  (( z+.45) + (own.worldPosition.z*15) )/16
  47.             if own['Target']!="None" and own['Target']!="None":
  48.                 target = own['Target']
  49.                 #print(target)
  50.                 if own.getDistanceTo(target[0])<4:
  51.                     target = target[0]
  52.                     own['nav_Point']= kd.find(own.scene.objects[target].worldPosition)[0]
  53.                    
  54.         own.worldLinearVelocity*=0
  55.         own.worldAngularVelocity*=0
  56.         own['Steer']=0
  57.         own['Speed']=0    
  58.         local = own.worldTransform.inverted() @ own['nav_Point']
  59.         local.z=0
  60.         #print(local.magnitude)
  61.         #bge.render.drawLine(own['nav_Point'],own['nav_Point']+Vector([0,0,1]),(0,0,1))
  62.        
  63.         if local.magnitude>2:
  64.             v1 = own.worldLinearVelocity.copy()
  65.             v1.z = 0
  66.             v1*=.125
  67.             end = own.worldPosition+(v1.normalized()*(v1.magnitude+.06125))
  68.             start=own.worldPosition
  69.             ray = own.rayCast(end,start,0,"",0,0,0)
  70.             #bge.render.drawLine(start,end,(1,0,0))
  71.             if not ray[0]:
  72.                 for wheel in own['Front_Wheels']:
  73.                     end = wheel.worldPosition+(v1*.05)
  74.                     start=wheel.worldPosition
  75.                     ray = own.rayCast(end,start,0,"",0,0,0)
  76.                     if ray[0]:
  77.                         break
  78.                
  79.             go = 1
  80.             if ray[0]:
  81.                 #bge.render.drawLine(start,end,(1,0,0))
  82.                 own['Speed']=-1
  83.                 go = 0
  84.                 own['Steer']+=.1
  85.                 own['Reverse']=15
  86.            
  87.             s=own['Steer']
  88.             if local.y>0:
  89.                 if 'Reverse' not in own:
  90.                     own['Forward']=15
  91.                    
  92.                     if own['Speed']<.15 and go==1:
  93.                         own['Speed']=.15
  94.                    
  95.                     if abs(local.x*1.5)-local.y<0:
  96.                         if local.magnitude>16:
  97.                             if own['Speed']<=1.9 and go==1:
  98.                                 own['Speed']+=.1
  99.                             else:
  100.                                 own['Speed']*=.99
  101.                         else:
  102.                             if own['Speed']<=.5 and go==1:
  103.                                 own['Speed']+=.05
  104.                             else:
  105.                                 own['Speed']*=.99
  106.                                        
  107.                                    
  108.                     if local.x>0 and own['Steer']<1.9:
  109.                         #own.applyTorque([0,0,-1.25],1)    
  110.                         own['Steer']-=.05
  111.                     if local.x<0 and own['Steer']>-1.9:
  112.                         own['Steer']+=.05
  113.                        
  114.                         #own.applyTorque([0,0,1.25],1)    
  115.                     if own.localLinearVelocity.x>topSpeed:
  116.                         own['Speed']*=.9    
  117.                 elif own['Reverse']>=1:
  118.                     own['Reverse']-=1
  119.                 else:
  120.                     del own['Reverse']            
  121.                
  122.                    
  123.                    
  124.                        
  125.                    
  126.                
  127.                  
  128.             else:
  129.                 if 'Forward' not in own:
  130.                     own['Reverse']=15
  131.                     own['Read']=str(local)
  132.                     if local.x>0 and own['Steer']<1.49:
  133.                        
  134.                         #own.applyTorque([0,0,-5.5],1)
  135.                         own['Steer']-=.05
  136.                     if local.x<0 and own['Steer']>-1.49:
  137.                         #own.applyTorque([0,0,5.5],1)
  138.                         own['Steer']+=.05
  139.                 elif own['Forward']>=1:
  140.                     own['Forward']-=1
  141.                 else:
  142.                     del own['Forward']
  143.                            
  144.                    
  145.            
  146.            
  147.                      
  148.                        
  149.         else:
  150.             own['Speed']*=.9
  151.             #print('rem')
  152.             if target==None:
  153.                 del own['nav_Point']            
  154.                
  155.                            
  156.                    
  157. def goal(data):
  158.     self = data[0]
  159.     self = own.childrenRecursive[self]
  160.     plan = data[1]
  161.     target = data[2]
  162.     if type(target) is str:
  163.         target= own.scene.objects[target]
  164.      
  165.     if own.getDistanceTo(target)<4 and  'Plan' in own:
  166.         del own['Plan']      
  167.     navmesh = own.scene.objects['Navmesh']
  168.     #print(target)
  169.     if plan == "Attack":
  170.         #print('think')
  171.         if target['Health']<=0:
  172.             if target.name!='Bob_3':
  173.                 own['Parts'][data[-1]][3] ='Bob_3'
  174.             #print('set next target')
  175.             path = navmesh.findPath(own.worldPosition,own.scene.objects['Bob_3'].worldPosition)
  176.             #print(path)
  177.            
  178.             #print('has plan')
  179.             own['nav_Point'] = path.pop(0)
  180.             self['Plan'] = path
  181.             own['Target']="None"
  182.            
  183.         if 'Plan' not in self:
  184.             if own.getDistanceTo(target)>4:
  185.                 #print(help(navmesh.findPath))
  186.                 path = navmesh.findPath(own.worldPosition,target.worldPosition)
  187.                 #print(path)
  188.                 self['Plan'] = path
  189.                 #print('has plan')
  190.             else:
  191.                 if 'KDTree' in own.scene.objects['Plane']:
  192.                     kd = own.scene.objects['Plane']['KDTree']
  193.                     own['nav_Point']= kd.find(target.worldPosition)[0]
  194.                 else:    
  195.                     own['nav_Point']=target.worldPosition
  196.                
  197.                 local = own.worldTransform.inverted() @ target.worldPosition
  198.                 if local.y>0:
  199.                     if local.x<0 and own['Steer']>-1.9:
  200.                        
  201.                         own['Steer']  -=.1
  202.                     if local.x>0 and own['Steer']<1.9:
  203.                        
  204.                         own['Steer']  +=.1    
  205.                 else:
  206.                     if local.x>0 and own['Steer']>-1.9:
  207.                        
  208.                         own['Steer']  -=.1
  209.                     if local.x<0 and own['Steer']<1.9:
  210.                        
  211.                         own['Steer']  +=.1    
  212.                            
  213.         if len(self['Plan'])>=1:
  214.             if 'nav_Point' not in own:
  215.                 if own.getDistanceTo(target)>4:
  216.                     own['nav_Point'] = self['Plan'].pop(0)
  217.                     print('set point')
  218.                 else:
  219.                    
  220.                     if 'KDTree' in own.scene.objects['Plane']:
  221.                         kd = own.scene.objects['Plane']['KDTree']
  222.                         own['nav_Point']= kd.find(target.worldPosition)[0]
  223.                     else:    
  224.                         own['nav_Point']=target.worldPosition    
  225.             if own.getDistanceTo(target)<2 and 'Steer' in own:
  226.                 #print('too close')
  227.                 own.worldLinearVelocity*=.9
  228.                 own.worldAngularVelocity*=.9
  229.                 if 'KDTree' in own.scene.objects['Plane']:
  230.                     kd = own.scene.objects['Plane']['KDTree']
  231.                     own['nav_Point']= kd.find(target.worldPosition)[0]
  232.                 else:    
  233.                     own['nav_Point']=target.worldPosition
  234.                              
  235.                 own['Speed']=-.25
  236.                 own['Reverse']=15
  237.                
  238.        
  239.                    
  240.         else:
  241.             if 'Plan' in own:
  242.                 if len(own['Plan'])>=2:
  243.                     if own.getDistanceTo(own['Plan'][0])<2 and 'nav_Point' in own:
  244.                         del own['nav_Point']
  245.             if target['Health']<=0:
  246.                 if target.name!='Bob_3':
  247.                     own['Parts'][data[-1]][3] ='Bob_3'
  248.                 print('set next target')
  249.                 path = navmesh.findPath(own.worldPosition,own.scene.objects['Bob_3'].worldPosition)
  250.                 print(path)
  251.                 self['Plan'] = path
  252.                 print('has plan')
  253.                 if 'nav_Point' in own:
  254.                     del own['nav_Point']    
  255.                 if own.getDistanceTo(target)<4 and 'nav_Point' in own:
  256.                     del own['nav_Point']
  257.                    
  258.         if 'Plan' in own and bge.logic.getRandomFloat()>.9:
  259.             del own['Plan']
  260.                
  261. def tracker(data):
  262.     self = data[0]
  263.     self = own.childrenRecursive[self]
  264.     if 'Aim' in own['Act']:
  265.         own['Aiming']=True
  266.     elif 'Aim' in own:
  267.         del own['Aiming']
  268.            
  269.     x = data[1]
  270.     if type(x) is str:
  271.         x = own[x]
  272.        
  273.     y = data[2]
  274.    
  275.     if type(y) is str:
  276.         y = own[y]
  277.    
  278.     if 'init_tracker_p' not in self:
  279.         self['init_tracker_p'] = self.parent.worldTransform.inverted() @ self.worldPosition
  280.         self['init_tracker_r'] = self.parent.worldOrientation.inverted() @ self.worldOrientation
  281.     else:
  282.         self.worldPosition = self.parent.worldTransform @ self['init_tracker_p']  
  283.         self.worldOrientation = self.parent.worldOrientation @ self['init_tracker_r']
  284.         self.applyRotation([y,0,x],1)
  285.     if own['Target']!=None and own['Target']!="None":    
  286.         if own['Target'][1]>=1:
  287.             target=own['Target'][0]
  288.             if type(target)==str:
  289.                 target = own.scene.objects[target]
  290.             if 'Armed' not in self:
  291.                 self['Armed']=[500, 0,0]
  292.                 print('armed')
  293.             d = self.getDistanceTo(target)
  294.             #v = abs((d*d)*.0225)
  295.             p = target.worldPosition.copy()  
  296.             ##p.z+=v
  297.            
  298.             local = self.worldTransform.inverted() @ p
  299.            
  300.            
  301.             if local.y>0:
  302.                 mod = local.y*.01
  303.                 own['Sens']=0
  304.                 if local.x<0:
  305.                     own['X']+=mod+.01
  306.                 elif local.x>0:
  307.                     own['X']-=mod+.01
  308.                
  309.                      
  310.                
  311.                            
  312.                        
  313.                 mod = local.y*.01
  314.            
  315.                 if local.z>0:
  316.                     own['Y']+=mod+.01
  317.                 elif local.z<0:
  318.                     own['Y']-=mod+.01
  319.                
  320.                    
  321.                            
  322.                      
  323.             else:
  324.                 own['X']*=.9
  325.                 own['Y']*=.9
  326.            
  327.            
  328.             p = own.worldPosition
  329.             p2 = p+(own.worldOrientation.col[1]*10)
  330.             #bge.render.drawLine(p,p2,(0,0,1))
  331.             self.alignAxisToVect(self.getVectTo(target)[1],1,.25)
  332.     else:
  333.         if 'Armed' in self:
  334.             if self['Armed'][0]>=1:
  335.                 #print('aligning')
  336.                 self['Armed'][0]-=1
  337.                 own['X']*= .99
  338.                 own['Y']*= .99
  339.                 #self.worldPosition = self.parent.worldTransform @ self['init_tracker_p']  
  340.                 #self.worldOrientation = self.worldOrientation.lerp( self.parent.worldOrientation @ self['init_tracker_r'],.01)
  341.                 #self.applyRotation([0,0,0],1)
  342.                
  343.             else:
  344.                 del self['Armed']    
  345.                 #print('safe')  
  346.                 #own['X']=0
  347.                 #own['Y']=0  
  348.                    
  349.            
  350. def barrel(data):
  351.     self = data[0]
  352.     self = own.childrenRecursive[self]
  353.     projectile = data[1]
  354.     axis = data[2]
  355.     speed = data[3]
  356.     offset = data[4]
  357.     owner = own
  358.    
  359.    
  360.     cost =1      
  361.     if 'cost' in self:
  362.         cost=self['cost']
  363.     if 'owner' in own:
  364.         owner = own['owner']
  365.    
  366.     if 'Charge' in self:
  367.         if 'HOLDING' not in owner:
  368.             if self['Charge']>=1:
  369.            
  370.                 added = own.scene.addObject(projectile,own,0)
  371.                 added['Charge']=self['Charge']
  372.                 added.applyMovement((0,offset,0),1)
  373.                        
  374.                 self['Charge']=0
  375.                 if 'FX' in self:
  376.                     self['FX'].applyMovement((0,offset,0),1)
  377.                     self['FX'].setParent(added,0,1)
  378.                     self['FX'].localScale*= Vector([1.25,2,1.25])
  379.                     added['FX'] = self['FX']
  380.                     cont.deactivate(cont.actuators['Action'])
  381.                    
  382.                     del self['FX']    
  383.                     print('destroyedFX')
  384.            
  385.         elif 'FX' not in self:
  386.             self['FX'] = own.scene.addObject(self['ChargeFX'],self,0)
  387.             print('createdFX')
  388.             cont.activate(cont.actuators['Action'])
  389.             self['FX'].setParent(self,0,1)
  390.             self['FX'].applyMovement((0,.25,0),1)
  391.             self['FX2'] = own.scene.addObject('Point',self,0)
  392.             self['FX2'].applyMovement((0,.25,0),1)
  393.             self['FX2'].setParent( self['FX'],0,1)
  394.            
  395.         else:
  396.             c = .125+(self['Charge']*.001)
  397.             self['FX'].localScale = [c*1.5,c,c*1.5]
  398.             self['FX'].color = [c*64, c*64 , c*64,1]
  399.             self['FX'].applyRotation((0,c*-.1*c,0),1)
  400.             bpy.data.lights[self['FX2'].name].energy = (self['Charge']*8)*(.5 + (bge.logic.getRandomFloat()*.5))
  401.            
  402.                    
  403.                
  404.     if 'Firing' in own:
  405.        
  406.         prop = projectile+"s_Clip"
  407.         #print(prop)
  408.        
  409.         if 'Running' in own:
  410.             own['Running']+=(math.pi*2)/60
  411.             if own['Running']>math.pi*2:
  412.                 own['Running']=0
  413.         if prop in own:
  414.             if own[prop]>=cost:
  415.                 if 'Charge' not in self:
  416.                     #print(self)
  417.                     own[prop]-=cost
  418.                
  419.                     added = own.scene.addObject(projectile,self,0)
  420.                     if 'extra' in self:
  421.                         own.scene.addObject(self['extra'],own,0)
  422.                        
  423.                     if 'Stay' in added:
  424.                         added.setParent(own,0,1)
  425.                     added.worldLinearVelocity = self.worldOrientation.col[axis]*(speed*1)
  426.                     try:
  427.                         added.worldLinearVelocity+=own.worldLinearVelocity
  428.                     except:
  429.                         pass
  430.                     added.worldPosition += self.worldOrientation.col[axis]*offset
  431.                     added['owner']=own
  432.                     if 'Color' in self:
  433.                         added.color = self.color
  434.                         print(self.color)
  435.                         print('set')
  436.                     if 'Damage' in self:
  437.                         added['Damage']=self['Damage']    
  438.                     added['owner']=own
  439.                     if 'Target' in owner:
  440.                         if owner['Target']!="None" and owner['Target']!=None:
  441.                             added['Target']=owner['Target']
  442.                 elif self['Charge']+cost<100:
  443.                     #print(self['Charge'])
  444.                     own[prop]-=cost
  445.                     self['Charge']+=cost            
  446.             else:
  447.                 own['reload_needed']=True    
  448.     elif 'Punch' not in owner['Act'] and 'Running' in own:
  449.         own['Running']=0
  450.     if 'Charge' in self:    
  451.         own['Charge']=self['Charge']
  452.                
  453.            
  454. def fire_control(data):
  455.     self = data[0]
  456.     if self!=own.name:
  457.         self = own.childrenRecursive[self]
  458.     else:
  459.         self = own    
  460.     if 'owner' not in own:    
  461.         owner =  own
  462.     else:
  463.         owner = own['owner']
  464.        
  465.     if own['Fire']==4:
  466.         if owner!=own:
  467.             owner['Recoil']=own['Recoil']
  468.                    
  469.     if own['Fire']==1:
  470.         own['Fire']=2
  471.         own['Firing']=True
  472.        
  473.     elif 'Firing' in own:
  474.         del own['Firing']
  475.            
  476.     if own['Fire']>1 and own['Fire']<own['Rechamber']:
  477.         own['Fire']+=1
  478.        
  479.     elif own['Rechamber']<=own['Fire']:
  480.         own['Fire']=0
  481.         if 'Recharge' in own:
  482.             #print(own['Recharge'])
  483.             added = own.scene.addObject(own['Recharge'],own,0)
  484.             if 'Charge' in own:
  485.                 added.actuators['Sound'].pitch =-.66+( own['Charge']*.2)
  486.                
  487.                
  488.            
  489.        
  490.     if own['Fire']==own['Reload']:
  491.         own.scene.addObject(own['Reload_Sound'],own,0)
  492.         own['Reloading']=True
  493.         #print('start')
  494.         #print(owner.name)
  495.         for prop in own.getPropertyNames():
  496.            
  497.            
  498.             if prop[-6:]=="s_Clip":
  499.                 #print(prop)
  500.                 length = len(prop)
  501.                 ammo = prop[:-5 or None]
  502.                 #ammo = prop[:7]
  503.                 #print('ammo'+ammo)
  504.                 max = prop+"_Max"
  505.                 diff = own[max]-own[prop]
  506.                 if max in own and ammo in owner:
  507.                     #print('set')
  508.                     if owner[ammo]-diff>0:
  509.                         owner[ammo]-=diff
  510.                         own[prop]=own[max]
  511.                     else:
  512.                         own[prop]+=owner[ammo]
  513.                         owner[ammo]=0
  514.                 else:
  515.                     if max in own:
  516.                         print('found '+max)
  517.                     if ammo in owner:
  518.                         print('found '+ammo)
  519.                     else:
  520.                         print('did not find'+ammo)    
  521.                        
  522.                        
  523.                    
  524.     else:
  525.         if 'Reloading' in own:
  526.             del own['Reloading']
  527.                
  528.     if own['Fire']<0:
  529.         own['Fire']+=1
  530.                
  531.        
  532. #print(own.children)
  533. def push(data):
  534.     self = data[0]
  535.     self = own.childrenRecursive[self]
  536.    
  537.     if 'push_init' not in self:
  538.         self['push_init']=own.worldTransform.inverted() @ self.worldPosition
  539.     amount = data[1]
  540.     if type(amount) is str:
  541.         amount = own[amount]*1.5
  542.        
  543.     axis = data[2]
  544.     distance = data[3]
  545.     start = (self.parent.worldTransform @ self['push_init'] )  + (own.worldOrientation.col[2] *0)
  546.     end = start + own.worldOrientation.col[2]* -distance
  547.     end2 = end+own.worldOrientation.col[1]*(own.localLinearVelocity.y*.1)
  548.     ray = own.rayCast(end,start,0,"",0,0,0)
  549.     if ray[0]==self:
  550.         ray = self.rayCast(end,ray[1],0,"",0,0,0)
  551.     ray2 = self.rayCast(end2,self.worldPosition,0,"",0,0,0)
  552.     if ray2[0]:
  553.         own.alignAxisToVect(ray2[2],2,.075)
  554.     #print(ray)    
  555.     if ray[0]:
  556.         #print(ray[0])
  557.         #own.alignAxisToVect(ray[2],2,.075)
  558.         own['hit'].append((ray[1],ray[2]) )
  559.         #bge.render.drawLine(start,ray[1],(0,0,1))
  560.         d = (start - ray[1]).magnitude
  561.         d2 = (start-end).magnitude
  562.         d = d/d2
  563.         d = 1-d
  564.         d*=d*.2
  565.         if axis =="X":
  566.             angle = own.worldOrientation.col[1]
  567.             #print(amount)
  568.             if d>1:
  569.                 d=1
  570.             own.applyImpulse(ray[1],own.worldOrientation.col[2]*(d))
  571.             if abs(own['Speed'])<.1:
  572.                 own.localLinearVelocity.y*=.95
  573.             own.localLinearVelocity.x*=.1
  574.             if own.localLinearVelocity.z<.0125:
  575.                 own.localLinearVelocity.z*=.95
  576.             own.localLinearVelocity.z*=.95    
  577.             if 'Controller' in own:    
  578.                 self.removeParent()
  579.             own.localAngularVelocity.x*=.01
  580.             #own.localLinearVelocity.y*=.1
  581.             own.localAngularVelocity.z*=.1
  582.             own.localAngularVelocity.y*=.01
  583.            
  584.             if abs(own['Speed'])<.1:
  585.                 own.localLinearVelocity.x*=.01
  586.                 own.localLinearVelocity.y*=.01
  587.                
  588.                        
  589.                
  590.             self.worldPosition = ray[1]+(own.worldOrientation.col[2]*.175)
  591.             if 'Controller' in own:    
  592.                 self.setParent(own,0,1)
  593.             self.worldPosition+=own.worldOrientation.col[2] * -.05
  594.             if own['Speed']>0:
  595.                 if own.localLinearVelocity.y<own['Speed']*1.5:
  596.                     own.applyForce((0,own['Speed']*2,0),1)
  597.             else:
  598.                 if own.localLinearVelocity.y>own['Speed']*1.5:
  599.                     own.applyForce((0,own['Speed']*2,0),1)        
  600.             own.alignAxisToVect(ray[2],2,.1)
  601.             if 'steer_init' in self:
  602.                
  603.                 if own['Speed']>.01:
  604.                     own.applyRotation((0,0,own['Steer']*.05),1)
  605.                 elif own['Speed']<-.01:
  606.                     own.applyRotation((0,0,-own['Steer']*.05),1)
  607.     else:                
  608.         if 'Controller' in own:    
  609.             self.removeParent()
  610.         self.worldPosition = self.worldPosition.lerp((own.worldTransform @ self['push_init']) + (own.worldOrientation.col[2]*-(distance-.125) ),.06125)
  611.         if 'Controller' in own:    
  612.             self.setParent(own,0,1)
  613.        
  614. def steer(data):
  615.    
  616.     self = data[0]
  617.     self = own.childrenRecursive[self]
  618.     amount = data[1]
  619.    
  620.     if type(amount) is str:
  621.         amount = own[amount]
  622.     axis = data[2]
  623.     if 'steer_init' not in self:
  624.         self['steer_init']=self.parent.worldOrientation.inverted() @ self.worldOrientation
  625.     self.worldOrientation = self.parent.worldOrientation @ self['steer_init']
  626.        
  627.     if axis=="Z":
  628.         local = own.worldTransform.inverted() @ self.worldPosition
  629.         val=-1
  630.         if local.y>0:
  631.             val=1
  632.        
  633.         self.applyRotation(own.worldOrientation.col[2]*(amount*val),0)
  634.        
  635.         if 'R' in self:
  636.             self.applyRotation((self['R'],0,0),1)
  637.         if own.localLinearVelocity.y>.1:    
  638.             own.localAngularVelocity.z = ((own.localAngularVelocity.z*15) + (own['Steer']*64))/16
  639.         elif own.localLinearVelocity.y<-.1:
  640.             own.localAngularVelocity.z = ((own.localAngularVelocity.z*15) + (-own['Steer']*64))/16
  641.                
  642.            
  643.     #print('ran')    
  644.    
  645.    
  646.  
  647. def sense(data):
  648.     #print(own.name)
  649.     self = data[0]
  650.     self = own.childrenRecursive[self]
  651.     prop = data[1]
  652.     value = data[2]
  653.     freqency = data[3]
  654.     #print(data)
  655.     if prop+'Track' not in self:
  656.         l = []
  657.         for obj in own.scene.objects:
  658.             if 'Health' in obj:
  659.                 if obj['Health']>=1:
  660.                     if prop in obj:
  661.                         if value =="Any":
  662.                             l.append(obj)
  663.                         elif obj[prop]==value:
  664.                             l.append(obj)
  665.                    
  666.         self[prop+'Track'] = [ l, data[3]]
  667.     elif self[prop+'Track'][1]>=1:
  668.         self[prop+'Track'][1]-=1
  669.         targets = []
  670.         dat = self[prop+'Track'][0]
  671.         #print(dat)
  672.            
  673.         for obj in dat:
  674.             local = self.worldTransform.inverted() @ obj.worldPosition
  675.             #print(local)
  676.             if local.z>0 and abs(local.x*2)-local.z<0 and abs(local.y*2)-local.z<0 and obj['Health']>=1:
  677.                 ray = own.rayCast(obj.worldPosition,self.worldPosition,0,"",0,0,0)
  678.                 #if ray[0]:
  679.                   #  print(ray[0])
  680.                 if ray[0]==obj:
  681.                     targets.append( (obj,local, local.magnitude) )
  682.         closest = None
  683.         d = 10000
  684.         for obj in targets:
  685.             if obj[2]<d:
  686.                 d = obj[2]
  687.                 closest = obj
  688.         if closest!=None and d<data[4]:
  689.             own['Target']=  [obj[0].name ,int(freqency)]
  690.             if 'Fire' in own and 'owner' not in own:
  691.                 if 'reload_needed' not in own:
  692.                
  693.                     if own['Fire']==0:
  694.                         own['Fire']=1
  695.                 else:
  696.                     own['Fire']=own['Reload']
  697.                     del own['reload_needed']
  698.          
  699.                    
  700.                
  701.             own['Read']= str(own['Target'])
  702.         else:
  703.             if 'Target' in own:
  704.                 if own['Target']!=None and own['Target']!="None":
  705.                
  706.                     if own['Target'][1]>=1:
  707.                         own['Target'][1]-=1
  708.                     else:
  709.                         own['Target']="None"    
  710.                 own['Read']= str(own['Target'])        
  711.                        
  712.                
  713.     else:
  714.         del self[prop+'Track']    
  715.            
  716. def track(data):
  717.     aiming = data[0]
  718.    
  719.     target = None
  720.     g=1
  721.     if own['Target']!="None":
  722.         if own['Target'][1]>=1:
  723.             target = own.scene.objects[own['Target'][0]]
  724.             own['Target'][1]-=1
  725.            
  726.         else:
  727.             own['Target']= "None"    
  728.            
  729.             g=0
  730.        
  731.    
  732.     if g==1:
  733.         if type(target) is str:
  734.             target = own.scene.objects[target]    
  735.        
  736.     axis = data[2]
  737.     amount = data[3]
  738.    
  739.     aiming = own.childrenRecursive[aiming]
  740.     if 'track_init' not in aiming:
  741.         aiming['track_init']=aiming.parent.worldOrientation.inverted() @ aiming.worldOrientation
  742.         aiming['track_init2']=aiming.parent.worldTransform.inverted() @ aiming.worldPosition
  743.     aiming.worldPosition = aiming.parent.worldTransform @ aiming['track_init2']
  744.     aiming.worldOrientation = aiming.worldOrientation.lerp(aiming.parent.worldOrientation @ aiming['track_init'],.5)
  745.    
  746.     if target!=None:
  747.         #print(target)
  748.         aiming.alignAxisToVect(aiming.getVectTo(target)[1],axis,amount)
  749.    
  750.    
  751.    
  752.        
  753.    
  754.    
  755.    
  756.            
  757. def glow(data):
  758.     target =data[0]
  759.     target = own.childrenRecursive[target]
  760.     color1 = Vector(data[1])
  761.     color2 = Vector(data[2])
  762.     freqency  = data[3]*.5
  763.     time = data[4]
  764.     if type(time) is str:
  765.         time = own[time]
  766.        
  767.     value = abs(math.sin(time*freqency))
  768.    
  769.     target.color = color1.lerp(color2,value)
  770.      
  771. def piston(data):
  772.     target = data[0]
  773.     target = own.childrenRecursive[target]
  774.     time = data[1]
  775.    
  776.     if type(time) is str:
  777.         min = data[5]
  778.         max = data[6]
  779.         consume = data[7]
  780.        
  781.         time = own[time]
  782.         if time<min:
  783.             time=0
  784.         elif time>max:
  785.             time=0
  786.         if own[consume]<=0:
  787.             time =0
  788.                    
  789.     axis   = data[2]
  790.     freqency = data[3]*.5
  791.     distance = data[4]
  792.     #print(data)
  793.     #print(time)
  794.     if 'piston_init' not in target:
  795.         pt =  target.parent
  796.         matrix = pt.worldTransform.inverted()
  797.        
  798.         target['piston_init'] = matrix @ target.worldPosition
  799.        
  800.     target.worldPosition = target.parent.worldTransform @ target['piston_init']
  801.     if axis==1 and time!=0:
  802.         #print('fire')
  803.         target.applyMovement([0,math.sin(time*freqency)*distance,0],1)
  804.        
  805.        
  806. def rotate(data):
  807.     #print(data)
  808.     target = data[0]
  809.     target = own.childrenRecursive[target]
  810.    
  811.     amount = data[1]
  812.     if type(amount) is str:
  813.         amount = own[amount]
  814.     #print(data)    
  815.     axis   = data[2]
  816.     if axis=="X":
  817.         if data[1]=="Speed":
  818.             local = target.parent.worldTransform.inverted() @ target.worldPosition
  819.             if local.x>0:
  820.                 val=1
  821.             else:
  822.                 val=-1    
  823.             if 'R' in target:
  824.                 target['R']+=(amount*val)
  825.                 if target['R']>math.pi*2:
  826.                     target['R']-=math.pi*2
  827.                 elif target['R']<0:
  828.                     target['R']=math.pi + target['R']    
  829.             target.applyRotation((amount,0,0),1)
  830.     elif axis==0:
  831.        
  832.         target.applyRotation((amount,0,0),1)
  833.     elif axis==1:
  834.         target.applyRotation((0,amount,0),1)
  835.     else:
  836.        target.applyRotation((0,0,amount),1)    
  837.        
  838. Functions = { 'Rotate':rotate, 'Piston':piston, 'Glow':glow ,"Track":track, "Sense":sense, 'Steer':steer, "Push":push ,"Fire_Control":fire_control, 'Barrel':barrel, 'Tracker':tracker, 'Goal':goal, 'Navigate':navigate}
  839.  
  840.  
  841. def main():
  842.     #if 'start_stick' not in own.scene.objects['Plane']:
  843.         #own.scene.objects['Plane']['start_stick']=True
  844.         #own.scene.pre_draw.append(stick)
  845.         #own.scene.post_draw.append(stick)
  846.        
  847.    
  848.            
  849.    
  850.     own['hit']=[]
  851.     if 'init' not in own:
  852.         own['Impacted']=0
  853.         own['Parts']= []
  854.         own['Front_Wheels']=[]
  855.         own['wheels']=[]
  856.         own['Move']=[]
  857.         own['Act']=[]
  858.         own['Seats']=[]
  859.         own['Up']=own.worldOrientation.col[2]
  860.         w=0
  861.         for prop in own.getPropertyNames():
  862.             if prop[:4]=="part":
  863.                
  864.                 prop1 = ast.literal_eval(own[prop])
  865.                 if prop1[1]=="self":
  866.                     prop1[1]=own.name
  867.                     own['Parts'].append(prop1)  
  868.                
  869.                      
  870.         for child in own.childrenRecursive:
  871.             if 'Helper' in child:
  872.                 own['Helper']=child
  873.             if 'Seat_Check' in child:
  874.                 local = own.worldTransform.inverted() @ child.worldPosition
  875.                 diff = own.worldOrientation.inverted() @ child.worldOrientation
  876.                
  877.                 own['Seats'].append([local,diff,child['Seat_Check'] ])
  878.                 child.endObject()  
  879.                  
  880.             else:
  881.                 for prop in child.getPropertyNames():
  882.                     #print(prop[:4])
  883.                     if prop[:4]=="part":
  884.                         if child[prop]!="None":
  885.                             prop1 = ast.literal_eval(child[prop])
  886.                             prop1.append(own.name)
  887.                             if prop1[1]=="self":
  888.                                 prop1[1]=child.name
  889.                             own['Parts'].append(prop1)
  890.                             if prop1[0]=="Steer":
  891.                                 own['Front_Wheels'].append(child)
  892.                             if prop1[0]=="Push":
  893.                                 own['wheels'].append(child)  
  894.                                 prop1.append(w)  
  895.                                 w+=1    
  896.                             if prop1[0]=="Tracker":
  897.                                 own['Tracker']=child    
  898.                                    
  899.                 child['owner']=own        
  900.                 if child.parent==own or 'Phys' in child and 'Seat_Check' not in child:
  901.                     child.setParent(own,1 ,0)    
  902.                 elif 'Seat_Check' not in child:
  903.                     child.suspendPhysics()        
  904.                
  905.         own['init']=True
  906.     if 'Rider' in own and 'VehicleCheck' in own['Act']:
  907.         own['Rider'][0].worldPosition = own.worldTransform @ own['Rider'][1]
  908.         own['Rider'][0].worldOrientation = own.worldOrientation @ own['Rider'][2]
  909.         own['Rider'][0].applyRotation((0,0,math.pi),1)
  910.         del own['Rider'][0]['ActStrip'][0]
  911.         own['Rider'][0]['Jump']=15
  912.         own.scene.objects['Controller']['Target']=own['Rider'][0].name
  913.         if 'GFX' in own:
  914.             for gfx in own['GFX']:
  915.                 gfx[0].endObject()
  916.         own['Rider'][0].removeParent()    
  917.         del own['Rider']
  918.                
  919.     i=0
  920.     if own['Impacted']==1:
  921.         if 'suspended' in own:
  922.             del own['suspended']
  923.             own.restoreDynamics()
  924.     if own['Health']>=1 and cont.sensors['Property'].positive:
  925.         if 'Impact' in own:
  926.            
  927.             pos = own.worldTransform @ own['Impact'][0]
  928.             own.applyImpulse(pos,own['Impact'][2]*(own['Impact'][1]*.05))
  929.            
  930.         for part in own['Parts']:
  931.            
  932.             function = Functions[part[0]]
  933.             data = part[1:]
  934.             data.append(i)
  935.             ret = function(data)
  936.             i+=1
  937.            
  938.                
  939.        
  940.                    
  941.             #print(part[0])
  942.            #print(data)
  943.                
  944.             #own['Helper'].worldPosition = own.worldPosition + (own.worldLinearVelocity*1.5)
  945.             if 'Target' in own:
  946.                 if type(own['Target'][1]) is not str:
  947.                     if own['Target'][1]>=1:
  948.                         own['Target'][1]-=1
  949.                         if 'Player' not in own:
  950.                             own['X']*=.95
  951.                             own['Y']*=.95
  952.                     else:
  953.                        
  954.                         own['Target']="None"
  955.     elif own['Health']<1:
  956.         if 'Team' in own:
  957.             del own['Team']
  958.         if 'Impact' in own:
  959.            
  960.             pos = own.worldTransform @ own['Impact'][0]
  961.             own.applyImpulse(pos,own['Impact'][2]*(own['Impact'][1]*.05))
  962.             for child in own.children:
  963.                 if child.getDistanceTo(pos)<own['Impact'][1]:
  964.                     if 'rag_part' in child:
  965.                         added = own.scene.addObject(child['rag_part'],child,0)
  966.                         if 'rag_rot' in child:
  967.                             rot = ast.literal_eval(child['rag_rot'])
  968.                            
  969.                             added.applyRotation(rot,1)
  970.                         added.applyImpulse(pos,-(pos-added.worldPosition).normalized()*(own['Impact'][1]*.5) )
  971.                            
  972.                        
  973.                         for child2 in child.children:
  974.                             if 'rag_part' in child2:
  975.                                 added = own.scene.addObject(child2['rag_part'],child2,0)
  976.                                 added.applyImpulse(pos,-(pos-added.worldPosition).normalized()*(own['Impact'][1]*.5) )
  977.                                 child2.removeParent()
  978.                                 child2.endObject()
  979.                         child.removeParent()        
  980.                         child.endObject()
  981.             del own['Impact']                        
  982.             own ['Impacted']=0        
  983.                  
  984.         own['Fire']=0
  985.         for child in own.childrenRecursive:
  986.             child.color = [0,0,0,1]
  987.         if own['On']==True:
  988.             own['On']=False
  989.            
  990.        
  991.             own.restoreDynamics()
  992.             for wheel in own['wheels']:
  993.                 pos = wheel.worldPosition
  994.                 wheel.removeParent()
  995.                 wheel.worldPosition =pos
  996.                 wheel.setParent(own,1,0)
  997.                
  998.     if 'Controller' in own:          
  999.        
  1000.         if 'Right' in own['Move'] and own['Steer']>=-1:
  1001.             if own['Steer']>0:
  1002.                 own['Steer']*=.9
  1003.                
  1004.             own['Steer']-=.0125
  1005.         elif 'Left' in own['Move'] and own['Steer']<=1:
  1006.             if own['Steer']<0:
  1007.                 own['Steer']*=.9
  1008.             own['Steer']+=.0125
  1009.         else:
  1010.             own['Steer']*=.5
  1011.         if 'Forward' in own['Move'] and own['Speed']<=1.8:
  1012.             own['Speed']+=.1
  1013.         elif 'Back' in own['Move'] and own['Speed']>=-1.8:
  1014.             own['Speed']-=.1        
  1015.         else:
  1016.             own['Speed']*=.5
  1017.              
  1018.             if abs(own['Speed'])<.01:
  1019.                 own['Speed']=0
  1020.         if 'Punch' in own['Act'] and own['Fire']==0 and 'owner' not in own:
  1021.             own['Fire']=1
  1022.         elif 'Reload' in own['Act'] and own['Fire']==0 and 'owner' not in own:
  1023.             own['Fire']=own['Reload']
  1024.             print('reload')
  1025.            
  1026.     if own.getDistanceTo(own.scene.objects['Cube'])>=10.5 and 'suspended' not in own:
  1027.        
  1028.         own.suspendDynamics()
  1029.         own['suspended']=True
  1030.         #print('ran')
  1031.     elif own.getDistanceTo(own.scene.objects['Cube'])<10.25 and 'suspended' in own:
  1032.         own.restoreDynamics()
  1033.         del own['suspended']
  1034.         #print('rem')    
  1035.                            
  1036.     own['Move']=[]
  1037.     own['Act']=[]  
  1038.     own['hit']=[]        
  1039.        
  1040.    
  1041. main()
  1042.  
Advertisement
Add Comment
Please, Sign In to add comment