1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 #include "common.h"
14 
15 /* interface header */
16 #include "CustomMeshFace.h"
17 
18 /* bzfs implementation headers */
19 #include "ParseMaterial.h"
20 
21 /* common implementation headers */
22 #include "PhysicsDriver.h"
23 
24 /* system headers */
25 #include <sstream>
26 #include <iostream>
27 
28 
CustomMeshFace(const BzMaterial & _material,int physics,bool _noclusters,bool bounce,bool drive,bool shoot)29 CustomMeshFace::CustomMeshFace(const BzMaterial& _material, int physics,
30                                bool _noclusters,
31                                bool bounce, bool drive, bool shoot)
32 {
33     phydrv = physics;
34     noclusters = _noclusters;
35     smoothBounce = bounce;
36     shootThrough = shoot;
37     driveThrough = drive;
38     material = _material;
39     ricochet = false;
40     return;
41 }
42 
43 
getIntList(std::istream & input,std::vector<int> & list)44 static void getIntList (std::istream& input, std::vector<int>& list)
45 {
46     std::string args;
47     int value;
48 
49     list.clear();
50     std::getline(input, args);
51     std::istringstream parms(args);
52     input.putback('\n');
53 
54     while (parms >> value)
55         list.push_back(value);
56 
57     return;
58 }
59 
60 
read(const char * cmd,std::istream & input)61 bool CustomMeshFace::read(const char *cmd, std::istream& input)
62 {
63     bool materror;
64 
65     if (strcasecmp(cmd, "vertices") == 0)
66     {
67         getIntList (input, vertices);
68         if (vertices.size() < 3)
69         {
70             std::cout << "mesh faces need at least 3 vertices" << std::endl;
71             return false;
72         }
73     }
74     else if (strcasecmp(cmd, "normals") == 0)
75     {
76         getIntList (input, normals);
77         if (normals.size() < 3)
78         {
79             std::cout << "mesh faces need at least 3 normals" << std::endl;
80             return false;
81         }
82     }
83     else if (strcasecmp(cmd, "texcoords") == 0)
84     {
85         getIntList (input, texcoords);
86         if (texcoords.size() < 3)
87         {
88             std::cout << "mesh faces need at least 3 texcoords" << std::endl;
89             return false;
90         }
91     }
92     else if (strcasecmp(cmd, "phydrv") == 0)
93     {
94         std::string drvname;
95         if (!(input >> drvname))
96         {
97             std::cout << "missing Physics Driver parameter" << std::endl;
98             return false;
99         }
100         phydrv = PHYDRVMGR.findDriver(drvname);
101         if ((phydrv == -1) && (drvname != "-1"))
102             std::cout << "couldn't find PhysicsDriver: " << drvname << std::endl;
103     }
104     else if (strcasecmp(cmd, "smoothbounce") == 0)
105         smoothBounce = true;
106     else if (strcasecmp(cmd, "noclusters") == 0)
107         noclusters = true;
108     else if (strcasecmp(cmd, "drivethrough") == 0)
109         driveThrough = true;
110     else if (strcasecmp(cmd, "shootthrough") == 0)
111         shootThrough = true;
112     else if (strcasecmp(cmd, "passable") == 0)
113         driveThrough = shootThrough = true;
114     else if (strcasecmp(cmd, "ricochet") == 0)
115         ricochet = true;
116     else if (parseMaterials(cmd, input, &material, 1, materror))
117     {
118         if (materror)
119             return false;
120     }
121     else
122     {
123         std::cout << "unknown mesh face property: " << cmd << std::endl;
124         return false;
125     }
126 
127     return true;
128 }
129 
130 
write(MeshObstacle * mesh) const131 void CustomMeshFace::write(MeshObstacle *mesh) const
132 {
133     const BzMaterial* matref = MATERIALMGR.addMaterial(&material);
134     mesh->addFace(vertices, normals, texcoords, matref, phydrv,
135                   noclusters, smoothBounce, driveThrough, shootThrough, ricochet,
136                   true /* triangulate if required */);
137     return;
138 }
139 
140 
141 // Local variables: ***
142 // mode: C++ ***
143 // tab-width: 4***
144 // c-basic-offset: 4 ***
145 // indent-tabs-mode: nil ***
146 // End: ***
147 // ex: shiftwidth=4 tabstop=4
148