<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def add_core(self, x, y, p):
 """Add a new core to the region tree. Raises ------ ValueError If the co-ordinate is not contained within this part of the tree or the core number is out of range. Returns ------- bool True if the specified core is to be loaded to all subregions. """

        # Check that the co-ordinate is contained in this region
        if ((p < 0 or p > 17) or
                (x < self.base_x or x >= self.base_x + self.scale) or
                (y < self.base_y or y >= self.base_y + self.scale)):
            raise ValueError((x, y, p))

        # Determine which subregion this refers to
        subregion = ((x >> self.shift) & 0x3) + 4*((y >> self.shift) & 0x3)

        if self.level == 3:
            # If level-3 then we just add to the locally selected regions
            self.locally_selected[p] |= 1 << subregion
        elif not self.locally_selected[p] & (1 << subregion):
            # If the subregion isn't in `locally_selected` for this core number
            # then add the core to the subregion.
            if self.subregions[subregion] is None:
                # "Lazy": if the subtree doesn't exist yet then add it
                base_x = int(self.base_x + (self.scale / 4) * (subregion % 4))
                base_y = int(self.base_y + (self.scale / 4) * (subregion // 4))
                self.subregions[subregion] = RegionCoreTree(base_x, base_y,
                                                            self.level + 1)

            # If the subregion reports that all of its subregions for this core
            # are selected then we need to add it to `locally_selected`.
            if self.subregions[subregion].add_core(x, y, p):
                self.locally_selected[p] |= 1 << subregion

        # If all subregions are selected for this core and this is not the top
        # level in the hierarchy then return True after emptying the local
        # selection for the core.
        if self.locally_selected[p] == 0xffff and self.level != 0:
            self.locally_selected[p] = 0x0
            return True
        else:
            return False