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