dxx-rebirth/similar/main/robot.cpp

315 lines
8.2 KiB
C++
Raw Normal View History

2006-03-20 17:12:09 +00:00
/*
2014-06-01 17:55:23 +00:00
* Portions of this file are copyright Rebirth contributors and licensed as
* described in COPYING.txt.
* Portions of this file are copyright Parallax Software and licensed
* according to the Parallax license below.
* See COPYING.txt for license details.
2006-03-20 17:12:09 +00:00
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
*
* Code for handling robots
*
*/
#include "dxxerror.h"
2006-03-20 17:12:09 +00:00
#include "inferno.h"
#include "robot.h"
#include "object.h"
#include "polyobj.h"
2014-07-20 01:09:55 +00:00
#include "physfsx.h"
2006-03-20 17:12:09 +00:00
2014-10-26 22:51:27 +00:00
#include "compiler-range_for.h"
#include "partial_range.h"
2014-09-20 23:47:27 +00:00
unsigned N_robot_types;
2014-12-18 04:12:38 +00:00
unsigned N_robot_joints;
2006-03-20 17:12:09 +00:00
// Robot stuff
2015-02-28 19:36:01 +00:00
array<robot_info, MAX_ROBOT_TYPES> Robot_info;
2006-03-20 17:12:09 +00:00
//Big array of joint positions. All robots index into this array
2014-12-18 04:12:38 +00:00
array<jointpos, MAX_ROBOT_JOINTS> Robot_joints;
2006-03-20 17:12:09 +00:00
2014-12-18 04:12:38 +00:00
static inline void PHYSFSX_writeAngleVec(PHYSFS_file *fp, const vms_angvec &v)
{
PHYSFS_writeSLE16(fp, v.p);
PHYSFS_writeSLE16(fp, v.b);
PHYSFS_writeSLE16(fp, v.h);
}
2006-03-20 17:12:09 +00:00
//given an object and a gun number, return position in 3-space of gun
//fills in gun_point
2014-11-02 03:41:01 +00:00
void calc_gun_point(vms_vector &gun_point,const vcobjptr_t obj,int gun_num)
2006-03-20 17:12:09 +00:00
{
polymodel *pm;
robot_info *r;
vms_vector pnt;
int mn; //submodel number
Assert(obj->render_type==RT_POLYOBJ || obj->render_type==RT_MORPH);
Assert(get_robot_id(obj) < N_robot_types);
2006-03-20 17:12:09 +00:00
r = &Robot_info[get_robot_id(obj)];
2006-03-20 17:12:09 +00:00
pm =&Polygon_models[r->model_num];
if (gun_num >= r->n_guns)
{
gun_num = 0;
}
pnt = r->gun_points[gun_num];
mn = r->gun_submodels[gun_num];
//instance up the tree for this gun
while (mn != 0) {
const auto m = vm_transposed_matrix(vm_angles_2_matrix(obj->rtype.pobj_info.anim_angles[mn]));
2014-11-04 01:31:22 +00:00
const auto tpnt = vm_vec_rotate(pnt,m);
2006-03-20 17:12:09 +00:00
2014-09-28 21:11:40 +00:00
vm_vec_add(pnt,tpnt,pm->submodel_offsets[mn]);
2006-03-20 17:12:09 +00:00
mn = pm->submodel_parents[mn];
}
//now instance for the entire object
const auto m = vm_transposed_matrix(obj->orient);
2014-11-02 03:41:01 +00:00
vm_vec_rotate(gun_point,pnt,m);
vm_vec_add2(gun_point,obj->pos);
2006-03-20 17:12:09 +00:00
}
//fills in ptr to list of joints, and returns the number of joints in list
//takes the robot type (object id), gun number, and desired state
2012-07-07 21:38:03 +00:00
int robot_get_anim_state(const jointpos **jp_list_ptr,int robot_type,int gun_num,int state)
2006-03-20 17:12:09 +00:00
{
Assert(gun_num <= Robot_info[robot_type].n_guns);
*jp_list_ptr = &Robot_joints[Robot_info[robot_type].anim_states[gun_num][state].offset];
return Robot_info[robot_type].anim_states[gun_num][state].n_joints;
}
#ifndef NDEBUG
2006-03-20 17:12:09 +00:00
//for test, set a robot to a specific state
2014-10-02 03:02:34 +00:00
static void set_robot_state(const vobjptr_t obj,int state) __attribute_used;
static void set_robot_state(const vobjptr_t obj,int state)
2006-03-20 17:12:09 +00:00
{
int g,j,jo;
robot_info *ri;
jointlist *jl;
Assert(obj->type == OBJ_ROBOT);
ri = &Robot_info[get_robot_id(obj)];
2006-03-20 17:12:09 +00:00
for (g=0;g<ri->n_guns+1;g++) {
jl = &ri->anim_states[g][state];
jo = jl->offset;
for (j=0;j<jl->n_joints;j++,jo++) {
int jn;
jn = Robot_joints[jo].jointnum;
obj->rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
}
}
}
#endif
2006-03-20 17:12:09 +00:00
//set the animation angles for this robot. Gun fields of robot info must
//be filled in.
2015-04-02 02:36:52 +00:00
void robot_set_angles(robot_info *r,polymodel *pm,array<array<vms_angvec, MAX_SUBMODELS>, N_ANIM_STATES> &angs)
2006-03-20 17:12:09 +00:00
{
int m,g,state;
2014-10-26 22:51:27 +00:00
array<int, MAX_SUBMODELS> gun_nums; //which gun each submodel is part of
2006-03-20 17:12:09 +00:00
2014-10-26 22:51:27 +00:00
range_for (auto &m, partial_range(gun_nums, pm->n_models))
m = r->n_guns; //assume part of body...
2006-03-20 17:12:09 +00:00
gun_nums[0] = -1; //body never animates, at least for now
for (g=0;g<r->n_guns;g++) {
m = r->gun_submodels[g];
while (m != 0) {
gun_nums[m] = g; //...unless we find it in a gun
m = pm->submodel_parents[m];
}
}
for (g=0;g<r->n_guns+1;g++) {
for (state=0;state<N_ANIM_STATES;state++) {
r->anim_states[g][state].n_joints = 0;
r->anim_states[g][state].offset = N_robot_joints;
for (m=0;m<pm->n_models;m++) {
if (gun_nums[m] == g) {
Robot_joints[N_robot_joints].jointnum = m;
Robot_joints[N_robot_joints].angles = angs[state][m];
r->anim_states[g][state].n_joints++;
N_robot_joints++;
Assert(N_robot_joints < MAX_ROBOT_JOINTS);
}
}
}
}
}
/*
* reads n jointlist structs from a PHYSFS_file
2006-03-20 17:12:09 +00:00
*/
2015-02-14 22:48:30 +00:00
static void jointlist_read(PHYSFS_File *fp, array<jointlist, N_ANIM_STATES> &jl)
2006-03-20 17:12:09 +00:00
{
2015-02-14 22:48:30 +00:00
range_for (auto &i, jl)
{
i.n_joints = PHYSFSX_readShort(fp);
i.offset = PHYSFSX_readShort(fp);
2006-03-20 17:12:09 +00:00
}
}
/*
* reads n robot_info structs from a PHYSFS_file
2006-03-20 17:12:09 +00:00
*/
2015-01-23 03:55:04 +00:00
void robot_info_read(PHYSFS_File *fp, robot_info &ri)
2006-03-20 17:12:09 +00:00
{
2015-01-23 03:55:04 +00:00
ri.model_num = PHYSFSX_readInt(fp);
#if defined(DXX_BUILD_DESCENT_I)
2015-01-23 03:55:04 +00:00
ri.n_guns = PHYSFSX_readInt(fp);
#endif
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.gun_points)
PHYSFSX_readVector(fp, j);
range_for (auto &j, ri.gun_submodels)
j = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.exp1_vclip_num = PHYSFSX_readShort(fp);
ri.exp1_sound_num = PHYSFSX_readShort(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.exp2_vclip_num = PHYSFSX_readShort(fp);
ri.exp2_sound_num = PHYSFSX_readShort(fp);
2006-03-20 17:12:09 +00:00
#if defined(DXX_BUILD_DESCENT_I)
2015-01-23 03:55:04 +00:00
ri.weapon_type = PHYSFSX_readShort(fp);
#elif defined(DXX_BUILD_DESCENT_II)
2015-01-23 03:55:04 +00:00
ri.weapon_type = PHYSFSX_readByte(fp);
ri.weapon_type2 = PHYSFSX_readByte(fp);
ri.n_guns = PHYSFSX_readByte(fp);
#endif
2015-01-23 03:55:04 +00:00
ri.contains_id = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.contains_count = PHYSFSX_readByte(fp);
ri.contains_prob = PHYSFSX_readByte(fp);
ri.contains_type = PHYSFSX_readByte(fp);
#if defined(DXX_BUILD_DESCENT_I)
2015-01-23 03:55:04 +00:00
ri.score_value = PHYSFSX_readInt(fp);
#elif defined(DXX_BUILD_DESCENT_II)
2015-01-23 03:55:04 +00:00
ri.kamikaze = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.score_value = PHYSFSX_readShort(fp);
ri.badass = PHYSFSX_readByte(fp);
ri.energy_drain = PHYSFSX_readByte(fp);
#endif
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.lighting = PHYSFSX_readFix(fp);
ri.strength = PHYSFSX_readFix(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.mass = PHYSFSX_readFix(fp);
ri.drag = PHYSFSX_readFix(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.field_of_view)
j = PHYSFSX_readFix(fp);
range_for (auto &j, ri.firing_wait)
j = PHYSFSX_readFix(fp);
#if defined(DXX_BUILD_DESCENT_II)
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.firing_wait2)
j = PHYSFSX_readFix(fp);
#endif
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.turn_time)
j = PHYSFSX_readFix(fp);
#if defined(DXX_BUILD_DESCENT_I)
2015-01-23 03:55:04 +00:00
for (unsigned j = 0; j < NDL * 2; j++)
PHYSFSX_readFix(fp);
#endif
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.max_speed)
j = PHYSFSX_readFix(fp);
range_for (auto &j, ri.circle_distance)
j = PHYSFSX_readFix(fp);
range_for (auto &j, ri.rapidfire_count)
j = PHYSFSX_readByte(fp);
range_for (auto &j, ri.evade_speed)
j = PHYSFSX_readByte(fp);
ri.cloak_type = PHYSFSX_readByte(fp);
ri.attack_type = PHYSFSX_readByte(fp);
#if defined(DXX_BUILD_DESCENT_I)
2015-01-23 03:55:04 +00:00
ri.boss_flag = PHYSFSX_readByte(fp);
#endif
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.see_sound = PHYSFSX_readByte(fp);
ri.attack_sound = PHYSFSX_readByte(fp);
ri.claw_sound = PHYSFSX_readByte(fp);
#if defined(DXX_BUILD_DESCENT_II)
2015-01-23 03:55:04 +00:00
ri.taunt_sound = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.boss_flag = PHYSFSX_readByte(fp);
ri.companion = PHYSFSX_readByte(fp);
ri.smart_blobs = PHYSFSX_readByte(fp);
ri.energy_blobs = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.thief = PHYSFSX_readByte(fp);
ri.pursuit = PHYSFSX_readByte(fp);
ri.lightcast = PHYSFSX_readByte(fp);
ri.death_roll = PHYSFSX_readByte(fp);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.flags = PHYSFSX_readByte(fp);
2015-04-19 04:18:49 +00:00
array<char, 3> pad;
PHYSFS_read(fp, pad, pad.size(), 1);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.deathroll_sound = PHYSFSX_readByte(fp);
ri.glow = PHYSFSX_readByte(fp);
2015-04-26 20:15:51 +00:00
ri.behavior = static_cast<ai_behavior>(PHYSFSX_readByte(fp));
2015-01-23 03:55:04 +00:00
ri.aim = PHYSFSX_readByte(fp);
#endif
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
range_for (auto &j, ri.anim_states)
2015-02-14 22:48:30 +00:00
jointlist_read(fp, j);
2006-03-20 17:12:09 +00:00
2015-01-23 03:55:04 +00:00
ri.always_0xabcd = PHYSFSX_readInt(fp);
2006-03-20 17:12:09 +00:00
}
/*
* reads n jointpos structs from a PHYSFS_file
2006-03-20 17:12:09 +00:00
*/
2014-12-18 04:12:38 +00:00
void jointpos_read(PHYSFS_file *fp, jointpos &jp)
2006-03-20 17:12:09 +00:00
{
2014-12-18 04:12:38 +00:00
jp.jointnum = PHYSFSX_readShort(fp);
PHYSFSX_readAngleVec(&jp.angles, fp);
}
2006-03-20 17:12:09 +00:00
2014-12-18 04:12:38 +00:00
void jointpos_write(PHYSFS_file *fp, const jointpos &jp)
{
PHYSFS_writeSLE16(fp, jp.jointnum);
PHYSFSX_writeAngleVec(fp, jp.angles);
2006-03-20 17:12:09 +00:00
}