MCPcopy Create free account
hub / github.com/DFHack/dfhack / apply_impact_damage

Function apply_impact_damage

plugins/siege-engine.cpp:268–321  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

266static const int WEAR_TICKS = 806400;
267
268static bool apply_impact_damage(df::item *item, int minv, int maxv)
269{
270 MaterialInfo info(item);
271 if (!info.isValid())
272 {
273 item->setWear(3);
274 return false;
275 }
276
277 auto &strength = info.material->strength;
278
279 // Use random strain type excluding COMPRESSIVE (conveniently last)
280 int type = rng.random(strain_type::COMPRESSIVE);
281 int power = minv + rng.random(maxv-minv+1);
282
283 // High elasticity materials just bend
284 if (strength.strain_at_yield[type] >= 5000)
285 return true;
286
287 // Instant fracture?
288 int fracture = strength.fracture[type];
289 if (fracture <= power || info.material->flags.is_set(material_flags::IS_GLASS))
290 {
291 item->setWear(3);
292 return false;
293 }
294
295 // Impact within elastic strain range?
296 int yield = strength.yield[type];
297 if (yield > power)
298 return true;
299
300 // Can wear?
301 auto actual = virtual_cast<df::item_actual>(item);
302 if (!actual)
303 return false;
304
305 // Transform plastic deformation to wear
306 int max_wear = WEAR_TICKS * 4;
307 int cur_wear = WEAR_TICKS * actual->wear + actual->wear_timer;
308 cur_wear += int64_t(power - yield)*max_wear/(fracture - yield);
309
310 if (cur_wear >= max_wear)
311 {
312 actual->wear = 3;
313 return false;
314 }
315 else
316 {
317 actual->wear = cur_wear / WEAR_TICKS;
318 actual->wear_timer = cur_wear % WEAR_TICKS;
319 return true;
320 }
321}
322
323/*
324 * Configuration object

Callers 1

doLaunchContentsMethod · 0.85

Calls 2

is_setMethod · 0.80
isValidMethod · 0.45

Tested by

no test coverage detected