# Best way to implement "reverse battery compartment mod" within current code structure?

**URL:** <https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718>\
**Category:** The Lab\
**Created:** [July 21, 2021, 12:30pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718 "2021-07-21T12:30:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 21, 2021, 12:30pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/1 "2021-07-21T12:30:49Z")

</div>

One thing has been nagging me since forever: you _can_ use car batteries in handheld tools via battery compartment mod ( [Battery compartment mod - Cataclysm: Dark Days Ahead](http://cdda-trunk.chezzo.com/magazine_battery_mod) ), but you _can’t_ use handheld batteries in vehicles. And there isn’t really a logical reason why you can’t: standard medium battery has the same energy capacity as a “vehicle” motorbike battery, for example.

However, after thinking about the actual implementation for a while, it’s clear that you can’t directly use the code from magazine\_battery\_mod in reverse (vehicle don’t have “pockets” for batteries the way inventory items do). As such, there are at least 4 ways you can accomplish this “reverse compatibility”:

- add a separate recipe for each type of handheld battery that directly turns it into vehicle-compatible battery. This will require 7 new recipes (to cover all rechargeable battery types), but is rather straight-forward to implement (for me, anyway) and unlikely case unexpected issues.
- add a new vehicle part that, when installed into the vehicle, will allow user to install handheld batteries into the same vehicle tile (problem is, I have no practical knowledge of that area of the vehicle code, and it smells heavily of C++, which is not exactly my forte either)
- create an new item mod that, when installed onto a battery, makes it vehicle-compatible (here I’m not even sure there is a precedent for such thing in the code. Can a simple item mod change how _vehicle_ code sees an item?)
- create an new item that accepts handheld batteries into it’s pocket, and can be installed into the vehicle thereafter (again, no idea if current vehicle code base even supports two-way interaction with an item inside a pocket of another item)

Right now I’m leaning towards option #1, as I understand the code base for that implementation much better. However, I’d like to hear some other opinions here before I push the PR.

---

<div class="post-metadata">

**Author:** ![mlangsdorf](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/mlangsdorf/32/2462_2.png) [@mlangsdorf](https://discourse.cataclysmdda.org/u/mlangsdorf)\
**Post date:** [July 21, 2021, 12:49pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/2 "2021-07-21T12:49:00Z")

</div>

I think your best solution would actually be #2:

1. create a “handheld battery mount” vehicle part based on the existing “storage\_part\_mount”
2. create a bunch of removable battery vehicle parts for handheld batteries
3. whine at me until I write some code for setting limits on the size of batteries you can install in a BATTERY\_MOUNT flagged part. or alternately write it yourself, it’s straightforward C++ IMO but I also know the code pretty well.

However, option #1 is probably the easiest and isn’t unreasonable.

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 21, 2021, 2:51pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/3 "2021-07-21T14:51:06Z")

</div>

I’m trying to implement #2, it’s going fine, but an obvious problem comes up:

I’ve used two new flags that are not yet defined:  
“flags”: [“HANDHELD\_BATTERY\_MOUNT”],  
“flags”: [“NEEDS\_HANDHELD\_BATTERY\_MOUNT”]

But I don’t actually know _where_ in the code such flags are defined. Any help with that?

---

<div class="post-metadata">

**Author:** ![mlangsdorf](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/mlangsdorf/32/2462_2.png) [@mlangsdorf](https://discourse.cataclysmdda.org/u/mlangsdorf)\
**Post date:** [July 21, 2021, 9:45pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/4 "2021-07-21T21:45:27Z")

</div>

Flags aren’t defined in the code generally. They’re strings, and stored in a collection of strings that you can search with `has_flag( "FLAG_STRING" )`. See:  
`git grep has_flag src/veh*cpp`  
for more examples than you probably want to read.

There are certain flags that are defined in the vehicle code because they’re referenced multiple times per second and doing a linear search via `has_flag()` would have performance implications. But an installation flag is not a performance equivalent flag so `has_flag()` will work fine.

If you search for “BATTERY\_MOUNT” in the code:

```auto
$ git grep BATTERY_MOUNT src/veh*cpp
src/vehicle.cpp: remove_dependent_part( "BATTERY_MOUNT", "NEEDS_BATTERY_MOUNT" );

```

you’ll see that the existing BATTERY\_MOUNT flag is only used to make sure that parts get removed in the correct order, and that would all you’d need to do for HANDHELD\_BATTERY\_MOUNT.

Thanks for taking a swing at this!

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 21, 2021, 10:01pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/5 "2021-07-21T22:01:07Z")

</div>

> [@mlangsdorf](#):
>
> you’ll see that the existing BATTERY\_MOUNT flag is only used to make sure that parts get removed in the correct order, and that would all you’d need to do for HANDHELD\_BATTERY\_MOUNT

Shouldn’t they also be checked when _Installing_ the part?  
I was under the impression the main use for these flags was to insure you need BATTERY\_MOUNT already present in the tile to install anything with NEEDS\_BATTERY\_MOUNT

---

<div class="post-metadata">

**Author:** ![TaintedHolyWater](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@TaintedHolyWater](https://discourse.cataclysmdda.org/u/TaintedHolyWater)\
**Post date:** [July 21, 2021, 11:59pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/6 "2021-07-21T23:59:15Z")

</div>

My coding is… lets be generous and say elementary at best, but I may have found something useful.

There is the Recharging station in vehicle\_parts.json that goes from vehicle battery power to handheld batteries placed in a cargo container. If we made a copy of that using a reversed version of the “recharge” flag than it could drain batteries in a compartment to charge the vehicle.  
Does that sound in any way workable?

Unfortunately I haven’t been able to find the code for how the recharge flag works. The file labeled Flags.json just says  
“id”: “RECHARGE”,  
“type”: “json\_flag”,  
“context”: []  
},

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 22, 2021, 12:10am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/7 "2021-07-22T00:10:02Z")

</div>

> [@TaintedHolyWater](#):
>
> If we made a copy of that using a reversed version of the “recharge” flag than it could drain batteries in a compartment to charge the vehicle.

To “charge” a vehicle it needs to have some “internal” batteries in the first place to absorb that charge. I don’t think the current vehicle code will be able to treat an “outside” battery as it’s “primary internal battery” well (if at all).

---

<div class="post-metadata">

**Author:** ![TaintedHolyWater](https://avatars.discourse-cdn.com/v4/letter/t/b5e925/32.png) [@TaintedHolyWater](https://discourse.cataclysmdda.org/u/TaintedHolyWater)\
**Post date:** [July 22, 2021, 12:36am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/8 "2021-07-22T00:36:14Z")

</div>

Ah, yes. I had misread the goal of this. Had assumed they wanted to charge a vehicle off handheld batteries not wholly replace the car’s battery with a handheld one. Clearly I need more coffee before I continue reading.

---

<div class="post-metadata">

**Author:** ![mlangsdorf](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/mlangsdorf/32/2462_2.png) [@mlangsdorf](https://discourse.cataclysmdda.org/u/mlangsdorf)\
**Post date:** [July 22, 2021, 12:05pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/9 "2021-07-22T12:05:28Z")

</div>

You’d think you’d need to check BATTERY\_MOUNT when you install, but actually it’s a lot simpler: battery mounts provide the on\_battery location, and batteries install in the on\_battery location. If you don’t have a mount installed, you can’t install a battery because there is no where to install it.

---

<div class="post-metadata">

**Author:** ![GiggleGrassGatherer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/gigglegrassgatherer/32/7743_2.png) [@GiggleGrassGatherer](https://discourse.cataclysmdda.org/u/GiggleGrassGatherer)\
**Post date:** [July 22, 2021, 3:15pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/10 "2021-07-22T15:15:59Z")

</div>

So the fact that vehicle batteries are an order of magnitude heavier than comparable (charge-wise) non-vehicle batteries is just ignored? Or those recipes are making them heavier after conversion, somehow?

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 22, 2021, 11:01pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/11 "2021-07-22T23:01:14Z")

</div>

> [@GiggleGrassGatherer](#):
>
> So the fact that vehicle batteries are an order of magnitude heavier than comparable (charge-wise) non-vehicle batteries is just ignored?

[Medium battery - Cataclysm: Dark Days Ahead](http://cdda-trunk.chezzo.com/medium_battery_cell) is 600g at 500 kJ  
[Small storage battery - Cataclysm: Dark Days Ahead](http://cdda-trunk.chezzo.com/small_storage_battery) 2500g at 500kJ  
Heavier, but not exactly “order of magnitude”.

> [@GiggleGrassGatherer](#):
>
> Or those recipes are making them heavier after conversion, somehow?

The recipe adds extra weight to the vehicle, sure. Plus, handheld batteries are more fragile, too.  
But much more to the point: why would a weight difference of less than 2 kg matter _on a vehicle_?

The frame and whatever-else-is-actually-using-that-battery-in-that-vehicle will most likely weight so much that a kilo or two would be irrelevant.

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 22, 2021, 11:55pm UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/12 "2021-07-22T23:55:14Z")

</div>

> [@mlangsdorf](#):
>
> You’d think you’d need to check BATTERY\_MOUNT when you install, but actually it’s a lot simpler: battery mounts provide the on\_battery location

Ok, but where is _that_ property defined, then? It’s not in their .json definitions, as far as I can see…

I mean, I can see that swappable storage batteries go into  
“location”: “on\_battery\_mount”,

But where is it defined that “swappable storage battery case”, when installed, _provides_ this “on\_battery\_mount” location?

**P.S. Never mind, already found relevant code in vp\_flags.json**  
(It _does_ work via flags, though… Nothing to do with “location”.)

P.P.S. The best part is — flags (as defined in vp\_flags.json) already prevent removal of dependent parts. So I have no idea what the hell C++ code even does at this point…

---

<div class="post-metadata">

**Author:** ![Valase](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/valase/32/6972_2.png) [@Valase](https://discourse.cataclysmdda.org/u/Valase)\
**Post date:** [July 23, 2021, 1:21am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/13 "2021-07-23T01:21:02Z")

</div>

> [@Tamior](#):
>
> So I have no idea what the hell C++ code even does at this point…

… Provide the support structure and interpret the JSON code?

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 23, 2021, 1:24am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/14 "2021-07-23T01:24:05Z")

</div>

Nope.  
Because similar .json-defined pair of newly-added flags works without the C++ counterpart.

---

<div class="post-metadata">

**Author:** ![Valase](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/valase/32/6972_2.png) [@Valase](https://discourse.cataclysmdda.org/u/Valase)\
**Post date:** [July 23, 2021, 1:26am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/15 "2021-07-23T01:26:32Z")

</div>

Uh… no. JSON on it’s own can not be executed. It needs C++ (or any other lower programming language) to translate it into machine-readable code.

You can’t just write a game based only on JSON.

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 23, 2021, 1:27am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/16 "2021-07-23T01:27:28Z")

</div>

I’m specifically talking about the line in C++ code that _explicitly_ mentions the flags.

```auto
remove_dependent_part( "BATTERY_MOUNT", "NEEDS_BATTERY_MOUNT" )

```

---

<div class="post-metadata">

**Author:** ![Valase](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/valase/32/6972_2.png) [@Valase](https://discourse.cataclysmdda.org/u/Valase)\
**Post date:** [July 23, 2021, 1:29am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/17 "2021-07-23T01:29:31Z")

</div>

> [@Tamior](#):
>
> I’m specifically talking about the line in C++ […]

Which line?  
The part I quoted and answered 8 minutes ago did not mention any line…:

> [@Tamior](#):
>
> So I have no idea what the hell C++ code even does at this point…

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 23, 2021, 1:30am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/18 "2021-07-23T01:30:47Z")

</div>

See above, I added the exact line.

---

<div class="post-metadata">

**Author:** ![Valase](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/valase/32/6972_2.png) [@Valase](https://discourse.cataclysmdda.org/u/Valase)\
**Post date:** [July 23, 2021, 1:35am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/19 "2021-07-23T01:35:38Z")

</div>

Sigh…  
So you’re talking about [vehicle.cpp](https://github.com/CleverRaven/Cataclysm-DDA/blob/f16c224166ffac3c83881fd97d0f6df182346c8d/src/vehicle.cpp#L1926)?

---

<div class="post-metadata">

**Author:** ![Tamior](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Tamior](https://discourse.cataclysmdda.org/u/Tamior)\
**Post date:** [July 23, 2021, 1:41am UTC](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718/20 "2021-07-23T01:41:19Z")

</div>

Sigh…  
Yes, I am talking about a line in [vehicle.cpp](https://github.com/CleverRaven/Cataclysm-DDA/blob/f16c224166ffac3c83881fd97d0f6df182346c8d/src/vehicle.cpp#L1926)  
Always has been. That’s the only line in C++ code directly and explicitly relevant to the flags in question.

[Next page](https://discourse.cataclysmdda.org/t/best-way-to-implement-reverse-battery-compartment-mod-within-current-code-structure/26718.md?page=2)
