| | 1 | #include "system.h" |
| | 2 | #include "prototyp.h" |
| | 3 | #include "stratdef.h" |
| | 4 | #include "bh_types.h" |
| | 5 | #include "bh_cable.h" |
| | 6 | #include "pvisible.h" |
| | 7 | #include <assert.h> |
| | 8 | #include <stdlib.h> |
| | 9 | |
| | 10 | void PowerCableBehaveInit(void* bhdata,STRATEGYBLOCK* sbptr) |
| | 11 | { |
| | 12 | assert(sbptr); |
| | 13 | assert(bhdata); |
| | 14 | |
| | 15 | POWER_CABLE_BEHAV_BLOCK* pc_bhv = malloc(sizeof(POWER_CABLE_BEHAV_BLOCK)); |
| | 16 | |
| | 17 | if(pc_bhv) |
| | 18 | { |
| | 19 | sbptr->dataptr = pc_bhv; |
| | 20 | pc_bhv->bhvr_type = I_BehaviourPowerCable; |
| | 21 | |
| | 22 | POWER_CABLE_TOOLS_TEMPLATE* pc_tt = (POWER_CABLE_TOOLS_TEMPLATE*)bhdata; |
| | 23 | |
| | 24 | //copy stuff from tools template |
| | 25 | COPY_NAME(sbptr->SBname, pc_tt->nameID); |
| | 26 | pc_bhv->position = pc_tt->position; |
| | 27 | pc_bhv->max_charge = pc_tt->max_charge; |
| | 28 | pc_bhv->current_charge = pc_tt->current_charge; |
| | 29 | pc_bhv->recharge_rate = pc_tt->recharge_rate; |
| | 30 | pc_bhv->position.vy += 10; //temporarily move cable down in case rounding errors have put cable just outside of module |
| | 31 | sbptr->containingModule =ModuleFromPosition(&pc_bhv->position, 0); |
| | 32 | pc_bhv->position.vy -= 10; |
| | 33 | |
| | 34 | assert(sbptr->containingModule); |
| | 35 | } |
| | 36 | else |
| | 37 | { |
| | 38 | RemoveBehaviourStrategy(sbptr); |
| | 39 | } |
| | 40 | } |
| | 41 | |
| | 42 | void PowerCableBehaveFun(STRATEGYBLOCK* sbptr) |
| | 43 | { |
| | 44 | POWER_CABLE_BEHAV_BLOCK* pc_bhv; |
| | 45 | assert(sbptr); |
| | 46 | pc_bhv = (POWER_CABLE_BEHAV_BLOCK*)sbptr->dataptr; |
| | 47 | assert((pc_bhv->bhvr_type == I_BehaviourPowerCable)); |
| | 48 | |
| | 49 | //see if player can get health from cable |
| | 50 | //player must be an alien |
| | 51 | if(AvP.PlayerType == I_Alien) |
| | 52 | { |
| | 53 | //the cable needs to be in a near module |
| | 54 | assert(sbptr->containingModule); |
| | 55 | |
| | 56 | if(ModuleCurrVisArray[sbptr->containingModule->m_index]) |
| | 57 | { |
| | 58 | if(VectorDistance(&PlayerStatus.DisplayBlock->ObWorld, &pc_bhv->position) < CABLE_HEALTH_DISTANCE) |
| | 59 | { |
| | 60 | //give the player some health |
| | 61 | int health_gained; |
| | 62 | int current_health; |
| | 63 | int max_health; |
| | 64 | const NPC_DATA *NpcData = &NpcDataList[I_NPC_Alien]; |
| | 65 | |
| | 66 | max_health = NpcData->StartingStats.Health << ONE_FIXED_SHIFT; |
| | 67 | current_health = PlayerStatus.sbptr->DamageBlock.Health; |
| | 68 | |
| | 69 | health_gained = min(pc_bhv->current_charge, max_health-current_health); |
| | 70 | pc_bhv->current_charge -= health_gained; |
| | 71 | |
| | 72 | PlayerStatus.sbptr->DamageBlock.Health += health_gained; |
| | 73 | PlayerStatus.Health = PlayerStatus.sbptr->DamageBlock.Health; |
| | 74 | } |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | //increase charge if currently below maximum |
| | 79 | if(pc_bhv->current_charge < pc_bhv->max_charge) |
| | 80 | { |
| | 81 | if(pc_bhv->recharge_rate) |
| | 82 | { |
| | 83 | pc_bhv->current_charge += MUL_FIXED(pc_bhv->recharge_rate,NormalFrameTime); |
| | 84 | |
| | 85 | if(pc_bhv->current_charge > pc_bhv->max_charge) |
| | 86 | pc_bhv->current_charge = pc_bhv->max_charge; |
| | 87 | } |
| | 88 | } |
| | 89 | } |